Reimplemented dispatcher database lock and synchronization primitives.
[reactos.git] / reactos / ntoskrnl / ke / sem.c
1 /*
2 * ReactOS kernel
3 * Copyright (C) 1998, 1999, 2000, 2001 ReactOS Team
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19 /* $Id: sem.c,v 1.14 2003/11/02 01:15:15 ekohl Exp $
20 *
21 * PROJECT: ReactOS kernel
22 * FILE: ntoskrnl/ke/sem.c
23 * PURPOSE: Implements kernel semaphores
24 * PROGRAMMER: David Welch (welch@mcmail.com)
25 * UPDATE HISTORY:
26 * Created 22/05/98
27 */
28
29 /* INCLUDES *****************************************************************/
30
31 #include <ddk/ntddk.h>
32 #include <internal/ke.h>
33 #include <internal/ps.h>
34 #include <internal/id.h>
35
36 #define NDEBUG
37 #include <internal/debug.h>
38
39 /* FUNCTIONS *****************************************************************/
40
41 /*
42 * @implemented
43 */
44 VOID STDCALL
45 KeInitializeSemaphore (PKSEMAPHORE Semaphore,
46 LONG Count,
47 LONG Limit)
48 {
49 KeInitializeDispatcherHeader(&Semaphore->Header,
50 InternalSemaphoreType,
51 sizeof(KSEMAPHORE)/sizeof(ULONG),
52 Count);
53 Semaphore->Limit=Limit;
54 }
55
56 /*
57 * @implemented
58 */
59 LONG STDCALL
60 KeReadStateSemaphore (PKSEMAPHORE Semaphore)
61 {
62 return(Semaphore->Header.SignalState);
63 }
64
65 /*
66 * @implemented
67 */
68 LONG STDCALL
69 KeReleaseSemaphore (PKSEMAPHORE Semaphore,
70 KPRIORITY Increment,
71 LONG Adjustment,
72 BOOLEAN Wait)
73 /*
74 * FUNCTION: KeReleaseSemaphore releases a given semaphore object. This
75 * routine supplies a runtime priority boost for waiting threads. If this
76 * call sets the semaphore to the Signaled state, the semaphore count is
77 * augmented by the given value. The caller can also specify whether it
78 * will call one of the KeWaitXXX routines as soon as KeReleaseSemaphore
79 * returns control.
80 * ARGUMENTS:
81 * Semaphore = Points to an initialized semaphore object for which the
82 * caller provides the storage.
83 * Increment = Specifies the priority increment to be applied if
84 * releasing the semaphore causes a wait to be
85 * satisfied.
86 * Adjustment = Specifies a value to be added to the current semaphore
87 * count. This value must be positive
88 * Wait = Specifies whether the call to KeReleaseSemaphore is to be
89 * followed immediately by a call to one of the KeWaitXXX.
90 * RETURNS: If the return value is zero, the previous state of the semaphore
91 * object is Not-Signaled.
92 */
93 {
94 ULONG InitialState;
95 KIRQL OldIrql;
96
97 DPRINT("KeReleaseSemaphore(Semaphore %x, Increment %d, Adjustment %d, "
98 "Wait %d)\n", Semaphore, Increment, Adjustment, Wait);
99
100 OldIrql = KeAcquireDispatcherDatabaseLock();
101
102 InitialState = Semaphore->Header.SignalState;
103 if (Semaphore->Limit < (LONG) InitialState + Adjustment ||
104 InitialState > InitialState + Adjustment)
105 {
106 ExRaiseStatus(STATUS_SEMAPHORE_LIMIT_EXCEEDED);
107 }
108
109 Semaphore->Header.SignalState += Adjustment;
110 if (InitialState == 0)
111 {
112 KeDispatcherObjectWake(&Semaphore->Header);
113 }
114
115 if (Wait == FALSE)
116 {
117 KeReleaseDispatcherDatabaseLock(OldIrql);
118 }
119 else
120 {
121 KTHREAD *Thread = KeGetCurrentThread();
122 Thread->WaitNext = Wait;
123 Thread->WaitIrql = OldIrql;
124 }
125
126 return(InitialState);
127 }
128
129 /* EOF */