rpc_thread.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. #define __FORCE_GLIBC
  7. #include <features.h>
  8. #include <stdio.h>
  9. #include <assert.h>
  10. #include "rpc_private.h"
  11. #ifdef __UCLIBC_HAS_THREADS__
  12. #include <bits/libc-tsd.h>
  13. #include <bits/libc-lock.h>
  14. /* Variable used in non-threaded applications or for the first thread. */
  15. static struct rpc_thread_variables __libc_tsd_RPC_VARS_mem;
  16. __libc_tsd_define (, RPC_VARS)
  17. /*
  18. * Task-variable destructor
  19. */
  20. void
  21. __rpc_thread_destroy (void)
  22. {
  23. struct rpc_thread_variables *tvp = __libc_tsd_get (RPC_VARS);
  24. if (tvp != NULL && tvp != &__libc_tsd_RPC_VARS_mem) {
  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);
  34. __libc_tsd_set (RPC_VARS, NULL);
  35. }
  36. }
  37. /*
  38. * Initialize RPC multi-threaded operation
  39. */
  40. static void
  41. rpc_thread_multi (void)
  42. {
  43. __libc_tsd_set (RPC_VARS, &__libc_tsd_RPC_VARS_mem);
  44. }
  45. struct rpc_thread_variables attribute_hidden *
  46. __rpc_thread_variables (void)
  47. {
  48. __libc_once_define (static, once);
  49. struct rpc_thread_variables *tvp;
  50. tvp = __libc_tsd_get (RPC_VARS);
  51. if (tvp == NULL) {
  52. __libc_once (once, rpc_thread_multi);
  53. tvp = __libc_tsd_get (RPC_VARS);
  54. if (tvp == NULL) {
  55. tvp = calloc (1, sizeof *tvp);
  56. if (tvp != NULL)
  57. __libc_tsd_set (RPC_VARS, tvp);
  58. else
  59. tvp = __libc_tsd_get (RPC_VARS);
  60. }
  61. }
  62. return tvp;
  63. }
  64. /* Global variables If we're single-threaded, or if this is the first
  65. thread using the variable, use the existing global variable. This
  66. provides backwards compatability for existing applications which
  67. dynamically link against this code. */
  68. #undef svc_fdset
  69. #undef rpc_createerr
  70. #undef svc_pollfd
  71. #undef svc_max_pollfd
  72. fd_set *
  73. __rpc_thread_svc_fdset (void)
  74. {
  75. struct rpc_thread_variables *tvp;
  76. tvp = __rpc_thread_variables ();
  77. if (tvp == &__libc_tsd_RPC_VARS_mem)
  78. return &svc_fdset;
  79. return &tvp->svc_fdset_s;
  80. }
  81. struct rpc_createerr *
  82. __rpc_thread_createerr (void)
  83. {
  84. struct rpc_thread_variables *tvp;
  85. tvp = __rpc_thread_variables ();
  86. if (tvp == &__libc_tsd_RPC_VARS_mem)
  87. return &rpc_createerr;
  88. return &tvp->rpc_createerr_s;
  89. }
  90. struct pollfd **
  91. __rpc_thread_svc_pollfd (void)
  92. {
  93. struct rpc_thread_variables *tvp;
  94. tvp = __rpc_thread_variables ();
  95. if (tvp == &__libc_tsd_RPC_VARS_mem)
  96. return &svc_pollfd;
  97. return &tvp->svc_pollfd_s;
  98. }
  99. int *
  100. __rpc_thread_svc_max_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_max_pollfd;
  106. return &tvp->svc_max_pollfd_s;
  107. }
  108. #else
  109. #undef svc_fdset
  110. #undef rpc_createerr
  111. #undef svc_pollfd
  112. #undef svc_max_pollfd
  113. extern fd_set svc_fdset;
  114. fd_set * __rpc_thread_svc_fdset (void)
  115. {
  116. return &(svc_fdset);
  117. }
  118. extern struct rpc_createerr rpc_createerr;
  119. struct rpc_createerr * __rpc_thread_createerr (void)
  120. {
  121. return &(rpc_createerr);
  122. }
  123. extern struct pollfd *svc_pollfd;
  124. struct pollfd ** __rpc_thread_svc_pollfd (void)
  125. {
  126. return &(svc_pollfd);
  127. }
  128. extern int svc_max_pollfd;
  129. int * __rpc_thread_svc_max_pollfd (void)
  130. {
  131. return &(svc_max_pollfd);
  132. }
  133. #endif /* __UCLIBC_HAS_THREADS__ */
  134. libc_hidden_def(__rpc_thread_svc_fdset)
  135. libc_hidden_def(__rpc_thread_createerr)
  136. libc_hidden_def(__rpc_thread_svc_pollfd)
  137. libc_hidden_def(__rpc_thread_svc_max_pollfd)