2222
2323#include " ur_client_library/comm/package.h"
2424#include " ur_client_library/log.h"
25+ #include " ur_client_library/helpers.h"
2526#include " ur_client_library/queue/readerwriterqueue.h"
2627#include < atomic>
2728#include < chrono>
@@ -244,9 +245,17 @@ class Pipeline
244245 * \param consumer The consumer to run in the pipeline
245246 * \param name The pipeline's name
246247 * \param notifier The notifier to use
248+ * \param producer_fifo_scheduling Should the producer thread use FIFO scheduling?
247249 */
248- Pipeline (IProducer<T>& producer, IConsumer<T>* consumer, std::string name, INotifier& notifier)
249- : producer_(producer), consumer_(consumer), name_(name), notifier_(notifier), queue_{ 32 }, running_{ false }
250+ Pipeline (IProducer<T>& producer, IConsumer<T>* consumer, std::string name, INotifier& notifier,
251+ const bool producer_fifo_scheduling = false )
252+ : producer_(producer)
253+ , consumer_(consumer)
254+ , name_(name)
255+ , notifier_(notifier)
256+ , queue_{ 32 }
257+ , running_{ false }
258+ , producer_fifo_scheduling_(producer_fifo_scheduling)
250259 {
251260 }
252261 /* !
@@ -256,9 +265,16 @@ class Pipeline
256265 * \param producer The producer to run in the pipeline
257266 * \param name The pipeline's name
258267 * \param notifier The notifier to use
268+ * \param producer_fifo_scheduling Should the producer thread use FIFO scheduling?
259269 */
260- Pipeline (IProducer<T>& producer, std::string name, INotifier& notifier)
261- : producer_(producer), consumer_(nullptr ), name_(name), notifier_(notifier), queue_{ 32 }, running_{ false }
270+ Pipeline (IProducer<T>& producer, std::string name, INotifier& notifier, const bool producer_fifo_scheduling = false )
271+ : producer_(producer)
272+ , consumer_(nullptr )
273+ , name_(name)
274+ , notifier_(notifier)
275+ , queue_{ 32 }
276+ , running_{ false }
277+ , producer_fifo_scheduling_(producer_fifo_scheduling)
262278 {
263279 }
264280
@@ -349,61 +365,16 @@ class Pipeline
349365 moodycamel::BlockingReaderWriterQueue<std::unique_ptr<T>> queue_;
350366 std::atomic<bool > running_;
351367 std::thread pThread_, cThread_;
368+ bool producer_fifo_scheduling_;
352369
353370 void runProducer ()
354371 {
355372 URCL_LOG_DEBUG (" Starting up producer" );
356- std::ifstream realtime_file (" /sys/kernel/realtime" , std::ios::in);
357- bool has_realtime;
358- realtime_file >> has_realtime;
359- if (has_realtime)
373+ if (producer_fifo_scheduling_)
360374 {
375+ pthread_t this_thread = pthread_self ();
361376 const int max_thread_priority = sched_get_priority_max (SCHED_FIFO);
362- if (max_thread_priority != -1 )
363- {
364- // We'll operate on the currently running thread.
365- pthread_t this_thread = pthread_self ();
366-
367- // struct sched_param is used to store the scheduling priority
368- struct sched_param params;
369-
370- // We'll set the priority to the maximum.
371- params.sched_priority = max_thread_priority;
372-
373- int ret = pthread_setschedparam (this_thread, SCHED_FIFO, ¶ms);
374- if (ret != 0 )
375- {
376- URCL_LOG_ERROR (" Unsuccessful in setting producer thread realtime priority. Error code: %d" , ret);
377- }
378- // Now verify the change in thread priority
379- int policy = 0 ;
380- ret = pthread_getschedparam (this_thread, &policy, ¶ms);
381- if (ret != 0 )
382- {
383- std::cout << " Couldn't retrieve real-time scheduling paramers" << std::endl;
384- }
385-
386- // Check the correct policy was applied
387- if (policy != SCHED_FIFO)
388- {
389- URCL_LOG_ERROR (" Producer thread: Scheduling is NOT SCHED_FIFO!" );
390- }
391- else
392- {
393- URCL_LOG_INFO (" Producer thread: SCHED_FIFO OK" );
394- }
395-
396- // Print thread scheduling priority
397- URCL_LOG_INFO (" Thread priority is %d" , params.sched_priority );
398- }
399- else
400- {
401- URCL_LOG_ERROR (" Could not get maximum thread priority for producer thread" );
402- }
403- }
404- else
405- {
406- URCL_LOG_WARN (" No realtime capabilities found. Consider using a realtime system for better performance" );
377+ setFiFoScheduling (this_thread, max_thread_priority);
407378 }
408379 std::vector<std::unique_ptr<T>> products;
409380 while (running_)
0 commit comments