]> oss.titaniummirror.com Git - tinyos-2.x.git/blobdiff - tos/lib/tosthreads/system/TinyThreadSchedulerP.nc
Changed to only do timer turnoff logic if desired via compiler-tie flag
[tinyos-2.x.git] / tos / lib / tosthreads / system / TinyThreadSchedulerP.nc
index e93af1916ba4dbc45ae2f1e745f04d7b2a22a693..45e63e703c06584e640a0fd52b74a34e185397d2 100644 (file)
@@ -57,10 +57,21 @@ implementation {
   //Pointer to yielding thread
   thread_t* yielding_thread;
   //Number of threads started, and currently capable of running if given the chance
-  uint8_t num_started_threads;
+  uint8_t num_runnable_threads;
   //Thread queue for keeping track of threads waiting to run
   thread_queue_t ready_queue;
   
+#ifdef TOSTHREADS_TIMER_OPTIMIZATION   
+  void task timerTask() {
+    uint8_t temp;
+    atomic temp = num_runnable_threads;
+    if(temp <= 1)
+      call PreemptionAlarm.stop();
+    if(temp > 1)
+      call PreemptionAlarm.startOneShot(TOSTHREAD_PREEMPTION_PERIOD);
+  }
+#endif
+  
   /* switch_threads()
    * This routine swaps the stack and allows a thread to run.
    * Needs to be in a separate function like this so that the 
@@ -89,7 +100,7 @@ implementation {
     while(TRUE) {
       bool mt;
       atomic mt = (call ThreadQueue.isEmpty(&ready_queue) == TRUE);
-      if(!mt) break;
+      if(!mt || tos_thread->state == TOSTHREAD_STATE_READY) break;
       call McuSleep.sleep();
     }
   }
@@ -101,7 +112,7 @@ implementation {
    */
   void scheduleNextThread() {
     if(tos_thread->state == TOSTHREAD_STATE_READY)
-      current_thread = call ThreadQueue.remove(&ready_queue, tos_thread);
+      current_thread = tos_thread;
     else
       current_thread = call ThreadQueue.dequeue(&ready_queue);
 
@@ -129,6 +140,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
+    #ifdef TOSTHREADS_TIMER_OPTIMIZATION   
+      num_runnable_threads--;
+         post timerTask();
+       #endif
     sleepWhileIdle();
     interrupt(thread);
   }
@@ -138,18 +153,19 @@ 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_started_threads--;
-     for(i=0; i<TOSTHREAD_MAX_NUM_THREADS; i++) {
-       if(call BitArrayUtils.getBit(t->joinedOnMe, i))
-         call ThreadScheduler.wakeupThread(i);
-        }
-     if(num_started_threads == 1)
-       call PreemptionAlarm.stop();
-     signal ThreadCleanup.cleanup[t->id]();
-   }
+  void stop(thread_t* t) {
+    int i;
+    t->state = TOSTHREAD_STATE_INACTIVE;
+    #ifdef TOSTHREADS_TIMER_OPTIMIZATION        
+      num_runnable_threads--;
+      post timerTask();
+    #endif
+    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]();
+  }
   
   /* This executes and cleans up a thread
    */
@@ -169,7 +185,7 @@ implementation {
   } 
   
   event void ThreadSchedulerBoot.booted() {
-    num_started_threads = 0;
+    num_runnable_threads = 0;
     tos_thread = call ThreadInfo.get[TOSTHREAD_TOS_THREAD_ID]();
     tos_thread->id = TOSTHREAD_TOS_THREAD_ID;
     call ThreadQueue.init(&ready_queue);
@@ -193,11 +209,12 @@ implementation {
     atomic {
       thread_t* t = (call ThreadInfo.get[id]());
       if(t->state == TOSTHREAD_STATE_INACTIVE) {
-        num_started_threads++;
-        if(num_started_threads == 2)
-          call PreemptionAlarm.startOneShot(TOSTHREAD_PREEMPTION_PERIOD);
         t->state = TOSTHREAD_STATE_READY;
         call ThreadQueue.enqueue(&ready_queue, t);
+        #ifdef TOSTHREADS_TIMER_OPTIMIZATION   
+          num_runnable_threads++;
+          post timerTask();
+        #endif
         return SUCCESS;
       }
     }
@@ -257,7 +274,13 @@ implementation {
     thread_t* t = call ThreadInfo.get[id]();
     if((t->state) == TOSTHREAD_STATE_SUSPENDED) {
       t->state = TOSTHREAD_STATE_READY;
-      call ThreadQueue.enqueue(&ready_queue, call ThreadInfo.get[id]());
+        if(t != tos_thread) {
+          call ThreadQueue.enqueue(&ready_queue, call ThreadInfo.get[id]());
+          #ifdef TOSTHREADS_TIMER_OPTIMIZATION   
+            atomic num_runnable_threads++;
+            post timerTask();
+          #endif
+        }
       return SUCCESS;
     }
     return FAIL;