3 * - By thePowersGang (John Hodge)
6 * - Implements the select() system call (and supporting code)
13 #include <semaphore.h>
16 #define NUM_THREADS_PER_ALLOC 4
19 typedef struct sVFS_SelectThread tVFS_SelectThread;
20 typedef struct sVFS_SelectListEnt tVFS_SelectListEnt;
23 struct sVFS_SelectListEnt
25 tVFS_SelectListEnt *Next;
26 tVFS_SelectThread *Threads[NUM_THREADS_PER_ALLOC];
29 struct sVFS_SelectList
32 tVFS_SelectListEnt FirstEnt;
35 struct sVFS_SelectThread
37 //! \brief Semaphore to atomically put the listener to sleep
38 tSemaphore SleepHandle; // TODO: Allow timeouts (by setting an alarm?)
42 // int VFS_SelectNode(tVFS_Node *Node, enum eVFS_SelectTypes Type, tTime *Timeout);
43 // int VFS_Select(int MaxHandle, fd_set *ReadHandles, fd_set *WriteHandles, fd_set *ErrHandles, tTime *Timeout, BOOL IsKernel);
44 // int VFS_MarkFull(tVFS_Node *Node, BOOL IsBufferFull);
45 // int VFS_MarkAvaliable(tVFS_Node *Node, BOOL IsDataAvaliable);
46 // int VFS_MarkError(tVFS_Node *Node, BOOL IsErrorState);
47 int VFS_int_Select_GetType(int Type, tVFS_Node *Node, tVFS_SelectList ***List, int **Flag, int *WantedFlag, int *MaxAllowed);
48 int VFS_int_Select_Register(tVFS_SelectThread *Thread, int MaxHandle, fd_set *Handles, int Type, BOOL IsKernel);
49 int VFS_int_Select_Deregister(tVFS_SelectThread *Thread, int MaxHandle, fd_set *Handles, int Type, BOOL IsKernel);
50 int VFS_int_Select_AddThread(tVFS_SelectList *List, tVFS_SelectThread *Thread, int MaxAllowed);
51 void VFS_int_Select_RemThread(tVFS_SelectList *List, tVFS_SelectThread *Thread);
52 void VFS_int_Select_SignalAll(tVFS_SelectList *List);
57 int VFS_SelectNode(tVFS_Node *Node, int TypeFlags, tTime *Timeout, const char *Name)
59 tVFS_SelectThread *thread_info;
62 ENTER("pNode iTypeFlags pTimeout", Node, TypeFlags, Timeout);
64 thread_info = malloc(sizeof(tVFS_SelectThread));
65 if(!thread_info) LEAVE_RET('i', -1);
67 Semaphore_Init(&thread_info->SleepHandle, 0, 0, "VFS_SelectNode()", Name);
70 for( type = 0; type < 3; type ++ )
72 tVFS_SelectList **list;
73 int *flag, wanted, maxAllowed;
74 if( !(TypeFlags & (1 << type)) ) continue;
75 if( VFS_int_Select_GetType(type, Node, &list, &flag, &wanted, &maxAllowed) ) {
82 if( !*list ) *list = calloc(1, sizeof(tVFS_SelectList));
84 VFS_int_Select_AddThread(*list, thread_info, maxAllowed);
87 VFS_int_Select_RemThread(*list, thread_info);
94 // - Fast return for polling
95 if( Timeout && *Timeout == 0 ) return 0;
98 if( !Timeout || *Timeout > 0 )
100 LOG("Semaphore_Wait()");
101 // TODO: Actual timeout
102 Semaphore_Wait(&thread_info->SleepHandle, 1);
107 for( type = 0; type < 3; type ++ )
109 tVFS_SelectList **list;
110 int *flag, wanted, maxAllowed;
111 if( !(TypeFlags & (1 << type)) ) continue;
112 VFS_int_Select_GetType(type, Node, &list, &flag, &wanted, &maxAllowed);
113 LOG("VFS_int_Select_RemThread()");
114 VFS_int_Select_RemThread(*list, thread_info);
115 ret = ret || *flag == wanted;
124 int VFS_Select(int MaxHandle, fd_set *ReadHandles, fd_set *WriteHandles, fd_set *ErrHandles, tTime *Timeout, BOOL IsKernel)
126 tVFS_SelectThread *thread_info;
129 thread_info = malloc(sizeof(tVFS_SelectThread));
130 if(!thread_info) return -1;
132 ENTER("iMaxHandle pReadHandles pWriteHandles pErrHandles pTimeout bIsKernel",
133 MaxHandle, ReadHandles, WriteHandles, ErrHandles, Timeout, IsKernel);
135 Semaphore_Init(&thread_info->SleepHandle, 0, 0, "VFS_Select()", "");
137 // Notes: The idea is to make sure we only enter wait (on the semaphore)
138 // if we are going to be woken up (either by an event at a later time,
139 // or by an event that happened while or before we were registering).
140 // Hence, register must happen _before_ we check the state flag
141 // (See VFS_int_Select_Register), that way either we pick up the flag,
142 // or the semaphore is incremeneted (or both, but never none)
144 // Register with nodes
145 ret = VFS_int_Select_Register(thread_info, MaxHandle, ReadHandles, 0, IsKernel);
146 ret += VFS_int_Select_Register(thread_info, MaxHandle, WriteHandles, 1, IsKernel);
147 ret += VFS_int_Select_Register(thread_info, MaxHandle, ErrHandles, 2, IsKernel);
149 LOG("Register ret = %i", ret);
151 // If there were events waiting, de-register and return
154 ret = VFS_int_Select_Deregister(thread_info, MaxHandle, ReadHandles, 0, IsKernel);
155 ret += VFS_int_Select_Deregister(thread_info, MaxHandle, WriteHandles, 1, IsKernel);
156 ret += VFS_int_Select_Deregister(thread_info, MaxHandle, ErrHandles, 2, IsKernel);
162 // TODO: Implement timeout
164 // Wait (only if there is no timeout, or it is greater than zero
165 if( !Timeout || *Timeout > 0 )
167 ret = Semaphore_Wait(&thread_info->SleepHandle, 1);
168 // TODO: Do something with ret?
171 // Fill output (modify *Handles)
172 // - Also, de-register
173 ret = VFS_int_Select_Deregister(thread_info, MaxHandle, ReadHandles, 0, IsKernel);
174 ret += VFS_int_Select_Deregister(thread_info, MaxHandle, WriteHandles, 1, IsKernel);
175 ret += VFS_int_Select_Deregister(thread_info, MaxHandle, ErrHandles, 2, IsKernel);
181 // Mark a node as having data ready for reading
182 int VFS_MarkAvaliable(tVFS_Node *Node, BOOL IsDataAvaliable)
184 ENTER("pNode bIsDataAvaliable", Node, IsDataAvaliable);
185 Node->DataAvaliable = !!IsDataAvaliable;
186 if( Node->DataAvaliable )
187 VFS_int_Select_SignalAll(Node->ReadThreads);
192 // Mark a node as having a full buffer
193 int VFS_MarkFull(tVFS_Node *Node, BOOL IsBufferFull)
195 ENTER("pNode bIsDataAvaliable", Node, IsBufferFull);
196 Node->BufferFull = !!IsBufferFull;
197 if( !Node->BufferFull )
198 VFS_int_Select_SignalAll(Node->WriteThreads);
203 // Mark a node as errored
204 int VFS_MarkError(tVFS_Node *Node, BOOL IsErrorState)
206 ENTER("pNode bIsDataAvaliable", Node, IsErrorState);
207 Node->ErrorOccurred = !!IsErrorState;
208 if( Node->ErrorOccurred )
209 VFS_int_Select_SignalAll(Node->ErrorThreads);
215 int VFS_int_Select_GetType(int Type, tVFS_Node *Node, tVFS_SelectList ***List, int **Flag, int *WantedFlag, int *MaxAllowed)
217 // Get the type of the listen
221 if(List) *List = &Node->ReadThreads;
222 if(Flag) *Flag = &Node->DataAvaliable;
223 if(WantedFlag) *WantedFlag = 1;
224 if(MaxAllowed) *MaxAllowed = 1; // Max of 1 for read
227 if(List) *List = &Node->WriteThreads;
228 if(Flag) *Flag = &Node->BufferFull;
229 if(WantedFlag) *WantedFlag = 0;
230 if(MaxAllowed) *MaxAllowed = 1; // Max of 1 for write
233 if(List) *List = &Node->ErrorThreads;
234 if(Flag) *Flag = &Node->ErrorOccurred;
235 if(WantedFlag) *WantedFlag = 1;
236 if(MaxAllowed) *MaxAllowed = -1; // No max for error listeners
239 Log_Error("VFS", "VFS_int_Select_GetType: BUG CHECK, Unknown Type %i", Type);
246 * \return Number of files with an action
248 int VFS_int_Select_Register(tVFS_SelectThread *Thread, int MaxHandle, fd_set *Handles, int Type, BOOL IsKernel)
250 int i, numFlagged = 0;
251 tVFS_SelectList **list;
252 int *flag, wantedFlagValue;
255 if( !Handles ) return 0;
257 ENTER("pThread iMaxHandle pHandles iType iIsKernel", Thread, MaxHandle, Handles, Type, IsKernel);
259 for( i = 0; i < MaxHandle; i ++ )
263 // Is the descriptor set
264 if( !FD_ISSET(i, Handles) ) continue;
267 handle = VFS_GetHandle( i | (IsKernel?VFS_KERNEL_FLAG:0) );
268 // Is the handle valid?
269 if( !handle || !handle->Node )
271 if( Type == 2 ) { // Bad FD counts as an error
280 // Get the type of the listen
281 if( VFS_int_Select_GetType(Type, handle->Node, &list, &flag, &wantedFlagValue, &maxAllowed) ) {
288 *list = calloc(1, sizeof(tVFS_SelectList));
292 if( VFS_int_Select_AddThread(*list, Thread, maxAllowed ) )
294 // Oops, error (or just no space)
298 // Check for the flag
299 if( !!*flag == !!wantedFlagValue )
303 LEAVE('i', numFlagged);
308 * \return Number of files with an action
310 int VFS_int_Select_Deregister(tVFS_SelectThread *Thread, int MaxHandle, fd_set *Handles, int Type, BOOL IsKernel)
312 int i, numFlagged = 0;
313 tVFS_SelectList **list;
314 int *flag, wantedFlagValue;
316 if( !Handles ) return 0;
318 ENTER("pThread iMaxHandle pHandles iType iIsKernel", Thread, MaxHandle, Handles, Type, IsKernel);
320 for( i = 0; i < MaxHandle; i ++ )
324 // Is the descriptor set
325 if( !FD_ISSET(i, Handles) ) continue;
328 handle = VFS_GetHandle( i | (IsKernel?VFS_KERNEL_FLAG:0) );
329 // Is the handle valid?
330 if( !handle || !handle->Node )
332 if( Type == 2 ) { // Bad FD counts as an error
341 // Get the type of the listen
343 // Get the type of the listen
344 if( VFS_int_Select_GetType(Type, handle->Node, &list, &flag, &wantedFlagValue, NULL) ) {
350 VFS_int_Select_RemThread(*list, Thread );
352 // Check for the flag
353 if( !!*flag == !!wantedFlagValue ) {
361 LEAVE('i', numFlagged);
367 * \return Boolean failure
369 int VFS_int_Select_AddThread(tVFS_SelectList *List, tVFS_SelectThread *Thread, int MaxAllowed)
372 tVFS_SelectListEnt *block, *prev;
374 ENTER("pList pThread iMaxAllowed", List, Thread, MaxAllowed);
376 // Lock to avoid concurrency issues
377 Mutex_Acquire(&List->Lock);
379 block = &List->FirstEnt;
381 // Look for free space
384 for( i = 0; i < NUM_THREADS_PER_ALLOC; i ++ )
386 if( block->Threads[i] == NULL )
388 block->Threads[i] = Thread;
389 Mutex_Release(&List->Lock);
394 if( MaxAllowed && count >= MaxAllowed ) {
407 block = malloc( sizeof(tVFS_SelectListEnt) );
409 Log_Warning("VFS", "VFS_int_Select_AddThread: malloc() failed");
410 Mutex_Release(&List->Lock);
414 block->Threads[0] = Thread;
415 for( i = 1; i < NUM_THREADS_PER_ALLOC; i ++ )
417 block->Threads[i] = NULL;
424 Mutex_Release(&List->Lock);
430 void VFS_int_Select_RemThread(tVFS_SelectList *List, tVFS_SelectThread *Thread)
433 tVFS_SelectListEnt *block, *prev;
435 ENTER("pList pThread", List, Thread);
437 // Lock to avoid concurrency issues
438 Mutex_Acquire(&List->Lock);
440 block = &List->FirstEnt;
442 // Look for the thread
445 for( i = 0; i < NUM_THREADS_PER_ALLOC; i ++ )
447 if( block->Threads[i] == Thread )
449 block->Threads[i] = NULL;
451 // Check if this block is empty
452 if( block != &List->FirstEnt )
454 for( i = 0; i < NUM_THREADS_PER_ALLOC; i ++ )
455 if( block->Threads[i] )
458 if( i == NUM_THREADS_PER_ALLOC ) {
459 LOG("Deleting block");
460 prev->Next = block->Next;
463 //TODO: If not empty, check if it can be merged downwards
466 Mutex_Release(&List->Lock);
476 // Not on list, is this an error?
478 Mutex_Release(&List->Lock);
485 * \brief Signal all threads on a list
487 void VFS_int_Select_SignalAll(tVFS_SelectList *List)
490 tVFS_SelectListEnt *block;
494 ENTER("pList", List);
496 // Lock to avoid concurrency issues
497 Mutex_Acquire(&List->Lock);
499 block = &List->FirstEnt;
501 // Look for the thread
504 for( i = 0; i < NUM_THREADS_PER_ALLOC; i ++ )
506 if( block->Threads[i] )
508 LOG("block(%p)->Threads[%i] = %p", block, i, block->Threads[i]);
509 Semaphore_Signal( &block->Threads[i]->SleepHandle, 1 );
516 Mutex_Release(&List->Lock);