* malloc/thread-freeres.c: Conditionalize on _LIBC_REENTRANT.
[platform/upstream/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 __attribute__ ((section ("__libc_thread_freeres_fn")))
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                 __libc_tsd_set (RPC_VARS, NULL);
34         }
35 }
36 #ifdef _LIBC_REENTRANT
37 text_set_element (__libc_thread_subfreeres, __rpc_thread_destroy);
38 #endif
39 text_set_element (__libc_subfreeres, __rpc_thread_destroy);
40
41
42 /*
43  * Initialize RPC multi-threaded operation
44  */
45 static void
46 rpc_thread_multi (void)
47 {
48   __libc_tsd_set (RPC_VARS, &__libc_tsd_RPC_VARS_mem);
49 }
50
51
52 struct rpc_thread_variables *
53 __rpc_thread_variables (void)
54 {
55         __libc_once_define (static, once);
56         struct rpc_thread_variables *tvp;
57
58         tvp = __libc_tsd_get (RPC_VARS);
59         if (tvp == NULL) {
60                 __libc_once (once, rpc_thread_multi);
61                 tvp = __libc_tsd_get (RPC_VARS);
62                 if (tvp == NULL) {
63                         tvp = calloc (1, sizeof *tvp);
64                         if (tvp != NULL)
65                                 __libc_tsd_set (RPC_VARS, tvp);
66                         else
67                                 tvp = __libc_tsd_get (RPC_VARS);
68                 }
69         }
70         return tvp;
71 }
72
73
74 /* Global variables If we're single-threaded, or if this is the first
75    thread using the variable, use the existing global variable.  This
76    provides backwards compatability for existing applications which
77    dynamically link against this code.  */
78 #undef svc_fdset
79 #undef rpc_createerr
80 #undef svc_pollfd
81 #undef svc_max_pollfd
82
83 fd_set *
84 __rpc_thread_svc_fdset (void)
85 {
86         struct rpc_thread_variables *tvp;
87
88         tvp = __rpc_thread_variables ();
89         if (tvp == &__libc_tsd_RPC_VARS_mem)
90                 return &svc_fdset;
91         return &tvp->svc_fdset_s;
92 }
93 libc_hidden_def (__rpc_thread_svc_fdset)
94
95 struct rpc_createerr *
96 __rpc_thread_createerr (void)
97 {
98         struct rpc_thread_variables *tvp;
99
100         tvp = __rpc_thread_variables ();
101         if (tvp == &__libc_tsd_RPC_VARS_mem)
102                 return &rpc_createerr;
103         return &tvp->rpc_createerr_s;
104 }
105 libc_hidden_def (__rpc_thread_createerr)
106
107 struct pollfd **
108 __rpc_thread_svc_pollfd (void)
109 {
110         struct rpc_thread_variables *tvp;
111
112         tvp = __rpc_thread_variables ();
113         if (tvp == &__libc_tsd_RPC_VARS_mem)
114                 return &svc_pollfd;
115         return &tvp->svc_pollfd_s;
116 }
117 libc_hidden_def (__rpc_thread_svc_pollfd)
118
119 int *
120 __rpc_thread_svc_max_pollfd (void)
121 {
122         struct rpc_thread_variables *tvp;
123
124         tvp = __rpc_thread_variables ();
125         if (tvp == &__libc_tsd_RPC_VARS_mem)
126                 return &svc_max_pollfd;
127         return &tvp->svc_max_pollfd_s;
128 }
129 libc_hidden_def (__rpc_thread_svc_max_pollfd)
130
131 #endif /* _RPC_THREAD_SAFE_ */