pte_tkAssocCreate.c 4.0 KB

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