pthread_setspecific.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * pthread_setspecific.c
  3. *
  4. * Description:
  5. * POSIX thread functions which implement thread-specific data (TSD).
  6. *
  7. * --------------------------------------------------------------------------
  8. *
  9. * Pthreads-win32 - POSIX Threads Library for Win32
  10. * Copyright(C) 1998 John E. Bossom
  11. * Copyright(C) 1999,2005 Pthreads-win32 contributors
  12. *
  13. * Contact Email: [email protected]
  14. *
  15. * The current list of contributors is contained
  16. * in the file CONTRIBUTORS included with the source
  17. * code distribution. The list can also be seen at the
  18. * following World Wide Web location:
  19. * http://sources.redhat.com/pthreads-win32/contributors.html
  20. *
  21. * This library is free software; you can redistribute it and/or
  22. * modify it under the terms of the GNU Lesser General Public
  23. * License as published by the Free Software Foundation; either
  24. * version 2 of the License, or (at your option) any later version.
  25. *
  26. * This library is distributed in the hope that it will be useful,
  27. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  28. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  29. * Lesser General Public License for more details.
  30. *
  31. * You should have received a copy of the GNU Lesser General Public
  32. * License along with this library in the file COPYING.LIB;
  33. * if not, write to the Free Software Foundation, Inc.,
  34. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  35. */
  36. #include "pthread.h"
  37. #include "implement.h"
  38. int
  39. pthread_setspecific (pthread_key_t key, const void *value)
  40. /*
  41. * ------------------------------------------------------
  42. * DOCPUBLIC
  43. * This function sets the value of the thread specific
  44. * key in the calling thread.
  45. *
  46. * PARAMETERS
  47. * key
  48. * an instance of pthread_key_t
  49. * value
  50. * the value to set key to
  51. *
  52. *
  53. * DESCRIPTION
  54. * This function sets the value of the thread specific
  55. * key in the calling thread.
  56. *
  57. * RESULTS
  58. * 0 successfully set value
  59. * EAGAIN could not set value
  60. * ENOENT SERIOUS!!
  61. *
  62. * ------------------------------------------------------
  63. */
  64. {
  65. pthread_t self;
  66. int result = 0;
  67. if (key != ptw32_selfThreadKey)
  68. {
  69. /*
  70. * Using pthread_self will implicitly create
  71. * an instance of pthread_t for the current
  72. * thread if one wasn't explicitly created
  73. */
  74. self = pthread_self ();
  75. if (self.p == NULL)
  76. {
  77. return ENOENT;
  78. }
  79. }
  80. else
  81. {
  82. /*
  83. * Resolve catch-22 of registering thread with selfThread
  84. * key
  85. */
  86. ptw32_thread_t * sp = (ptw32_thread_t *) pthread_getspecific (ptw32_selfThreadKey);
  87. if (sp == NULL)
  88. {
  89. if (value == NULL)
  90. {
  91. return ENOENT;
  92. }
  93. self = *((pthread_t *) value);
  94. }
  95. else
  96. {
  97. self = sp->ptHandle;
  98. }
  99. }
  100. result = 0;
  101. if (key != NULL)
  102. {
  103. if (self.p != NULL && key->destructor != NULL && value != NULL)
  104. {
  105. ptw32_mcs_local_node_t keyLock;
  106. ptw32_mcs_local_node_t threadLock;
  107. ptw32_thread_t * sp = (ptw32_thread_t *) self.p;
  108. /*
  109. * Only require associations if we have to
  110. * call user destroy routine.
  111. * Don't need to locate an existing association
  112. * when setting data to NULL for WIN32 since the
  113. * data is stored with the operating system; not
  114. * on the association; setting assoc to NULL short
  115. * circuits the search.
  116. */
  117. ThreadKeyAssoc *assoc;
  118. ptw32_mcs_lock_acquire(&(key->keyLock), &keyLock);
  119. ptw32_mcs_lock_acquire(&(sp->threadLock), &threadLock);
  120. assoc = (ThreadKeyAssoc *) sp->keys;
  121. /*
  122. * Locate existing association
  123. */
  124. while (assoc != NULL)
  125. {
  126. if (assoc->key == key)
  127. {
  128. /*
  129. * Association already exists
  130. */
  131. break;
  132. }
  133. assoc = assoc->nextKey;
  134. }
  135. /*
  136. * create an association if not found
  137. */
  138. if (assoc == NULL)
  139. {
  140. result = ptw32_tkAssocCreate (sp, key);
  141. }
  142. ptw32_mcs_lock_release(&threadLock);
  143. ptw32_mcs_lock_release(&keyLock);
  144. }
  145. if (result == 0)
  146. {
  147. if (!TlsSetValue (key->key, (LPVOID) value))
  148. {
  149. result = EAGAIN;
  150. }
  151. }
  152. }
  153. return (result);
  154. } /* pthread_setspecific */