task_adaptors.hpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*! \file
  2. * \brief Task adaptors.
  3. *
  4. * This file contains adaptors for task function objects.
  5. *
  6. * Copyright (c) 2005-2007 Philipp Henkel
  7. *
  8. * Use, modification, and distribution are subject to the
  9. * Boost Software License, Version 1.0. (See accompanying file
  10. * LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  11. *
  12. * http://threadpool.sourceforge.net
  13. *
  14. */
  15. #ifndef THREADPOOL_TASK_ADAPTERS_HPP_INCLUDED
  16. #define THREADPOOL_TASK_ADAPTERS_HPP_INCLUDED
  17. #include <boost/smart_ptr.hpp>
  18. #include <boost/function.hpp>
  19. #include <boost/thread.hpp>
  20. namespace boost { namespace threadpool
  21. {
  22. /*! \brief Standard task function object.
  23. *
  24. * This function object wraps a nullary function which returns void.
  25. * The wrapped function is invoked by calling the operator ().
  26. *
  27. * \see boost function library
  28. *
  29. */
  30. typedef function0<void> task_func;
  31. /*! \brief Prioritized task function object.
  32. *
  33. * This function object wraps a task_func object and binds a priority to it.
  34. * prio_task_funcs can be compared using the operator < which realises a partial ordering.
  35. * The wrapped task function is invoked by calling the operator ().
  36. *
  37. * \see prio_scheduler
  38. *
  39. */
  40. class prio_task_func
  41. {
  42. private:
  43. unsigned int m_priority; //!< The priority of the task's function.
  44. task_func m_function; //!< The task's function.
  45. public:
  46. typedef void result_type; //!< Indicates the functor's result type.
  47. public:
  48. /*! Constructor.
  49. * \param priority The priority of the task.
  50. * \param function The task's function object.
  51. */
  52. prio_task_func(unsigned int const priority, task_func const & function)
  53. : m_priority(priority)
  54. , m_function(function)
  55. {
  56. }
  57. /*! Executes the task function.
  58. */
  59. void operator() (void) const
  60. {
  61. if(m_function)
  62. {
  63. m_function();
  64. }
  65. }
  66. /*! Comparison operator which realises a partial ordering based on priorities.
  67. * \param rhs The object to compare with.
  68. * \return true if the priority of *this is less than right hand side's priority, false otherwise.
  69. */
  70. bool operator< (const prio_task_func& rhs) const
  71. {
  72. return m_priority < rhs.m_priority;
  73. }
  74. }; // prio_task_func
  75. /*! \brief Looped task function object.
  76. *
  77. * This function object wraps a boolean thread function object.
  78. * The wrapped task function is invoked by calling the operator () and it is executed in regular
  79. * time intervals until false is returned. The interval length may be zero.
  80. * Please note that a pool's thread is engaged as long as the task is looped.
  81. *
  82. */
  83. class looped_task_func
  84. {
  85. private:
  86. function0<bool> m_function; //!< The task's function.
  87. unsigned int m_break_s; //!< Duration of breaks in seconds.
  88. unsigned int m_break_ns; //!< Duration of breaks in nano seconds.
  89. public:
  90. typedef void result_type; //!< Indicates the functor's result type.
  91. public:
  92. /*! Constructor.
  93. * \param function The task's function object which is looped until false is returned.
  94. * \param interval The minimum break time in milli seconds before the first execution of the task function and between the following ones.
  95. */
  96. looped_task_func(function0<bool> const & function, unsigned int const interval = 0)
  97. : m_function(function)
  98. {
  99. m_break_s = interval / 1000;
  100. m_break_ns = (interval - m_break_s * 1000) * 1000 * 1000;
  101. }
  102. /*! Executes the task function.
  103. */
  104. void operator() (void) const
  105. {
  106. if(m_function)
  107. {
  108. if(m_break_s > 0 || m_break_ns > 0)
  109. { // Sleep some time before first execution
  110. xtime xt;
  111. xtime_get(&xt, TIME_UTC);
  112. xt.nsec += m_break_ns;
  113. xt.sec += m_break_s;
  114. thread::sleep(xt);
  115. }
  116. while(m_function())
  117. {
  118. if(m_break_s > 0 || m_break_ns > 0)
  119. {
  120. xtime xt;
  121. xtime_get(&xt, TIME_UTC);
  122. xt.nsec += m_break_ns;
  123. xt.sec += m_break_s;
  124. thread::sleep(xt);
  125. }
  126. else
  127. {
  128. thread::yield(); // Be fair to other threads
  129. }
  130. }
  131. }
  132. }
  133. }; // looped_task_func
  134. } } // namespace boost::threadpool
  135. #endif // THREADPOOL_TASK_ADAPTERS_HPP_INCLUDED