ioqueue.h 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /**
  2. * @file ioqueue.h
  3. * @author simon (ynwdlxm@163.com)
  4. * @brief 环形缓冲区队列
  5. * @version 0.1
  6. * @date 2025-01-11
  7. *
  8. * @copyright Copyright (c) 2025
  9. *
  10. */
  11. #ifndef __DEVICE_IOQUEUE_H
  12. #define __DEVICE_IOQUEUE_H
  13. #include "../lib/stdint.h"
  14. #include "../thread/thread.h"
  15. #include "../thread/sync.h"
  16. #define BUF_SIZE 64
  17. /* 环形队列 */
  18. struct ioqueue
  19. {
  20. struct lock lock; // 锁,用于同步
  21. struct task_struct *producer; // 记录哪个生产者在此缓冲区睡眠
  22. struct task_struct *consumer; // 记录哪个消费者在此缓冲区睡眠
  23. char buf[BUF_SIZE]; // 缓冲区大小
  24. int32_t head; // 队首,数据往队首处写入
  25. int32_t tail; // 队尾,数据从队尾处读出
  26. };
  27. /// @brief 初始化 ioqueue 队列
  28. /// @param ioq
  29. void ioqueue_init(struct ioqueue *ioq);
  30. /// @brief 判断队列是否已满
  31. /// @param ioq
  32. /// @return TRUE OR FALSE
  33. bool ioq_full(struct ioqueue *ioq);
  34. /// @brief 判断队列是否为空
  35. /// @param ioq
  36. /// @return TRUE OR FALSE
  37. bool ioq_empty(struct ioqueue *ioq);
  38. /// @brief 使生产者或消费者在此缓冲区上等待
  39. /// @param waiter
  40. static void ioq_wait(struct task_struct **waiter);
  41. /// @brief 唤醒 waiter
  42. static void wakeup(struct task_struct **waiter);
  43. /// @brief 消费者从 ioq 队列中获取一个字符
  44. char ioq_getchar(struct ioqueue *ioq);
  45. /// @brief 生产者往 ioq 队列中添加一个字符
  46. void ioq_putchar(struct ioqueue *ioq, char byte);
  47. #endif //!__DEVICE_IOQUEUE_H