Guest User

Untitled

a guest
Jan 5th, 2016
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.36 KB | None | 0 0
  1. #define CAS(var, oldValue, newValue) __sync_bool_compare_and_swap(&(var), oldValue, newValue)
  2.  
  3. Thread * volatile threadQueue[SCHEDULER_MAX_THREAD_PRIORITY + 1];
  4. Thread *currentThread;
  5. volatile int threadMaxPriority;
  6.  
  7. inline Thread *schedulerNextThread(void) {
  8.     Thread *nextThread = NULL;
  9.     while (threadMaxPriority >= 0) {
  10.         int curPriority = threadMaxPriority;
  11.         if (threadQueue[curPriority] != NULL) {
  12.             nextThread = threadQueue[curPriority];
  13.             if (CAS(threadQueue[curPriority], nextThread, nextThread->nextScheduled)) {
  14.                 break;
  15.             }
  16.             continue;
  17.         }
  18.         int nextPriority = curPriority - 1;
  19.         CAS(threadMaxPriority, curPriority, nextPriority);
  20.     }
  21.     return nextThread;
  22. }
  23.  
  24. void schedulerResumeThread(Thread *thread) {
  25.     if (!CAS(thread->locked, false, true)) return;
  26.     if (thread->suspend == true) {
  27.         if (thread->priority < THREAD_PRIORITY_IDLE) return;
  28.         thread->suspend = false;
  29.         while (true) {
  30.             thread->nextScheduled = threadQueue[thread->priority];
  31.             if (thread->nextScheduled == NULL) {
  32.                 thread->nextScheduled = thread;
  33.                 if (CAS(threadQueue[thread->priority], NULL, thread)) {
  34.                     break;
  35.                 }
  36.             } else {
  37.                 if (CAS(threadQueue[thread->priority], thread->nextScheduled, thread)) {
  38.                     break;
  39.                 }
  40.             }
  41.         }
  42.         int maxPriority, newMaxPriority;
  43.         do {
  44.             maxPriority = threadMaxPriority;
  45.             newMaxPriority = (maxPriority >= thread->priority) ? maxPriority : thread->priority;
  46.         } while (!CAS(threadMaxPriority, maxPriority, newMaxPriority));
  47.     }
  48.     thread->locked = false;
  49. }
  50.  
  51. void schedulerSuspendThread(Thread *thread) {
  52.     if (!CAS(thread->locked, false, true)) return;
  53.     if (thread->suspend == false) {
  54.         thread->suspend = true;
  55.         while (true) {
  56.             Thread *prev = threadQueue[thread->priority];
  57.             while (prev->nextScheduled != thread) {
  58.                 prev = prev->nextScheduled;
  59.             }
  60.             if (CAS(prev->nextScheduled, thread, thread->nextScheduled)) {
  61.                 if (CAS(threadQueue[thread->priority], thread, thread->nextScheduled)) {
  62.                     CAS(threadQueue[thread->priority], thread, NULL);
  63.                 }
  64.                 break;
  65.             }
  66.         }
  67.     }
  68.     thread->locked = false;
  69. }
  70.  
  71. void schedulerInitThread(Thread *thread) {
  72.     thread->next = currentThread;
  73.     thread->prev = currentThread->prev;
  74.     thread->next->prev = thread;
  75.     thread->prev->next = thread;
  76. }
  77.  
  78. void schedulerInitFirstThread(Thread *thread) {
  79.     thread->next = thread;
  80.     thread->prev = thread;
  81.     currentThread = thread;
  82. }
Advertisement
Add Comment
Please, Sign In to add comment