ioqueue.h 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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 消费者从 ioq 队列中获取一个字符
  39. char ioq_getchar(struct ioqueue *ioq);
  40. /// @brief 生产者往 ioq 队列中添加一个字符
  41. void ioq_putchar(struct ioqueue *ioq, char byte);
  42. #endif //!__DEVICE_IOQUEUE_H