pthread_cond_init.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * pthread_cond_init.c
  3. *
  4. * Description:
  5. * This translation unit implements condition variables and their primitives.
  6. *
  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. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
  36. */
  37. #include "pthread.h"
  38. #include "implement.h"
  39. int
  40. pthread_cond_init (pthread_cond_t * cond, const pthread_condattr_t * attr)
  41. /*
  42. * ------------------------------------------------------
  43. * DOCPUBLIC
  44. * This function initializes a condition variable.
  45. *
  46. * PARAMETERS
  47. * cond
  48. * pointer to an instance of pthread_cond_t
  49. *
  50. * attr
  51. * specifies optional creation attributes.
  52. *
  53. *
  54. * DESCRIPTION
  55. * This function initializes a condition variable.
  56. *
  57. * RESULTS
  58. * 0 successfully created condition variable,
  59. * EINVAL 'attr' is invalid,
  60. * EAGAIN insufficient resources (other than
  61. * memory,
  62. * ENOMEM insufficient memory,
  63. * EBUSY 'cond' is already initialized,
  64. *
  65. * ------------------------------------------------------
  66. */
  67. {
  68. int result;
  69. pthread_cond_t cv = NULL;
  70. if (cond == NULL)
  71. {
  72. return EINVAL;
  73. }
  74. if ((attr != NULL && *attr != NULL) &&
  75. ((*attr)->pshared == PTHREAD_PROCESS_SHARED))
  76. {
  77. /*
  78. * Creating condition variable that can be shared between
  79. * processes.
  80. */
  81. result = ENOSYS;
  82. goto DONE;
  83. }
  84. cv = (pthread_cond_t) calloc (1, sizeof (*cv));
  85. if (cv == NULL)
  86. {
  87. result = ENOMEM;
  88. goto DONE;
  89. }
  90. cv->nWaitersBlocked = 0;
  91. cv->nWaitersToUnblock = 0;
  92. cv->nWaitersGone = 0;
  93. if (sem_init (&(cv->semBlockLock), 0, 1) != 0)
  94. {
  95. result = errno;
  96. goto FAIL0;
  97. }
  98. if (sem_init (&(cv->semBlockQueue), 0, 0) != 0)
  99. {
  100. result = errno;
  101. goto FAIL1;
  102. }
  103. if ((result = pthread_mutex_init (&(cv->mtxUnblockLock), 0)) != 0)
  104. {
  105. goto FAIL2;
  106. }
  107. result = 0;
  108. goto DONE;
  109. /*
  110. * -------------
  111. * Failed...
  112. * -------------
  113. */
  114. FAIL2:
  115. (void) sem_destroy (&(cv->semBlockQueue));
  116. FAIL1:
  117. (void) sem_destroy (&(cv->semBlockLock));
  118. FAIL0:
  119. (void) free (cv);
  120. cv = NULL;
  121. DONE:
  122. if (0 == result)
  123. {
  124. ptw32_mcs_local_node_t node;
  125. ptw32_mcs_lock_acquire(&ptw32_cond_list_lock, &node);
  126. cv->next = NULL;
  127. cv->prev = ptw32_cond_list_tail;
  128. if (ptw32_cond_list_tail != NULL)
  129. {
  130. ptw32_cond_list_tail->next = cv;
  131. }
  132. ptw32_cond_list_tail = cv;
  133. if (ptw32_cond_list_head == NULL)
  134. {
  135. ptw32_cond_list_head = cv;
  136. }
  137. ptw32_mcs_lock_release(&node);
  138. }
  139. *cond = cv;
  140. return result;
  141. } /* pthread_cond_init */