summaryrefslogtreecommitdiff
path: root/graphics/asymptote/LspCpp/third_party/threadpool/boost/threadpool/scheduling_policies.hpp
blob: 2d6c7c1baed9ad8c8d883fea36c4128ac65c8818 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/*! \file
* \brief Task scheduling policies.
*
* This file contains some fundamental scheduling policies for the pool class. 
* A scheduling policy is realized by a task container which controls the access to
* the tasks. 	Fundamentally the container determines the order the tasks are processed
* by the thread pool. 
* The task containers need not to be thread-safe because they are used by the pool 
* in thread-safe way. 
*
* Copyright (c) 2005-2007 Philipp Henkel
*
* Use, modification, and distribution are  subject to the
* Boost Software License, Version 1.0. (See accompanying  file
* LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
*
* http://threadpool.sourceforge.net
*
*/


#ifndef THREADPOOL_SCHEDULING_POLICIES_HPP_INCLUDED
#define THREADPOOL_SCHEDULING_POLICIES_HPP_INCLUDED


#include <queue>
#include <deque>

#include "task_adaptors.hpp"

namespace boost { namespace threadpool
{

  /*! \brief SchedulingPolicy which implements FIFO ordering. 
  *
  * This container implements a FIFO scheduling policy.
  * The first task to be added to the scheduler will be the first to be removed.
  * The processing proceeds sequentially in the same order. 
  * FIFO stands for "first in, first out".
  *
  * \param Task A function object which implements the operator()(void).
  *
  */ 
  template <typename Task = task_func>  
  class fifo_scheduler
  {
  public:
    typedef Task task_type; //!< Indicates the scheduler's task type.

  protected:
    std::deque<task_type> m_container;  //!< Internal task container.	


  public:
    /*! Adds a new task to the scheduler.
    * \param task The task object.
    * \return true, if the task could be scheduled and false otherwise. 
    */
    bool push(task_type const & task)
    {
      m_container.push_back(task);
      return true;
    }

    /*! Removes the task which should be executed next.
    */
    void pop()
    {
      m_container.pop_front();
    }

    /*! Gets the task which should be executed next.
    *  \return The task object to be executed.
    */
    task_type const & top() const
    {
      return m_container.front();
    }

    /*! Gets the current number of tasks in the scheduler.
    *  \return The number of tasks.
    *  \remarks Prefer empty() to size() == 0 to check if the scheduler is empty.
    */
    size_t size() const
    {
      return m_container.size();
    }

    /*! Checks if the scheduler is empty.
    *  \return true if the scheduler contains no tasks, false otherwise.
    *  \remarks Is more efficient than size() == 0. 
    */
    bool empty() const
    {
      return m_container.empty();
    }

    /*! Removes all tasks from the scheduler.
    */  
    void clear()
    {   
      m_container.clear();
    } 
  };



  /*! \brief SchedulingPolicy which implements LIFO ordering. 
  *
  * This container implements a LIFO scheduling policy.
  * The last task to be added to the scheduler will be the first to be removed.
  * LIFO stands for "last in, first out".
  *
  * \param Task A function object which implements the operator()(void).
  *
  */ 
  template <typename Task = task_func>  
  class lifo_scheduler
  {
  public:
    typedef Task task_type;  //!< Indicates the scheduler's task type.

  protected:
    std::deque<task_type> m_container;  //!< Internal task container.	

  public:
    /*! Adds a new task to the scheduler.
    * \param task The task object.
    * \return true, if the task could be scheduled and false otherwise. 
    */
    bool push(task_type const & task)
    {
      m_container.push_front(task);
      return true;
    }

    /*! Removes the task which should be executed next.
    */
    void pop()
    {
      m_container.pop_front();
    }

    /*! Gets the task which should be executed next.
    *  \return The task object to be executed.
    */
    task_type const & top() const
    {
      return m_container.front();
    }

    /*! Gets the current number of tasks in the scheduler.
    *  \return The number of tasks.
    *  \remarks Prefer empty() to size() == 0 to check if the scheduler is empty.
    */
    size_t size() const
    {
      return m_container.size();
    }

    /*! Checks if the scheduler is empty.
    *  \return true if the scheduler contains no tasks, false otherwise.
    *  \remarks Is more efficient than size() == 0. 
    */
    bool empty() const
    {
      return m_container.empty();
    }

    /*! Removes all tasks from the scheduler.
    */  
    void clear()
    {    
      m_container.clear();
    } 

  };



  /*! \brief SchedulingPolicy which implements prioritized ordering. 
  *
  * This container implements a scheduling policy based on task priorities.
  * The task with highest priority will be the first to be removed.
  * It must be possible to compare two tasks using operator<. 
  *
  * \param Task A function object which implements the operator() and operator<. operator< must be a partial ordering.
  *
  * \see prio_thread_func
  *
  */ 
  template <typename Task = prio_task_func>  
  class prio_scheduler
  {
  public:
    typedef Task task_type; //!< Indicates the scheduler's task type.

  protected:
    std::priority_queue<task_type> m_container;  //!< Internal task container.


  public:
    /*! Adds a new task to the scheduler.
    * \param task The task object.
    * \return true, if the task could be scheduled and false otherwise. 
    */
    bool push(task_type const & task)
    {
      m_container.push(task);
      return true;
    }

    /*! Removes the task which should be executed next.
    */
    void pop()
    {
      m_container.pop();
    }

    /*! Gets the task which should be executed next.
    *  \return The task object to be executed.
    */
    task_type const & top() const
    {
      return m_container.top();
    }

    /*! Gets the current number of tasks in the scheduler.
    *  \return The number of tasks.
    *  \remarks Prefer empty() to size() == 0 to check if the scheduler is empty.
    */
    size_t size() const
    {
      return m_container.size();
    }

    /*! Checks if the scheduler is empty.
    *  \return true if the scheduler contains no tasks, false otherwise.
    *  \remarks Is more efficient than size() == 0. 
    */
    bool empty() const
    {
      return m_container.empty();
    }

    /*! Removes all tasks from the scheduler.
    */  
    void clear()
    {    
      while(!m_container.empty())
      {
        m_container.pop();
      }
    } 
  };


} } // namespace boost::threadpool


#endif // THREADPOOL_SCHEDULING_POLICIES_HPP_INCLUDED