Fixing select() implementation (and usage)
[tpg/acess2.git] / Kernel / drv / vterm.c
index 5facebc..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,52 @@ Uint64 VT_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
        {
        // Text Mode (UTF-8)
        case TERM_MODE_TEXT:
-               while(pos < Length)
+               VFS_SelectNode(Node, VFS_SELECT_READ, NULL);
+               
+               avail = term->InputWrite - term->InputRead;
+               if(avail < 0)
+                       avail += MAX_INPUT_CHARS8;
+               if(avail > Length - pos)
+                       avail = Length - pos;
+               
+               while( avail -- )
                {
-                       int avail_bytes;
-                       avail_bytes = Semaphore_Wait( &term->InputSemaphore, Length );
-                       if( avail_bytes == -1 ) {
-                               break ;
-                       }
-                       
-                       while( avail_bytes -- )
-                       {
-                               ((char*)Buffer)[pos] = term->InputBuffer[term->InputRead];
-                               pos ++;
-                               term->InputRead ++;
-                               term->InputRead %= MAX_INPUT_CHARS8;
-                       }
+                       ((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;
-                       avail = Semaphore_Wait( &term->InputSemaphore, Length/4 * 4 );
-                       if( avail == -1 ) {
-                               break ;
-                       }
+               VFS_SelectNode(Node, VFS_SELECT_READ, NULL);
                        
-                       while( avail > 3 )
-                       {
-                               ((Uint32*)Buffer)[pos] = ((Uint32*)term->InputBuffer)[term->InputRead];
-                               pos ++;
-                               term->InputRead ++;
-                               term->InputRead %= MAX_INPUT_CHARS32;
-                               avail -= 4;
-                       }
+               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;
        }
        
+       // Mark none avaliable if buffer empty
+       if( term->InputRead == term->InputWrite )
+               VFS_MarkAvaliable(&term->Node, 0);
+       
        term->ReadingThread = -1;
+       
        Mutex_Release( &term->ReadingLock );
        
        return pos;
@@ -848,8 +852,6 @@ void VT_KBCallBack(Uint32 Codepoint)
                        term->InputRead = term->InputWrite + 1;
                        term->InputRead %= MAX_INPUT_CHARS8;
                }
-       
-               Semaphore_Signal(&term->InputSemaphore, 1);
        }
        else
        {
@@ -861,9 +863,10 @@ void VT_KBCallBack(Uint32 Codepoint)
                        term->InputRead ++;
                        term->InputRead %= MAX_INPUT_CHARS32;
                }
-               Semaphore_Signal(&term->InputSemaphore, 4);
        }
        
+       VFS_MarkAvaliable(&term->Node, 1);
+       
        // Wake up the thread waiting on us
        //if( term->ReadingThread >= 0 ) {
        //      Threads_WakeTID(term->ReadingThread);

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