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