scheduler.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. //----------------------------------------------------------------------------
  2. /// @file scheduler.hpp
  3. /// @brief This file contains the implementation of the scheduler for
  4. /// dispatch the works stored
  5. ///
  6. /// @author Copyright (c) 2010 2015 Francisco José Tapia (fjtapia@gmail.com )\n
  7. /// Distributed under the Boost Software License, Version 1.0.\n
  8. /// ( See accompanyingfile LICENSE_1_0.txt or copy at
  9. /// http://www.boost.org/LICENSE_1_0.txt )
  10. /// @version 0.1
  11. ///
  12. /// @remarks
  13. //-----------------------------------------------------------------------------
  14. #ifndef __BOOST_SORT_COMMON_SCHEDULER_HPP
  15. #define __BOOST_SORT_COMMON_SCHEDULER_HPP
  16. #include <boost/sort/common/spinlock.hpp>
  17. #include <boost/sort/common/search.hpp>
  18. #include <boost/sort/common/compare_traits.hpp>
  19. #include <scoped_allocator>
  20. #include <utility>
  21. #include <vector>
  22. #include <deque>
  23. #include <iostream>
  24. #include <unordered_map>
  25. namespace boost
  26. {
  27. namespace sort
  28. {
  29. namespace common
  30. {
  31. //
  32. //###########################################################################
  33. // ##
  34. // ################################################################ ##
  35. // # # ##
  36. // # C L A S S S C H E D U L E R # ##
  37. // # # ##
  38. // ################################################################ ##
  39. // ##
  40. //###########################################################################
  41. //
  42. //---------------------------------------------------------------------------
  43. /// @class scheduler
  44. /// @brief This class is a concurrent stack controled by a spin_lock
  45. /// @remarks
  46. //---------------------------------------------------------------------------
  47. template<typename Func_t, typename Allocator = std::allocator<Func_t> >
  48. struct scheduler
  49. {
  50. //-----------------------------------------------------------------------
  51. // D E F I N I T I O N S
  52. //-----------------------------------------------------------------------
  53. typedef std::scoped_allocator_adaptor <Allocator> scoped_alloc;
  54. typedef std::deque <Func_t, scoped_alloc> deque_t;
  55. typedef typename deque_t::iterator it_deque;
  56. typedef std::thread::id key_t;
  57. typedef std::hash <key_t> hash_t;
  58. typedef std::equal_to <key_t> equal_t;
  59. typedef std::unique_lock <spinlock_t> lock_t;
  60. typedef std::unordered_map <key_t, deque_t, hash_t,
  61. equal_t, scoped_alloc> map_t;
  62. typedef typename map_t::iterator it_map;
  63. //-----------------------------------------------------------------------
  64. // V A R I A B L E S
  65. //-----------------------------------------------------------------------
  66. map_t mp;
  67. size_t nelem;
  68. mutable spinlock_t spl;
  69. //------------------------------------------------------------------------
  70. // function : scheduler
  71. /// @brief constructor
  72. //------------------------------------------------------------------------
  73. scheduler(void) : mp(), nelem(0) { };
  74. //
  75. //-----------------------------------------------------------------------
  76. // function : scheduler
  77. /// @brief Copy & move constructor
  78. /// @param [in] VT : stack_cnc from where copy the data
  79. //-----------------------------------------------------------------------
  80. scheduler(scheduler && VT) = delete;
  81. scheduler(const scheduler & VT) = delete;
  82. //
  83. //------------------------------------------------------------------------
  84. // function : ~scheduler
  85. /// @brief Destructor
  86. //------------------------------------------------------------------------
  87. virtual ~scheduler(void) {mp.clear();};
  88. //
  89. //------------------------------------------------------------------------
  90. // function : operator =
  91. /// @brief Asignation operator
  92. /// @param [in] VT : stack_cnc from where copy the data
  93. /// @return Reference to the stack_cnc after the copy
  94. //------------------------------------------------------------------------
  95. scheduler & operator=(const scheduler &VT) = delete;
  96. //
  97. //------------------------------------------------------------------------
  98. // function : size
  99. /// @brief Asignation operator
  100. /// @param [in] VT : stack_cnc from where copy the data
  101. /// @return Reference to the stack_cnc after the copy
  102. //------------------------------------------------------------------------
  103. size_t size(void) const
  104. {
  105. lock_t s(spl);
  106. return nelem;
  107. };
  108. //
  109. //------------------------------------------------------------------------
  110. // function : clear
  111. /// @brief Delete all the elements of the stack_cnc.
  112. //------------------------------------------------------------------------
  113. void clear_all(void)
  114. {
  115. lock_t s(spl);
  116. mp.clear();
  117. nelem = 0;
  118. };
  119. //
  120. //------------------------------------------------------------------------
  121. // function : insert
  122. /// @brief Insert one element in the back of the container
  123. /// @param [in] D : value to insert. Can ve a value, a reference or an
  124. /// rvalue
  125. /// @return iterator to the element inserted
  126. /// @remarks This operation is O ( const )
  127. //------------------------------------------------------------------------
  128. void insert(Func_t & f)
  129. {
  130. lock_t s(spl);
  131. key_t th_id = std::this_thread::get_id();
  132. it_map itmp = mp.find(th_id);
  133. if (itmp == mp.end())
  134. {
  135. auto aux = mp.emplace(th_id, deque_t());
  136. if (aux.second == false) throw std::bad_alloc();
  137. itmp = aux.first;
  138. };
  139. itmp->second.emplace_back(std::move(f));
  140. nelem++;
  141. };
  142. //
  143. //------------------------------------------------------------------------
  144. // function :emplace
  145. /// @brief Insert one element in the back of the container
  146. /// @param [in] args :group of arguments for to build the object to insert
  147. /// @return iterator to the element inserted
  148. /// @remarks This operation is O ( const )
  149. //------------------------------------------------------------------------
  150. template<class ... Args>
  151. void emplace(Args && ... args)
  152. {
  153. lock_t s(spl);
  154. key_t th_id = std::this_thread::get_id();
  155. it_map itmp = mp.find(th_id);
  156. if (itmp == mp.end())
  157. {
  158. auto aux = mp.emplace(th_id, deque_t());
  159. if (aux.second == false) throw std::bad_alloc();
  160. itmp = aux.first;
  161. };
  162. itmp->second.emplace_back(std::forward <Args>(args) ...);
  163. nelem++;
  164. };
  165. //
  166. //------------------------------------------------------------------------
  167. // function : insert
  168. /// @brief Insert one element in the back of the container
  169. /// @param [in] D : value to insert. Can ve a value, a reference or an rvalue
  170. /// @return iterator to the element inserted
  171. /// @remarks This operation is O ( const )
  172. //------------------------------------------------------------------------
  173. template<class it_func>
  174. void insert_range(it_func first, it_func last)
  175. {
  176. //--------------------------------------------------------------------
  177. // Metaprogramming
  178. //--------------------------------------------------------------------
  179. typedef value_iter<it_func> value2_t;
  180. static_assert (std::is_same< Func_t, value2_t >::value,
  181. "Incompatible iterators\n");
  182. //--------------------------------------------------------------------
  183. // Code
  184. //--------------------------------------------------------------------
  185. assert((last - first) > 0);
  186. lock_t s(spl);
  187. key_t th_id = std::this_thread::get_id();
  188. it_map itmp = mp.find(th_id);
  189. if (itmp == mp.end())
  190. {
  191. auto aux = mp.emplace(th_id, deque_t());
  192. if (aux.second == true) throw std::bad_alloc();
  193. itmp = aux.first;
  194. };
  195. while (first != last)
  196. {
  197. itmp->second.emplace_back(std::move(*(first++)));
  198. nelem++;
  199. };
  200. };
  201. //
  202. //------------------------------------------------------------------------
  203. // function : extract
  204. /// @brief erase the last element of the tree and return a copy
  205. /// @param [out] V : reference to a variable where copy the element
  206. /// @return code of the operation
  207. /// 0- Element erased
  208. /// 1 - Empty tree
  209. /// @remarks This operation is O(1)
  210. //------------------------------------------------------------------------
  211. bool extract(Func_t & f)
  212. {
  213. lock_t s(spl);
  214. if (nelem == 0) return false;
  215. key_t th_id = std::this_thread::get_id();
  216. it_map itmp = mp.find(th_id);
  217. if (itmp != mp.end() and not itmp->second.empty())
  218. {
  219. f = std::move(itmp->second.back());
  220. itmp->second.pop_back();
  221. --nelem;
  222. return true;
  223. };
  224. for (itmp = mp.begin(); itmp != mp.end(); ++itmp)
  225. {
  226. if (itmp->second.empty()) continue;
  227. f = std::move(itmp->second.back());
  228. itmp->second.pop_back();
  229. --nelem;
  230. return true;
  231. }
  232. return false;
  233. };
  234. };
  235. // end class scheduler
  236. //*************************************************************************
  237. // P R I N T F U N C T I O N S
  238. //************************************************************************
  239. template<class ... Args>
  240. std::ostream & operator <<(std::ostream &out, const std::deque<Args ...> & dq)
  241. {
  242. for (uint32_t i = 0; i < dq.size(); ++i)
  243. out << dq[i] << " ";
  244. out << std::endl;
  245. return out;
  246. }
  247. template<typename Func_t, typename Allocator = std::allocator<Func_t> >
  248. std::ostream & operator <<(std::ostream &out,
  249. const scheduler<Func_t, Allocator> &sch)
  250. {
  251. std::unique_lock < spinlock_t > s(sch.spl);
  252. out << "Nelem :" << sch.nelem << std::endl;
  253. for (auto it = sch.mp.begin(); it != sch.mp.end(); ++it)
  254. {
  255. out << it->first << " :" << it->second << std::endl;
  256. }
  257. return out;
  258. }
  259. //***************************************************************************
  260. };// end namespace common
  261. };// end namespace sort
  262. };// end namespace boost
  263. //***************************************************************************
  264. #endif