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

Contents of /trunk/3rdparty/w32pthreads/pthread_cond_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: 4266 byte(s)
committing r3113 initial commit again...
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: rpj@callisto.canberra.edu.au
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
38 #include "ptw32pch.h"
39
40
41 int
42 pthread_cond_init (pthread_cond_t * cond, const pthread_condattr_t * attr)
43 /*
44 * ------------------------------------------------------
45 * DOCPUBLIC
46 * This function initializes a condition variable.
47 *
48 * PARAMETERS
49 * cond
50 * pointer to an instance of pthread_cond_t
51 *
52 * attr
53 * specifies optional creation attributes.
54 *
55 *
56 * DESCRIPTION
57 * This function initializes a condition variable.
58 *
59 * RESULTS
60 * 0 successfully created condition variable,
61 * EINVAL 'attr' is invalid,
62 * EAGAIN insufficient resources (other than
63 * memory,
64 * ENOMEM insufficient memory,
65 * EBUSY 'cond' is already initialized,
66 *
67 * ------------------------------------------------------
68 */
69 {
70 int result;
71 pthread_cond_t cv = NULL;
72
73 if (cond == NULL)
74 {
75 return EINVAL;
76 }
77
78 #ifdef PTW32_STATIC_LIB
79 // This allos for C++ static initializers to function without crashes. (air)
80 pthread_win32_process_attach_np();
81 #endif
82
83 if ((attr != NULL && *attr != NULL) &&
84 ((*attr)->pshared == PTHREAD_PROCESS_SHARED))
85 {
86 /*
87 * Creating condition variable that can be shared between
88 * processes.
89 */
90 result = ENOSYS;
91 goto DONE;
92 }
93
94 cv = (pthread_cond_t) calloc (1, sizeof (*cv));
95
96 if (cv == NULL)
97 {
98 result = ENOMEM;
99 goto DONE;
100 }
101
102 cv->nWaitersBlocked = 0;
103 cv->nWaitersToUnblock = 0;
104 cv->nWaitersGone = 0;
105
106 if (sem_init (&(cv->semBlockLock), 0, 1) != 0)
107 {
108 result = errno;
109 goto FAIL0;
110 }
111
112 if (sem_init (&(cv->semBlockQueue), 0, 0) != 0)
113 {
114 result = errno;
115 goto FAIL1;
116 }
117
118 if ((result = pthread_mutex_init (&(cv->mtxUnblockLock), 0)) != 0)
119 {
120 goto FAIL2;
121 }
122
123 result = 0;
124
125 goto DONE;
126
127 /*
128 * -------------
129 * Failed...
130 * -------------
131 */
132 FAIL2:
133 (void) sem_destroy (&(cv->semBlockQueue));
134
135 FAIL1:
136 (void) sem_destroy (&(cv->semBlockLock));
137
138 FAIL0:
139 (void) free (cv);
140 cv = NULL;
141
142 DONE:
143 if (0 == result)
144 {
145 EnterCriticalSection (&ptw32_cond_list_lock);
146
147 cv->next = NULL;
148 cv->prev = ptw32_cond_list_tail;
149
150 if (ptw32_cond_list_tail != NULL)
151 {
152 ptw32_cond_list_tail->next = cv;
153 }
154
155 ptw32_cond_list_tail = cv;
156
157 if (ptw32_cond_list_head == NULL)
158 {
159 ptw32_cond_list_head = cv;
160 }
161
162 LeaveCriticalSection (&ptw32_cond_list_lock);
163 }
164
165 *cond = cv;
166
167 return result;
168
169 } /* pthread_cond_init */

  ViewVC Help
Powered by ViewVC 1.1.22