iLab Neuromorphic Robotics Toolkit  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ThreadPoolsImpl.H
Go to the documentation of this file.
1 /*! @file
2  @author Shane Grant
3  @copyright GNU Public License (GPL v3)
4  @section License
5  @verbatim
6  // ////////////////////////////////////////////////////////////////////////
7  // The iLab Neuromorphic Robotics Toolkit (NRT) //
8  // Copyright 2010-2012 by the University of Southern California (USC) //
9  // and the iLab at USC. //
10  // //
11  // iLab - University of Southern California //
12  // Hedco Neurociences Building, Room HNB-10 //
13  // Los Angeles, Ca 90089-2520 - USA //
14  // //
15  // See http://ilab.usc.edu for information about this project. //
16  // ////////////////////////////////////////////////////////////////////////
17  // This file is part of The iLab Neuromorphic Robotics Toolkit. //
18  // //
19  // The iLab Neuromorphic Robotics Toolkit is free software: you can //
20  // redistribute it and/or modify it under the terms of the GNU General //
21  // Public License as published by the Free Software Foundation, either //
22  // version 3 of the License, or (at your option) any later version. //
23  // //
24  // The iLab Neuromorphic Robotics Toolkit is distributed in the hope //
25  // that it will be useful, but WITHOUT ANY WARRANTY; without even the //
26  // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR //
27  // PURPOSE. See the GNU General Public License for more details. //
28  // //
29  // You should have received a copy of the GNU General Public License //
30  // along with The iLab Neuromorphic Robotics Toolkit. If not, see //
31  // <http://www.gnu.org/licenses/>. //
32  // ////////////////////////////////////////////////////////////////////////
33  @endverbatim */
34 
35 #ifndef INCLUDE_NRT_CORE_DESIGN_DETAILS_THREADPOOLSIMPL_H_
36 #define INCLUDE_NRT_CORE_DESIGN_DETAILS_THREADPOOLSIMPL_H_
37 
38 template <class Func> inline
39 std::future<typename std::result_of<Func()>::type>
40 nrt::BoundedThreadPool::pushJob( Func f )
41 {
42  typedef typename std::result_of<Func()>::type result_type;
43 
44  std::packaged_task<result_type()> task( std::move( f ) );
45  std::future<result_type> result( task.get_future() );
46  itsWorkQueue.push( std::move( task ) );
47 
48  return result;
49 }
50 
51 // Clang does not support thread_local as of clang version 3.3 (trunk 178896) (llvm/trunk 178895)
52 #ifndef __clang__
53 
54 inline bool nrt::BoundedLocalQueueThreadPool::popTaskFromLocalQueue( FunctionWrapper & task )
55 {
56  return itsLocalWorkQueue && itsLocalWorkQueue->tryPop( task );
57 }
58 
59 inline bool nrt::BoundedLocalQueueThreadPool::popTaskFromPoolQueue( FunctionWrapper & task )
60 {
61  return itsWorkQueue.tryPop( task );
62 }
63 
64 inline bool nrt::BoundedLocalQueueThreadPool::popTaskFromOtherThreadQueue( FunctionWrapper & task )
65 {
66  for( size_t i = 0; i < itsQueues.size(); ++i )
67  {
68  size_t const index = ( itsIndex + i - 1 ) % itsQueues.size();
69 
70  if( itsQueues[index]->trySteal( task ) )
71  return true;
72  }
73 
74  return false;
75 }
76 
77 template <class Func> inline
78 std::future<typename std::result_of<Func()>::type>
79 nrt::BoundedLocalQueueThreadPool::pushJob( Func f )
80 {
81  typedef typename std::result_of<Func()>::type result_type;
82 
83  std::packaged_task<result_type()> task( std::move( f ) );
84  std::future<result_type> result( task.get_future() );
85 
86  if( itsLocalWorkQueue )
87  itsLocalWorkQueue->push( std::move( task ) );
88  else
89  itsWorkQueue.push( std::move( task ) );
90 
91  return result;
92 }
93 
94 #endif // ifndef __clang__
95 
96 template <class Func> inline
97 std::future<typename std::result_of<Func()>::type>
98 nrt::UnboundedThreadPool::pushJob( Func f )
99 {
100  typedef typename std::result_of<Func()>::type result_type;
101 
102  checkAndIncrease();
103 
104  std::packaged_task<result_type()> task( std::move( f ) );
105  std::future<result_type> result( task.get_future() );
106  itsWorkQueue.push( std::move( task ) );
107 
108  return result;
109 }
110 
111 // Clang does not support thread_local as of clang version 3.3 (trunk 178896) (llvm/trunk 178895)
112 #ifndef __clang__
113 inline bool nrt::UnboundedLocalQueueThreadPool::popTaskFromLocalQueue( FunctionWrapper & task )
114 {
115  return itsLocalWorkQueue && itsLocalWorkQueue->tryPop( task );
116 }
117 
118 inline bool nrt::UnboundedLocalQueueThreadPool::popTaskFromPoolQueue( FunctionWrapper & task )
119 {
120  return itsWorkQueue.tryPop( task );
121 }
122 
123 inline bool nrt::UnboundedLocalQueueThreadPool::popTaskFromOtherThreadQueue( FunctionWrapper & task )
124 {
125  for( size_t i = 0; i < itsQueues.size(); ++i )
126  {
127  size_t const index = ( itsIndex + i - 1 ) % itsQueues.size();
128 
129  if( itsQueues[index]->trySteal( task ) )
130  return true;
131  }
132 
133  return false;
134 }
135 
136 template <class Func> inline
137 std::future<typename std::result_of<Func()>::type>
138 nrt::UnboundedLocalQueueThreadPool::pushJob( Func f )
139 {
140  typedef typename std::result_of<Func()>::type result_type;
141 
142  checkAndIncrease();
143 
144  std::packaged_task<result_type()> task( std::move( f ) );
145  std::future<result_type> result( task.get_future() );
146 
147  if( itsLocalWorkQueue )
148  itsLocalWorkQueue->push( std::move( task ) );
149  else
150  itsWorkQueue.push( std::move( task ) );
151 
152  return result;
153 }
154 #endif // ifndef __clang__
155 
156 #endif // INCLUDE_NRT_CORE_DESIGN_DETAILS_THREADPOOLSIMPL_H_