]> oss.titaniummirror.com Git - tinyos-2.x.git/blobdiff - tos/lib/tosthreads/system/TinyThreadSchedulerP.nc
Optimization to kill the thread quanta timer when one or less threads are active...
[tinyos-2.x.git] / tos / lib / tosthreads / system / TinyThreadSchedulerP.nc
index 1ae67708558488bad103d2c6760a5a5d2dd75864..bd55adac7a7eb97e6ce80687bd91518e542b4da9 100644 (file)
@@ -43,6 +43,7 @@ module TinyThreadSchedulerP {
     interface Boot as ThreadSchedulerBoot;
     interface ThreadInfo[uint8_t id];
     interface ThreadQueue;
+    interface BitArrayUtils;
     interface McuSleep;
     interface Leds;
     interface Timer<TMilli> as PreemptionAlarm;
@@ -56,10 +57,19 @@ 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;
   
+  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);
+  }
+  
   /* 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 
@@ -128,6 +138,8 @@ 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();
     sleepWhileIdle();
     interrupt(thread);
   }
@@ -138,10 +150,14 @@ implementation {
    * threads by the thread scheduler.
    */
    void stop(thread_t* t) {
+     int i;
      t->state = TOSTHREAD_STATE_INACTIVE;
-     num_started_threads--;
-     if(num_started_threads == 1)
-       call PreemptionAlarm.stop();
+     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]();
    }
   
@@ -163,7 +179,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);
@@ -178,6 +194,7 @@ implementation {
     thread_t* t = (call ThreadInfo.get[id]());
     t->state = TOSTHREAD_STATE_INACTIVE;
     t->init_block = current_thread->init_block;
+    call BitArrayUtils.clrArray(t->joinedOnMe, sizeof(t->joinedOnMe));
     PREPARE_THREAD(t, threadWrapper);
     return SUCCESS;
   }
@@ -186,11 +203,10 @@ 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);
+        num_runnable_threads++;
+        post timerTask();
         return SUCCESS;
       }
     }
@@ -232,11 +248,27 @@ implementation {
     }
   }
   
+  async command error_t ThreadScheduler.joinThread(thread_id_t id) { 
+    thread_t* t = call ThreadInfo.get[id]();
+    atomic {
+      if(current_thread == tos_thread)
+        return FAIL;
+      if (t->state != TOSTHREAD_STATE_INACTIVE) {
+        call BitArrayUtils.setBit(t->joinedOnMe, current_thread->id);
+        call ThreadScheduler.suspendCurrentThread();
+        return SUCCESS;
+      }
+    }
+    return EALREADY;
+  }
+  
   async command error_t ThreadScheduler.wakeupThread(uint8_t id) {
     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();
       return SUCCESS;
     }
     return FAIL;