X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=Kernel%2Fdrv%2Ffifo.c;h=005705311c9cf44f6a85f9db52a2e265f6915d90;hb=635bc78017d8a4a16314a973e39c849b2afac795;hp=cbbd1074a41109f41d98c9fd46ab296add31d892;hpb=203882b7bbd4aacaf5c4e99be0cca17455690585;p=tpg%2Facess2.git diff --git a/Kernel/drv/fifo.c b/Kernel/drv/fifo.c index cbbd1074..00570531 100644 --- a/Kernel/drv/fifo.c +++ b/Kernel/drv/fifo.c @@ -4,6 +4,7 @@ #include #include #include +#include // === CONSTANTS === #define DEFAULT_RING_SIZE 2048 @@ -19,6 +20,7 @@ typedef struct sPipe { int WritePos; int BufSize; char *Buffer; + tSemaphore Semaphore; } tPipe; // === PROTOTYPES === @@ -219,20 +221,18 @@ Uint64 FIFO_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer) { // Wait for buffer to fill if(pipe->Flags & PF_BLOCKING) { - while(pipe->ReadPos == pipe->WritePos) { - Threads_Yield(); - //MAGIC_BREAK(); - } + len = Semaphore_Wait( &pipe->Semaphore, remaining ); } else + { if(pipe->ReadPos == pipe->WritePos) return 0; - - // Read buffer - if(pipe->WritePos - pipe->ReadPos < remaining) - len = pipe->WritePos - pipe->ReadPos; - else - len = remaining; + // Read buffer + if(pipe->WritePos - pipe->ReadPos < remaining) + len = pipe->WritePos - pipe->ReadPos; + else + len = remaining; + } // Check if read overflows buffer if(len > pipe->BufSize - pipe->ReadPos) @@ -275,18 +275,19 @@ Uint64 FIFO_Write(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer) while(remaining) { // Wait for buffer to empty - if(pipe->Flags & PF_BLOCKING) - while(pipe->ReadPos == (pipe->WritePos+1)%pipe->BufSize) - Threads_Yield(); + if(pipe->Flags & PF_BLOCKING) { + len = Semaphore_Signal( &pipe->Semaphore, remaining ); + } else + { if(pipe->ReadPos == (pipe->WritePos+1)%pipe->BufSize) return 0; - - // Write buffer - if(pipe->ReadPos - pipe->WritePos < remaining) - len = pipe->ReadPos - pipe->WritePos; - else - len = remaining; + // Write buffer + if(pipe->ReadPos - pipe->WritePos < remaining) + len = pipe->ReadPos - pipe->WritePos; + else + len = remaining; + } // Check if write overflows buffer if(len > pipe->BufSize - pipe->WritePos) @@ -321,24 +322,25 @@ Uint64 FIFO_Write(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer) tPipe *FIFO_Int_NewPipe(int Size, char *Name) { tPipe *ret; - int allocsize = sizeof(tPipe) + sizeof(tVFS_ACL) + Size; + int namelen = strlen(Name) + 1; + int allocsize = sizeof(tPipe) + sizeof(tVFS_ACL) + Size + namelen; ret = malloc(allocsize); if(!ret) return NULL; // Clear Return memset(ret, 0, allocsize); - - ret->Name = Name; ret->Flags = PF_BLOCKING; // Allocate Buffer ret->BufSize = Size; ret->Buffer = (void*)( (Uint)ret + sizeof(tPipe) + sizeof(tVFS_ACL) ); - if(!ret->Buffer) { - free(ret); - return NULL; - } + + // Set name (and FIFO name) + ret->Name = ret->Buffer + Size; + strcpy(ret->Name, Name); + // - Start empty, max of `Size` + Semaphore_Init( &ret->Semaphore, 0, Size, "FIFO", ret->Name ); // Set Node ret->Node.Size = 0;