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 Marks the thread as actively using this select
39 //! \brief Semaphore to atomically put the listener to sleep
40 tSemaphore SleepHandle; // TODO: Allow timeouts (by setting an alarm?)
44 int VFS_Select(int MaxHandle, fd_set *ReadHandles, fd_set *WriteHandles, fd_set *ErrHandles, tTime *Timeout, int IsKernel);
45 int VFS_MarkFull(tVFS_Node *Node, BOOL IsBufferFull);
46 int VFS_MarkAvaliable(tVFS_Node *Node, BOOL IsDataAvaliable);
47 int VFS_MarkError(tVFS_Node *Node, BOOL IsErrorState);
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);
56 int VFS_Select(int MaxHandle, fd_set *ReadHandles, fd_set *WriteHandles, fd_set *ErrHandles, tTime *Timeout, int IsKernel)
58 tVFS_SelectThread thread_info;
61 // Register with nodes
62 ret = VFS_int_Select_Register(&thread_info, MaxHandle, ReadHandles, 0, IsKernel);
63 ret += VFS_int_Select_Register(&thread_info, MaxHandle, WriteHandles, 1, IsKernel);
64 ret += VFS_int_Select_Register(&thread_info, MaxHandle, ErrHandles, 2, IsKernel);
66 // If there were events waiting, de-register and return
69 ret = VFS_int_Select_Deregister(&thread_info, MaxHandle, ReadHandles, 0, IsKernel);
70 ret += VFS_int_Select_Deregister(&thread_info, MaxHandle, ReadHandles, 1, IsKernel);
71 ret += VFS_int_Select_Deregister(&thread_info, MaxHandle, ReadHandles, 2, IsKernel);
75 // TODO: Implement timeout
77 // Wait (only if there is no timeout, or it is greater than zero
78 if( !Timeout || *Timeout > 0 )
80 ret = Semaphore_Wait(&thread_info.SleepHandle, 0);
83 // Fill output (modify *Handles)
84 // - Also, de-register
85 ret = VFS_int_Select_Deregister(&thread_info, MaxHandle, ReadHandles, 0, IsKernel);
86 ret += VFS_int_Select_Deregister(&thread_info, MaxHandle, ReadHandles, 1, IsKernel);
87 ret += VFS_int_Select_Deregister(&thread_info, MaxHandle, ReadHandles, 2, IsKernel);
93 * \return Number of files with an action
95 int VFS_int_Select_Register(tVFS_SelectThread *Thread, int MaxHandle, fd_set *Handles, int Type, BOOL IsKernel)
97 int i, numFlagged = 0;
98 tVFS_SelectList **list;
99 int *flag, wantedFlagValue;
102 if( !Handles ) return 0;
104 for( i = 0; i < MaxHandle; i ++ )
108 // Is the descriptor set
109 if( !FD_ISSET(i, Handles) ) continue;
111 handle = VFS_GetHandle( i | (IsKernel?VFS_KERNEL_FLAG:0) );
112 // Is the handle valid?
113 if( !handle || !handle->Node )
115 if( Type == 2 ) { // Bad FD counts as an error
124 // Get the type of the listen
128 list = &handle->Node->ReadThreads;
129 flag = &handle->Node->DataAvaliable;
131 maxAllowed = 1; // Max of 1 for read
134 list = &handle->Node->WriteThreads;
135 flag = &handle->Node->BufferFull;
137 maxAllowed = 1; // Max of 1 for write
140 list = &handle->Node->ErrorThreads;
141 flag = &handle->Node->ErrorOccurred;
143 maxAllowed = -1; // No max for error listeners
146 Log_Error("VFS", "VFS_int_Select_Deregister: BUG CHECK, Unknown Type %i", Type);
152 *list = calloc(1, sizeof(tVFS_SelectList));
156 if( VFS_int_Select_AddThread(*list, Thread, maxAllowed ) )
158 // Oops, error (or just no space)
162 // Check for the flag
163 if( !!*flag == !!wantedFlagValue )
170 * \return Number of files with an action
172 int VFS_int_Select_Deregister(tVFS_SelectThread *Thread, int MaxHandle, fd_set *Handles, int Type, BOOL IsKernel)
174 int i, numFlagged = 0;
175 tVFS_SelectList **list;
176 int *flag, wantedFlagValue;
178 if( !Handles ) return 0;
180 for( i = 0; i < MaxHandle; i ++ )
184 // Is the descriptor set
185 if( !FD_ISSET(i, Handles) ) continue;
187 handle = VFS_GetHandle( i | (IsKernel?VFS_KERNEL_FLAG:0) );
188 // Is the handle valid?
189 if( !handle || !handle->Node )
191 if( Type == 2 ) { // Bad FD counts as an error
200 // Get the type of the listen
204 list = &handle->Node->ReadThreads;
205 flag = &handle->Node->DataAvaliable;
209 list = &handle->Node->WriteThreads;
210 flag = &handle->Node->BufferFull;
214 list = &handle->Node->ErrorThreads;
215 flag = &handle->Node->ErrorOccurred;
219 Log_Error("VFS", "VFS_int_Select_Deregister: BUG CHECK, Unknown Type %i", Type);
224 VFS_int_Select_RemThread(*list, Thread );
226 // Check for the flag
227 if( !!*flag == !!wantedFlagValue ) {
239 * \return Boolean failure
241 int VFS_int_Select_AddThread(tVFS_SelectList *List, tVFS_SelectThread *Thread, int MaxAllowed)
244 tVFS_SelectListEnt *block, *prev;
246 // Lock to avoid concurrency issues
247 Mutex_Acquire(&List->Lock);
249 block = &List->FirstEnt;
251 // Look for free space
254 for( i = 0; i < NUM_THREADS_PER_ALLOC; i ++ )
256 if( block->Threads[i] == NULL )
258 block->Threads[i] = Thread;
259 Mutex_Release(&List->Lock);
263 if( MaxAllowed && count >= MaxAllowed ) {
273 block = malloc( sizeof(tVFS_SelectListEnt) );
275 Log_Warning("VFS", "VFS_int_Select_AddThread: malloc() failed");
276 Mutex_Release(&List->Lock);
280 block->Threads[0] = Thread;
281 for( i = 1; i < NUM_THREADS_PER_ALLOC; i ++ )
283 block->Threads[i] = NULL;
290 Mutex_Release(&List->Lock);
295 void VFS_int_Select_RemThread(tVFS_SelectList *List, tVFS_SelectThread *Thread)
298 tVFS_SelectListEnt *block, *prev;
300 // Lock to avoid concurrency issues
301 Mutex_Acquire(&List->Lock);
303 block = &List->FirstEnt;
305 // Look for the thread
308 for( i = 0; i < NUM_THREADS_PER_ALLOC; i ++ )
310 if( block->Threads[i] == Thread )
312 block->Threads[i] = NULL;
314 // Check if this block is empty
315 if( block != &List->FirstEnt )
317 for( i = 0; i < NUM_THREADS_PER_ALLOC; i ++ )
318 if( block->Threads[i] )
321 if( i == NUM_THREADS_PER_ALLOC ) {
322 prev->Next = block->Next;
325 //TODO: If not empty, check if it can be merged downwards
328 Mutex_Release(&List->Lock);
337 // Not on list, is this an error?