semaphore.h 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /**
  2. * @file semaphore.h
  3. * @author fslngjin (lonjin@RinGoTek.cn)
  4. * @brief 信号量
  5. * @version 0.1
  6. * @date 2022-04-12
  7. *
  8. * @copyright Copyright (c) 2022
  9. *
  10. */
  11. #pragma once
  12. #include <process/atomic.h>
  13. #include <process/process.h>
  14. #include <sched/sched.h>
  15. #include "wait_queue.h"
  16. /**
  17. * @brief 信号量的结构体
  18. *
  19. */
  20. typedef struct
  21. {
  22. atomic_t counter;
  23. wait_queue_node_t wait_queue;
  24. } semaphore_t;
  25. /**
  26. * @brief 初始化信号量
  27. *
  28. * @param sema 信号量对象
  29. * @param count 信号量的初始值
  30. */
  31. void semaphore_init(semaphore_t *sema, ul count)
  32. {
  33. atomic_set(&sema->counter, count);
  34. wait_queue_init(&sema->wait_queue, NULL);
  35. }
  36. /**
  37. * @brief 信号量down
  38. *
  39. * @param sema
  40. */
  41. void semaphore_down(semaphore_t *sema)
  42. {
  43. if (atomic_read(&sema->counter) > 0) // 信号量大于0,资源充足
  44. atomic_dec(&sema->counter);
  45. else // 资源不足,进程休眠
  46. {
  47. // 将当前进程加入信号量的等待队列
  48. wait_queue_node_t wait;
  49. wait_queue_init(&wait, current_pcb);
  50. current_pcb->state = PROC_UNINTERRUPTIBLE;
  51. list_append(&sema->wait_queue.wait_list, &wait.wait_list);
  52. // 执行调度
  53. sched_cfs();
  54. }
  55. }
  56. void semaphore_up(semaphore_t *sema)
  57. {
  58. if (list_empty(&sema->wait_queue.wait_list)) // 没有进程在等待资源
  59. {
  60. atomic_inc(&sema->counter);
  61. }
  62. else // 有进程在等待资源,唤醒进程
  63. {
  64. wait_queue_node_t *wq = container_of(list_next(&sema->wait_queue.wait_list), wait_queue_node_t, wait_list);
  65. list_del(&wq->wait_list);
  66. wq->pcb->state = PROC_RUNNING;
  67. sched_cfs_enqueue(wq->pcb);
  68. // 当前进程缺少需要的资源,立即标为需要被调度
  69. current_pcb->flags |= PF_NEED_SCHED;
  70. }
  71. }