Contains the source code from the course work throughout my undergraduate Computer Engineering degree at Brigham Young University. There is a mixture of Go, Python, C, C++, Java, VHDL, Verilog, Matlab, Bash, Assembly, etc..
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Semaphore.c 1.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #include "yakk.h"
  2. #include "clib.h"
  3. #include "PriorityQueue.h"
  4. extern PriorityQueue readyQueue;
  5. YKSEM* YKSemCreate(int initialValue) {
  6. YKSEM* newSemaphore;
  7. newSemaphore = getNewSem();
  8. if (newSemaphore == null) exit(NEW_SEM_FAILED);
  9. initializePriorityQueue(&(newSemaphore->queue));
  10. newSemaphore->value = initialValue;
  11. return newSemaphore;
  12. }
  13. void YKSemPend(YKSEM* semaphore) {
  14. TCB* runningTask;
  15. YKEnterMutex();
  16. if (semaphore->value < 1) {
  17. semaphore->value--;
  18. runningTask = removePriorityQueue(&readyQueue);
  19. runningTask->state = T_BLOCKED;
  20. insertPriorityQueue((&(semaphore->queue)), runningTask);
  21. YKExitMutex();
  22. asm("int 0x20");
  23. return;
  24. } else {
  25. semaphore->value--;
  26. YKExitMutex();
  27. return;
  28. }
  29. }
  30. void YKSemPost(YKSEM* semaphore) {
  31. TCB* readyTask;
  32. YKEnterMutex();
  33. semaphore->value++;
  34. readyTask = removePriorityQueue(&(semaphore->queue));
  35. if (readyTask == null) {
  36. YKExitMutex();
  37. return;
  38. }
  39. readyTask->state = T_READY;
  40. insertPriorityQueue(&readyQueue, readyTask);
  41. YKExitMutex();
  42. if (YKGetISRCallDepth() == 0) {
  43. asm("int 0x20");
  44. }
  45. return;
  46. }