xref: /freebsd/sys/sys/sem.h (revision a1a4f1a0d87b594d3f17a97dc0127eec1417e6f6)
1 /* $FreeBSD$ */
2 /*	$NetBSD: sem.h,v 1.5 1994/06/29 06:45:15 cgd Exp $	*/
3 
4 /*
5  * SVID compatible sem.h file
6  *
7  * Author:  Daniel Boulet
8  */
9 
10 #ifndef _SYS_SEM_H_
11 #define _SYS_SEM_H_
12 
13 #include <sys/ipc.h>
14 
15 struct sem {
16 	u_short	semval;		/* semaphore value */
17 	pid_t	sempid;		/* pid of last operation */
18 	u_short	semncnt;	/* # awaiting semval > cval */
19 	u_short	semzcnt;	/* # awaiting semval = 0 */
20 };
21 
22 struct semid_ds {
23 	struct	ipc_perm sem_perm;	/* operation permission struct */
24 	struct	sem *sem_base;	/* pointer to first semaphore in set */
25 	u_short	sem_nsems;	/* number of sems in set */
26 	time_t	sem_otime;	/* last operation time */
27 	long	sem_pad1;	/* SVABI/386 says I need this here */
28 	time_t	sem_ctime;	/* last change time */
29     				/* Times measured in secs since */
30     				/* 00:00:00 GMT, Jan. 1, 1970 */
31 	long	sem_pad2;	/* SVABI/386 says I need this here */
32 	long	sem_pad3[4];	/* SVABI/386 says I need this here */
33 };
34 
35 /*
36  * semop's sops parameter structure
37  */
38 struct sembuf {
39 	u_short	sem_num;	/* semaphore # */
40 	short	sem_op;		/* semaphore operation */
41 	short	sem_flg;	/* operation flags */
42 };
43 #define SEM_UNDO	010000
44 
45 #define MAX_SOPS	5	/* maximum # of sembuf's per semop call */
46 
47 /*
48  * semctl's arg parameter structure
49  */
50 union semun {
51 	int	val;		/* value for SETVAL */
52 	struct	semid_ds *buf;	/* buffer for IPC_STAT & IPC_SET */
53 	u_short	*array;		/* array for GETALL & SETALL */
54 };
55 
56 /*
57  * commands for semctl
58  */
59 #define GETNCNT	3	/* Return the value of semncnt {READ} */
60 #define GETPID	4	/* Return the value of sempid {READ} */
61 #define GETVAL	5	/* Return the value of semval {READ} */
62 #define GETALL	6	/* Return semvals into arg.array {READ} */
63 #define GETZCNT	7	/* Return the value of semzcnt {READ} */
64 #define SETVAL	8	/* Set the value of semval to arg.val {ALTER} */
65 #define SETALL	9	/* Set semvals from arg.array {ALTER} */
66 
67 /*
68  * Permissions
69  */
70 #define SEM_A		0200	/* alter permission */
71 #define SEM_R		0400	/* read permission */
72 
73 #ifdef KERNEL
74 /*
75  * Kernel implementation stuff
76  */
77 #define SEMVMX	32767		/* semaphore maximum value */
78 #define SEMAEM	16384		/* adjust on exit max value */
79 
80 
81 /*
82  * Undo structure (one per process)
83  */
84 struct sem_undo {
85 	struct	sem_undo *un_next;	/* ptr to next active undo structure */
86 	struct	proc *un_proc;		/* owner of this structure */
87 	short	un_cnt;			/* # of active entries */
88 	struct undo {
89 		short	un_adjval;	/* adjust on exit values */
90 		short	un_num;		/* semaphore # */
91 		int	un_id;		/* semid */
92 	} un_ent[1];			/* undo entries */
93 };
94 
95 /*
96  * semaphore info struct
97  */
98 struct seminfo {
99 	int	semmap,		/* # of entries in semaphore map */
100 		semmni,		/* # of semaphore identifiers */
101 		semmns,		/* # of semaphores in system */
102 		semmnu,		/* # of undo structures in system */
103 		semmsl,		/* max # of semaphores per id */
104 		semopm,		/* max # of operations per semop call */
105 		semume,		/* max # of undo entries per process */
106 		semusz,		/* size in bytes of undo structure */
107 		semvmx,		/* semaphore maximum value */
108 		semaem;		/* adjust on exit max value */
109 };
110 extern struct seminfo	seminfo;
111 
112 /* internal "mode" bits */
113 #define	SEM_ALLOC	01000	/* semaphore is allocated */
114 #define	SEM_DEST	02000	/* semaphore will be destroyed on last detach */
115 
116 /*
117  * Configuration parameters
118  */
119 #ifndef SEMMNI
120 #define SEMMNI	10		/* # of semaphore identifiers */
121 #endif
122 #ifndef SEMMNS
123 #define SEMMNS	60		/* # of semaphores in system */
124 #endif
125 #ifndef SEMUME
126 #define SEMUME	10		/* max # of undo entries per process */
127 #endif
128 #ifndef SEMMNU
129 #define SEMMNU	30		/* # of undo structures in system */
130 #endif
131 
132 /* shouldn't need tuning */
133 #ifndef SEMMAP
134 #define SEMMAP	30		/* # of entries in semaphore map */
135 #endif
136 #ifndef SEMMSL
137 #define SEMMSL	SEMMNS		/* max # of semaphores per id */
138 #endif
139 #ifndef SEMOPM
140 #define SEMOPM	100		/* max # of operations per semop call */
141 #endif
142 
143 /*
144  * Due to the way semaphore memory is allocated, we have to ensure that
145  * SEMUSZ is properly aligned.
146  */
147 
148 #define SEM_ALIGN(bytes) (((bytes) + (sizeof(long) - 1)) & ~(sizeof(long) - 1))
149 
150 /* actual size of an undo structure */
151 #define SEMUSZ	SEM_ALIGN(offsetof(struct sem_undo, un_ent[SEMUME]))
152 
153 extern struct semid_ds *sema;	/* semaphore id pool */
154 extern struct sem *sem;		/* semaphore pool */
155 extern int	*semu;		/* undo structure pool */
156 
157 /*
158  * Macro to find a particular sem_undo vector
159  */
160 #define SEMU(ix)	((struct sem_undo *)(((intptr_t)semu)+ix * seminfo.semusz))
161 
162 /*
163  * Process sem_undo vectors at proc exit.
164  */
165 void	semexit __P((struct proc *p));
166 
167 /*
168  * Parameters to the semconfig system call
169  */
170 typedef enum {
171 	SEM_CONFIG_FREEZE,	/* Freeze the semaphore facility. */
172 	SEM_CONFIG_THAW		/* Thaw the semaphore facility. */
173 } semconfig_ctl_t;
174 #endif /* KERNEL */
175 
176 #ifndef KERNEL
177 #include <sys/cdefs.h>
178 
179 __BEGIN_DECLS
180 int semsys __P((int, ...));
181 int semctl __P((int, int, int, ...));
182 int semget __P((key_t, int, int));
183 int semop __P((int, struct sembuf *,unsigned));
184 __END_DECLS
185 #endif /* !KERNEL */
186 
187 #endif /* !_SEM_H_ */
188