test-queue-impl.cpp 2.4 KB

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