]> git.saurik.com Git - apple/xnu.git/blob - bsd/sys/sem.h
3cf60ffd0aa97d2e61097ddea1837ad13c6f9f0c
[apple/xnu.git] / bsd / sys / sem.h
1 /*
2 * Copyright (c) 2000-2002 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_OSREFERENCE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
14 * agreement.
15 *
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
18 * file.
19 *
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
27 *
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
29 */
30 /* $NetBSD: sem.h,v 1.5 1994/06/29 06:45:15 cgd Exp $ */
31
32 /*
33 * SVID compatible sem.h file
34 *
35 * Author: Daniel Boulet
36 * John Bellardo modified the implementation for Darwin. 12/2000
37 */
38
39 #ifndef _SYS_SEM_H_
40 #define _SYS_SEM_H_
41
42
43 #include <sys/cdefs.h>
44 #include <sys/_types.h>
45
46 /*
47 * [XSI] All of the symbols from <sys/ipc.h> SHALL be defined
48 * when this header is included
49 */
50 #include <sys/ipc.h>
51
52
53 /*
54 * [XSI] The pid_t, time_t, key_t, and size_t types shall be defined as
55 * described in <sys/types.h>.
56 *
57 * NOTE: The definition of the key_t type is implicit from the
58 * inclusion of <sys/ipc.h>
59 */
60 #ifndef _PID_T
61 typedef __darwin_pid_t pid_t;
62 #define _PID_T
63 #endif
64
65 #ifndef _TIME_T
66 #define _TIME_T
67 typedef __darwin_time_t time_t;
68 #endif
69
70 #ifndef _SIZE_T
71 #define _SIZE_T
72 typedef __darwin_size_t size_t;
73 #endif
74
75 /*
76 * Technically, we should force all code references to the new structure
77 * definition, not in just the standards conformance case, and leave the
78 * legacy interface there for binary compatibility only. Currently, we
79 * are only forcing this for programs requesting standards conformance.
80 */
81 #if defined(__POSIX_C_SOURCE) || defined(kernel) || defined(__LP64__)
82 /*
83 * Structure used internally.
84 *
85 * This structure is exposed because standards dictate that it is used as
86 * the semun union member 'buf' as the fourth argment to semctl() when the
87 * third argument is IPC_STAT or IPC_SET.
88 *
89 * Note: only the fields sem_perm, sem_nsems, sem_otime, and sem_ctime
90 * are meaningful in user space.
91 */
92 struct __semid_ds_new {
93 struct __ipc_perm_new sem_perm; /* [XSI] operation permission struct */
94 __int32_t sem_base; /* 32 bit base ptr for semaphore set */
95 unsigned short sem_nsems; /* [XSI] number of sems in set */
96 time_t sem_otime; /* [XSI] last operation time */
97 __int32_t sem_pad1; /* RESERVED: DO NOT USE! */
98 time_t sem_ctime; /* [XSI] last change time */
99 /* Times measured in secs since */
100 /* 00:00:00 GMT, Jan. 1, 1970 */
101 __int32_t sem_pad2; /* RESERVED: DO NOT USE! */
102 __int32_t sem_pad3[4]; /* RESERVED: DO NOT USE! */
103 };
104 #define semid_ds __semid_ds_new
105 #else /* !_POSIX_C_SOURCE */
106 #define semid_ds __semid_ds_old
107 #endif /* !_POSIX_C_SOURCE */
108
109 #if !defined(__POSIX_C_SOURCE) && !defined(__LP64__)
110 struct __semid_ds_old {
111 struct __ipc_perm_old sem_perm; /* [XSI] operation permission struct */
112 __int32_t sem_base; /* 32 bit base ptr for semaphore set */
113 unsigned short sem_nsems; /* [XSI] number of sems in set */
114 time_t sem_otime; /* [XSI] last operation time */
115 __int32_t sem_pad1; /* RESERVED: DO NOT USE! */
116 time_t sem_ctime; /* [XSI] last change time */
117 /* Times measured in secs since */
118 /* 00:00:00 GMT, Jan. 1, 1970 */
119 __int32_t sem_pad2; /* RESERVED: DO NOT USE! */
120 __int32_t sem_pad3[4]; /* RESERVED: DO NOT USE! */
121 };
122 #endif /* !_POSIX_C_SOURCE */
123
124 /*
125 * Possible values for the third argument to semctl()
126 */
127 #define GETNCNT 3 /* [XSI] Return the value of semncnt {READ} */
128 #define GETPID 4 /* [XSI] Return the value of sempid {READ} */
129 #define GETVAL 5 /* [XSI] Return the value of semval {READ} */
130 #define GETALL 6 /* [XSI] Return semvals into arg.array {READ} */
131 #define GETZCNT 7 /* [XSI] Return the value of semzcnt {READ} */
132 #define SETVAL 8 /* [XSI] Set the value of semval to arg.val {ALTER} */
133 #define SETALL 9 /* [XSI] Set semvals from arg.array {ALTER} */
134
135
136 /* A semaphore; this is an anonymous structure, not for external use */
137 struct sem {
138 unsigned short semval; /* semaphore value */
139 pid_t sempid; /* pid of last operation */
140 unsigned short semncnt; /* # awaiting semval > cval */
141 unsigned short semzcnt; /* # awaiting semval == 0 */
142 };
143
144
145 /*
146 * Structure of array element for second argument to semop()
147 */
148 struct sembuf {
149 unsigned short sem_num; /* [XSI] semaphore # */
150 short sem_op; /* [XSI] semaphore operation */
151 short sem_flg; /* [XSI] operation flags */
152 };
153
154 /*
155 * Possible flag values for sem_flg
156 */
157 #define SEM_UNDO 010000 /* [XSI] Set up adjust on exit entry */
158
159
160 #ifndef _POSIX_C_SOURCE
161
162 /*
163 * System imposed limit on the value of the third parameter to semop().
164 * This is arbitrary, and the standards unfortunately do not provide a
165 * way for user applications to retrieve this value (e.g. via sysconf()
166 * or from a manifest value in <unistd.h>). The value shown here is
167 * informational, and subject to change in future revisions.
168 */
169 #define MAX_SOPS 5 /* maximum # of sembuf's per semop call */
170
171
172 /*
173 * Union used as the fourth argment to semctl() in all cases. Specific
174 * member values are used for different values of the third parameter:
175 *
176 * Command Member
177 * ------------------------------------------- ------
178 * GETALL, SETALL array
179 * SETVAL val
180 * IPC_STAT, IPC_SET buf
181 *
182 * The union definition is intended to be defined by the user application
183 * in conforming applications; it is provided here for two reasons:
184 *
185 * 1) Historical source compatability for non-conforming applications
186 * expecting this header to declare the union type on their behalf
187 *
188 * 2) Documentation; specifically, 64 bit applications that do not pass
189 * this structure for 'val', or, alternately, a 64 bit type, will
190 * not function correctly
191 */
192 union semun {
193 int val; /* value for SETVAL */
194 struct semid_ds *buf; /* buffer for IPC_STAT & IPC_SET */
195 unsigned short *array; /* array for GETALL & SETALL */
196 };
197 typedef union semun semun_t;
198
199
200 /*
201 * Permissions
202 */
203 #define SEM_A 0200 /* alter permission */
204 #define SEM_R 0400 /* read permission */
205
206 #endif /* !_POSIX_C_SOURCE */
207
208
209
210 #ifndef KERNEL
211
212 __BEGIN_DECLS
213 #ifndef _POSIX_C_SOURCE
214 int semsys(int, ...);
215 #endif /* !_POSIX_C_SOURCE */
216 int semctl(int, int, int, ...) __DARWIN_ALIAS(semctl);
217 int semget(key_t, int, int);
218 int semop(int, struct sembuf *, size_t);
219 __END_DECLS
220
221 #endif /* !KERNEL */
222
223 #endif /* !_SEM_H_ */