| 1 |
/* |
| 2 |
* Copyright (c) 1994 by Xerox Corporation. All rights reserved. |
| 3 |
* Copyright (c) 1996 by Silicon Graphics. All rights reserved. |
| 4 |
* Copyright (c) 1998 by Fergus Henderson. All rights reserved. |
| 5 |
* Copyright (c) 2000-2009 by Hewlett-Packard Development Company. |
| 6 |
* All rights reserved. |
| 7 |
* |
| 8 |
* THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED |
| 9 |
* OR IMPLIED. ANY USE IS AT YOUR OWN RISK. |
| 10 |
* |
| 11 |
* Permission is hereby granted to use or copy this program |
| 12 |
* for any purpose, provided the above notices are retained on all copies. |
| 13 |
* Permission to modify the code and to distribute modified code is granted, |
| 14 |
* provided the above notices are retained, and a notice that the code was |
| 15 |
* modified is included with the above copyright notice. |
| 16 |
*/ |
| 17 |
|
| 18 |
#ifndef GC_DARWIN_SEMAPHORE_H |
| 19 |
#define GC_DARWIN_SEMAPHORE_H |
| 20 |
|
| 21 |
#if !defined(GC_DARWIN_THREADS) && !defined(GC_WIN32_THREADS) |
| 22 |
# error darwin_semaphore.h included for improper target |
| 23 |
#endif |
| 24 |
|
| 25 |
#ifdef __cplusplus |
| 26 |
extern "C" { |
| 27 |
#endif |
| 28 |
|
| 29 |
/* This is a very simple semaphore implementation based on pthreads. */ |
| 30 |
/* It is not async-signal safe. But this is not a problem because */ |
| 31 |
/* signals are not used to suspend threads on the target. */ |
| 32 |
|
| 33 |
typedef struct { |
| 34 |
pthread_mutex_t mutex; |
| 35 |
pthread_cond_t cond; |
| 36 |
int value; |
| 37 |
} sem_t; |
| 38 |
|
| 39 |
GC_INLINE int sem_init(sem_t *sem, int pshared, int value) { |
| 40 |
if (pshared != 0) { |
| 41 |
errno = EPERM; /* unsupported */ |
| 42 |
return -1; |
| 43 |
} |
| 44 |
sem->value = value; |
| 45 |
if (pthread_mutex_init(&sem->mutex, NULL) != 0) |
| 46 |
return -1; |
| 47 |
if (pthread_cond_init(&sem->cond, NULL) != 0) { |
| 48 |
(void)pthread_mutex_destroy(&sem->mutex); |
| 49 |
return -1; |
| 50 |
} |
| 51 |
return 0; |
| 52 |
} |
| 53 |
|
| 54 |
GC_INLINE int sem_post(sem_t *sem) { |
| 55 |
if (pthread_mutex_lock(&sem->mutex) != 0) |
| 56 |
return -1; |
| 57 |
sem->value++; |
| 58 |
if (pthread_cond_signal(&sem->cond) != 0) { |
| 59 |
(void)pthread_mutex_unlock(&sem->mutex); |
| 60 |
return -1; |
| 61 |
} |
| 62 |
return pthread_mutex_unlock(&sem->mutex) != 0 ? -1 : 0; |
| 63 |
} |
| 64 |
|
| 65 |
GC_INLINE int sem_wait(sem_t *sem) { |
| 66 |
if (pthread_mutex_lock(&sem->mutex) != 0) |
| 67 |
return -1; |
| 68 |
while (sem->value == 0) { |
| 69 |
if (pthread_cond_wait(&sem->cond, &sem->mutex) != 0) { |
| 70 |
(void)pthread_mutex_unlock(&sem->mutex); |
| 71 |
return -1; |
| 72 |
} |
| 73 |
} |
| 74 |
sem->value--; |
| 75 |
return pthread_mutex_unlock(&sem->mutex) != 0 ? -1 : 0; |
| 76 |
} |
| 77 |
|
| 78 |
GC_INLINE int sem_destroy(sem_t *sem) { |
| 79 |
return pthread_cond_destroy(&sem->cond) != 0 |
| 80 |
|| pthread_mutex_destroy(&sem->mutex) != 0 ? -1 : 0; |
| 81 |
} |
| 82 |
|
| 83 |
#ifdef __cplusplus |
| 84 |
} /* extern "C" */ |
| 85 |
#endif |
| 86 |
|
| 87 |
#endif |