]> 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 bd55adac7a7eb97e6ce80687bd91518e542b4da9..45e63e703c06584e640a0fd52b74a34e185397d2 100644 (file)
@@ -61,6 +61,7 @@ implementation {
   //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;
@@ -69,6 +70,7 @@ implementation {
     if(temp > 1)
       call PreemptionAlarm.startOneShot(TOSTHREAD_PREEMPTION_PERIOD);
   }
+#endif
   
   /* switch_threads()
    * This routine swaps the stack and allows a thread to run.
@@ -98,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();
     }
   }
@@ -110,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);
 
@@ -138,8 +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
-    num_runnable_threads--;
-       post timerTask();
+    #ifdef TOSTHREADS_TIMER_OPTIMIZATION   
+      num_runnable_threads--;
+         post timerTask();
+       #endif
     sleepWhileIdle();
     interrupt(thread);
   }
@@ -149,17 +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_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;
+    #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
    */
@@ -205,8 +211,10 @@ implementation {
       if(t->state == TOSTHREAD_STATE_INACTIVE) {
         t->state = TOSTHREAD_STATE_READY;
         call ThreadQueue.enqueue(&ready_queue, t);
-        num_runnable_threads++;
-        post timerTask();
+        #ifdef TOSTHREADS_TIMER_OPTIMIZATION   
+          num_runnable_threads++;
+          post timerTask();
+        #endif
         return SUCCESS;
       }
     }
@@ -266,9 +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]());
-      atomic num_runnable_threads++;
-      post timerTask();
+        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;