pthread_key_create.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * pthread_key_create.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. /* TLS_OUT_OF_INDEXES not defined on WinCE */
  39. #if !defined(TLS_OUT_OF_INDEXES)
  40. #define TLS_OUT_OF_INDEXES 0xffffffff
  41. #endif
  42. int
  43. pthread_key_create (pthread_key_t * key, void (PTW32_CDECL *destructor) (void *))
  44. /*
  45. * ------------------------------------------------------
  46. * DOCPUBLIC
  47. * This function creates a thread-specific data key visible
  48. * to all threads. All existing and new threads have a value
  49. * NULL for key until set using pthread_setspecific. When any
  50. * thread with a non-NULL value for key terminates, 'destructor'
  51. * is called with key's current value for that thread.
  52. *
  53. * PARAMETERS
  54. * key
  55. * pointer to an instance of pthread_key_t
  56. *
  57. *
  58. * DESCRIPTION
  59. * This function creates a thread-specific data key visible
  60. * to all threads. All existing and new threads have a value
  61. * NULL for key until set using pthread_setspecific. When any
  62. * thread with a non-NULL value for key terminates, 'destructor'
  63. * is called with key's current value for that thread.
  64. *
  65. * RESULTS
  66. * 0 successfully created semaphore,
  67. * EAGAIN insufficient resources or PTHREAD_KEYS_MAX
  68. * exceeded,
  69. * ENOMEM insufficient memory to create the key,
  70. *
  71. * ------------------------------------------------------
  72. */
  73. {
  74. int result = 0;
  75. pthread_key_t newkey;
  76. if ((newkey = (pthread_key_t) calloc (1, sizeof (*newkey))) == NULL)
  77. {
  78. result = ENOMEM;
  79. }
  80. else if ((newkey->key = TlsAlloc ()) == TLS_OUT_OF_INDEXES)
  81. {
  82. result = EAGAIN;
  83. free (newkey);
  84. newkey = NULL;
  85. }
  86. else if (destructor != NULL)
  87. {
  88. /*
  89. * Have to manage associations between thread and key;
  90. * Therefore, need a lock that allows competing threads
  91. * to gain exclusive access to the key->threads list.
  92. *
  93. * The mutex will only be created when it is first locked.
  94. */
  95. newkey->keyLock = 0;
  96. newkey->destructor = destructor;
  97. }
  98. *key = newkey;
  99. return (result);
  100. }