test-queue-impl.cpp 2.3 KB

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