| 195 | /* -------------------------------------------------------------------- |
| 196 | Misc OSX compilation fixes, injected here (crudely) for convenience. |
| 197 | -------------------------------------------------------------------- */ |
| 198 | #ifdef __APPLE__ |
| 199 | #include <mach/clock.h> |
| 200 | #include <mach/mach.h> |
| 201 | #include <sys/time.h> |
| 202 | #include <semaphore.h> |
| 203 | #include <AvailabilityMacros.h> |
| 204 | |
| 205 | /* Only named semaphores work on OSX, so wrap the calls with names |
| 206 | Some snippets courtesy of Jorgen Lundman. */ |
| 207 | |
| 208 | struct timespec ts; |
| 209 | |
| 210 | static inline int mac_sem_init(sem_t *sem, int pshared, int value) |
| 211 | { |
| 212 | char *fname = strdup("/tmp/.mbuffer.XXXXXX"); |
| 213 | mktemp(fname); |
| 214 | *sem = sem_open(fname, O_CREAT, 0600, value); |
| 215 | unlink(fname); |
| 216 | free(fname); |
| 217 | return 0; |
| 218 | } |
| 219 | |
| 220 | static inline int mac_sem_destroy(sem_t *sem) |
| 221 | { |
| 222 | sem_close(sem); |
| 223 | return 0; |
| 224 | } |
| 225 | |
| 226 | static inline int mac_sem_wait(sem_t *sem) |
| 227 | { |
| 228 | return sem_wait(*sem); |
| 229 | } |
| 230 | |
| 231 | static inline int mac_sem_post(sem_t *sem) |
| 232 | { |
| 233 | return sem_post(*sem); |
| 234 | } |
| 235 | |
| 236 | static inline int mac_sem_getvalue(sem_t *sem, int *value) |
| 237 | { |
| 238 | *value = 0; |
| 239 | return 0; |
| 240 | } |
| 241 | |
| 242 | |
| 243 | /* If the version of OSX is <= 10.11, then we need to implement |
| 244 | * our own Linux clock_gettime() |
| 245 | */ |
| 246 | |
| 247 | #ifndef MAC_OS_X_VERSION_10_12 |
| 248 | #define MAC_OS_X_VERSION_10_12 101200 |
| 249 | #endif |
| 250 | |
| 251 | #define __APPLE_HAVE_CLOCK_GETTIME defined(__APPLE__) && MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_12 |
| 252 | |
| 253 | #if !(__APPLE_HAVE_CLOCK_GETTIME) |
| 254 | #warning "Injecting clock_gettime for OSX < 10.12" |
| 255 | static inline int clock_gettime(unsigned long int id, struct timespec *ts) |
| 256 | { |
| 257 | clock_serv_t cclock; |
| 258 | mach_timespec_t mts; |
| 259 | host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock); |
| 260 | clock_get_time(cclock, &mts); |
| 261 | mach_port_deallocate(mach_task_self(), cclock); |
| 262 | ts->tv_sec = mts.tv_sec; |
| 263 | ts->tv_nsec = mts.tv_nsec; |
| 264 | return 0; |
| 265 | } |
| 266 | |
| 267 | typedef unsigned long clockid_t; |
| 268 | #define CLOCK_REALTIME 0 |
| 269 | |
| 270 | #endif /* __APPLE_HAVE_CLOCK_GETTIME */ |
| 271 | |
| 272 | /* And now wrap the function calls to our inlined replacements: */ |
| 273 | #define sem_init mac_sem_init |
| 274 | #define sem_destroy mac_sem_destroy |
| 275 | #define sem_wait mac_sem_wait |
| 276 | #define sem_post mac_sem_post |
| 277 | #define sem_getvalue mac_sem_getvalue |
| 278 | |
| 279 | #endif /* __APPLE__ */ |
| 280 | |
| 281 | |
| 282 | |
| 283 | |