Kernel - Expose thread timer for EVENT_TIMER, multiple sleepers for workqueue, cleanu...
authorJohn Hodge <[email protected]>
Sat, 15 Mar 2014 07:58:05 +0000 (15:58 +0800)
committerJohn Hodge <[email protected]>
Sat, 15 Mar 2014 07:58:05 +0000 (15:58 +0800)
KernelLand/Kernel/include/acess.h
KernelLand/Kernel/include/timers.h
KernelLand/Kernel/include/workqueue.h
KernelLand/Kernel/threads.c
KernelLand/Kernel/time.c
KernelLand/Kernel/workqueue.c

index 8a413a2..fe1a0f9 100644 (file)
@@ -10,6 +10,7 @@
  */
 
 #include <stddef.h>
+#include <stdbool.h>
 #include <arch.h>
 
 #ifndef HALT_CPU
@@ -51,7 +52,6 @@ typedef Uint32        tGID;   //!< Group ID Type
 typedef Sint64 tTimestamp;     //!< Timestamp (miliseconds since 00:00 1 Jan 1970)
 typedef Sint64 tTime;  //!< Same again
 typedef struct sShortSpinlock  tShortSpinlock; //!< Opaque (kinda) spinlock
-typedef int    bool;   //!< Boolean type
 typedef Uint64 off_t;  //!< VFS Offset
 typedef struct { char _[PAGE_SIZE];}   tPage;  // Representation of a page for pointer arithmatic
 
index 2483298..e120060 100644 (file)
@@ -53,6 +53,11 @@ extern void  Time_ScheduleTimer(tTimer *Timer, int Delta);
  */
 extern void    Time_RemoveTimer(tTimer *Timer);
 
+/**
+ * Schedule a THREAD_EVENT_TIMER to fire in \a Delay milliseconds
+ */
+extern void    Time_ScheduleEvent(int Delay);
+
 /**
  * \brief Wait for a period of milliseconds
  */
index ff0c7f4..1f5169a 100644 (file)
@@ -21,6 +21,7 @@ struct sWorkqueue
        void    *Head;
        void    *Tail;
        struct sThread  *Sleeper;
+       struct sThread  *SleepTail;
 };
 
 extern void    Workqueue_Init(tWorkqueue *Queue, const char *Name, size_t NextOfset);
index 73a2b58..545896c 100644 (file)
@@ -783,9 +783,13 @@ int Threads_int_Sleep(enum eThreadStatus Status, void *Ptr, int Num, tThread **L
                }
                *ListTail = us;
        }
-       else {
+       else if( ListHead ) {
+               us->Next = *ListHead;
                *ListHead = us;
        }
+       else {
+               // Nothing
+       }
        
        //if( Proc_ThreadSync(us) )
        //      return ;
index a6ccd8b..15b6190 100644 (file)
@@ -231,6 +231,14 @@ void Time_FreeTimer(tTimer *Timer)
        LOG("%p deallocated %p", __builtin_return_address(0), Timer);
 }
 
+void Time_ScheduleEvent(int Delay)
+{
+       tTimer  *t = &Proc_GetCurThread()->ThreadTimer;
+       Time_RemoveTimer(t);
+       Time_InitTimer(t, NULL, NULL);
+       Time_ScheduleTimer(t, Delay);
+}
+
 /**
  * \fn void Time_Delay(int Delay)
  * \brief Delay for a small ammount of time
@@ -238,9 +246,8 @@ void Time_FreeTimer(tTimer *Timer)
 void Time_Delay(int Delay)
 {
        LOG("(%i)", Delay);
-       tTimer  *t = &Proc_GetCurThread()->ThreadTimer;
-       Time_InitTimer(t, NULL, NULL);
-       Time_ScheduleTimer(t, Delay);
+       Threads_ClearEvent(THREAD_EVENT_TIMER);
+       Time_ScheduleEvent(Delay);
        Threads_WaitEvents(THREAD_EVENT_TIMER);
 }
 
index 3e35b06..f5d78f8 100644 (file)
@@ -16,6 +16,8 @@ void Workqueue_Init(tWorkqueue *Queue, const char *Name, size_t NextOfset)
 {
        Queue->Name = Name;
        Queue->NextOffset = NextOfset;
+       Queue->Sleeper = NULL;
+       Queue->SleepTail = NULL;
 }
 
 void *Workqueue_GetWork(tWorkqueue *Queue)
@@ -36,7 +38,7 @@ void *Workqueue_GetWork(tWorkqueue *Queue)
                
                Threads_int_Sleep(THREAD_STAT_QUEUESLEEP,
                        Queue, 0,
-                       &Queue->Sleeper, NULL, &Queue->Protector);
+                       &Queue->Sleeper, &Queue->SleepTail, &Queue->Protector);
        }
 }
 
@@ -53,9 +55,12 @@ void Workqueue_AddWork(tWorkqueue *Queue, void *Ptr)
 
        if( Queue->Sleeper )
        {       
-               if( Queue->Sleeper->Status != THREAD_STAT_ACTIVE )
-                       Threads_AddActive(Queue->Sleeper);
-               Queue->Sleeper = NULL;
+               ASSERTC( Queue->Sleeper->Status, !=, THREAD_STAT_ACTIVE );
+               tThread *next_sleeper = Queue->Sleeper->Next;
+               Threads_AddActive(Queue->Sleeper);
+               Queue->Sleeper = next_sleeper;
+               if(!next_sleeper)
+                       Queue->SleepTail = NULL;
        }
        SHORTREL(&Queue->Protector);
 }

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