Advertisement
Guest User

Untitled

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