5 * This translation unit implements condition variables and their primitives.
8 * --------------------------------------------------------------------------
10 * Pthreads-win32 - POSIX Threads Library for Win32
11 * Copyright(C) 1998 John E. Bossom
12 * Copyright(C) 1999,2005 Pthreads-win32 contributors
14 * Contact Email: rpj@callisto.canberra.edu.au
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
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.
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.
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
39 #include "implement.h"
43 pthread_cond_init (pthread_cond_t
* cond
, const pthread_condattr_t
* attr
)
45 * ------------------------------------------------------
47 * This function initializes a condition variable.
51 * pointer to an instance of pthread_cond_t
54 * specifies optional creation attributes.
58 * This function initializes a condition variable.
61 * 0 successfully created condition variable,
62 * EINVAL 'attr' is invalid,
63 * EAGAIN insufficient resources (other than
65 * ENOMEM insufficient memory,
66 * EBUSY 'cond' is already initialized,
68 * ------------------------------------------------------
72 pthread_cond_t cv
= NULL
;
79 if ((attr
!= NULL
&& *attr
!= NULL
) &&
80 ((*attr
)->pshared
== PTHREAD_PROCESS_SHARED
))
83 * Creating condition variable that can be shared between
90 cv
= (pthread_cond_t
) calloc (1, sizeof (*cv
));
98 cv
->nWaitersBlocked
= 0;
99 cv
->nWaitersToUnblock
= 0;
100 cv
->nWaitersGone
= 0;
102 if (sem_init (&(cv
->semBlockLock
), 0, 1) != 0)
108 if (sem_init (&(cv
->semBlockQueue
), 0, 0) != 0)
114 if ((result
= pthread_mutex_init (&(cv
->mtxUnblockLock
), 0)) != 0)
129 (void) sem_destroy (&(cv
->semBlockQueue
));
132 (void) sem_destroy (&(cv
->semBlockLock
));
141 ptw32_mcs_local_node_t node
;
143 ptw32_mcs_lock_acquire(&ptw32_cond_list_lock
, &node
);
146 cv
->prev
= ptw32_cond_list_tail
;
148 if (ptw32_cond_list_tail
!= NULL
)
150 ptw32_cond_list_tail
->next
= cv
;
153 ptw32_cond_list_tail
= cv
;
155 if (ptw32_cond_list_head
== NULL
)
157 ptw32_cond_list_head
= cv
;
160 ptw32_mcs_lock_release(&node
);
167 } /* pthread_cond_init */