ce7d4ba05f4ef40f37583a47ec5c310658c39fe1
[tpg/acess2.git] / KernelLand / Kernel / semaphore.c
1 /*
2  * Acess2 Kernel
3  * - By John Hodge (thePowersGang)
4  *
5  * semaphore.c
6  * - Semaphores
7  */
8 #include <acess.h>
9 #include <semaphore.h>
10 #include <threads_int.h>
11
12 #define SEMAPHORE_DEBUG         0       // Debug semaphores
13
14 // === CODE ===
15 //
16 // Initialise a semaphore
17 //
18 void Semaphore_Init(tSemaphore *Sem, int Value, int MaxValue, const char *Module, const char *Name)
19 {
20         memset(Sem, 0, sizeof(tSemaphore));
21         Sem->Value = Value;
22         Sem->ModName = Module;
23         Sem->Name = Name;
24         Sem->MaxValue = MaxValue;
25 }
26 //
27 // Wait for items to be avaliable
28 //
29 int Semaphore_Wait(tSemaphore *Sem, int MaxToTake)
30 {
31         tThread *us;
32          int    taken;
33         if( MaxToTake < 0 ) {
34                 Log_Warning("Threads", "Semaphore_Wait: User bug - MaxToTake(%i) < 0, Sem=%p(%s)",
35                         MaxToTake, Sem, Sem->Name);
36                 MaxToTake = 0;
37         }
38         
39         SHORTLOCK( &Sem->Protector );
40         
41         // Check if there's already items avaliable
42         if( Sem->Value > 0 )
43         {
44                 // Take what we need
45                 if( MaxToTake && Sem->Value > MaxToTake )
46                         taken = MaxToTake;
47                 else
48                         taken = Sem->Value;
49                 Sem->Value -= taken;
50         }
51         else
52         {
53                 SHORTLOCK( &glThreadListLock );
54                 
55                 // - Remove from active list
56                 us = Threads_RemActive();
57                 us->Next = NULL;
58                 // - Mark as sleeping
59                 us->Status = THREAD_STAT_SEMAPHORESLEEP;
60                 us->WaitPointer = Sem;
61                 us->RetStatus = MaxToTake;      // Use RetStatus as a temp variable
62                 
63                 // - Add to waiting
64                 if(Sem->LastWaiting) {
65                         Sem->LastWaiting->Next = us;
66                         Sem->LastWaiting = us;
67                 }
68                 else {
69                         Sem->Waiting = us;
70                         Sem->LastWaiting = us;
71                 }
72                 
73                 #if DEBUG_TRACE_STATE || SEMAPHORE_DEBUG
74                 Log("%p (%i %s) waiting on semaphore %p %s:%s",
75                         us, us->TID, us->ThreadName,
76                         Sem, Sem->ModName, Sem->Name);
77                 #endif
78                 
79                 SHORTREL( &Sem->Protector );    // Release first to make sure it is released
80                 SHORTREL( &glThreadListLock );
81                 // Sleep until woken (either by getting what we need, or a timer event)
82                 while( us->Status == THREAD_STAT_SEMAPHORESLEEP )
83                 {
84                         Threads_Yield();
85                         if(us->Status == THREAD_STAT_SEMAPHORESLEEP)
86                                 Log_Warning("Threads", "Semaphore %p %s:%s re-schedulued while asleep",
87                                         Sem, Sem->ModName, Sem->Name);
88                 }
89                 #if DEBUG_TRACE_STATE || SEMAPHORE_DEBUG
90                 Log("Semaphore %p %s:%s woken", Sem, Sem->ModName, Sem->Name);
91                 #endif
92                 // We're only woken when there's something avaliable (or a signal arrives)
93                 us->WaitPointer = NULL;
94                 
95                 taken = us->RetStatus;
96                 
97                 // Get the lock again
98                 SHORTLOCK( &Sem->Protector );
99         }
100         
101         // While there is space, and there are thread waiting
102         // wake the first thread and give it what it wants (or what's left)
103         while( (Sem->MaxValue == 0 || Sem->Value < Sem->MaxValue) && Sem->Signaling )
104         {
105                  int    given;
106                 tThread *toWake = Sem->Signaling;
107                 
108                 Sem->Signaling = Sem->Signaling->Next;
109                 // Reset ->LastWaiting to NULL if we have just removed the last waiting thread
110                 if( Sem->Signaling == NULL )
111                         Sem->LastSignaling = NULL;
112                 
113                 // Figure out how much to give
114                 if( toWake->RetStatus && Sem->Value + toWake->RetStatus < Sem->MaxValue )
115                         given = toWake->RetStatus;
116                 else
117                         given = Sem->MaxValue - Sem->Value;
118                 Sem->Value -= given;
119                 
120                 
121                 #if DEBUG_TRACE_STATE || SEMAPHORE_DEBUG
122                 Log("%p (%i %s) woken by wait on %p %s:%s",
123                         toWake, toWake->TID, toWake->ThreadName,
124                         Sem, Sem->ModName, Sem->Name);
125                 #endif
126                 
127                 // Save the number we gave to the thread's status
128                 toWake->RetStatus = given;
129                 
130                 // Wake the sleeper
131                 if( toWake->Status != THREAD_STAT_ACTIVE )
132                         Threads_AddActive(toWake);
133         }
134         SHORTREL( &Sem->Protector );
135         
136         #if DEBUG_TRACE_STATE || SEMAPHORE_DEBUG
137         Log("Semaphore %p %s:%s took %i by wait",
138                 Sem, Sem->ModName, Sem->Name, taken);
139         #endif
140
141         return taken;
142 }
143
144 //
145 // Add items to a semaphore
146 //
147 int Semaphore_Signal(tSemaphore *Sem, int AmmountToAdd)
148 {
149          int    given;
150          int    added;
151         
152         if( AmmountToAdd < 0 ) {
153                 Log_Warning("Threads", "Semaphore_Signal: User bug - AmmountToAdd(%i) < 0, Sem=%p(%s)",
154                         AmmountToAdd, Sem, Sem->Name);
155         }
156         SHORTLOCK( &Sem->Protector );
157         
158         // Check if we have to block
159         if( Sem->MaxValue && Sem->Value == Sem->MaxValue )
160         {
161                 tThread *us;
162                 #if 0
163                 Log_Debug("Threads", "Semaphore_Signal: IDLE Sem = %s:%s", Sem->ModName, Sem->Name);
164                 Log_Debug("Threads", "Semaphore_Signal: Sem->Value(%i) == Sem->MaxValue(%i)", Sem->Value, Sem->MaxValue);
165                 #endif
166                 
167                 SHORTLOCK( &glThreadListLock );
168                 // - Remove from active list
169                 us = Threads_RemActive();
170                 us->Next = NULL;
171                 // - Mark as sleeping
172                 us->Status = THREAD_STAT_SEMAPHORESLEEP;
173                 us->WaitPointer = Sem;
174                 us->RetStatus = AmmountToAdd;   // Use RetStatus as a temp variable
175                 
176                 // - Add to waiting
177                 if(Sem->LastSignaling) {
178                         Sem->LastSignaling->Next = us;
179                         Sem->LastSignaling = us;
180                 }
181                 else {
182                         Sem->Signaling = us;
183                         Sem->LastSignaling = us;
184                 }
185                 
186                 #if DEBUG_TRACE_STATE || SEMAPHORE_DEBUG
187                 Log("%p (%i %s) signaling semaphore %p %s:%s",
188                         us, us->TID, us->ThreadName,
189                         Sem, Sem->ModName, Sem->Name);
190                 #endif
191                 
192                 SHORTREL( &glThreadListLock );  
193                 SHORTREL( &Sem->Protector );
194                 while(us->Status == THREAD_STAT_SEMAPHORESLEEP) Threads_Yield();
195                 // We're only woken when there's something avaliable
196                 us->WaitPointer = NULL;
197                 
198                 added = us->RetStatus;
199                 
200                 // Get the lock again
201                 SHORTLOCK( &Sem->Protector );
202         }
203         // Non blocking
204         else
205         {
206                 // Figure out how much we need to take off
207                 if( Sem->MaxValue && Sem->Value + AmmountToAdd > Sem->MaxValue)
208                         added = Sem->MaxValue - Sem->Value;
209                 else
210                         added = AmmountToAdd;
211                 Sem->Value += added;
212         }
213         
214         // While there are items avaliable, and there are thread waiting
215         // wake the first thread and give it what it wants (or what's left)
216         while( Sem->Value && Sem->Waiting )
217         {
218                 tThread *toWake = Sem->Waiting;
219                 
220                 // Remove thread from list (double ended, so clear LastWaiting if needed)
221                 Sem->Waiting = Sem->Waiting->Next;
222                 if( Sem->Waiting == NULL )
223                         Sem->LastWaiting = NULL;
224                 
225                 // Figure out how much to give to woken thread
226                 // - Requested count is stored in ->RetStatus
227                 if( toWake->RetStatus && Sem->Value > toWake->RetStatus )
228                         given = toWake->RetStatus;
229                 else
230                         given = Sem->Value;
231                 Sem->Value -= given;
232                 
233                 // Save the number we gave to the thread's status
234                 toWake->RetStatus = given;
235                 
236                 if(toWake->bInstrTrace)
237                         Log("%s(%i) given %i from %p", toWake->ThreadName, toWake->TID, given, Sem);
238                 #if DEBUG_TRACE_STATE || SEMAPHORE_DEBUG
239                 Log("%p (%i %s) woken by signal on %p %s:%s",
240                         toWake, toWake->TID, toWake->ThreadName,
241                         Sem, Sem->ModName, Sem->Name);
242                 #endif
243                 
244                 // Wake the sleeper
245                 if( toWake->Status != THREAD_STAT_ACTIVE )
246                         Threads_AddActive(toWake);
247                 else
248                         Warning("Thread %p (%i %s) is already awake",
249                                 toWake, toWake->TID, toWake->ThreadName);
250         }
251         SHORTREL( &Sem->Protector );
252         
253         return added;
254 }
255
256 void Semaphore_ForceWake(tThread *Thread)
257 {
258         if( !CPU_HAS_LOCK(&Thread->IsLocked) ) {
259                 Log_Error("Semaphore", "Force wake should be called with the thread lock held");
260                 return ;
261         }
262         if( Thread->Status != THREAD_STAT_SEMAPHORESLEEP ) {
263                 Log_Error("Semaphore", "_ForceWake called on non-semaphore thread");
264                 return ;
265         }
266
267         tSemaphore *sem = Thread->WaitPointer;
268         SHORTLOCK( &sem->Protector );
269         tThread *prev = NULL;
270         if( sem->Waiting == Thread )
271                 sem->Waiting = sem->Waiting->Next;
272         else
273         {
274                 for( prev = sem->Waiting; prev && prev->Next != Thread; prev = prev->Next )
275                         ;
276                 if( prev )
277                         prev->Next = Thread->Next;
278         }
279         if( sem->LastWaiting == Thread )
280                 sem->LastWaiting = prev;
281         SHORTREL( &sem->Protector );
282         Thread->RetStatus = 0;
283         Threads_AddActive(Thread);
284 }
285
286 //
287 // Get the current value of a semaphore
288 //
289 int Semaphore_GetValue(tSemaphore *Sem)
290 {
291         return Sem->Value;
292 }
293
294 // === EXPORTS ===
295 EXPORT(Semaphore_Init);
296 EXPORT(Semaphore_Wait);
297 EXPORT(Semaphore_Signal);

UCC git Repository :: git.ucc.asn.au