xref: /illumos-gate/usr/src/head/semaphore.h (revision 8119dad84d6416f13557b0ba8e2aaf9064cbcfd3)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2014 Garrett D'Amore <garrett@damore.org>
24  *
25  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
26  * Use is subject to license terms.
27  */
28 
29 #ifndef _SEMAPHORE_H
30 #define	_SEMAPHORE_H
31 
32 #include <sys/feature_tests.h>
33 
34 #include <sys/types.h>
35 #include <sys/fcntl.h>
36 
37 #ifdef	__cplusplus
38 extern "C" {
39 #endif
40 
41 /*
42  * Including <time.h> has an unfortunate XPG/POSIX history. In XPG6 and POSIX
43  * 1999, sem_timedwait() was added which required making the struct timespec
44  * declared in some form. In XPG7, this is permitted to make all of time.h
45  * visible. In XPG8, the use of the clockid_t is required from time.h. For
46  * better and worse, that is how the ifdef soup is here. We prefer including
47  * time.h so that folks can get a full definition for the time struct timespec
48  * rather than the forward declaration that most other OSes do here.
49  */
50 #if !defined(_STRICT_POSIX) || defined(_XPG7)
51 #include <time.h>
52 #elif defined(_XPG6)
53 struct timespec;
54 #endif
55 
56 typedef struct {
57 	/* this structure must be the same as sema_t in <synch.h> */
58 	uint32_t	sem_count;	/* semaphore count */
59 	uint16_t	sem_type;
60 	uint16_t	sem_magic;
61 	upad64_t	sem_pad1[3];	/* reserved for a mutex_t */
62 	upad64_t	sem_pad2[2];	/* reserved for a cond_t */
63 } sem_t;
64 
65 #define	SEM_FAILED	((sem_t *)(-1))
66 
67 /*
68  * function prototypes
69  */
70 int	sem_init(sem_t *, int, unsigned int);
71 int	sem_destroy(sem_t *);
72 sem_t	*sem_open(const char *, int, ...);
73 int	sem_close(sem_t *);
74 int	sem_unlink(const char *);
75 int	sem_wait(sem_t *);
76 
77 #if !defined(_STRICT_POSIX) || defined(_XPG6)
78 int	sem_timedwait(sem_t *_RESTRICT_KYWD,
79 		const struct timespec *_RESTRICT_KYWD);
80 int	sem_reltimedwait_np(sem_t *_RESTRICT_KYWD,
81 		const struct timespec *_RESTRICT_KYWD);
82 #endif	/* !_STRICT_POSIX || _XPG6 */
83 
84 #if !defined(_STRICT_POSIX) || defined(_XPG8)
85 int	sem_clockwait(sem_t *_RESTRICT_KYWD, clockid_t,
86 		const struct timespec *_RESTRICT_KYWD);
87 int	sem_relclockwait_np(sem_t *_RESTRICT_KYWD, clockid_t,
88 		const struct timespec *_RESTRICT_KYWD);
89 #endif	/* !_STRICT_POSIX || _XPG8 */
90 int	sem_trywait(sem_t *);
91 int	sem_post(sem_t *);
92 int	sem_getvalue(sem_t *_RESTRICT_KYWD, int *_RESTRICT_KYWD);
93 
94 #ifdef	__cplusplus
95 }
96 #endif
97 
98 #endif	/* _SEMAPHORE_H */
99