sync.h 930 B

12345678910111213141516171819202122232425262728293031323334353637
  1. #ifndef __THREAD_SYNC_H
  2. #define __THREAD_SYNC_H
  3. #include "../lib/kernel/list.h"
  4. #include "../lib/stdint.h"
  5. #include "thread.h"
  6. #include "../kernel/interrupt.h"
  7. #include "../kernel/debug.h"
  8. // 信号量结构
  9. struct semaphore
  10. {
  11. uint8_t value;
  12. struct list waiters; // 等待该信号量的线程列表
  13. };
  14. struct lock
  15. {
  16. struct task_struct *holder; // 持有该锁的线程
  17. struct semaphore sem; // 用二元信号量实现锁
  18. uint32_t holder_repeat_count; // 持有该锁的线程重入的次数
  19. };
  20. // 初始化信号量
  21. void semaphore_init(struct semaphore *sem, uint8_t value);
  22. // 初始化锁
  23. void lock_init(struct lock *lock);
  24. // 信号量 down 操作 P
  25. void semaphore_down(struct semaphore *sem);
  26. // 信号量 up 操作 V
  27. void semaphore_up(struct semaphore *sem);
  28. // 获取锁
  29. void lock_acquire(struct lock *plock);
  30. // 释放锁
  31. void lock_release(struct lock *plock);
  32. #endif // __THREAD_SYNC_H