pthread_setspecific.c 5.0 KB

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