| 1 |
/* |
|---|
| 2 |
* Copyright (c) 2005-2007, OmniTI Computer Consulting, Inc. |
|---|
| 3 |
* All rights reserved. |
|---|
| 4 |
* |
|---|
| 5 |
* Redistribution and use in source and binary forms, with or without |
|---|
| 6 |
* modification, are permitted provided that the following conditions are |
|---|
| 7 |
* met: |
|---|
| 8 |
* |
|---|
| 9 |
* * Redistributions of source code must retain the above copyright |
|---|
| 10 |
* notice, this list of conditions and the following disclaimer. |
|---|
| 11 |
* * Redistributions in binary form must reproduce the above |
|---|
| 12 |
* copyright notice, this list of conditions and the following |
|---|
| 13 |
* disclaimer in the documentation and/or other materials provided |
|---|
| 14 |
* with the distribution. |
|---|
| 15 |
* * Neither the name OmniTI Computer Consulting, Inc. nor the names |
|---|
| 16 |
* of its contributors may be used to endorse or promote products |
|---|
| 17 |
* derived from this software without specific prior written |
|---|
| 18 |
* permission. |
|---|
| 19 |
* |
|---|
| 20 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|---|
| 21 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|---|
| 22 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|---|
| 23 |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|---|
| 24 |
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|---|
| 25 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|---|
| 26 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|---|
| 27 |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|---|
| 28 |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|---|
| 29 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|---|
| 30 |
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|---|
| 31 |
*/ |
|---|
| 32 |
|
|---|
| 33 |
#ifndef _UTILS_NOIT_SEM_H |
|---|
| 34 |
#define _UTILS_NOIT_SEM_H |
|---|
| 35 |
|
|---|
| 36 |
/*! \fn int noit_sem_init(noit_sem_t *s, int unused, int value) |
|---|
| 37 |
\brief initializes a counting semaphore for first time use. |
|---|
| 38 |
\param s the semaphore to be initialized |
|---|
| 39 |
\param unused is unused (keeps API combatibility with sem_init() |
|---|
| 40 |
\param value sets the initial value of the semaphore |
|---|
| 41 |
\return 0 on success or -1 on failure |
|---|
| 42 |
*/ |
|---|
| 43 |
/*! \fn int noit_sem_wait(noit_sem_t *s) |
|---|
| 44 |
\brief decrements the value of the semaphore waiting if required. |
|---|
| 45 |
\param s the semaphore on which to wait |
|---|
| 46 |
\return 0 on success or -1 on failure |
|---|
| 47 |
*/ |
|---|
| 48 |
/*! \fn int noit_sem_post(noit_sem_t *s) |
|---|
| 49 |
\brief increments the value of the semaphore releasing any waiters. |
|---|
| 50 |
\param s the semaphore on which to wait |
|---|
| 51 |
\return 0 on success or -1 on failure |
|---|
| 52 |
*/ |
|---|
| 53 |
/*! \fn int noit_sem_trywait(noit_sem_t *s) |
|---|
| 54 |
\brief decrements the value of the semaphore if greater than 0 or fails |
|---|
| 55 |
\param s the semaphore on which to wait |
|---|
| 56 |
\return 0 on success or -1 on failure |
|---|
| 57 |
*/ |
|---|
| 58 |
/*! \fn int noit_sem_getvalue(noit_sem_t *s, int *value) |
|---|
| 59 |
\brief retrieves the current value of a semaphore, placing it in *value |
|---|
| 60 |
\param s the semaphore on which to operate |
|---|
| 61 |
\param value a pointer an integer that will be populated with the current value of the semaphore |
|---|
| 62 |
\return 0 on success or -1 on failure |
|---|
| 63 |
*/ |
|---|
| 64 |
/*! \fn int noit_sem_destroy(noit_sem_t *s) |
|---|
| 65 |
\brief releases all resources related to a semaphore |
|---|
| 66 |
\param s the semaphore to destroy |
|---|
| 67 |
\return 0 on success or -1 on failure |
|---|
| 68 |
*/ |
|---|
| 69 |
#ifndef WORKING_SEM_INIT |
|---|
| 70 |
|
|---|
| 71 |
#include <pthread.h> |
|---|
| 72 |
|
|---|
| 73 |
typedef struct { |
|---|
| 74 |
unsigned int value; |
|---|
| 75 |
pthread_mutex_t lock; |
|---|
| 76 |
pthread_cond_t cond; |
|---|
| 77 |
} noit_sem_t; |
|---|
| 78 |
|
|---|
| 79 |
API_EXPORT(int) noit_sem_init(noit_sem_t *, int, int); |
|---|
| 80 |
API_EXPORT(int) noit_sem_wait(noit_sem_t *); |
|---|
| 81 |
API_EXPORT(void) noit_sem_post(noit_sem_t *); |
|---|
| 82 |
API_EXPORT(int) noit_sem_trywait(noit_sem_t *); |
|---|
| 83 |
API_EXPORT(int) noit_sem_getvalue(noit_sem_t *, int *); |
|---|
| 84 |
API_EXPORT(void) noit_sem_destroy(noit_sem_t *); |
|---|
| 85 |
|
|---|
| 86 |
#define sem_t noit_sem_t |
|---|
| 87 |
#define sem_init noit_sem_init |
|---|
| 88 |
#define sem_wait noit_sem_wait |
|---|
| 89 |
#define sem_trywait noit_sem_trywait |
|---|
| 90 |
#define sem_post noit_sem_post |
|---|
| 91 |
#define sem_getvalue noit_sem_getvalue |
|---|
| 92 |
#define sem_destroy noit_sem_destroy |
|---|
| 93 |
|
|---|
| 94 |
#else /* WORKING_SEM_INIT */ |
|---|
| 95 |
|
|---|
| 96 |
#ifdef HAVE_SEMAPHORE_H |
|---|
| 97 |
#include <semaphore.h> |
|---|
| 98 |
#endif |
|---|
| 99 |
|
|---|
| 100 |
#endif |
|---|
| 101 |
|
|---|
| 102 |
#endif |
|---|