rpc_thread.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. #define __FORCE_GLIBC
  2. #include <features.h>
  3. #include <stdio.h>
  4. #include <assert.h>
  5. #include "rpc_private.h"
  6. #ifdef __UCLIBC_HAS_THREADS__
  7. #include <bits/libc-tsd.h>
  8. /* Variable used in non-threaded applications or for the first thread. */
  9. static struct rpc_thread_variables __libc_tsd_RPC_VARS_mem;
  10. static struct rpc_thread_variables *__libc_tsd_RPC_VARS_data =
  11. &__libc_tsd_RPC_VARS_mem;
  12. /*
  13. * Task-variable destructor
  14. */
  15. void
  16. __rpc_thread_destroy (void)
  17. {
  18. struct rpc_thread_variables *tvp = __rpc_thread_variables();
  19. if (tvp != NULL && tvp != &__libc_tsd_RPC_VARS_mem) {
  20. __rpc_thread_svc_cleanup ();
  21. __rpc_thread_clnt_cleanup ();
  22. //__rpc_thread_key_cleanup ();
  23. free (tvp->authnone_private_s);
  24. free (tvp->clnt_perr_buf_s);
  25. free (tvp->clntraw_private_s);
  26. free (tvp->svcraw_private_s);
  27. free (tvp->authdes_cache_s);
  28. free (tvp->authdes_lru_s);
  29. free (tvp);
  30. }
  31. }
  32. extern int weak_function __pthread_once (pthread_once_t *__once_control,
  33. void (*__init_routine) (void));
  34. # define __libc_once_define(CLASS, NAME) \
  35. CLASS pthread_once_t NAME = PTHREAD_ONCE_INIT
  36. /* Call handler iff the first call. */
  37. #define __libc_once(ONCE_CONTROL, INIT_FUNCTION) \
  38. do { \
  39. if (__pthread_once != NULL) \
  40. __pthread_once (&(ONCE_CONTROL), (INIT_FUNCTION)); \
  41. else if ((ONCE_CONTROL) == PTHREAD_ONCE_INIT) { \
  42. INIT_FUNCTION (); \
  43. (ONCE_CONTROL) = !PTHREAD_ONCE_INIT; \
  44. } \
  45. } while (0)
  46. /*
  47. * Initialize RPC multi-threaded operation
  48. */
  49. static void
  50. rpc_thread_multi (void)
  51. {
  52. __libc_tsd_set (RPC_VARS, &__libc_tsd_RPC_VARS_mem);
  53. }
  54. struct rpc_thread_variables attribute_hidden *
  55. __rpc_thread_variables (void)
  56. {
  57. __libc_once_define (static, once);
  58. struct rpc_thread_variables *tvp;
  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_RPC_VARS_data;
  69. }
  70. }
  71. return tvp;
  72. }
  73. /* Global variables If we're single-threaded, or if this is the first
  74. thread using the variable, use the existing global variable. This
  75. provides backwards compatability for existing applications which
  76. dynamically link against this code. */
  77. #undef svc_fdset
  78. #undef rpc_createerr
  79. #undef svc_pollfd
  80. #undef svc_max_pollfd
  81. fd_set attribute_hidden *
  82. __libc_rpc_thread_svc_fdset (void)
  83. {
  84. struct rpc_thread_variables *tvp;
  85. tvp = __rpc_thread_variables ();
  86. if (tvp == &__libc_tsd_RPC_VARS_mem)
  87. return &svc_fdset;
  88. return &tvp->svc_fdset_s;
  89. }
  90. strong_alias(__libc_rpc_thread_svc_fdset,__rpc_thread_svc_fdset)
  91. struct rpc_createerr *
  92. __rpc_thread_createerr (void)
  93. {
  94. struct rpc_thread_variables *tvp;
  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. struct pollfd attribute_hidden **
  101. __libc_rpc_thread_svc_pollfd (void)
  102. {
  103. struct rpc_thread_variables *tvp;
  104. tvp = __rpc_thread_variables ();
  105. if (tvp == &__libc_tsd_RPC_VARS_mem)
  106. return &svc_pollfd;
  107. return &tvp->svc_pollfd_s;
  108. }
  109. strong_alias(__libc_rpc_thread_svc_pollfd,__rpc_thread_svc_pollfd)
  110. int attribute_hidden *
  111. __libc_rpc_thread_svc_max_pollfd (void)
  112. {
  113. struct rpc_thread_variables *tvp;
  114. tvp = __rpc_thread_variables ();
  115. if (tvp == &__libc_tsd_RPC_VARS_mem)
  116. return &svc_max_pollfd;
  117. return &tvp->svc_max_pollfd_s;
  118. }
  119. strong_alias(__libc_rpc_thread_svc_max_pollfd,__rpc_thread_svc_max_pollfd)
  120. #else
  121. #undef svc_fdset
  122. #undef rpc_createerr
  123. #undef svc_pollfd
  124. #undef svc_max_pollfd
  125. fd_set attribute_hidden * __libc_rpc_thread_svc_fdset (void)
  126. {
  127. extern fd_set svc_fdset;
  128. return &(svc_fdset);
  129. }
  130. strong_alias(__libc_rpc_thread_svc_fdset,__rpc_thread_svc_fdset)
  131. struct rpc_createerr * __rpc_thread_createerr (void)
  132. {
  133. extern struct rpc_createerr rpc_createerr;
  134. return &(rpc_createerr);
  135. }
  136. struct pollfd attribute_hidden ** __libc_rpc_thread_svc_pollfd (void)
  137. {
  138. extern struct pollfd *svc_pollfd;
  139. return &(svc_pollfd);
  140. }
  141. strong_alias(__libc_rpc_thread_svc_pollfd,__rpc_thread_svc_pollfd)
  142. int attribute_hidden * __libc_rpc_thread_svc_max_pollfd (void)
  143. {
  144. extern int svc_max_pollfd;
  145. return &(svc_max_pollfd);
  146. }
  147. strong_alias(__libc_rpc_thread_svc_max_pollfd,__rpc_thread_svc_max_pollfd)
  148. #endif /* __UCLIBC_HAS_THREADS__ */