3 * - By thePowersGang (John Hodge)
6 * - Implements the select() system call (and supporting code)
8 * TODO: Implment timeouts (via an alarm event?)
9 * TODO: Remove malloc for read/write queues
16 #include <semaphore.h>
22 #define NUM_THREADS_PER_ALLOC 4
25 typedef struct sVFS_SelectListEnt tVFS_SelectListEnt;
28 struct sVFS_SelectListEnt
30 tVFS_SelectListEnt *Next;
31 tThread *Threads[NUM_THREADS_PER_ALLOC];
34 // NOTE: Typedef is in vfs.h
35 struct sVFS_SelectList
38 tVFS_SelectListEnt FirstEnt;
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, Uint32 ExtraEvents, 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(tThread *Thread, int MaxHandle, fd_set *Handles, int Type, BOOL IsKernel);
49 int VFS_int_Select_Deregister(tThread *Thread, int MaxHandle, fd_set *Handles, int Type, BOOL IsKernel);
50 int VFS_int_Select_AddThread(tVFS_SelectList *List, tThread *Thread, int MaxAllowed);
51 void VFS_int_Select_RemThread(tVFS_SelectList *List, tThread *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 tThread *thisthread = Proc_GetCurThread();
62 ENTER("pNode iTypeFlags pTimeout sName", Node, TypeFlags, Timeout, Name);
65 for( type = 0; type < 3; type ++ )
67 tVFS_SelectList **list;
68 int *flag, wanted, maxAllowed;
69 if( !(TypeFlags & (1 << type)) ) continue;
70 if( VFS_int_Select_GetType(type, Node, &list, &flag, &wanted, &maxAllowed) ) {
76 if( !*list ) *list = calloc(1, sizeof(tVFS_SelectList));
78 VFS_int_Select_AddThread(*list, thisthread, maxAllowed);
81 VFS_int_Select_RemThread(*list, thisthread);
90 LOG("Semaphore_Wait()");
91 // TODO: Actual timeout
92 Threads_WaitEvents( THREAD_EVENT_VFS );
94 else if( *Timeout > 0 )
96 tTimer *t = Time_AllocateTimer(NULL, NULL);
98 Threads_ClearEvent( THREAD_EVENT_TIMER );
99 // TODO: Convert *Timeout?
100 LOG("Timeout %lli ms", *Timeout);
101 Time_ScheduleTimer( t, *Timeout );
102 // Wait for the timer or a VFS event
103 Threads_WaitEvents( THREAD_EVENT_VFS|THREAD_EVENT_TIMER );
109 for( type = 0; type < 3; type ++ )
111 tVFS_SelectList **list;
112 int *flag, wanted, maxAllowed;
113 if( !(TypeFlags & (1 << type)) ) continue;
114 VFS_int_Select_GetType(type, Node, &list, &flag, &wanted, &maxAllowed);
115 LOG("VFS_int_Select_RemThread()");
117 VFS_int_Select_RemThread(*list, thisthread);
118 ret = ret || *flag == wanted;
121 Threads_ClearEvent( THREAD_EVENT_VFS );
122 Threads_ClearEvent( THREAD_EVENT_TIMER );
128 int VFS_Select(int MaxHandle, fd_set *ReadHandles, fd_set *WriteHandles, fd_set *ErrHandles, tTime *Timeout, Uint32 ExtraEvents, BOOL IsKernel)
130 tThread *thisthread = Proc_GetCurThread();
133 ENTER("iMaxHandle pReadHandles pWriteHandles pErrHandles pTimeout xExtraEvents bIsKernel",
134 MaxHandle, ReadHandles, WriteHandles, ErrHandles, Timeout, ExtraEvents, IsKernel);
136 // Notes: The idea is to make sure we only enter wait (Threads_WaitEvents)
137 // if we are going to be woken up (either by an event at a later time,
138 // or by an event that happened while or before we were registering).
139 // Hence, register must happen _before_ we check the state flag
140 // (See VFS_int_Select_Register), that way either we pick up the flag,
141 // or the semaphore is incremeneted (or both, but never none)
143 // Register with nodes
145 LOG(" - ReadHandles[0] = %x", ReadHandles->flags[0]);
146 ret = VFS_int_Select_Register(thisthread, MaxHandle, ReadHandles, 0, IsKernel);
147 ret += VFS_int_Select_Register(thisthread, MaxHandle, WriteHandles, 1, IsKernel);
148 ret += VFS_int_Select_Register(thisthread, MaxHandle, ErrHandles, 2, IsKernel);
150 LOG("Register ret = %i", ret);
152 // If there were events waiting, de-register and return
155 LOG(" - ReadHandles[0] = %x", ReadHandles->flags[0]);
156 ret = VFS_int_Select_Deregister(thisthread, MaxHandle, ReadHandles, 0, IsKernel);
157 ret += VFS_int_Select_Deregister(thisthread, MaxHandle, WriteHandles, 1, IsKernel);
158 ret += VFS_int_Select_Deregister(thisthread, MaxHandle, ErrHandles, 2, IsKernel);
159 LOG(" - ReadHandles[0] = %x", ReadHandles->flags[0]);
167 LOG("Semaphore_Wait()");
168 // TODO: Actual timeout
169 Threads_WaitEvents( THREAD_EVENT_VFS|ExtraEvents );
171 else if( *Timeout > 0 )
173 tTimer *t = Time_AllocateTimer(NULL, NULL);
175 Threads_ClearEvent( THREAD_EVENT_TIMER );
176 // TODO: Convert *Timeout?
177 LOG("Timeout %lli ms", *Timeout);
178 Time_ScheduleTimer( t, *Timeout );
179 // Wait for the timer or a VFS event
180 Threads_WaitEvents( THREAD_EVENT_VFS|THREAD_EVENT_TIMER|ExtraEvents );
183 // Fill output (modify *Handles)
184 // - Also, de-register
185 ret = VFS_int_Select_Deregister(thisthread, MaxHandle, ReadHandles, 0, IsKernel);
186 ret += VFS_int_Select_Deregister(thisthread, MaxHandle, WriteHandles, 1, IsKernel);
187 ret += VFS_int_Select_Deregister(thisthread, MaxHandle, ErrHandles, 2, IsKernel);
189 Threads_ClearEvent( THREAD_EVENT_VFS );
190 Threads_ClearEvent( THREAD_EVENT_TIMER );
196 // Mark a node as having data ready for reading
197 int VFS_MarkAvaliable(tVFS_Node *Node, BOOL IsDataAvaliable)
199 ENTER("pNode bIsDataAvaliable", Node, IsDataAvaliable);
200 Node->DataAvaliable = !!IsDataAvaliable;
201 if( Node->DataAvaliable )
202 VFS_int_Select_SignalAll(Node->ReadThreads);
207 // Mark a node as having a full buffer
208 int VFS_MarkFull(tVFS_Node *Node, BOOL IsBufferFull)
210 ENTER("pNode bIsBufferFull", Node, IsBufferFull);
211 Node->BufferFull = !!IsBufferFull;
212 if( !Node->BufferFull )
213 VFS_int_Select_SignalAll(Node->WriteThreads);
218 // Mark a node as errored
219 int VFS_MarkError(tVFS_Node *Node, BOOL IsErrorState)
221 ENTER("pNode bIsErrorState", Node, IsErrorState);
222 Node->ErrorOccurred = !!IsErrorState;
223 if( Node->ErrorOccurred )
224 VFS_int_Select_SignalAll(Node->ErrorThreads);
230 int VFS_int_Select_GetType(int Type, tVFS_Node *Node, tVFS_SelectList ***List, int **Flag, int *WantedFlag, int *MaxAllowed)
232 // Get the type of the listen
236 if(List) *List = &Node->ReadThreads;
237 if(Flag) *Flag = &Node->DataAvaliable;
238 if(WantedFlag) *WantedFlag = 1;
239 if(MaxAllowed) *MaxAllowed = 1; // Max of 1 for read
242 if(List) *List = &Node->WriteThreads;
243 if(Flag) *Flag = &Node->BufferFull;
244 if(WantedFlag) *WantedFlag = 0;
245 if(MaxAllowed) *MaxAllowed = 1; // Max of 1 for write
248 if(List) *List = &Node->ErrorThreads;
249 if(Flag) *Flag = &Node->ErrorOccurred;
250 if(WantedFlag) *WantedFlag = 1;
251 if(MaxAllowed) *MaxAllowed = -1; // No max for error listeners
254 Log_Error("VFS", "VFS_int_Select_GetType: BUG CHECK, Unknown Type %i", Type);
261 * \return Number of files with an action
263 int VFS_int_Select_Register(tThread *Thread, int MaxHandle, fd_set *Handles, int Type, BOOL IsKernel)
265 int i, numFlagged = 0;
266 tVFS_SelectList **list;
267 int *flag, wantedFlagValue;
270 if( !Handles ) return 0;
272 ENTER("pThread iMaxHandle pHandles iType iIsKernel", Thread, MaxHandle, Handles, Type, IsKernel);
274 for( i = 0; i < MaxHandle; i ++ )
278 // Is the descriptor set
279 if( !FD_ISSET(i, Handles) ) continue;
282 handle = VFS_GetHandle( i | (IsKernel?VFS_KERNEL_FLAG:0) );
283 // Is the handle valid?
284 if( !handle || !handle->Node )
286 if( Type == 2 ) { // Bad FD counts as an error
295 // Get the type of the listen
296 if( VFS_int_Select_GetType(Type, handle->Node, &list, &flag, &wantedFlagValue, &maxAllowed) ) {
303 *list = calloc(1, sizeof(tVFS_SelectList));
307 if( VFS_int_Select_AddThread(*list, Thread, maxAllowed ) )
309 // Oops, error (or just no space)
313 // Check for the flag
314 if( !!*flag == !!wantedFlagValue ) {
315 LOG(" %i == want %i", !!*flag, !!wantedFlagValue);
320 LEAVE('i', numFlagged);
325 * \return Number of files with an action
327 int VFS_int_Select_Deregister(tThread *Thread, int MaxHandle, fd_set *Handles, int Type, BOOL IsKernel)
329 int i, numFlagged = 0;
330 tVFS_SelectList **list;
331 int *flag, wantedFlagValue;
333 if( !Handles ) return 0;
335 ENTER("pThread iMaxHandle pHandles iType iIsKernel", Thread, MaxHandle, Handles, Type, IsKernel);
337 for( i = 0; i < MaxHandle; i ++ )
341 // Is the descriptor set
342 if( !FD_ISSET(i, Handles) ) continue;
345 handle = VFS_GetHandle( i | (IsKernel?VFS_KERNEL_FLAG:0) );
346 // Is the handle valid?
347 if( !handle || !handle->Node )
349 if( Type == 2 ) { // Bad FD counts as an error
358 // Get the type of the listen
359 if( VFS_int_Select_GetType(Type, handle->Node, &list, &flag, &wantedFlagValue, NULL) ) {
365 VFS_int_Select_RemThread(*list, Thread );
367 // Check for the flag
368 if( !!*flag == !!wantedFlagValue ) {
370 LOG(" %i == want %i", !!*flag, !!wantedFlagValue);
378 LEAVE('i', numFlagged);
384 * \return Boolean failure
386 int VFS_int_Select_AddThread(tVFS_SelectList *List, tThread *Thread, int MaxAllowed)
389 tVFS_SelectListEnt *block, *prev;
391 ENTER("pList pThread iMaxAllowed", List, Thread, MaxAllowed);
393 // Lock to avoid concurrency issues
394 Mutex_Acquire(&List->Lock);
396 block = &List->FirstEnt;
398 // Look for free space
401 for( i = 0; i < NUM_THREADS_PER_ALLOC; i ++ )
403 if( block->Threads[i] == NULL )
405 block->Threads[i] = Thread;
406 Mutex_Release(&List->Lock);
411 if( MaxAllowed && count >= MaxAllowed ) {
412 Mutex_Release(&List->Lock);
425 block = malloc( sizeof(tVFS_SelectListEnt) );
427 Log_Warning("VFS", "VFS_int_Select_AddThread: malloc() failed");
428 Mutex_Release(&List->Lock);
432 block->Threads[0] = Thread;
433 for( i = 1; i < NUM_THREADS_PER_ALLOC; i ++ )
435 block->Threads[i] = NULL;
442 Mutex_Release(&List->Lock);
448 void VFS_int_Select_RemThread(tVFS_SelectList *List, tThread *Thread)
450 tVFS_SelectListEnt *block, *prev = NULL;
452 ENTER("pList pThread", List, Thread);
454 // Lock to avoid concurrency issues
455 Mutex_Acquire(&List->Lock);
457 block = &List->FirstEnt;
459 // Look for the thread
462 for( int i = 0; i < NUM_THREADS_PER_ALLOC; i ++ )
464 if( block->Threads[i] == Thread )
466 block->Threads[i] = NULL;
468 // Check if this block is empty
469 if( block != &List->FirstEnt )
471 for( i = 0; i < NUM_THREADS_PER_ALLOC; i ++ )
472 if( block->Threads[i] )
475 if( i == NUM_THREADS_PER_ALLOC ) {
476 LOG("Deleting block");
477 prev->Next = block->Next;
480 //TODO: If not empty, check if it can be merged downwards
483 Mutex_Release(&List->Lock);
493 // Not on list, is this an error?
495 Mutex_Release(&List->Lock);
502 * \brief Signal all threads on a list
504 void VFS_int_Select_SignalAll(tVFS_SelectList *List)
507 tVFS_SelectListEnt *block;
511 ENTER("pList", List);
513 // Lock to avoid concurrency issues
514 Mutex_Acquire(&List->Lock);
516 block = &List->FirstEnt;
518 // Look for the thread
521 for( i = 0; i < NUM_THREADS_PER_ALLOC; i ++ )
523 if( block->Threads[i] )
525 LOG("block(%p)->Threads[%i] = %p", block, i, block->Threads[i]);
526 Threads_PostEvent( block->Threads[i], THREAD_EVENT_VFS );
533 Mutex_Release(&List->Lock);