Fixed compile errors, added Semaphore code
authorJohn Hodge <[email protected]>
Wed, 2 Feb 2011 13:50:27 +0000 (21:50 +0800)
committerJohn Hodge <[email protected]>
Wed, 2 Feb 2011 13:50:27 +0000 (21:50 +0800)
I will compile before committing
I will compile before committing
I will compile before committing

Kernel/include/acess.h
Kernel/include/threads.h
Kernel/threads.c
Modules/Display/VESA/main.c
Modules/Network/NE2000/ne2000.c

index a27ebd6..1f969f8 100644 (file)
@@ -22,14 +22,24 @@ typedef Uint        tGID;
 typedef Sint64 tTimestamp;
 typedef struct sShortSpinlock  tShortSpinlock;
 typedef struct sMutex  tMutex;
 typedef Sint64 tTimestamp;
 typedef struct sShortSpinlock  tShortSpinlock;
 typedef struct sMutex  tMutex;
+typedef struct sSemaphore      tSemaphore;
 
 struct sMutex {
        tShortSpinlock  Protector;      //!< Protector for the lock strucure
 
 struct sMutex {
        tShortSpinlock  Protector;      //!< Protector for the lock strucure
+       const char      *Name;  //!< Human-readable name
        struct sThread  *volatile Owner;        //!< Owner of the lock (set upon getting the lock)
        struct sThread  *Waiting;       //!< Waiting threads
        struct sThread  *LastWaiting;   //!< Waiting threads
 };
 
        struct sThread  *volatile Owner;        //!< Owner of the lock (set upon getting the lock)
        struct sThread  *Waiting;       //!< Waiting threads
        struct sThread  *LastWaiting;   //!< Waiting threads
 };
 
+struct sSemaphore {
+       tShortSpinlock  Protector;      //!< Protector for the lock strucure
+       const char      *Name;  //!< Human-readable name
+       volatile int    Value;  //!< Current mutex value
+       struct sThread  *Waiting;       //!< Waiting threads
+       struct sThread  *LastWaiting;   //!< Waiting threads
+};
+
 // --- Helper Macros ---
 /**
  * \name Helper Macros
 // --- Helper Macros ---
 /**
  * \name Helper Macros
index 2b8f9fb..6fbc11f 100644 (file)
@@ -74,6 +74,7 @@ enum {
        THREAD_STAT_ACTIVE,     // Running and schedulable process
        THREAD_STAT_SLEEPING,   // Message Sleep
        THREAD_STAT_MUTEXSLEEP, // Mutex Sleep
        THREAD_STAT_ACTIVE,     // Running and schedulable process
        THREAD_STAT_SLEEPING,   // Message Sleep
        THREAD_STAT_MUTEXSLEEP, // Mutex Sleep
+       THREAD_STAT_SEMAPHORESLEEP,     // Semaphore Sleep
        THREAD_STAT_WAITING,    // ??? (Waiting for a thread)
        THREAD_STAT_PREINIT,    // Being created
        THREAD_STAT_ZOMBIE,     // Died/Killed, but parent not informed
        THREAD_STAT_WAITING,    // ??? (Waiting for a thread)
        THREAD_STAT_PREINIT,    // Being created
        THREAD_STAT_ZOMBIE,     // Died/Killed, but parent not informed
index 73c3f3c..8700154 100644 (file)
@@ -62,6 +62,7 @@ tGID  Threads_GetGID(void);
  int   Threads_SetGID(Uint *Errno, tUID ID);
 void   Threads_Dump(void);
 void   Threads_DumpActive(void);
  int   Threads_SetGID(Uint *Errno, tUID ID);
 void   Threads_Dump(void);
 void   Threads_DumpActive(void);
+
 void   Mutex_Acquire(tMutex *Mutex);
 void   Mutex_Release(tMutex *Mutex);
  int   Mutex_IsLocked(tMutex *Mutex);
 void   Mutex_Acquire(tMutex *Mutex);
 void   Mutex_Release(tMutex *Mutex);
  int   Mutex_IsLocked(tMutex *Mutex);
@@ -1227,6 +1228,86 @@ int Mutex_IsLocked(tMutex *Mutex)
        return Mutex->Owner != NULL;
 }
 
        return Mutex->Owner != NULL;
 }
 
+/**
+ * \brief Initialise the semaphore
+ * \param Value        Initial value of the semaphore
+ * \param Label        Symbolic name
+ */
+void Semaphore_Init(tSemaphore *Sem, int Value, const char *Label)
+{
+       Sem->Value = Value;
+       Sem->Name = Label;
+}
+
+/**
+ * \brief Acquire a "item" from the semaphore
+ */
+void Semaphore_Wait(tSemaphore *Sem)
+{
+       tThread *us;
+       
+       SHORTLOCK( &Sem->Protector );
+       if( Sem->Value > 0 ) {
+               Sem->Value --;
+               SHORTREL( &Sem->Protector );
+               return ;
+       }
+       
+       SHORTLOCK( &glThreadListLock );
+       
+       // - Remove from active list
+       us = Threads_RemActive();
+       us->Next = NULL;
+       // - Mark as sleeping
+       us->Status = THREAD_STAT_SEMAPHORESLEEP;
+       us->WaitPointer = Sem;
+       
+       // - Add to waiting
+       if(Sem->LastWaiting) {
+               Sem->LastWaiting->Next = us;
+               Sem->LastWaiting = us;
+       }
+       else {
+               Sem->Waiting = us;
+               Sem->LastWaiting = us;
+       }
+       
+       SHORTREL( &glThreadListLock );
+       SHORTREL( &Sem->Protector );
+       while(us->Status == THREAD_STAT_MUTEXSLEEP)     Threads_Yield();
+       // We're only woken when there's something avaliable
+       us->WaitPointer = NULL;
+}
+
+/**
+ * \brief Add an "item" to the semaphore
+ */
+void Semaphore_Signal(tSemaphore *Sem)
+{
+       SHORTLOCK( &Sem->Protector );
+       Sem->Value ++;
+       
+       if( Sem->Waiting )
+       {
+               tThread *toWake = Sem->Waiting;
+               
+               Sem->Waiting = Sem->Waiting->Next;      // Next!
+               // Reset ->LastWaiting to NULL if we have just removed the last waiting thread
+               if( Sem->Waiting == NULL )
+                       Sem->LastWaiting = NULL;
+               
+               // Wake new owner
+               SHORTLOCK( &glThreadListLock );
+               if( toWake->Status != THREAD_STAT_ACTIVE )
+                       Threads_AddActive(toWake);
+               SHORTREL( &glThreadListLock );
+               
+               // Decrement (the value is now "owned" by `toWake`)
+               Sem->Value --;
+       }
+       SHORTREL( &Sem->Protector );
+}
+
 // === EXPORTS ===
 EXPORT(Threads_GetUID);
 EXPORT(Threads_GetGID);
 // === EXPORTS ===
 EXPORT(Threads_GetUID);
 EXPORT(Threads_GetGID);
