1 /* 2 Semaphore wrappers Copyright (c) 2013 <edscott.wilson.garcia@gmail.com> 3 4 Permission is hereby granted, free of charge, to any person obtaining a 5 copy of this software and associated documentation files (the "Software"), 6 to deal in the Software without restriction, including without limitation 7 the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 and/or sell copies of the Software, and to permit persons to whom the 9 Software is furnished to do so, subject to the following conditions: 10 11 The above copyright notice and this permission notice shall be included in 12 all copies or substantial portions of the Software. 13 14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 19 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 20 DEALINGS IN THE SOFTWARE. 21 */ 22 23 24 25 #ifndef _SEMAPHORE_H 26 #define _SEMAPHORE_H 27 # define _WIN32_WINNT 0x502 28 # include "debug.h" 29 # include <windows.h> 30 31 # include <sys/stat.h> 32 # include <sys/time.h> 33 # include <semaphore.h> 34 35 # include <stdio.h> 36 # include <errno.h> 37 38 39 40 41 #ifdef __cplusplus 42 extern "C" { 43 #endif 44 45 #ifndef _TIMESPEC_DEFINED 46 #define _TIMESPEC_DEFINED 47 struct timespec { 48 time_t tv_sec; /* Seconds */ 49 long tv_nsec; /* Nanoseconds */ 50 }; 51 52 struct itimerspec { 53 struct timespec it_interval; /* Timer period */ 54 struct timespec it_value; /* Timer expiration */ 55 }; 56 #endif 57 58 #ifdef PATCHED_SEM 59 #define _sem_t sem_t 60 #else 61 typedef struct _sem_t { 62 HANDLE handle; 63 BOOL named; 64 HANDLE controller; 65 } _sem_t; 66 #endif 67 68 69 70 #ifndef SEM_FAILED 71 #define SEM_FAILED NULL 72 #endif 73 #ifndef SEM_VALUE_MAX 74 #define SEM_VALUE_MAX INT_MAX 75 #endif 76 77 #ifndef ETIMEDOUT 78 #define ETIMEDOUT 138 79 #endif 80 81 82 int _sem_init(_sem_t *sem, int pshared, unsigned int value); 83 84 int _sem_wait(_sem_t *sem); 85 86 int _sem_timedwait(_sem_t *sem, const struct timespec *abs_timeout); 87 88 int _sem_post(_sem_t *sem); 89 90 _sem_t * _sem_open(const char *name, int oflag, ...); 91 92 int _sem_close(_sem_t *sem); 93 94 int _sem_unlink(const char *name); 95 96 int _sem_destroy(_sem_t *sem); 97 98 int _sem_post_multiple(_sem_t *sem, int count); 99 100 int _sem_getvalue(_sem_t * _sem, int * sval); 101 102 #ifdef __cplusplus 103 } 104 #endif 105 106 #endif /* _SEMAPHORE_H */ 107 108 109 110