Fixing select() implementation (and usage)
authorJohn Hodge <[email protected]>
Tue, 1 Mar 2011 06:39:34 +0000 (14:39 +0800)
committerJohn Hodge <[email protected]>
Tue, 1 Mar 2011 06:39:34 +0000 (14:39 +0800)
Kernel/drv/vterm.c
Kernel/vfs/select.c
Modules/IPStack/tcp.c

index b261130..fc89b3a 100644 (file)
@@ -419,6 +419,7 @@ int VT_Root_IOCtl(tVFS_Node *Node, int Id, void *Data)
 Uint64 VT_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
 {
         int    pos = 0;
+        int    avail;
        tVTerm  *term = &gVT_Terminals[ Node->Inode ];
        
        Mutex_Acquire( &term->ReadingLock );
@@ -428,49 +429,41 @@ Uint64 VT_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
        {
        // Text Mode (UTF-8)
        case TERM_MODE_TEXT:
-               while(pos < Length)
-               {
-                       int avail_bytes;
-                       VFS_SelectNode(Node, VFS_SELECT_READ, NULL);
-                       avail_bytes = term->InputRead - term->InputWrite;
+               VFS_SelectNode(Node, VFS_SELECT_READ, NULL);
                
-                       if(avail_bytes < 0)
-                               avail_bytes += MAX_INPUT_CHARS8;
-                       if(avail_bytes > Length - pos)
-                               avail_bytes = Length - pos;
-                       
-                       while( avail_bytes -- )
-                       {
-                               ((char*)Buffer)[pos] = term->InputBuffer[term->InputRead];
-                               pos ++;
-                               term->InputRead ++;
-                               term->InputRead %= MAX_INPUT_CHARS8;
-                       }
+               avail = term->InputWrite - term->InputRead;
+               if(avail < 0)
+                       avail += MAX_INPUT_CHARS8;
+               if(avail > Length - pos)
+                       avail = Length - pos;
+               
+               while( avail -- )
+               {
+                       ((char*)Buffer)[pos] = term->InputBuffer[term->InputRead];
+                       pos ++;
+                       term->InputRead ++;
+                       term->InputRead %= MAX_INPUT_CHARS8;
                }
                break;
        
        //case TERM_MODE_FB:
        // Other - UCS-4
        default:
-               while(pos < Length)
-               {
-                        int avail;
-                       VFS_SelectNode(Node, VFS_SELECT_READ, NULL);
+               VFS_SelectNode(Node, VFS_SELECT_READ, NULL);
                        
-                       avail = term->InputRead - term->InputWrite;
-                       if(avail < 0)
-                               avail += MAX_INPUT_CHARS32;
-                       if(avail > Length - pos)
-                               avail = Length/4 - pos;
-                       
-                       
-                       while( avail -- )
-                       {
-                               ((Uint32*)Buffer)[pos] = ((Uint32*)term->InputBuffer)[term->InputRead];
-                               pos ++;
-                               term->InputRead ++;
-                               term->InputRead %= MAX_INPUT_CHARS32;
-                       }
+               avail = term->InputWrite - term->InputRead;
+               if(avail < 0)
+                       avail += MAX_INPUT_CHARS32;
+               if(avail > Length - pos)
+                       avail = Length/4 - pos;
+               
+               
+               while( avail -- )
+               {
+                       ((Uint32*)Buffer)[pos] = ((Uint32*)term->InputBuffer)[term->InputRead];
+                       pos ++;
+                       term->InputRead ++;
+                       term->InputRead %= MAX_INPUT_CHARS32;
                }
                pos *= 4;
                break;
index c695bb6..3a0b292 100644 (file)
@@ -229,7 +229,7 @@ int VFS_int_Select_Register(tVFS_SelectThread *Thread, int MaxHandle, fd_set *Ha
        
        if( !Handles )  return 0;
        
-       ENTER("pThread iMaxHandle pHandles iType BIsKernel", Thread, MaxHandle, Handles, Type, IsKernel);
+       ENTER("pThread iMaxHandle pHandles iType iIsKernel", Thread, MaxHandle, Handles, Type, IsKernel);
        
        for( i = 0; i < MaxHandle; i ++ )
        {
@@ -289,7 +289,7 @@ int VFS_int_Select_Deregister(tVFS_SelectThread *Thread, int MaxHandle, fd_set *
        
        if( !Handles )  return 0;
        
-       ENTER("pThread iMaxHandle pHandles iType BIsKernel", Thread, MaxHandle, Handles, Type, IsKernel);
+       ENTER("pThread iMaxHandle pHandles iType iIsKernel", Thread, MaxHandle, Handles, Type, IsKernel);
        
        for( i = 0; i < MaxHandle; i ++ )
        {
@@ -462,6 +462,8 @@ void VFS_int_Select_SignalAll(tVFS_SelectList *List)
         int    i;
        tVFS_SelectListEnt      *block;
        
+       if( !List )     return ;
+       
        ENTER("pList", List);
        
        // Lock to avoid concurrency issues
index 23d028c..87838f8 100644 (file)
@@ -7,6 +7,8 @@
 #include "ipv4.h"
 #include "tcp.h"
 
+#define USE_SELECT     1
+
 #define TCP_MIN_DYNPORT        0xC000
 #define TCP_MAX_HALFOPEN       1024    // Should be enough
 
@@ -319,6 +321,13 @@ void TCP_INT_HandleConnectionPacket(tTCPConnection *Connection, tTCPHeader *Head
        if(Header->Flags & TCP_FLAG_ACK) {
                // TODO: Process an ACKed Packet
                Log_Log("TCP", "Conn %p, Packet 0x%x ACKed", Connection, Header->AcknowlegementNumber);
+               
+               // HACK
+               // TODO: Make sure that the packet is actually ACKing the FIN
+               if( Connection->State == TCP_ST_FIN_SENT ) {
+                       Connection->State = TCP_ST_FINISHED;
+                       return ;
+               }
        }
        
        // TODO: Check what to do here
@@ -767,30 +776,19 @@ Uint64 TCP_Client_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buff
        // Poll packets
        for(;;)
        {
-               #if USE_SELECT
                // Wait
                VFS_SelectNode(Node, VFS_SELECT_READ, NULL);
                // Lock list and read
                Mutex_Acquire( &conn->lRecievedPackets );
-               #else
-               // Lock list and check if there is a packet
-               Mutex_Acquire( &conn->lRecievedPackets );
-               if( conn->RecievedBuffer->Length == 0 ) {
-                       // If not, release the lock, yield and try again
-                       Mutex_Release( &conn->lRecievedPackets );
-                       Threads_Yield();        // TODO: Less expensive wait
-                       continue;
-               }
-               #endif
                
                // Attempt to read all `Length` bytes
                len = RingBuffer_Read( destbuf, conn->RecievedBuffer, Length );
                
-               #if USE_SELECT
-               if( conn->RecievedBuffer->Length == 0 )
+               if( len == 0 || conn->RecievedBuffer->Length == 0 ) {
+                       LOG("Marking as none avaliable (len = %i)\n", len);
                        VFS_MarkAvaliable(Node, 0);
-               #endif
-               
+               }
+                       
                // Release the lock (we don't need it any more)
                Mutex_Release( &conn->lRecievedPackets );
        

UCC git Repository :: git.ucc.asn.au