pthread_cond_init.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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-embedded (PTE) - POSIX Threads Library for embedded systems
  11. * Copyright(C) 2008 Jason Schmidlapp
  12. *
  13. * Contact Email: [email protected]
  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: [email protected]
  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. pthread_cond_init (pthread_cond_t * cond, const pthread_condattr_t * attr)
  48. /*
  49. * ------------------------------------------------------
  50. * DOCPUBLIC
  51. * This function initializes a condition variable.
  52. *
  53. * PARAMETERS
  54. * cond
  55. * pointer to an instance of pthread_cond_t
  56. *
  57. * attr
  58. * specifies optional creation attributes.
  59. *
  60. *
  61. * DESCRIPTION
  62. * This function initializes a condition variable.
  63. *
  64. * RESULTS
  65. * 0 successfully created condition variable,
  66. * EINVAL 'attr' is invalid,
  67. * EAGAIN insufficient resources (other than
  68. * memory,
  69. * ENOMEM insufficient memory,
  70. * EBUSY 'cond' is already initialized,
  71. *
  72. * ------------------------------------------------------
  73. */
  74. {
  75. int result;
  76. pthread_cond_t cv = NULL;
  77. if (cond == NULL)
  78. {
  79. return EINVAL;
  80. }
  81. if ((attr != NULL && *attr != NULL) &&
  82. ((*attr)->pshared == PTHREAD_PROCESS_SHARED))
  83. {
  84. /*
  85. * Creating condition variable that can be shared between
  86. * processes.
  87. */
  88. result = ENOSYS;
  89. goto DONE;
  90. }
  91. cv = (pthread_cond_t) calloc (1, sizeof (*cv));
  92. if (cv == NULL)
  93. {
  94. result = ENOMEM;
  95. goto DONE;
  96. }
  97. cv->nWaitersBlocked = 0;
  98. cv->nWaitersToUnblock = 0;
  99. cv->nWaitersGone = 0;
  100. if (sem_init (&(cv->semBlockLock), 0, 1) != 0)
  101. {
  102. result = errno;
  103. goto FAIL0;
  104. }
  105. if (sem_init (&(cv->semBlockQueue), 0, 0) != 0)
  106. {
  107. result = errno;
  108. goto FAIL1;
  109. }
  110. if ((result = pthread_mutex_init (&(cv->mtxUnblockLock), 0)) != 0)
  111. {
  112. goto FAIL2;
  113. }
  114. result = 0;
  115. goto DONE;
  116. /*
  117. * -------------
  118. * Failed...
  119. * -------------
  120. */
  121. FAIL2:
  122. (void) sem_destroy (&(cv->semBlockQueue));
  123. FAIL1:
  124. (void) sem_destroy (&(cv->semBlockLock));
  125. FAIL0:
  126. (void) free (cv);
  127. cv = NULL;
  128. DONE:
  129. if (0 == result)
  130. {
  131. pte_osMutexLock (pte_cond_list_lock);
  132. cv->next = NULL;
  133. cv->prev = pte_cond_list_tail;
  134. if (pte_cond_list_tail != NULL)
  135. {
  136. pte_cond_list_tail->next = cv;
  137. }
  138. pte_cond_list_tail = cv;
  139. if (pte_cond_list_head == NULL)
  140. {
  141. pte_cond_list_head = cv;
  142. }
  143. pte_osMutexUnlock(pte_cond_list_lock);
  144. }
  145. *cond = cv;
  146. return result;
  147. } /* pthread_cond_init */