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