3 * - By John Hodge (thePowersGang)
10 #include <threads_int.h>
12 #define SEMAPHORE_DEBUG 0 // Debug semaphores
16 // Initialise a semaphore
18 void Semaphore_Init(tSemaphore *Sem, int Value, int MaxValue, const char *Module, const char *Name)
20 memset(Sem, 0, sizeof(tSemaphore));
22 Sem->ModName = Module;
24 Sem->MaxValue = MaxValue;
27 // Wait for items to be avaliable
29 int Semaphore_Wait(tSemaphore *Sem, int MaxToTake)
34 Log_Warning("Threads", "Semaphore_Wait: User bug - MaxToTake(%i) < 0, Sem=%p(%s)",
35 MaxToTake, Sem, Sem->Name);
39 SHORTLOCK( &Sem->Protector );
41 // Check if there's already items avaliable
45 if( MaxToTake && Sem->Value > MaxToTake )
53 SHORTLOCK( &glThreadListLock );
55 // - Remove from active list
56 us = Threads_RemActive();
59 us->Status = THREAD_STAT_SEMAPHORESLEEP;
60 us->WaitPointer = Sem;
61 us->RetStatus = MaxToTake; // Use RetStatus as a temp variable
64 if(Sem->LastWaiting) {
65 Sem->LastWaiting->Next = us;
66 Sem->LastWaiting = us;
70 Sem->LastWaiting = us;
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);
79 SHORTREL( &Sem->Protector ); // Release first to make sure it is released
80 SHORTREL( &glThreadListLock );
81 while( us->Status == THREAD_STAT_SEMAPHORESLEEP )
84 if(us->Status == THREAD_STAT_SEMAPHORESLEEP)
85 Log_Warning("Threads", "Semaphore %p %s:%s re-schedulued while asleep",
86 Sem, Sem->ModName, Sem->Name);
88 #if DEBUG_TRACE_STATE || SEMAPHORE_DEBUG
89 Log("Semaphore %p %s:%s woken", Sem, Sem->ModName, Sem->Name);
91 // We're only woken when there's something avaliable (or a signal arrives)
92 us->WaitPointer = NULL;
94 taken = us->RetStatus;
97 SHORTLOCK( &Sem->Protector );
100 // While there is space, and there are thread waiting
101 // wake the first thread and give it what it wants (or what's left)
102 while( (Sem->MaxValue == 0 || Sem->Value < Sem->MaxValue) && Sem->Signaling )
105 tThread *toWake = Sem->Signaling;
107 Sem->Signaling = Sem->Signaling->Next;
108 // Reset ->LastWaiting to NULL if we have just removed the last waiting thread
109 if( Sem->Signaling == NULL )
110 Sem->LastSignaling = NULL;
112 // Figure out how much to give
113 if( toWake->RetStatus && Sem->Value + toWake->RetStatus < Sem->MaxValue )
114 given = toWake->RetStatus;
116 given = Sem->MaxValue - Sem->Value;
120 #if DEBUG_TRACE_STATE || SEMAPHORE_DEBUG
121 Log("%p (%i %s) woken by wait on %p %s:%s",
122 toWake, toWake->TID, toWake->ThreadName,
123 Sem, Sem->ModName, Sem->Name);
126 // Save the number we gave to the thread's status
127 toWake->RetStatus = given;
130 if( toWake->Status != THREAD_STAT_ACTIVE )
131 Threads_AddActive(toWake);
133 SHORTREL( &Sem->Protector );
135 #if DEBUG_TRACE_STATE || SEMAPHORE_DEBUG
136 Log("Semaphore %p %s:%s took %i by wait",
137 Sem, Sem->ModName, Sem->Name, taken);
144 // Add items to a semaphore
146 int Semaphore_Signal(tSemaphore *Sem, int AmmountToAdd)
151 if( AmmountToAdd < 0 ) {
152 Log_Warning("Threads", "Semaphore_Signal: User bug - AmmountToAdd(%i) < 0, Sem=%p(%s)",
153 AmmountToAdd, Sem, Sem->Name);
155 SHORTLOCK( &Sem->Protector );
157 // Check if we have to block
158 if( Sem->MaxValue && Sem->Value == Sem->MaxValue )
162 Log_Debug("Threads", "Semaphore_Signal: IDLE Sem = %s:%s", Sem->ModName, Sem->Name);
163 Log_Debug("Threads", "Semaphore_Signal: Sem->Value(%i) == Sem->MaxValue(%i)", Sem->Value, Sem->MaxValue);
166 SHORTLOCK( &glThreadListLock );
167 // - Remove from active list
168 us = Threads_RemActive();
170 // - Mark as sleeping
171 us->Status = THREAD_STAT_SEMAPHORESLEEP;
172 us->WaitPointer = Sem;
173 us->RetStatus = AmmountToAdd; // Use RetStatus as a temp variable
176 if(Sem->LastSignaling) {
177 Sem->LastSignaling->Next = us;
178 Sem->LastSignaling = us;
182 Sem->LastSignaling = us;
185 #if DEBUG_TRACE_STATE || SEMAPHORE_DEBUG
186 Log("%p (%i %s) signaling semaphore %p %s:%s",
187 us, us->TID, us->ThreadName,
188 Sem, Sem->ModName, Sem->Name);
191 SHORTREL( &glThreadListLock );
192 SHORTREL( &Sem->Protector );
193 while(us->Status == THREAD_STAT_SEMAPHORESLEEP) Threads_Yield();
194 // We're only woken when there's something avaliable
195 us->WaitPointer = NULL;
197 added = us->RetStatus;
199 // Get the lock again
200 SHORTLOCK( &Sem->Protector );
205 // Figure out how much we need to take off
206 if( Sem->MaxValue && Sem->Value + AmmountToAdd > Sem->MaxValue)
207 added = Sem->MaxValue - Sem->Value;
209 added = AmmountToAdd;
213 // While there are items avaliable, and there are thread waiting
214 // wake the first thread and give it what it wants (or what's left)
215 while( Sem->Value && Sem->Waiting )
217 tThread *toWake = Sem->Waiting;
219 // Remove thread from list (double ended, so clear LastWaiting if needed)
220 Sem->Waiting = Sem->Waiting->Next;
221 if( Sem->Waiting == NULL )
222 Sem->LastWaiting = NULL;
224 // Figure out how much to give to woken thread
225 // - Requested count is stored in ->RetStatus
226 if( toWake->RetStatus && Sem->Value > toWake->RetStatus )
227 given = toWake->RetStatus;
232 // Save the number we gave to the thread's status
233 toWake->RetStatus = given;
235 if(toWake->bInstrTrace)
236 Log("%s(%i) given %i from %p", toWake->ThreadName, toWake->TID, given, Sem);
237 #if DEBUG_TRACE_STATE || SEMAPHORE_DEBUG
238 Log("%p (%i %s) woken by signal on %p %s:%s",
239 toWake, toWake->TID, toWake->ThreadName,
240 Sem, Sem->ModName, Sem->Name);
244 if( toWake->Status != THREAD_STAT_ACTIVE )
245 Threads_AddActive(toWake);
247 Warning("Thread %p (%i %s) is already awake", toWake, toWake->TID, toWake->ThreadName);
249 SHORTREL( &Sem->Protector );
255 // Get the current value of a semaphore
257 int Semaphore_GetValue(tSemaphore *Sem)
263 EXPORT(Semaphore_Init);
264 EXPORT(Semaphore_Wait);
265 EXPORT(Semaphore_Signal);