Guest User

Untitled

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