2 * -------------------------------------------------------------
7 * Semaphores aren't actually part of PThreads.
8 * They are defined by the POSIX Standard:
12 * -------------------------------------------------------------
14 * Pthreads-win32 - POSIX Threads Library for Win32
15 * Copyright(C) 1998 John E. Bossom
16 * Copyright(C) 1999,2005 Pthreads-win32 contributors
18 * Contact Email: rpj@callisto.canberra.edu.au
20 * The current list of contributors is contained
21 * in the file CONTRIBUTORS included with the source
22 * code distribution. The list can also be seen at the
23 * following World Wide Web location:
24 * http://sources.redhat.com/pthreads-win32/contributors.html
26 * This library is free software; you can redistribute it and/or
27 * modify it under the terms of the GNU Lesser General Public
28 * License as published by the Free Software Foundation; either
29 * version 2 of the License, or (at your option) any later version.
31 * This library is distributed in the hope that it will be useful,
32 * but WITHOUT ANY WARRANTY; without even the implied warranty of
33 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
34 * Lesser General Public License for more details.
36 * You should have received a copy of the GNU Lesser General Public
37 * License along with this library in the file COPYING.LIB;
38 * if not, write to the Free Software Foundation, Inc.,
39 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
43 #include "semaphore.h"
44 #include "implement.h"
47 sem_init (sem_t
* sem
, int pshared
, unsigned int value
)
49 * ------------------------------------------------------
51 * This function initializes a semaphore. The
52 * initial value of the semaphore is 'value'
56 * pointer to an instance of sem_t
59 * if zero, this semaphore may only be shared between
60 * threads in the same process.
61 * if nonzero, the semaphore can be shared between
65 * initial value of the semaphore counter
68 * This function initializes a semaphore. The
69 * initial value of the semaphore is set to 'value'.
72 * 0 successfully created semaphore,
73 * -1 failed, error in errno
75 * EINVAL 'sem' is not a valid semaphore, or
76 * 'value' >= SEM_VALUE_MAX
77 * ENOMEM out of memory,
78 * ENOSPC a required resource has been exhausted,
79 * ENOSYS semaphores are not supported,
80 * EPERM the process lacks appropriate privilege
82 * ------------------------------------------------------
91 * Creating a semaphore that can be shared between
96 else if (value
> (unsigned int)SEM_VALUE_MAX
)
102 s
= (sem_t
) calloc (1, sizeof (*s
));
112 if (pthread_mutex_init(&s
->lock
, NULL
) == 0)
115 #if defined(NEED_SEM)
117 s
->sem
= CreateEvent (NULL
,
118 PTW32_FALSE
, /* auto (not manual) reset */
119 PTW32_FALSE
, /* initial state is unset */
125 (void) pthread_mutex_destroy(&s
->lock
);
130 s
->leftToUnblock
= 0;
135 if ((s
->sem
= CreateSemaphore (NULL
, /* Always NULL */
136 (long) 0, /* Force threads to wait */
137 (long) SEM_VALUE_MAX
, /* Maximum value */
138 NULL
)) == 0) /* Name */
140 (void) pthread_mutex_destroy(&s
->lock
);
144 #endif /* NEED_SEM */