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(enum eVFS_SelectTypes 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, enum eVFS_SelectTypes Type, BOOL IsKernel);
49 int VFS_int_Select_Deregister(tVFS_SelectThread *Thread, int MaxHandle, fd_set *Handles, enum eVFS_SelectTypes 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, enum eVFS_SelectTypes Type, tTime *Timeout, const char *Name)
59 tVFS_SelectThread *thread_info;
60 tVFS_SelectList **list;
61 int *flag, wanted, maxAllowed;
63 ENTER("pNode iType pTimeout", Node, Type, Timeout);
65 if( VFS_int_Select_GetType(Type, Node, &list, &flag, &wanted, &maxAllowed) ) {
70 thread_info = malloc(sizeof(tVFS_SelectThread));
71 if(!thread_info) return -1;
73 Semaphore_Init(&thread_info->SleepHandle, 0, 0, "VFS_SelectNode()", Name);
75 LOG("list=%p, flag=%p, wanted=%i, maxAllowed=%i", list, flag, wanted, maxAllowed);
79 *list = calloc(1, sizeof(tVFS_SelectList));
82 VFS_int_Select_AddThread(*list, thread_info, maxAllowed);
85 VFS_int_Select_RemThread(*list, thread_info);
91 if( !Timeout || *Timeout > 0 )
93 LOG("Semaphore_Wait()");
94 // TODO: Actual timeout
95 Semaphore_Wait(&thread_info->SleepHandle, 1);
98 LOG("VFS_int_Select_RemThread()");
99 VFS_int_Select_RemThread(*list, thread_info);
103 LEAVE('i', *flag == wanted);
104 return *flag == wanted;
107 int VFS_Select(int MaxHandle, fd_set *ReadHandles, fd_set *WriteHandles, fd_set *ErrHandles, tTime *Timeout, BOOL IsKernel)
109 tVFS_SelectThread *thread_info;
112 thread_info = malloc(sizeof(tVFS_SelectThread));
113 if(!thread_info) return -1;
115 ENTER("iMaxHandle pReadHandles pWriteHandles pErrHandles pTimeout bIsKernel",
116 MaxHandle, ReadHandles, WriteHandles, ErrHandles, Timeout, IsKernel);
118 Semaphore_Init(&thread_info->SleepHandle, 0, 0, "VFS_Select()", "");
120 // Notes: The idea is to make sure we only enter wait (on the semaphore)
121 // if we are going to be woken up (either by an event at a later time,
122 // or by an event that happened while or before we were registering).
123 // Hence, register must happen _before_ we check the state flag
124 // (See VFS_int_Select_Register), that way either we pick up the flag,
125 // or the semaphore is incremeneted (or both, but never none)
127 // Register with nodes
128 ret = VFS_int_Select_Register(thread_info, MaxHandle, ReadHandles, 0, IsKernel);
129 ret += VFS_int_Select_Register(thread_info, MaxHandle, WriteHandles, 1, IsKernel);
130 ret += VFS_int_Select_Register(thread_info, MaxHandle, ErrHandles, 2, IsKernel);
132 LOG("Register ret = %i", ret);
134 // If there were events waiting, de-register and return
137 ret = VFS_int_Select_Deregister(thread_info, MaxHandle, ReadHandles, 0, IsKernel);
138 ret += VFS_int_Select_Deregister(thread_info, MaxHandle, WriteHandles, 1, IsKernel);
139 ret += VFS_int_Select_Deregister(thread_info, MaxHandle, ErrHandles, 2, IsKernel);
145 // TODO: Implement timeout
147 // Wait (only if there is no timeout, or it is greater than zero
148 if( !Timeout || *Timeout > 0 )
150 ret = Semaphore_Wait(&thread_info->SleepHandle, 1);
153 // Fill output (modify *Handles)
154 // - Also, de-register
155 ret = VFS_int_Select_Deregister(thread_info, MaxHandle, ReadHandles, 0, IsKernel);
156 ret += VFS_int_Select_Deregister(thread_info, MaxHandle, WriteHandles, 1, IsKernel);
157 ret += VFS_int_Select_Deregister(thread_info, MaxHandle, ErrHandles, 2, IsKernel);
163 // Mark a node as having data ready for reading
164 int VFS_MarkAvaliable(tVFS_Node *Node, BOOL IsDataAvaliable)
166 ENTER("pNode bIsDataAvaliable", Node, IsDataAvaliable);
167 Node->DataAvaliable = !!IsDataAvaliable;
168 if( Node->DataAvaliable )
169 VFS_int_Select_SignalAll(Node->ReadThreads);
174 // Mark a node as having a full buffer
175 int VFS_MarkFull(tVFS_Node *Node, BOOL IsBufferFull)
177 ENTER("pNode bIsDataAvaliable", Node, IsBufferFull);
178 Node->BufferFull = !!IsBufferFull;
179 if( !Node->BufferFull )
180 VFS_int_Select_SignalAll(Node->WriteThreads);
185 // Mark a node as errored
186 int VFS_MarkError(tVFS_Node *Node, BOOL IsErrorState)
188 ENTER("pNode bIsDataAvaliable", Node, IsErrorState);
189 Node->ErrorOccurred = !!IsErrorState;
190 if( Node->ErrorOccurred )
191 VFS_int_Select_SignalAll(Node->ErrorThreads);
197 int VFS_int_Select_GetType(enum eVFS_SelectTypes Type, tVFS_Node *Node, tVFS_SelectList ***List, int **Flag, int *WantedFlag, int *MaxAllowed)
199 // Get the type of the listen
203 if(List) *List = &Node->ReadThreads;
204 if(Flag) *Flag = &Node->DataAvaliable;
205 if(WantedFlag) *WantedFlag = 1;
206 if(MaxAllowed) *MaxAllowed = 1; // Max of 1 for read
209 if(List) *List = &Node->WriteThreads;
210 if(Flag) *Flag = &Node->BufferFull;
211 if(WantedFlag) *WantedFlag = 0;
212 if(MaxAllowed) *MaxAllowed = 1; // Max of 1 for write
215 if(List) *List = &Node->ErrorThreads;
216 if(Flag) *Flag = &Node->ErrorOccurred;
217 if(WantedFlag) *WantedFlag = 1;
218 if(MaxAllowed) *MaxAllowed = -1; // No max for error listeners
221 Log_Error("VFS", "VFS_int_Select_GetType: BUG CHECK, Unknown Type %i", Type);
228 * \return Number of files with an action
230 int VFS_int_Select_Register(tVFS_SelectThread *Thread, int MaxHandle, fd_set *Handles, enum eVFS_SelectTypes Type, BOOL IsKernel)
232 int i, numFlagged = 0;
233 tVFS_SelectList **list;
234 int *flag, wantedFlagValue;
237 if( !Handles ) return 0;
239 ENTER("pThread iMaxHandle pHandles iType iIsKernel", Thread, MaxHandle, Handles, Type, IsKernel);
241 for( i = 0; i < MaxHandle; i ++ )
245 // Is the descriptor set
246 if( !FD_ISSET(i, Handles) ) continue;
249 handle = VFS_GetHandle( i | (IsKernel?VFS_KERNEL_FLAG:0) );
250 // Is the handle valid?
251 if( !handle || !handle->Node )
253 if( Type == 2 ) { // Bad FD counts as an error
262 // Get the type of the listen
263 if( VFS_int_Select_GetType(Type, handle->Node, &list, &flag, &wantedFlagValue, &maxAllowed) ) {
270 *list = calloc(1, sizeof(tVFS_SelectList));
274 if( VFS_int_Select_AddThread(*list, Thread, maxAllowed ) )
276 // Oops, error (or just no space)
280 // Check for the flag
281 if( !!*flag == !!wantedFlagValue )
285 LEAVE('i', numFlagged);
290 * \return Number of files with an action
292 int VFS_int_Select_Deregister(tVFS_SelectThread *Thread, int MaxHandle, fd_set *Handles, enum eVFS_SelectTypes Type, BOOL IsKernel)
294 int i, numFlagged = 0;
295 tVFS_SelectList **list;
296 int *flag, wantedFlagValue;
298 if( !Handles ) return 0;
300 ENTER("pThread iMaxHandle pHandles iType iIsKernel", Thread, MaxHandle, Handles, Type, IsKernel);
302 for( i = 0; i < MaxHandle; i ++ )
306 // Is the descriptor set
307 if( !FD_ISSET(i, Handles) ) continue;
310 handle = VFS_GetHandle( i | (IsKernel?VFS_KERNEL_FLAG:0) );
311 // Is the handle valid?
312 if( !handle || !handle->Node )
314 if( Type == 2 ) { // Bad FD counts as an error
323 // Get the type of the listen
325 // Get the type of the listen
326 if( VFS_int_Select_GetType(Type, handle->Node, &list, &flag, &wantedFlagValue, NULL) ) {
332 VFS_int_Select_RemThread(*list, Thread );
334 // Check for the flag
335 if( !!*flag == !!wantedFlagValue ) {
343 LEAVE('i', numFlagged);
349 * \return Boolean failure
351 int VFS_int_Select_AddThread(tVFS_SelectList *List, tVFS_SelectThread *Thread, int MaxAllowed)
354 tVFS_SelectListEnt *block, *prev;
356 ENTER("pList pThread iMaxAllowed", List, Thread, MaxAllowed);
358 // Lock to avoid concurrency issues
359 Mutex_Acquire(&List->Lock);
361 block = &List->FirstEnt;
363 // Look for free space
366 for( i = 0; i < NUM_THREADS_PER_ALLOC; i ++ )
368 if( block->Threads[i] == NULL )
370 block->Threads[i] = Thread;
371 Mutex_Release(&List->Lock);
376 if( MaxAllowed && count >= MaxAllowed ) {
389 block = malloc( sizeof(tVFS_SelectListEnt) );
391 Log_Warning("VFS", "VFS_int_Select_AddThread: malloc() failed");
392 Mutex_Release(&List->Lock);
396 block->Threads[0] = Thread;
397 for( i = 1; i < NUM_THREADS_PER_ALLOC; i ++ )
399 block->Threads[i] = NULL;
406 Mutex_Release(&List->Lock);
412 void VFS_int_Select_RemThread(tVFS_SelectList *List, tVFS_SelectThread *Thread)
415 tVFS_SelectListEnt *block, *prev;
417 ENTER("pList pThread", List, Thread);
419 // Lock to avoid concurrency issues
420 Mutex_Acquire(&List->Lock);
422 block = &List->FirstEnt;
424 // Look for the thread
427 for( i = 0; i < NUM_THREADS_PER_ALLOC; i ++ )
429 if( block->Threads[i] == Thread )
431 block->Threads[i] = NULL;
433 // Check if this block is empty
434 if( block != &List->FirstEnt )
436 for( i = 0; i < NUM_THREADS_PER_ALLOC; i ++ )
437 if( block->Threads[i] )
440 if( i == NUM_THREADS_PER_ALLOC ) {
441 LOG("Deleting block");
442 prev->Next = block->Next;
445 //TODO: If not empty, check if it can be merged downwards
448 Mutex_Release(&List->Lock);
458 // Not on list, is this an error?
460 Mutex_Release(&List->Lock);
467 * \brief Signal all threads on a list
469 void VFS_int_Select_SignalAll(tVFS_SelectList *List)
472 tVFS_SelectListEnt *block;
476 ENTER("pList", List);
478 // Lock to avoid concurrency issues
479 Mutex_Acquire(&List->Lock);
481 block = &List->FirstEnt;
483 // Look for the thread
486 for( i = 0; i < NUM_THREADS_PER_ALLOC; i ++ )
488 LOG("block->Threads[i] = %p", block->Threads[i]);
489 if( block->Threads[i] )
491 Semaphore_Signal( &block->Threads[i]->SleepHandle, 1 );
498 Mutex_Release(&List->Lock);