scheduler.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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 <scoped_allocator>
  17. #include <utility>
  18. #include <vector>
  19. #include <deque>
  20. #include <iostream>
  21. #include <unordered_map>
  22. #include <boost/sort/common/spinlock.hpp>
  23. #include <boost/sort/common/util/search.hpp>
  24. #include <boost/sort/common/util/traits.hpp>
  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. template <class T>
  55. using alloc_t = typename std::allocator_traits<Allocator>::
  56. template rebind_alloc<T>;
  57. typedef std::deque <Func_t, scoped_alloc> deque_t;
  58. typedef typename deque_t::iterator it_deque;
  59. typedef std::thread::id key_t;
  60. typedef std::hash <key_t> hash_t;
  61. typedef std::equal_to <key_t> equal_t;
  62. typedef std::unique_lock <spinlock_t> lock_t;
  63. typedef std::pair<const key_t, deque_t> pair_t;
  64. typedef std::unordered_map <key_t, deque_t, hash_t,
  65. equal_t, alloc_t <pair_t> > map_t;
  66. typedef typename map_t::iterator it_map;
  67. //-----------------------------------------------------------------------
  68. // V A R I A B L E S
  69. //-----------------------------------------------------------------------
  70. map_t mp;
  71. size_t nelem;
  72. mutable spinlock_t spl;
  73. //------------------------------------------------------------------------
  74. // function : scheduler
  75. /// @brief constructor
  76. //------------------------------------------------------------------------
  77. scheduler(void) : mp(), nelem(0) { };
  78. //
  79. //-----------------------------------------------------------------------
  80. // function : scheduler
  81. /// @brief Copy & move constructor
  82. /// @param [in] VT : stack_cnc from where copy the data
  83. //-----------------------------------------------------------------------
  84. scheduler(scheduler && VT) = delete;
  85. scheduler(const scheduler & VT) = delete;
  86. //
  87. //------------------------------------------------------------------------
  88. // function : ~scheduler
  89. /// @brief Destructor
  90. //------------------------------------------------------------------------
  91. virtual ~scheduler(void) {mp.clear();};
  92. //
  93. //------------------------------------------------------------------------
  94. // function : operator =
  95. /// @brief Asignation operator
  96. /// @param [in] VT : stack_cnc from where copy the data
  97. /// @return Reference to the stack_cnc after the copy
  98. //------------------------------------------------------------------------
  99. scheduler & operator=(const scheduler &VT) = delete;
  100. //
  101. //------------------------------------------------------------------------
  102. // function : size
  103. /// @brief Asignation operator
  104. /// @param [in] VT : stack_cnc from where copy the data
  105. /// @return Reference to the stack_cnc after the copy
  106. //------------------------------------------------------------------------
  107. size_t size(void) const
  108. {
  109. lock_t s(spl);
  110. return nelem;
  111. };
  112. //
  113. //------------------------------------------------------------------------
  114. // function : clear
  115. /// @brief Delete all the elements of the stack_cnc.
  116. //------------------------------------------------------------------------
  117. void clear_all(void)
  118. {
  119. lock_t s(spl);
  120. mp.clear();
  121. nelem = 0;
  122. };
  123. //
  124. //------------------------------------------------------------------------
  125. // function : insert
  126. /// @brief Insert one element in the back of the container
  127. /// @param [in] D : value to insert. Can ve a value, a reference or an
  128. /// rvalue
  129. /// @return iterator to the element inserted
  130. /// @remarks This operation is O ( const )
  131. //------------------------------------------------------------------------
  132. void insert(Func_t & f)
  133. {
  134. lock_t s(spl);
  135. key_t th_id = std::this_thread::get_id();
  136. it_map itmp = mp.find(th_id);
  137. if (itmp == mp.end())
  138. {
  139. auto aux = mp.emplace(th_id, deque_t());
  140. if (aux.second == false) throw std::bad_alloc();
  141. itmp = aux.first;
  142. };
  143. itmp->second.emplace_back(std::move(f));
  144. nelem++;
  145. };
  146. //
  147. //------------------------------------------------------------------------
  148. // function :emplace
  149. /// @brief Insert one element in the back of the container
  150. /// @param [in] args :group of arguments for to build the object to insert
  151. /// @return iterator to the element inserted
  152. /// @remarks This operation is O ( const )
  153. //------------------------------------------------------------------------
  154. template<class ... Args>
  155. void emplace(Args && ... args)
  156. {
  157. lock_t s(spl);
  158. key_t th_id = std::this_thread::get_id();
  159. it_map itmp = mp.find(th_id);
  160. if (itmp == mp.end())
  161. {
  162. auto aux = mp.emplace(th_id, deque_t());
  163. if (aux.second == false) throw std::bad_alloc();
  164. itmp = aux.first;
  165. };
  166. itmp->second.emplace_back(std::forward <Args>(args) ...);
  167. nelem++;
  168. };
  169. //
  170. //------------------------------------------------------------------------
  171. // function : insert
  172. /// @brief Insert one element in the back of the container
  173. /// @param [in] D : value to insert. Can ve a value, a reference or an rvalue
  174. /// @return iterator to the element inserted
  175. /// @remarks This operation is O ( const )
  176. //------------------------------------------------------------------------
  177. template<class it_func>
  178. void insert_range(it_func first, it_func last)
  179. {
  180. //--------------------------------------------------------------------
  181. // Metaprogramming
  182. //--------------------------------------------------------------------
  183. typedef util::value_iter<it_func> value2_t;
  184. static_assert (std::is_same< Func_t, value2_t >::value,
  185. "Incompatible iterators\n");
  186. //--------------------------------------------------------------------
  187. // Code
  188. //--------------------------------------------------------------------
  189. assert((last - first) > 0);
  190. lock_t s(spl);
  191. key_t th_id = std::this_thread::get_id();
  192. it_map itmp = mp.find(th_id);
  193. if (itmp == mp.end())
  194. {
  195. auto aux = mp.emplace(th_id, deque_t());
  196. if (aux.second == true) throw std::bad_alloc();
  197. itmp = aux.first;
  198. };
  199. while (first != last)
  200. {
  201. itmp->second.emplace_back(std::move(*(first++)));
  202. nelem++;
  203. };
  204. };
  205. //
  206. //------------------------------------------------------------------------
  207. // function : extract
  208. /// @brief erase the last element of the tree and return a copy
  209. /// @param [out] V : reference to a variable where copy the element
  210. /// @return code of the operation
  211. /// 0- Element erased
  212. /// 1 - Empty tree
  213. /// @remarks This operation is O(1)
  214. //------------------------------------------------------------------------
  215. bool extract(Func_t & f)
  216. {
  217. lock_t s(spl);
  218. if (nelem == 0) return false;
  219. key_t th_id = std::this_thread::get_id();
  220. it_map itmp = mp.find(th_id);
  221. if (itmp != mp.end() && ! itmp->second.empty())
  222. {
  223. f = std::move(itmp->second.back());
  224. itmp->second.pop_back();
  225. --nelem;
  226. return true;
  227. };
  228. for (itmp = mp.begin(); itmp != mp.end(); ++itmp)
  229. {
  230. if (itmp->second.empty()) continue;
  231. f = std::move(itmp->second.back());
  232. itmp->second.pop_back();
  233. --nelem;
  234. return true;
  235. }
  236. return false;
  237. };
  238. };
  239. // end class scheduler
  240. //*************************************************************************
  241. // P R I N T F U N C T I O N S
  242. //************************************************************************
  243. template<class ... Args>
  244. std::ostream & operator <<(std::ostream &out, const std::deque<Args ...> & dq)
  245. {
  246. for (uint32_t i = 0; i < dq.size(); ++i)
  247. out << dq[i] << " ";
  248. out << std::endl;
  249. return out;
  250. }
  251. template<typename Func_t, typename Allocator = std::allocator<Func_t> >
  252. std::ostream & operator <<(std::ostream &out,
  253. const scheduler<Func_t, Allocator> &sch)
  254. {
  255. std::unique_lock < spinlock_t > s(sch.spl);
  256. out << "Nelem :" << sch.nelem << std::endl;
  257. for (auto it = sch.mp.begin(); it != sch.mp.end(); ++it)
  258. {
  259. out << it->first << " :" << it->second << std::endl;
  260. }
  261. return out;
  262. }
  263. //***************************************************************************
  264. };// end namespace common
  265. };// end namespace sort
  266. };// end namespace boost
  267. //***************************************************************************
  268. #endif