rpc_thread.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. #define __FORCE_GLIBC
  2. #include <features.h>
  3. #include <stdio.h>
  4. #include <assert.h>
  5. #include <bits/libc-tsd.h>
  6. #include "rpc_private.h"
  7. #ifdef __UCLIBC_HAS_THREADS__
  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 *
  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 *
  82. __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. struct rpc_createerr *
  91. __rpc_thread_createerr (void)
  92. {
  93. struct rpc_thread_variables *tvp;
  94. tvp = __rpc_thread_variables ();
  95. if (tvp == &__libc_tsd_RPC_VARS_mem)
  96. return &rpc_createerr;
  97. return &tvp->rpc_createerr_s;
  98. }
  99. struct pollfd **
  100. __rpc_thread_svc_pollfd (void)
  101. {
  102. struct rpc_thread_variables *tvp;
  103. tvp = __rpc_thread_variables ();
  104. if (tvp == &__libc_tsd_RPC_VARS_mem)
  105. return &svc_pollfd;
  106. return &tvp->svc_pollfd_s;
  107. }
  108. int *
  109. __rpc_thread_svc_max_pollfd (void)
  110. {
  111. struct rpc_thread_variables *tvp;
  112. tvp = __rpc_thread_variables ();
  113. if (tvp == &__libc_tsd_RPC_VARS_mem)
  114. return &svc_max_pollfd;
  115. return &tvp->svc_max_pollfd_s;
  116. }
  117. #else
  118. #undef svc_fdset
  119. #undef rpc_createerr
  120. #undef svc_pollfd
  121. #undef svc_max_pollfd
  122. fd_set * __rpc_thread_svc_fdset (void)
  123. {
  124. extern fd_set svc_fdset;
  125. return &(svc_fdset);
  126. }
  127. struct rpc_createerr * __rpc_thread_createerr (void)
  128. {
  129. extern struct rpc_createerr rpc_createerr;
  130. return &(rpc_createerr);
  131. }
  132. struct pollfd ** __rpc_thread_svc_pollfd (void)
  133. {
  134. extern struct pollfd *svc_pollfd;
  135. return &(svc_pollfd);
  136. }
  137. int * __rpc_thread_svc_max_pollfd (void)
  138. {
  139. extern int svc_max_pollfd;
  140. return &(svc_max_pollfd);
  141. }
  142. #endif /* __UCLIBC_HAS_THREADS__ */