This repository has been archived by the owner on Jul 8, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmbuffer_darwin.h
79 lines (62 loc) · 1.54 KB
/
mbuffer_darwin.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#ifndef MBUFFER_DARWIN_H
#define MBUFFER_DARWIN_H
#include <mach/clock.h>
#include <mach/mach.h>
#include <sys/time.h>
#include <semaphore.h>
/*
* semaphores on OS X only work in named mode, ie, sem_open, but not
* sem_init. The sem_wait and sem_port take the literal sem value, not
* a ptr to it.
*/
struct timespec ts;
static inline int mac_sem_init(sem_t *sem, int pshared, int value)
{
char *fname = strdup("/tmp/.mbuffer.XXXXXX");
mktemp(fname);
*sem = sem_open(fname, O_CREAT, 0600, value);
unlink(fname);
free(fname);
return 0;
}
static inline int mac_sem_destroy(sem_t *sem)
{
sem_close(sem);
return 0;
}
static inline int mac_sem_wait(sem_t *sem)
{
return sem_wait(*sem);
}
static inline int mac_sem_post(sem_t *sem)
{
return sem_post(*sem);
}
static inline int mac_sem_getvalue(sem_t *sem, int *value)
{
*value = 0;
return 0;
}
/*
* Simulate Linux clock_gettime()
*/
static inline int clock_gettime(unsigned long int id, struct timespec *ts)
{
clock_serv_t cclock;
mach_timespec_t mts;
host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
clock_get_time(cclock, &mts);
mach_port_deallocate(mach_task_self(), cclock);
ts->tv_sec = mts.tv_sec;
ts->tv_nsec = mts.tv_nsec;
return 0;
}
/* Other Linux porting wrappers */
typedef unsigned long clockid_t;
#define CLOCK_REALTIME 0
#define sem_init mac_sem_init
#define sem_destroy mac_sem_destroy
#define sem_wait mac_sem_wait
#define sem_post mac_sem_post
#define sem_getvalue mac_sem_getvalue
#endif