1 /**
2  * D header file for POSIX.
3  *
4  * Copyright: Public Domain
5  * License:   Public Domain
6  * Authors:   Sean Kelly
7  * Standards: The Open Group Base Specifications Issue 6, IEEE Std 1003.1, 2004 Edition
8  */
9 module tango.stdc.posix.semaphore;
10 
11 private import tango.stdc.posix.config;
12 private import tango.stdc.posix.time;
13 version( solaris ) {
14 	private import tango.stdc.stdint;
15 }
16 
17 
18 extern (C):
19 
20 //
21 // Required
22 //
23 /*
24 sem_t
25 SEM_FAILED
26 
27 int sem_close(sem_t*);
28 int sem_destroy(sem_t*);
29 int sem_getvalue(sem_t*, int*);
30 int sem_init(sem_t*, int, uint);
31 sem_t* sem_open(in char*, int, ...);
32 int sem_post(sem_t*);
33 int sem_trywait(sem_t*);
34 int sem_unlink(in char*);
35 int sem_wait(sem_t*);
36 */
37 
38 version( linux )
39 {
40     private alias int __atomic_lock_t;
41 
42     private struct _pthread_fastlock
43     {
44       c_long            __status;
45       __atomic_lock_t   __spinlock;
46     }
47 
48     struct sem_t
49     {
50       _pthread_fastlock __sem_lock;
51       int               __sem_value;
52       void*             __sem_waiting;
53     }
54 
55     const SEM_FAILED    = cast(sem_t*) null;
56 }
57 else version( darwin )
58 {
59     alias int sem_t;
60 
61     const SEM_FAILED    = cast(sem_t*) null;
62     // mach_port based semaphores (the working anonymous semaphores)
63     alias uint mach_port_t;
64     alias mach_port_t semaphore_t;
65     alias mach_port_t thread_t;
66     alias mach_port_t task_t;
67     alias int kern_return_t;
68     enum KERN_RETURN: kern_return_t{
69         SUCCESS=0,
70         ABORTED=14,
71         OPERATION_TIMED_OUT=49
72     }
73     kern_return_t   semaphore_signal        (semaphore_t semaphore);
74     kern_return_t   semaphore_signal_all    (semaphore_t semaphore);
75     kern_return_t   semaphore_signal_thread (semaphore_t semaphore,
76                                              thread_t thread);
77     
78     kern_return_t   semaphore_wait          (semaphore_t semaphore);
79     kern_return_t   semaphore_timedwait     (semaphore_t semaphore, 
80                      timespec wait_time);
81     
82     kern_return_t   semaphore_wait_signal   (semaphore_t wait_semaphore,
83                                              semaphore_t signal_semaphore);
84     
85     kern_return_t semaphore_timedwait_signal(semaphore_t wait_semaphore,
86                                                      semaphore_t signal_semaphore,
87                                                      timespec wait_time);
88     kern_return_t semaphore_destroy(task_t task,
89                               semaphore_t semaphore);
90     kern_return_t semaphore_create(task_t task,
91                            semaphore_t *semaphore,
92                            int policy,
93                            int value);
94    alias int sync_policy_t;
95    
96    task_t mach_task_self();// returns the task port of the current  thread
97    /*
98     *   These options define the wait ordering of the synchronizers
99     */
100    enum MACH_SYNC_POLICY{
101        SYNC_POLICY_FIFO=0x0,
102        SYNC_POLICY_FIXED_PRIORITY=0x1,
103        SYNC_POLICY_REVERSED=0x2,
104        SYNC_POLICY_ORDER_MASK=0x3,
105        SYNC_POLICY_LIFO=(SYNC_POLICY_FIFO|SYNC_POLICY_REVERSED),
106        SYNC_POLICY_MAX=0x7
107    }
108 
109 }
110 else version( FreeBSD )
111 {
112     const uint SEM_MAGIC = 0x09fa4012;
113     const SEM_USER = 0;
114 
115     alias void* sem_t;
116 
117     const SEM_FAILED = cast(sem_t*) null;
118 }
119 else version( solaris )
120 {
121 	struct sem_t
122 	{
123 		/* this structure must be the same as sema_t in <synch.h> */
124 		uint32_t	sem_count;	/* semaphore count */
125 		uint16_t	sem_type;
126 		uint16_t	sem_magic;
127 		upad64_t[3]	sem_pad1;	/* reserved for a mutex_t */
128 		upad64_t[2]	sem_pad2;	/* reserved for a cond_t */
129 	}
130 }
131 
132 int sem_close(sem_t*);
133 int sem_destroy(sem_t*);
134 int sem_getvalue(sem_t*, int*);
135 int sem_init(sem_t*, int, uint);
136 sem_t* sem_open(in char*, int, ...);
137 int sem_post(sem_t*);
138 int sem_trywait(sem_t*);
139 int sem_unlink(in char*);
140 int sem_wait(sem_t*);
141 
142 //
143 // Timeouts (TMO)
144 //
145 /*
146 int sem_timedwait(sem_t*, in timespec*);
147 */
148 
149 version( linux )
150 {
151     int sem_timedwait(sem_t*, in timespec*);
152 }
153 else version( darwin )
154 {
155     // int sem_timedwait(sem_t*, in timespec*); // not defined, use mach semaphores instead
156 }
157 else version( FreeBSD )
158 {
159     int sem_timedwait(sem_t*, in timespec*);
160 }
161 else version( solaris )
162 {
163     int sem_timedwait(sem_t*, in timespec*);
164 }