]> git.ipfire.org Git - thirdparty/glibc.git/blob - sunrpc/rpc_thread.c
2002-08-05 Jakub Jelinek <jakub@redhat.com>
[thirdparty/glibc.git] / sunrpc / rpc_thread.c
1 #include <stdio.h>
2 #include <bits/libc-lock.h>
3 #include <rpc/rpc.h>
4 #include <assert.h>
5
6 #include <bits/libc-lock.h>
7 #include <bits/libc-tsd.h>
8
9 #ifdef _RPC_THREAD_SAFE_
10
11 /* Variable used in non-threaded applications or for the first thread. */
12 static struct rpc_thread_variables __libc_tsd_RPC_VARS_mem;
13 __libc_tsd_define (, RPC_VARS)
14
15 /*
16 * Task-variable destructor
17 */
18 void
19 __rpc_thread_destroy (void)
20 {
21 struct rpc_thread_variables *tvp = __libc_tsd_get (RPC_VARS);
22
23 if (tvp != NULL && tvp != &__libc_tsd_RPC_VARS_mem) {
24 __rpc_thread_svc_cleanup ();
25 __rpc_thread_clnt_cleanup ();
26 __rpc_thread_key_cleanup ();
27 free (tvp->clnt_perr_buf_s);
28 free (tvp->clntraw_private_s);
29 free (tvp->svcraw_private_s);
30 free (tvp->authdes_cache_s);
31 free (tvp->authdes_lru_s);
32 free (tvp);
33 }
34 }
35
36
37 /*
38 * Initialize RPC multi-threaded operation
39 */
40 static void
41 rpc_thread_multi (void)
42 {
43 __libc_tsd_set (RPC_VARS, &__libc_tsd_RPC_VARS_mem);
44 }
45
46
47 struct rpc_thread_variables *
48 __rpc_thread_variables (void)
49 {
50 __libc_once_define (static, once);
51 struct rpc_thread_variables *tvp;
52
53 tvp = __libc_tsd_get (RPC_VARS);
54 if (tvp == NULL) {
55 __libc_once (once, rpc_thread_multi);
56 tvp = __libc_tsd_get (RPC_VARS);
57 if (tvp == NULL) {
58 tvp = calloc (1, sizeof *tvp);
59 if (tvp != NULL)
60 __libc_tsd_set (RPC_VARS, tvp);
61 else
62 tvp = __libc_tsd_get (RPC_VARS);
63 }
64 }
65 return tvp;
66 }
67
68
69 /* Global variables If we're single-threaded, or if this is the first
70 thread using the variable, use the existing global variable. This
71 provides backwards compatability for existing applications which
72 dynamically link against this code. */
73 #undef svc_fdset
74 #undef rpc_createerr
75 #undef svc_pollfd
76 #undef svc_max_pollfd
77
78 fd_set *
79 __rpc_thread_svc_fdset (void)
80 {
81 struct rpc_thread_variables *tvp;
82
83 tvp = __rpc_thread_variables ();
84 if (tvp == &__libc_tsd_RPC_VARS_mem)
85 return &svc_fdset;
86 return &tvp->svc_fdset_s;
87 }
88 libc_hidden_def (__rpc_thread_svc_fdset)
89
90 struct rpc_createerr *
91 __rpc_thread_createerr (void)
92 {
93 struct rpc_thread_variables *tvp;
94
95 tvp = __rpc_thread_variables ();
96 if (tvp == &__libc_tsd_RPC_VARS_mem)
97 return &rpc_createerr;
98 return &tvp->rpc_createerr_s;
99 }
100 libc_hidden_def (__rpc_thread_createerr)
101
102 struct pollfd **
103 __rpc_thread_svc_pollfd (void)
104 {
105 struct rpc_thread_variables *tvp;
106
107 tvp = __rpc_thread_variables ();
108 if (tvp == &__libc_tsd_RPC_VARS_mem)
109 return &svc_pollfd;
110 return &tvp->svc_pollfd_s;
111 }
112 libc_hidden_def (__rpc_thread_svc_pollfd)
113
114 int *
115 __rpc_thread_svc_max_pollfd (void)
116 {
117 struct rpc_thread_variables *tvp;
118
119 tvp = __rpc_thread_variables ();
120 if (tvp == &__libc_tsd_RPC_VARS_mem)
121 return &svc_max_pollfd;
122 return &tvp->svc_max_pollfd_s;
123 }
124 libc_hidden_def (__rpc_thread_svc_max_pollfd)
125
126 #endif /* _RPC_THREAD_SAFE_ */