rpc_thread.c 3.1 KB

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