[469] | 1 | /* |
---|
| 2 | * pthread.h - User level <semaphore> library definition. |
---|
| 3 | * |
---|
[573] | 4 | * Author Alain Greiner (2016,2017,2018) |
---|
[469] | 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 |
---|
[619] | 29 | // |
---|
| 30 | // This synchonisation object allows several thread in a given process to share one |
---|
| 31 | // (or several) shared resource(s). |
---|
| 32 | // - The sem_wait() function allows a thread to take one resource and atomically |
---|
| 33 | // decrement the semaphore count. If the count value is zero, the calling thread |
---|
| 34 | // blocks and deschedules. It is unblocked by another thread when it becomes possible |
---|
| 35 | // to make the decrement. |
---|
| 36 | // - the sem_post() functions allows a thread to release a resource and atomicalli |
---|
| 37 | // increment the semaphore count. |
---|
[469] | 38 | ////////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 39 | |
---|
| 40 | #include <shared_semaphore.h> |
---|
| 41 | |
---|
| 42 | /********************************************************************************************* |
---|
| 43 | * This function initializes an unnamed semaphore. |
---|
| 44 | * Initializing a semaphore that has already been initialized results in undefined behavior. |
---|
| 45 | ********************************************************************************************* |
---|
| 46 | * @ sem : [in] pointer on semaphore. |
---|
| 47 | * @ pshared : [in] unsupported => must be zero. |
---|
[619] | 48 | * @ count : [in] initial semaphore value. |
---|
[469] | 49 | * @ return 0 if success / return -1 if failure. |
---|
| 50 | ********************************************************************************************/ |
---|
| 51 | int sem_init( sem_t * sem, |
---|
| 52 | int pshared, |
---|
[619] | 53 | unsigned int count ); |
---|
[469] | 54 | |
---|
| 55 | /********************************************************************************************* |
---|
[573] | 56 | * This blocking function takes a semaphore. It decrements the semaphore pointed to by <sem>. |
---|
[469] | 57 | * If the semaphore's value is greater than zero, then the decrement proceeds, and the |
---|
[573] | 58 | * function returns immediately. If the semaphore currently has the value zero, the calling |
---|
| 59 | * thread registers in the associated waiting queue, blocks and deschedules. |
---|
| 60 | * It will be unblocked by another thread when it becomes possible to perform the decrement. |
---|
[469] | 61 | ********************************************************************************************* |
---|
| 62 | * @ sem : [in] pointer on semaphore. |
---|
| 63 | * @ return 0 if success / return -1 if failure. |
---|
| 64 | ********************************************************************************************/ |
---|
| 65 | int sem_wait( sem_t * sem ); |
---|
| 66 | |
---|
| 67 | /********************************************************************************************* |
---|
[573] | 68 | * This function releases a semaphore. It increments the semaphore pointed to by <sem>. |
---|
[469] | 69 | * If the semaphore's value consequently becomes greater than zero, then another thread |
---|
[573] | 70 | * blocked in a sem_wait() call will be woken up to try to take the semaphore. |
---|
[469] | 71 | ********************************************************************************************* |
---|
| 72 | * @ sem : [in] pointer on semaphore. |
---|
| 73 | * @ return 0 if success / return -1 if failure. |
---|
| 74 | ********************************************************************************************/ |
---|
| 75 | int sem_post( sem_t * sem ); |
---|
| 76 | |
---|
| 77 | /********************************************************************************************* |
---|
| 78 | * This function destroys the semaphore pointed to by <sem>. |
---|
| 79 | ********************************************************************************************* |
---|
| 80 | * @ sem : [in] pointer on semaphore. |
---|
| 81 | * @ return 0 if success / return -1 if failure. |
---|
| 82 | ********************************************************************************************/ |
---|
| 83 | int sem_destroy( sem_t * sem ); |
---|
| 84 | |
---|
| 85 | /********************************************************************************************* |
---|
| 86 | * This function places the current value of the semaphore pointed to by <sem> |
---|
| 87 | * into the integer pointed to by <sval>. |
---|
| 88 | ********************************************************************************************* |
---|
| 89 | * @ sem : [in] pointer on semaphore. |
---|
| 90 | * @ value : [out] buffer for semaphore current value. |
---|
| 91 | * @ return 0 if success / return -1 if failure. |
---|
| 92 | ********************************************************************************************/ |
---|
| 93 | int sem_getvalue( sem_t * sem, |
---|
| 94 | int * value ); |
---|
| 95 | |
---|
| 96 | |
---|
| 97 | #endif // _SEMAPHORE_H_ |
---|