semaphore.h 771 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. /**
  2. * @file semaphore.h
  3. * @author fslngjin ([email protected])
  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. /**
  24. * @brief 初始化信号量
  25. *
  26. * @param sema 信号量对象
  27. * @param count 信号量的初始值
  28. */
  29. static __always_inline void semaphore_init(semaphore_t *sema, ul count)
  30. {
  31. atomic_set(&sema->counter, count);
  32. wait_queue_init(&sema->wait_queue, NULL);
  33. }
  34. /**
  35. * @brief 信号量down
  36. *
  37. * @param sema
  38. */
  39. void semaphore_down(semaphore_t *sema);
  40. void semaphore_up(semaphore_t *sema);