semaphore.h 871 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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 <common/atomic.h>
  13. #include <common/wait_queue.h>
  14. /**
  15. * @brief 信号量的结构体
  16. *
  17. */
  18. typedef struct
  19. {
  20. atomic_t counter;
  21. wait_queue_node_t wait_queue;
  22. } semaphore_t;
  23. void __semaphore_invoke_sched();
  24. void __semaphore_sched_enqueue(struct process_control_block *pcb);
  25. /**
  26. * @brief 初始化信号量
  27. *
  28. * @param sema 信号量对象
  29. * @param count 信号量的初始值
  30. */
  31. static __always_inline 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. void semaphore_up(semaphore_t *sema);