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