123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284 |
- #ifndef __BOOST_SORT_COMMON_SCHEDULER_HPP
- #define __BOOST_SORT_COMMON_SCHEDULER_HPP
- #include <ciso646>
- #include <scoped_allocator>
- #include <utility>
- #include <vector>
- #include <deque>
- #include <iostream>
- #include <unordered_map>
- #include <boost/sort/common/spinlock.hpp>
- #include <boost/sort/common/util/search.hpp>
- #include <boost/sort/common/util/traits.hpp>
- namespace boost
- {
- namespace sort
- {
- namespace common
- {
- template<typename Func_t, typename Allocator = std::allocator<Func_t> >
- struct scheduler
- {
-
-
-
- typedef std::scoped_allocator_adaptor <Allocator> scoped_alloc;
- template <class T>
- using alloc_t = typename std::allocator_traits<Allocator>::
- template rebind_alloc<T>;
- typedef std::deque <Func_t, scoped_alloc> deque_t;
- typedef typename deque_t::iterator it_deque;
- typedef std::thread::id key_t;
- typedef std::hash <key_t> hash_t;
- typedef std::equal_to <key_t> equal_t;
- typedef std::unique_lock <spinlock_t> lock_t;
- typedef std::pair<const key_t, deque_t> pair_t;
- typedef std::unordered_map <key_t, deque_t, hash_t,
- equal_t, alloc_t <pair_t> > map_t;
- typedef typename map_t::iterator it_map;
-
-
-
- map_t mp;
- size_t nelem;
- mutable spinlock_t spl;
-
-
-
-
- scheduler(void) : mp(), nelem(0) { };
-
-
-
-
-
-
- scheduler(scheduler && VT) = delete;
- scheduler(const scheduler & VT) = delete;
-
-
-
-
-
- virtual ~scheduler(void) {mp.clear();};
-
-
-
-
-
-
-
- scheduler & operator=(const scheduler &VT) = delete;
-
-
-
-
-
-
-
- size_t size(void) const
- {
- lock_t s(spl);
- return nelem;
- };
-
-
-
-
-
- void clear_all(void)
- {
- lock_t s(spl);
- mp.clear();
- nelem = 0;
- };
-
-
-
-
-
-
-
-
-
- void insert(Func_t & f)
- {
- lock_t s(spl);
- key_t th_id = std::this_thread::get_id();
- it_map itmp = mp.find(th_id);
- if (itmp == mp.end())
- {
- auto aux = mp.emplace(th_id, deque_t());
- if (aux.second == false) throw std::bad_alloc();
- itmp = aux.first;
- };
- itmp->second.emplace_back(std::move(f));
- nelem++;
- };
-
-
-
-
-
-
-
-
- template<class ... Args>
- void emplace(Args && ... args)
- {
- lock_t s(spl);
- key_t th_id = std::this_thread::get_id();
- it_map itmp = mp.find(th_id);
- if (itmp == mp.end())
- {
- auto aux = mp.emplace(th_id, deque_t());
- if (aux.second == false) throw std::bad_alloc();
- itmp = aux.first;
- };
- itmp->second.emplace_back(std::forward <Args>(args) ...);
- nelem++;
- };
-
-
-
-
-
-
-
-
- template<class it_func>
- void insert_range(it_func first, it_func last)
- {
-
-
-
- typedef util::value_iter<it_func> value2_t;
- static_assert (std::is_same< Func_t, value2_t >::value,
- "Incompatible iterators\n");
-
-
-
- assert((last - first) > 0);
- lock_t s(spl);
- key_t th_id = std::this_thread::get_id();
- it_map itmp = mp.find(th_id);
- if (itmp == mp.end())
- {
- auto aux = mp.emplace(th_id, deque_t());
- if (aux.second == true) throw std::bad_alloc();
- itmp = aux.first;
- };
- while (first != last)
- {
- itmp->second.emplace_back(std::move(*(first++)));
- nelem++;
- };
- };
-
-
-
-
-
-
-
-
-
-
- bool extract(Func_t & f)
- {
- lock_t s(spl);
- if (nelem == 0) return false;
- key_t th_id = std::this_thread::get_id();
- it_map itmp = mp.find(th_id);
- if (itmp != mp.end() and not itmp->second.empty())
- {
- f = std::move(itmp->second.back());
- itmp->second.pop_back();
- --nelem;
- return true;
- };
- for (itmp = mp.begin(); itmp != mp.end(); ++itmp)
- {
- if (itmp->second.empty()) continue;
- f = std::move(itmp->second.back());
- itmp->second.pop_back();
- --nelem;
- return true;
- }
- return false;
- };
- };
- template<class ... Args>
- std::ostream & operator <<(std::ostream &out, const std::deque<Args ...> & dq)
- {
- for (uint32_t i = 0; i < dq.size(); ++i)
- out << dq[i] << " ";
- out << std::endl;
- return out;
- }
- template<typename Func_t, typename Allocator = std::allocator<Func_t> >
- std::ostream & operator <<(std::ostream &out,
- const scheduler<Func_t, Allocator> &sch)
- {
- std::unique_lock < spinlock_t > s(sch.spl);
- out << "Nelem :" << sch.nelem << std::endl;
- for (auto it = sch.mp.begin(); it != sch.mp.end(); ++it)
- {
- out << it->first << " :" << it->second << std::endl;
- }
- return out;
- }
- };
- };
- };
- #endif
|