ptw32_tkAssocCreate.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * ptw32_tkAssocCreate.c
  3. *
  4. * Description:
  5. * This translation unit implements routines which are private to
  6. * the implementation and may be used throughout it.
  7. *
  8. * --------------------------------------------------------------------------
  9. *
  10. * Pthreads-win32 - POSIX Threads Library for Win32
  11. * Copyright(C) 1998 John E. Bossom
  12. * Copyright(C) 1999,2005 Pthreads-win32 contributors
  13. *
  14. * Contact Email: [email protected]
  15. *
  16. * The current list of contributors is contained
  17. * in the file CONTRIBUTORS included with the source
  18. * code distribution. The list can also be seen at the
  19. * following World Wide Web location:
  20. * http://sources.redhat.com/pthreads-win32/contributors.html
  21. *
  22. * This library is free software; you can redistribute it and/or
  23. * modify it under the terms of the GNU Lesser General Public
  24. * License as published by the Free Software Foundation; either
  25. * version 2 of the License, or (at your option) any later version.
  26. *
  27. * This library is distributed in the hope that it will be useful,
  28. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  29. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  30. * Lesser General Public License for more details.
  31. *
  32. * You should have received a copy of the GNU Lesser General Public
  33. * License along with this library in the file COPYING.LIB;
  34. * if not, write to the Free Software Foundation, Inc.,
  35. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  36. */
  37. #include "pthread.h"
  38. #include "implement.h"
  39. int
  40. ptw32_tkAssocCreate (ptw32_thread_t * sp, pthread_key_t key)
  41. /*
  42. * -------------------------------------------------------------------
  43. * This routine creates an association that
  44. * is unique for the given (thread,key) combination.The association
  45. * is referenced by both the thread and the key.
  46. * This association allows us to determine what keys the
  47. * current thread references and what threads a given key
  48. * references.
  49. * See the detailed description
  50. * at the beginning of this file for further details.
  51. *
  52. * Notes:
  53. * 1) New associations are pushed to the beginning of the
  54. * chain so that the internal ptw32_selfThreadKey association
  55. * is always last, thus allowing selfThreadExit to
  56. * be implicitly called last by pthread_exit.
  57. * 2)
  58. *
  59. * Parameters:
  60. * thread
  61. * current running thread.
  62. * key
  63. * key on which to create an association.
  64. * Returns:
  65. * 0 - if successful,
  66. * ENOMEM - not enough memory to create assoc or other object
  67. * EINVAL - an internal error occurred
  68. * ENOSYS - an internal error occurred
  69. * -------------------------------------------------------------------
  70. */
  71. {
  72. ThreadKeyAssoc *assoc;
  73. /*
  74. * Have to create an association and add it
  75. * to both the key and the thread.
  76. *
  77. * Both key->keyLock and thread->threadLock are locked before
  78. * entry to this routine.
  79. */
  80. assoc = (ThreadKeyAssoc *) calloc (1, sizeof (*assoc));
  81. if (assoc == NULL)
  82. {
  83. return ENOMEM;
  84. }
  85. assoc->thread = sp;
  86. assoc->key = key;
  87. /*
  88. * Register assoc with key
  89. */
  90. assoc->prevThread = NULL;
  91. assoc->nextThread = (ThreadKeyAssoc *) key->threads;
  92. if (assoc->nextThread != NULL)
  93. {
  94. assoc->nextThread->prevThread = assoc;
  95. }
  96. key->threads = (void *) assoc;
  97. /*
  98. * Register assoc with thread
  99. */
  100. assoc->prevKey = NULL;
  101. assoc->nextKey = (ThreadKeyAssoc *) sp->keys;
  102. if (assoc->nextKey != NULL)
  103. {
  104. assoc->nextKey->prevKey = assoc;
  105. }
  106. sp->keys = (void *) assoc;
  107. return (0);
  108. } /* ptw32_tkAssocCreate */