| 1 | /* | 
|---|
| 2 | * pthread.h - User level <semaphore> library definition. | 
|---|
| 3 | * | 
|---|
| 4 | * Author     Alain Greiner (2016,2017,2018) | 
|---|
| 5 | * | 
|---|
| 6 | * Copyright (c) UPMC Sorbonne Universites | 
|---|
| 7 | * | 
|---|
| 8 | * This file is part of ALMOS-MKH. | 
|---|
| 9 | * | 
|---|
| 10 | * ALMOS-MKH is free software; you can redistribute it and/or modify it | 
|---|
| 11 | * under the terms of the GNU General Public License as published by | 
|---|
| 12 | * the Free Software Foundation; version 2.0 of the License. | 
|---|
| 13 | * | 
|---|
| 14 | * ALMOS-MKH is distributed in the hope that it will be useful, but | 
|---|
| 15 | * WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU | 
|---|
| 17 | * General Public License for more details. | 
|---|
| 18 | * | 
|---|
| 19 | * You should have received a copy of the GNU General Public License | 
|---|
| 20 | * along with ALMOS-MKH; if not, write to the Free Software Foundation, | 
|---|
| 21 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | 
|---|
| 22 | */ | 
|---|
| 23 |  | 
|---|
| 24 | #ifndef _SEMAPHORE_H_ | 
|---|
| 25 | #define _SEMAPHORE_H_ | 
|---|
| 26 |  | 
|---|
| 27 | ////////////////////////////////////////////////////////////////////////////////////////////// | 
|---|
| 28 | //             POSIX unnamed semaphores related functions | 
|---|
| 29 | ////////////////////////////////////////////////////////////////////////////////////////////// | 
|---|
| 30 |  | 
|---|
| 31 | #include <shared_semaphore.h> | 
|---|
| 32 |  | 
|---|
| 33 | /********************************************************************************************* | 
|---|
| 34 | * This function initializes an unnamed semaphore. | 
|---|
| 35 | * Initializing a semaphore that has already been initialized results in undefined behavior. | 
|---|
| 36 | ********************************************************************************************* | 
|---|
| 37 | * @ sem         : [in]  pointer on semaphore. | 
|---|
| 38 | * @ pshared     : [in]  unsupported => must be zero. | 
|---|
| 39 | * @ value       : [in]  initial semaphore value. | 
|---|
| 40 | * @ return 0 if success / return -1 if failure. | 
|---|
| 41 | ********************************************************************************************/ | 
|---|
| 42 | int sem_init( sem_t        * sem, | 
|---|
| 43 | int            pshared, | 
|---|
| 44 | unsigned int   value ); | 
|---|
| 45 |  | 
|---|
| 46 | /********************************************************************************************* | 
|---|
| 47 | * This blocking function takes a semaphore. It decrements the semaphore pointed to by <sem>. | 
|---|
| 48 | * If the semaphore's value is greater than zero, then the decrement proceeds, and the | 
|---|
| 49 | * function returns immediately. If the semaphore currently has the value zero, the calling | 
|---|
| 50 | * thread registers in the associated waiting queue, blocks and deschedules. | 
|---|
| 51 | * It will be unblocked by another thread when it becomes possible to perform the decrement. | 
|---|
| 52 | ********************************************************************************************* | 
|---|
| 53 | * @ sem         : [in]  pointer on semaphore. | 
|---|
| 54 | * @ return 0 if success / return -1 if failure. | 
|---|
| 55 | ********************************************************************************************/ | 
|---|
| 56 | int sem_wait( sem_t * sem ); | 
|---|
| 57 |  | 
|---|
| 58 | /********************************************************************************************* | 
|---|
| 59 | * This function releases a semaphore. It increments the semaphore pointed to by <sem>. | 
|---|
| 60 | * If the semaphore's value consequently becomes greater than zero, then another thread | 
|---|
| 61 | * blocked in a sem_wait() call will be woken up to try to take the semaphore. | 
|---|
| 62 | ********************************************************************************************* | 
|---|
| 63 | * @ sem         : [in]  pointer on semaphore. | 
|---|
| 64 | * @ return 0 if success / return -1 if failure. | 
|---|
| 65 | ********************************************************************************************/ | 
|---|
| 66 | int sem_post( sem_t * sem ); | 
|---|
| 67 |  | 
|---|
| 68 | /********************************************************************************************* | 
|---|
| 69 | * This function destroys the semaphore pointed to by <sem>. | 
|---|
| 70 | ********************************************************************************************* | 
|---|
| 71 | * @ sem         : [in]  pointer on semaphore. | 
|---|
| 72 | * @ return 0 if success / return -1 if failure. | 
|---|
| 73 | ********************************************************************************************/ | 
|---|
| 74 | int sem_destroy( sem_t * sem ); | 
|---|
| 75 |  | 
|---|
| 76 | /********************************************************************************************* | 
|---|
| 77 | * This function places the current value of the semaphore pointed to by <sem> | 
|---|
| 78 | * into the integer pointed to by <sval>. | 
|---|
| 79 | ********************************************************************************************* | 
|---|
| 80 | * @ sem         : [in]  pointer on semaphore. | 
|---|
| 81 | * @ value       : [out] buffer for semaphore current value. | 
|---|
| 82 | * @ return 0 if success / return -1 if failure. | 
|---|
| 83 | ********************************************************************************************/ | 
|---|
| 84 | int sem_getvalue( sem_t * sem, | 
|---|
| 85 | int   * value ); | 
|---|
| 86 |  | 
|---|
| 87 |  | 
|---|
| 88 | #endif  // _SEMAPHORE_H_ | 
|---|