test-queue-impl.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // test-queue-impl.cpp - Linux queue implementation for thread-safe testing
  2. // This file provides the actual implementation of the queue functions
  3. // to override the weak symbols defined in TaskScheduler.h
  4. #include "Arduino.h"
  5. // Define thread-safe features before including TaskScheduler
  6. #define _TASK_NON_ARDUINO
  7. #define _TASK_THREAD_SAFE
  8. #define _TASK_STATUS_REQUEST
  9. #define _TASK_TIMEOUT
  10. #include "TaskSchedulerDeclarations.h"
  11. #include <thread>
  12. #include <mutex>
  13. #include <queue>
  14. #include <condition_variable>
  15. // ============================================
  16. // Thread-Safe Queue Implementation for Linux
  17. // ============================================
  18. // Queue for task requests
  19. std::queue<_task_request_t> taskRequestQueue;
  20. std::mutex queueMutex;
  21. std::condition_variable queueCV;
  22. const size_t MAX_QUEUE_SIZE = 100;
  23. /**
  24. * @brief Enqueue a task request (Linux implementation)
  25. *
  26. * Thread-safe enqueue operation using std::mutex.
  27. * Simulates FreeRTOS queue behavior for Linux testing.
  28. *
  29. * @param req Pointer to the request structure
  30. * @return true if enqueued successfully, false if queue is full
  31. */
  32. bool _task_enqueue_request(_task_request_t* req) {
  33. std::unique_lock<std::mutex> lock(queueMutex);
  34. // Check queue size limit
  35. if (taskRequestQueue.size() >= MAX_QUEUE_SIZE) {
  36. return false; // Queue full
  37. }
  38. // Add request to queue
  39. taskRequestQueue.push(*req);
  40. // Notify waiting threads
  41. queueCV.notify_one();
  42. return true;
  43. }
  44. /**
  45. * @brief Dequeue a task request (Linux implementation)
  46. *
  47. * Thread-safe dequeue operation. Non-blocking for scheduler.
  48. *
  49. * @param req Pointer to store the dequeued request
  50. * @return true if request dequeued, false if queue empty
  51. */
  52. bool _task_dequeue_request(_task_request_t* req) {
  53. std::unique_lock<std::mutex> lock(queueMutex);
  54. if (taskRequestQueue.empty()) {
  55. return false; // No requests
  56. }
  57. // Get request from queue
  58. *req = taskRequestQueue.front();
  59. taskRequestQueue.pop();
  60. return true;
  61. }
  62. /**
  63. * @brief Helper to clear the queue between tests
  64. */
  65. void clearTaskRequestQueue() {
  66. std::unique_lock<std::mutex> lock(queueMutex);
  67. while (!taskRequestQueue.empty()) {
  68. taskRequestQueue.pop();
  69. }
  70. }
  71. /**
  72. * @brief Helper to get current queue size
  73. */
  74. size_t getQueueSize() {
  75. std::unique_lock<std::mutex> lock(queueMutex);
  76. return taskRequestQueue.size();
  77. }