index 425533b..479be30 100644 (file)
@@ -2,7 +2,7 @@
  * AcessOS 1\r
  * Video BIOS Extensions (Vesa) Driver\r
  */\r
  * AcessOS 1\r
  * Video BIOS Extensions (Vesa) Driver\r
  */\r
-#define DEBUG  0\r
+#define DEBUG  1\r
 #define VERSION        0x100\r
 \r
 #include <acess.h>\r
 #define VERSION        0x100\r
 \r
 #include <acess.h>\r
@@ -93,13 +93,15 @@ int Vesa_Install(char **Arguments)
                return MODULE_ERR_NOTNEEDED;\r
        }\r
        \r
                return MODULE_ERR_NOTNEEDED;\r
        }\r
        \r
-       Log_Debug("VESA", "info->VideoModes = %04x:%04x", info->VideoModes.seg, info->VideoModes.ofs);\r
+       //Log_Debug("VESA", "info->VideoModes = %04x:%04x", info->VideoModes.seg, info->VideoModes.ofs);\r
        modes = (Uint16 *) VM8086_GetPointer(gpVesa_BiosState, info->VideoModes.seg, info->VideoModes.ofs);\r
        \r
        // Read Modes\r
        for( giVesaModeCount = 0; modes[giVesaModeCount] != 0xFFFF; giVesaModeCount++ );\r
        gVesa_Modes = (tVesa_Mode *)malloc( giVesaModeCount * sizeof(tVesa_Mode) );\r
        \r
        modes = (Uint16 *) VM8086_GetPointer(gpVesa_BiosState, info->VideoModes.seg, info->VideoModes.ofs);\r
        \r
        // Read Modes\r
        for( giVesaModeCount = 0; modes[giVesaModeCount] != 0xFFFF; giVesaModeCount++ );\r
        gVesa_Modes = (tVesa_Mode *)malloc( giVesaModeCount * sizeof(tVesa_Mode) );\r
        \r
+       Log_Debug("VESA", "%i Modes", giVesaModeCount);\r
+       \r
        // Insert Text Mode\r
        gVesa_Modes[0].width = 80;\r
        gVesa_Modes[0].height = 25;\r
        // Insert Text Mode\r
        gVesa_Modes[0].width = 80;\r
        gVesa_Modes[0].height = 25;\r
index d5eadb7..d752580 100644 (file)
@@ -252,7 +252,6 @@ static const char *casIOCtls[] = { DRV_IOCTLNAMES, DRV_NETWORK_IOCTLNAMES, NULL
  */
 int Ne2k_IOCtl(tVFS_Node *Node, int ID, void *Data)
 {
  */
 int Ne2k_IOCtl(tVFS_Node *Node, int ID, void *Data)
 {
-        int    tmp;
        ENTER("pNode iID pData", Node, ID, Data);
        switch( ID )
        {
        ENTER("pNode iID pData", Node, ID, Data);
        switch( ID )
        {

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