]> git.saurik.com Git - apple/xnu.git/blame - bsd/sys/sem.h
xnu-517.12.7.tar.gz
[apple/xnu.git] / bsd / sys / sem.h
CommitLineData
1c79356b 1/*
9bccf70c 2 * Copyright (c) 2000-2002 Apple Computer, Inc. All rights reserved.
1c79356b
A
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
e5568f75
A
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.
1c79356b 11 *
e5568f75
A
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
1c79356b
A
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
e5568f75
A
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.
1c79356b
A
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 */
9bccf70c
A
29/*
30 * John Bellardo modified the implementation for Darwin. 12/2000
31 */
1c79356b
A
32
33#ifndef _SYS_SEM_H_
34#define _SYS_SEM_H_
35
9bccf70c 36#include <sys/appleapiopts.h>
1c79356b
A
37#include <sys/ipc.h>
38
39struct sem {
40 u_short semval; /* semaphore value */
41 pid_t sempid; /* pid of last operation */
42 u_short semncnt; /* # awaiting semval > cval */
43 u_short semzcnt; /* # awaiting semval = 0 */
44};
45
46struct semid_ds {
47 struct ipc_perm sem_perm; /* operation permission struct */
48 struct sem *sem_base; /* pointer to first semaphore in set */
49 u_short sem_nsems; /* number of sems in set */
50 time_t sem_otime; /* last operation time */
51 long sem_pad1; /* SVABI/386 says I need this here */
52 time_t sem_ctime; /* last change time */
53 /* Times measured in secs since */
54 /* 00:00:00 GMT, Jan. 1, 1970 */
55 long sem_pad2; /* SVABI/386 says I need this here */
56 long sem_pad3[4]; /* SVABI/386 says I need this here */
57};
58
59/*
60 * semop's sops parameter structure
61 */
62struct sembuf {
63 u_short sem_num; /* semaphore # */
64 short sem_op; /* semaphore operation */
65 short sem_flg; /* operation flags */
66};
67#define SEM_UNDO 010000
68
69#define MAX_SOPS 5 /* maximum # of sembuf's per semop call */
70
71/*
72 * semctl's arg parameter structure
73 */
74union semun {
75 int val; /* value for SETVAL */
76 struct semid_ds *buf; /* buffer for IPC_STAT & IPC_SET */
77 u_short *array; /* array for GETALL & SETALL */
78};
79
80/*
81 * commands for semctl
82 */
83#define GETNCNT 3 /* Return the value of semncnt {READ} */
84#define GETPID 4 /* Return the value of sempid {READ} */
85#define GETVAL 5 /* Return the value of semval {READ} */
86#define GETALL 6 /* Return semvals into arg.array {READ} */
87#define GETZCNT 7 /* Return the value of semzcnt {READ} */
88#define SETVAL 8 /* Set the value of semval to arg.val {ALTER} */
89#define SETALL 9 /* Set semvals from arg.array {ALTER} */
90
91/*
92 * Permissions
93 */
94#define SEM_A 0200 /* alter permission */
95#define SEM_R 0400 /* read permission */
96
97#ifdef KERNEL
9bccf70c 98#ifdef __APPLE_API_PRIVATE
1c79356b
A
99/*
100 * Kernel implementation stuff
101 */
102#define SEMVMX 32767 /* semaphore maximum value */
103#define SEMAEM 16384 /* adjust on exit max value */
104
9bccf70c
A
105/*
106 * Configuration parameters. SEMMNI, SEMMNS, and SEMMNU are hard limits.
107 * The code dynamically allocates enough memory to satisfy the current
108 * demand in even increments of SEMMNI_INC, SEMMNS_INC, and SEMMNU_INC.
109 * The code will never allocate more than the hard limits. The *_INC's
110 * are defined in the kernel section of the header.
111 */
112/*
113 * Configuration parameters
114 */
115#ifndef SEMMNS /* # of semaphores in system */
116#define SEMMNS (1048576/sizeof(struct sem))
117#endif /* no more than 1M of semaphore data */
118#ifndef SEMMNI /* # of semaphore identifiers */
119#define SEMMNI SEMMNS /* max of 1 for each semaphore */
120#endif
121#ifndef SEMUME
122#define SEMUME 10 /* max # of undo entries per process */
123#endif
124#ifndef SEMMNU /* # of undo structures in system */
125#define SEMMNU SEMMNS /* 1 for each semaphore. This is quite large */
126#endif /* This should be max 1 for each process */
127
128/* shouldn't need tuning */
129#ifndef SEMMAP
130#define SEMMAP 30 /* # of entries in semaphore map */
131#endif
132#ifndef SEMMSL
133#define SEMMSL SEMMNS /* max # of semaphores per id */
134#endif
135#ifndef SEMOPM
136#define SEMOPM 100 /* max # of operations per semop call */
137#endif
138
1c79356b
A
139
140/*
141 * Undo structure (one per process)
142 */
143struct sem_undo {
144 struct sem_undo *un_next; /* ptr to next active undo structure */
145 struct proc *un_proc; /* owner of this structure */
146 short un_cnt; /* # of active entries */
147 struct undo {
148 short un_adjval; /* adjust on exit values */
149 short un_num; /* semaphore # */
150 int un_id; /* semid */
9bccf70c 151 } un_ent[SEMUME]; /* undo entries */
1c79356b
A
152};
153
154/*
155 * semaphore info struct
156 */
157struct seminfo {
158 int semmap, /* # of entries in semaphore map */
159 semmni, /* # of semaphore identifiers */
160 semmns, /* # of semaphores in system */
161 semmnu, /* # of undo structures in system */
162 semmsl, /* max # of semaphores per id */
163 semopm, /* max # of operations per semop call */
164 semume, /* max # of undo entries per process */
165 semusz, /* size in bytes of undo structure */
166 semvmx, /* semaphore maximum value */
167 semaem; /* adjust on exit max value */
168};
169extern struct seminfo seminfo;
170
171/* internal "mode" bits */
172#define SEM_ALLOC 01000 /* semaphore is allocated */
173#define SEM_DEST 02000 /* semaphore will be destroyed on last detach */
174
9bccf70c
A
175#define SEMMNI_INC 8 /* increment value for semaphore identifiers */
176#define SEMMNS_INC 64 /* increment value for semaphores */
177#define SEMMNU_INC 32 /* increment value for undo structures */
1c79356b
A
178
179/*
180 * Due to the way semaphore memory is allocated, we have to ensure that
181 * SEMUSZ is properly aligned.
9bccf70c
A
182 *
183 * We are not doing strange semaphore memory allocation anymore, so
184 * these macros are no longer needed.
1c79356b
A
185 */
186
9bccf70c
A
187/*
188 * #define SEM_ALIGN(bytes) (((bytes) + (sizeof(long) - 1)) & ~(sizeof(long) - 1))
189 */
1c79356b
A
190
191/* actual size of an undo structure */
9bccf70c
A
192/*
193 * #define SEMUSZ SEM_ALIGN(offsetof(struct sem_undo, un_ent[SEMUME]))
194 */
195#define SEMUSZ sizeof(struct sem_undo)
1c79356b
A
196
197extern struct semid_ds *sema; /* semaphore id pool */
198extern struct sem *sem; /* semaphore pool */
9bccf70c
A
199/* This is now a struct sem_undo with the new memory allocation
200 * extern int *semu; /* undo structure pool
201 */
202extern struct sem_undo *semu; /* undo structure pool */
1c79356b
A
203
204/*
205 * Macro to find a particular sem_undo vector
206 */
9bccf70c
A
207/* Until we can initialize seminfo.semusz to SEMUSZ, we hard code the size macro
208 * in SEMU. This should be fixed when (if) we implement dynamic pool sizes
209 *
210 * #define SEMU(ix) ((struct sem_undo *)(((intptr_t)semu)+ix * seminfo.semusz))
211 */
212/*
213 * This macro doesn't work because we are using a staticly allocated array
214 * for semu now.
215 * #define SEMU(ix) ((struct sem_undo *)(((intptr_t)semu)+ix * SEMUSZ))
216 */
217#define SEMU(ix) (&semu[ix])
218
1c79356b
A
219
220/*
221 * Process sem_undo vectors at proc exit.
222 */
223void semexit __P((struct proc *p));
224
225/*
226 * Parameters to the semconfig system call
227 */
228typedef enum {
229 SEM_CONFIG_FREEZE, /* Freeze the semaphore facility. */
230 SEM_CONFIG_THAW /* Thaw the semaphore facility. */
231} semconfig_ctl_t;
9bccf70c
A
232
233#endif /* __APPLE_API_PRIVATE */
234
1c79356b
A
235#endif /* KERNEL */
236
237#ifndef KERNEL
238#include <sys/cdefs.h>
239
240__BEGIN_DECLS
241int semsys __P((int, ...));
55e303ae 242int semctl __P((int, int, int, ...));
1c79356b
A
243int semget __P((key_t, int, int));
244int semop __P((int, struct sembuf *,unsigned));
245__END_DECLS
246#endif /* !KERNEL */
247
248#endif /* !_SEM_H_ */