]> oss.titaniummirror.com Git - tinyos-2.x.git/blobdiff - tos/lib/tosthreads/system/TinyThreadSchedulerP.nc
bug in adding compile time directive to preemption timer separation. Now fixed
[tinyos-2.x.git] / tos / lib / tosthreads / system / TinyThreadSchedulerP.nc
index 9345bb7614282adf789daa02d9857f03d0a8237e..ba7f77812e1c35715ac0cd521897560ffd635f89 100644 (file)
@@ -61,12 +61,12 @@ implementation {
   //Thread queue for keeping track of threads waiting to run
   thread_queue_t ready_queue;
   
-  void task timerTask() {
+  void task alarmTask() {
     uint8_t temp;
     atomic temp = num_runnable_threads;
     if(temp <= 1)
       call PreemptionAlarm.stop();
-    if(temp > 1)
+    else if(temp > 1)
       call PreemptionAlarm.startOneShot(TOSTHREAD_PREEMPTION_PERIOD);
   }
   
@@ -98,7 +98,7 @@ implementation {
     while(TRUE) {
       bool mt;
       atomic mt = (call ThreadQueue.isEmpty(&ready_queue) == TRUE);
-      if(!mt || tos_thread->state == TOSTHREAD_STATE_READY) break;
+      if(!mt) break;
       call McuSleep.sleep();
     }
   }
@@ -110,7 +110,7 @@ implementation {
    */
   void scheduleNextThread() {
     if(tos_thread->state == TOSTHREAD_STATE_READY)
-      current_thread = tos_thread;
+      current_thread = call ThreadQueue.remove(&ready_queue, tos_thread);
     else
       current_thread = call ThreadQueue.dequeue(&ready_queue);
 
@@ -138,8 +138,10 @@ implementation {
   void suspend(thread_t* thread) {
     //if there are no active threads, put the MCU to sleep
     //Then wakeup the TinyOS thread whenever the MCU wakes up again
-    num_runnable_threads--;
-       post timerTask();
+    #ifdef TOSTHREADS_TIMER_OPTIMIZATION
+      num_runnable_threads--;
+         post alarmTask();    
+       #endif
     sleepWhileIdle();
     interrupt(thread);
   }
@@ -149,17 +151,22 @@ implementation {
    * and decrementing any necessary variables used to keep track of
    * threads by the thread scheduler.
    */
-   void stop(thread_t* t) {
-     int i;
-     t->state = TOSTHREAD_STATE_INACTIVE;
-     num_runnable_threads--;
-     post timerTask();
-     for(i=0; i<TOSTHREAD_MAX_NUM_THREADS; i++) {
-       if(call BitArrayUtils.getBit(t->joinedOnMe, i))
-         call ThreadScheduler.wakeupThread(i);
-        }
-     signal ThreadCleanup.cleanup[t->id]();
-   }
+  void stop(thread_t* t) {
+    int i;
+    t->state = TOSTHREAD_STATE_INACTIVE;
+    num_runnable_threads--;
+    for(i=0; i<TOSTHREAD_MAX_NUM_THREADS; i++) {
+      if(call BitArrayUtils.getBit(t->joinedOnMe, i))
+        call ThreadScheduler.wakeupThread(i);
+    }
+    #ifdef TOSTHREADS_TIMER_OPTIMIZATION
+         post alarmTask();    
+       #else
+      if(num_runnable_threads == 1)
+        call PreemptionAlarm.stop();
+    #endif
+    signal ThreadCleanup.cleanup[t->id]();
+  }
   
   /* This executes and cleans up a thread
    */
@@ -203,10 +210,15 @@ implementation {
     atomic {
       thread_t* t = (call ThreadInfo.get[id]());
       if(t->state == TOSTHREAD_STATE_INACTIVE) {
+        num_runnable_threads++;
+        #ifdef TOSTHREADS_TIMER_OPTIMIZATION
+          post alarmTask();
+        #else 
+          if(num_runnable_threads == 2)
+            call PreemptionAlarm.startOneShot(TOSTHREAD_PREEMPTION_PERIOD);
+        #endif
         t->state = TOSTHREAD_STATE_READY;
         call ThreadQueue.enqueue(&ready_queue, t);
-        num_runnable_threads++;
-        post timerTask();
         return SUCCESS;
       }
     }
@@ -266,11 +278,11 @@ implementation {
     thread_t* t = call ThreadInfo.get[id]();
     if((t->state) == TOSTHREAD_STATE_SUSPENDED) {
       t->state = TOSTHREAD_STATE_READY;
-      if(t != tos_thread) {
-        call ThreadQueue.enqueue(&ready_queue, call ThreadInfo.get[id]());
+      call ThreadQueue.enqueue(&ready_queue, call ThreadInfo.get[id]());
+      #ifdef TOSTHREADS_TIMER_OPTIMIZATION
         atomic num_runnable_threads++;
-        post timerTask();
-      }
+        post alarmTask();
+      #endif
       return SUCCESS;
     }
     return FAIL;
@@ -301,4 +313,3 @@ implementation {
     return NULL;
   }
 }
-