/[pcsx2_0.9.7]/trunk/3rdparty/w32pthreads/sem_init.c
ViewVC logotype

Contents of /trunk/3rdparty/w32pthreads/sem_init.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 31 - (show annotations) (download)
Tue Sep 7 03:24:11 2010 UTC (9 years, 11 months ago) by william
File MIME type: text/plain
File size: 4503 byte(s)
committing r3113 initial commit again...
1 /*
2 * -------------------------------------------------------------
3 *
4 * Module: sem_init.c
5 *
6 * Purpose:
7 * Semaphores aren't actually part of PThreads.
8 * They are defined by the POSIX Standard:
9 *
10 * POSIX 1003.1-2001
11 *
12 * -------------------------------------------------------------
13 *
14 * Pthreads-win32 - POSIX Threads Library for Win32
15 * Copyright(C) 1998 John E. Bossom
16 * Copyright(C) 1999,2005 Pthreads-win32 contributors
17 *
18 * Contact Email: rpj@callisto.canberra.edu.au
19 *
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
25 *
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.
30 *
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.
35 *
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
40 */
41
42 #include "ptw32pch.h"
43 #include "semaphore.h"
44
45 int
46 sem_init (sem_t * sem, int pshared, unsigned int value)
47 /*
48 * ------------------------------------------------------
49 * DOCPUBLIC
50 * This function initializes a semaphore. The
51 * initial value of the semaphore is 'value'
52 *
53 * PARAMETERS
54 * sem
55 * pointer to an instance of sem_t
56 *
57 * pshared
58 * if zero, this semaphore may only be shared between
59 * threads in the same process.
60 * if nonzero, the semaphore can be shared between
61 * processes
62 *
63 * value
64 * initial value of the semaphore counter
65 *
66 * DESCRIPTION
67 * This function initializes a semaphore. The
68 * initial value of the semaphore is set to 'value'.
69 *
70 * RESULTS
71 * 0 successfully created semaphore,
72 * -1 failed, error in errno
73 * ERRNO
74 * EINVAL 'sem' is not a valid semaphore, or
75 * 'value' >= SEM_VALUE_MAX
76 * ENOMEM out of memory,
77 * ENOSPC a required resource has been exhausted,
78 * ENOSYS semaphores are not supported,
79 * EPERM the process lacks appropriate privilege
80 *
81 * ------------------------------------------------------
82 */
83 {
84 int result = 0;
85 sem_t s = NULL;
86
87 if (pshared != 0)
88 {
89 /*
90 * Creating a semaphore that can be shared between
91 * processes
92 */
93 result = EPERM;
94 }
95 else if (value > (unsigned int)SEM_VALUE_MAX)
96 {
97 result = EINVAL;
98 }
99 else
100 {
101 s = (sem_t) calloc (1, sizeof (*s));
102
103 if (NULL == s)
104 {
105 result = ENOMEM;
106 }
107 else
108 {
109
110 s->value = value;
111 if (pthread_mutex_init(&s->lock, NULL) == 0)
112 {
113
114 #ifdef NEED_SEM
115
116 s->sem = CreateEvent (NULL,
117 PTW32_FALSE, /* auto (not manual) reset */
118 PTW32_FALSE, /* initial state is unset */
119 NULL);
120
121 if (0 == s->sem)
122 {
123 free (s);
124 (void) pthread_mutex_destroy(&s->lock);
125 result = ENOSPC;
126 }
127 else
128 {
129 s->leftToUnblock = 0;
130 }
131
132 #else /* NEED_SEM */
133
134 if ((s->sem = CreateSemaphore (NULL, /* Always NULL */
135 (long) 0, /* Force threads to wait */
136 (long) SEM_VALUE_MAX, /* Maximum value */
137 NULL)) == 0) /* Name */
138 {
139 (void) pthread_mutex_destroy(&s->lock);
140 result = ENOSPC;
141 }
142
143 #endif /* NEED_SEM */
144
145 }
146 else
147 {
148 result = ENOSPC;
149 }
150
151 if (result != 0)
152 {
153 free(s);
154 }
155 }
156 }
157
158 if (result != 0)
159 {
160 errno = result;
161 return -1;
162 }
163
164 *sem = s;
165
166 return 0;
167
168 } /* sem_init */

  ViewVC Help
Powered by ViewVC 1.1.22