FreeBSD/Linux Kernel Cross Reference
sys/sqt/sema.c
1 /*
2 * Mach Operating System
3 * Copyright (c) 1992,1991 Carnegie Mellon University
4 * Copyright (c) 1992,1991 Sequent Computer Systems
5 * All Rights Reserved.
6 *
7 * Permission to use, copy, modify and distribute this software and its
8 * documentation is hereby granted, provided that both the copyright
9 * notice and this permission notice appear in all copies of the
10 * software, derivative works or modified versions, and any portions
11 * thereof, and that both notices appear in supporting documentation.
12 *
13 * CARNEGIE MELLON AND SEQUENT COMPUTER SYSTEMS ALLOW FREE USE OF
14 * THIS SOFTWARE IN ITS "AS IS" CONDITION. CARNEGIE MELLON AND
15 * SEQUENT COMPUTER SYSTEMS DISCLAIM ANY LIABILITY OF ANY KIND FOR
16 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
17 *
18 * Carnegie Mellon requests users of this software to return to
19 *
20 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
21 * School of Computer Science
22 * Carnegie Mellon University
23 * Pittsburgh PA 15213-3890
24 *
25 * any improvements or extensions that they make and grant Carnegie Mellon
26 * the rights to redistribute these changes.
27 */
28
29 /*
30 * HISTORY
31 * $Log: sema.c,v $
32 * Revision 2.4 93/01/14 17:56:09 danner
33 * Fix calls to sched_prim functions.
34 * [92/12/31 dbg]
35 *
36 * Revision 2.3 91/07/31 18:03:32 dbg
37 * Changed copyright.
38 * [91/07/31 dbg]
39 *
40 * Revision 2.2 91/05/08 12:58:36 dbg
41 * Created.
42 * [91/04/26 14:56:44 dbg]
43 *
44 */
45
46 /*
47 * Dynix semaphores.
48 */
49 #include <kern/sched_prim.h>
50
51 #include <sqt/mutex.h>
52
53 #define sema_lock(sema) (bit_lock(0, &(sema)->lock))
54
55 #define sema_unlock(sema) (bit_unlock(0, &(sema)->lock))
56
57 /*ARGSUSED*/
58 init_sema(sema, val, flags, gate)
59 register sema_t *sema;
60 int val;
61 {
62 sema->lock = 0;
63 sema->waiting = 0;
64 sema->count = val;
65 }
66
67 /*ARGUSED*/
68 p_sema(sema, pri)
69 register sema_t *sema;
70 int pri;
71 {
72 sema_lock(sema);
73
74 if (--sema->count >= 0) {
75 sema_unlock(sema);
76 return;
77 }
78 sema->waiting = TRUE;
79 assert_wait(sema, FALSE);
80 sema_unlock(sema);
81 thread_block((void (*)()) 0);
82 }
83
84 p_sema_v_lock(sema, pri, l, spl)
85 register sema_t *sema;
86 int pri;
87 simple_lock_t l;
88 int spl;
89 {
90 sema_lock(sema);
91
92 if (--sema->count >= 0) {
93 sema_unlock(sema);
94 v_lock(l,spl);
95 return;
96 }
97 sema->waiting = TRUE;
98 assert_wait(sema, FALSE);
99 sema_unlock(sema);
100 v_lock(l,spl);
101 thread_block((void (*)()) 0);
102 }
103
104 cp_sema(sema)
105 register sema_t *sema;
106 {
107 sema_lock(sema);
108 if (--sema->count >= 0) {
109 sema_unlock(sema);
110 return (TRUE);
111 }
112 sema_unlock(sema);
113 return (FALSE);
114 }
115
116 v_sema(sema)
117 register sema_t *sema;
118 {
119 sema_lock(sema);
120 if (++sema->count >= 0) {
121 if (sema->waiting) {
122 thread_wakeup(sema);
123 sema->waiting = 0;
124 }
125 }
126 sema_unlock(sema);
127 }
128
129 cv_sema(sema)
130 register sema_t *sema;
131 {
132 v_sema(sema);
133 }
134
135 vall_sema(sema)
136 register sema_t *sema;
137 {
138 sema_lock(sema);
139 while (++sema->count <= 0) {
140 if (sema->waiting) {
141 thread_wakeup(sema);
142 sema->waiting = 0;
143 }
144 }
145 sema_unlock(sema);
146 }
147
Cache object: 79457ccd91239ed79b5a77f1d59c15af
|