| 140 | | int gen_win_cond_init(HANDLE cond, void *attr) |
| 141 | | { |
| 142 | | return pthread_cond_init(cond, attr); |
| | 170 | int gen_win_cond_init(pgen_cond_t *cond) |
| | 171 | { |
| | 172 | int rc; |
| | 173 | pgen_cond_t cv = NULL; |
| | 174 | |
| | 175 | if (!cond) |
| | 176 | { |
| | 177 | return EINVAL; |
| | 178 | } |
| | 179 | |
| | 180 | /* Allocate condition variable */ |
| | 181 | cv = (pgen_cond_t) calloc(1, sizeof(*cv)); |
| | 182 | if (cv == NULL) |
| | 183 | { |
| | 184 | rc = ENOMEM; |
| | 185 | goto DONE; |
| | 186 | } |
| | 187 | |
| | 188 | /* Create locking semaphore */ |
| | 189 | cv->semBlockLock = CreateSemaphore(NULL, 1, LONG_MAX, NULL); |
| | 190 | if (cv->semBlockLock == NULL) |
| | 191 | { |
| | 192 | rc = (int) GetLastError(); |
| | 193 | goto FAIL0; |
| | 194 | } |
| | 195 | |
| | 196 | /* Create queue semaphore */ |
| | 197 | cv->semBlockQueue = CreateSemaphore(NULL, 0, LONG_MAX, NULL); |
| | 198 | if (cv->semBlockQueue == NULL) |
| | 199 | { |
| | 200 | rc = (int) GetLastError(); |
| | 201 | goto FAIL1; |
| | 202 | } |
| | 203 | |
| | 204 | /* Create unblock/lock mutex */ |
| | 205 | if ((rc = gen_mutex_init(&(cv->mtxUnblockLock))) != 0) |
| | 206 | { |
| | 207 | goto FAIL2; |
| | 208 | } |
| | 209 | |
| | 210 | rc = 0; |
| | 211 | |
| | 212 | goto DONE; |
| | 213 | |
| | 214 | /* |
| | 215 | * Error conditions |
| | 216 | */ |
| | 217 | FAIL2: |
| | 218 | CloseHandle(cv->semBlockQueue); |
| | 219 | |
| | 220 | FAIL1: |
| | 221 | CloseHandle(cv->semBlockLock); |
| | 222 | |
| | 223 | FAIL0: |
| | 224 | free(cv); |
| | 225 | cv = NULL; |
| | 226 | |
| | 227 | DONE: |
| | 228 | if (rc == 0) |
| | 229 | { |
| | 230 | if (cond_list_lock == NULL) |
| | 231 | { |
| | 232 | InitializeCriticalSection(cond_list_lock); |
| | 233 | } |
| | 234 | |
| | 235 | EnterCriticalSection(cond_list_lock); |
| | 236 | |
| | 237 | cv->next = NULL; |
| | 238 | cv->prev = cond_list_tail; |
| | 239 | |
| | 240 | if (cond_list_tail != NULL) |
| | 241 | { |
| | 242 | cond_list_tail->next = cv; |
| | 243 | } |
| | 244 | |
| | 245 | cond_list_tail = cv; |
| | 246 | |
| | 247 | if (cond_list_head == NULL) |
| | 248 | { |
| | 249 | cond_list_head = cv; |
| | 250 | } |
| | 251 | |
| | 252 | LeaveCriticalSection(cond_list_lock); |
| | 253 | |
| | 254 | } |
| | 255 | |
| | 256 | *cond = cv; |
| | 257 | |
| | 258 | return rc; |