1 /*
2  * Copyright (C) 2000-2006 Erik Andersen <andersen@uclibc.org>
3  *
4  * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
5  */
6 
7 #include <stdio.h>
8 #include <assert.h>
9 #include "rpc_private.h"
10 
11 
12 #ifdef __UCLIBC_HAS_THREADS__
13 
14 #include <bits/libc-tsd.h>
15 #include <bits/libc-lock.h>
16 
17 /* Variable used in non-threaded applications or for the first thread.  */
18 static struct rpc_thread_variables __libc_tsd_RPC_VARS_mem;
19 __libc_tsd_define (, RPC_VARS)
20 
21 /*
22  * Task-variable destructor
23  */
24 void
__rpc_thread_destroy(void)25 __rpc_thread_destroy (void)
26 {
27 	struct rpc_thread_variables *tvp = __libc_tsd_get (RPC_VARS);
28 
29 	if (tvp != NULL && tvp != &__libc_tsd_RPC_VARS_mem) {
30 		__rpc_thread_svc_cleanup ();
31 		__rpc_thread_clnt_cleanup ();
32 		/*__rpc_thread_key_cleanup (); */
33 		free (tvp->clnt_perr_buf_s);
34 		free (tvp->clntraw_private_s);
35 		free (tvp->svcraw_private_s);
36 		free (tvp->authdes_cache_s);
37 		free (tvp->authdes_lru_s);
38 		free (tvp);
39 		__libc_tsd_set (RPC_VARS, NULL);
40 	}
41 }
42 
43 /*
44  * Initialize RPC multi-threaded operation
45  */
46 static void
rpc_thread_multi(void)47 rpc_thread_multi (void)
48 {
49   __libc_tsd_set (RPC_VARS, &__libc_tsd_RPC_VARS_mem);
50 }
51 
52 
53 struct rpc_thread_variables attribute_hidden *
__rpc_thread_variables(void)54 __rpc_thread_variables (void)
55 {
56 	__libc_once_define (static, once);
57 	struct rpc_thread_variables *tvp;
58 
59 	tvp = __libc_tsd_get (RPC_VARS);
60 	if (tvp == NULL) {
61 		__libc_once (once, rpc_thread_multi);
62 		tvp = __libc_tsd_get (RPC_VARS);
63 		if (tvp == NULL) {
64 			tvp = calloc (1, sizeof *tvp);
65 			if (tvp != NULL)
66 				__libc_tsd_set (RPC_VARS, tvp);
67 			else
68 				tvp = __libc_tsd_get (RPC_VARS);
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 compatability 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 
84 fd_set *
__rpc_thread_svc_fdset(void)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 }
94 
95 struct rpc_createerr *
__rpc_thread_createerr(void)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 
106 struct pollfd **
__rpc_thread_svc_pollfd(void)107 __rpc_thread_svc_pollfd (void)
108 {
109 	struct rpc_thread_variables *tvp;
110 
111 	tvp = __rpc_thread_variables ();
112 	if (tvp == &__libc_tsd_RPC_VARS_mem)
113 		return &svc_pollfd;
114 	return &tvp->svc_pollfd_s;
115 }
116 
117 int *
__rpc_thread_svc_max_pollfd(void)118 __rpc_thread_svc_max_pollfd (void)
119 {
120 	struct rpc_thread_variables *tvp;
121 
122 	tvp = __rpc_thread_variables ();
123 	if (tvp == &__libc_tsd_RPC_VARS_mem)
124 		return &svc_max_pollfd;
125 	return &tvp->svc_max_pollfd_s;
126 }
127 #else
128 
129 #undef svc_fdset
130 #undef rpc_createerr
131 #undef svc_pollfd
132 #undef svc_max_pollfd
133 
134 extern fd_set svc_fdset;
__rpc_thread_svc_fdset(void)135 fd_set * __rpc_thread_svc_fdset (void)
136 {
137     return &(svc_fdset);
138 }
139 
140 extern struct rpc_createerr rpc_createerr;
__rpc_thread_createerr(void)141 struct rpc_createerr * __rpc_thread_createerr (void)
142 {
143     return &(rpc_createerr);
144 }
145 
146 extern struct pollfd *svc_pollfd;
__rpc_thread_svc_pollfd(void)147 struct pollfd ** __rpc_thread_svc_pollfd (void)
148 {
149     return &(svc_pollfd);
150 }
151 
152 extern int svc_max_pollfd;
__rpc_thread_svc_max_pollfd(void)153 int * __rpc_thread_svc_max_pollfd (void)
154 {
155     return &(svc_max_pollfd);
156 }
157 
158 #endif /* __UCLIBC_HAS_THREADS__ */
159 
160 libc_hidden_def(__rpc_thread_svc_fdset)
161 libc_hidden_def(__rpc_thread_createerr)
162 libc_hidden_def(__rpc_thread_svc_pollfd)
163 libc_hidden_def(__rpc_thread_svc_max_pollfd)
164