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