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