Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members

FXThreadPool.h
1 /********************************************************************************
2 * *
3 * T h r e a d P o o l *
4 * *
5 *********************************************************************************
6 * Copyright (C) 2006,2017 by Jeroen van der Zijp. All Rights Reserved. *
7 *********************************************************************************
8 * This library is free software; you can redistribute it and/or modify *
9 * it under the terms of the GNU Lesser General Public License as published by *
10 * the Free Software Foundation; either version 3 of the License, or *
11 * (at your option) any later version. *
12 * *
13 * This library is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU Lesser General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU Lesser General Public License *
19 * along with this program. If not, see <http://www.gnu.org/licenses/> *
20 ********************************************************************************/
21 #ifndef FXTHREADPOOL_H
22 #define FXTHREADPOOL_H
23 
24 #ifndef FXRUNNABLE_H
25 #include "FXRunnable.h"
26 #endif
27 
28 namespace FX {
29 
30 
31 class FXWorker;
32 
34 typedef FXLFQueueOf<FXRunnable> FXTaskQueue;
35 
36 
68 class FXAPI FXThreadPool : public FXRunnable {
69 private:
70  FXTaskQueue queue; // Task queue
71  FXCompletion tasks; // Active tasks
72  FXCompletion threads; // Active threads
73  FXSemaphore freeslots; // Free slots in queue
74  FXSemaphore usedslots; // Used slots in queue
75  FXuval stacksize; // Stack size
76  FXTime expiration; // Quit if no task within this time
77  volatile FXuint maximum; // Maximum threads
78  volatile FXuint minimum; // Minimum threads
79  volatile FXuint workers; // Working threads
80  volatile FXuint running; // Context is running
81 private:
82  static FXAutoThreadStorageKey reference;
83 private:
84  FXbool startWorker();
85  void runWhile(FXCompletion& comp,FXTime timeout);
86  virtual FXint run();
87 private:
88  FXThreadPool(const FXThreadPool&);
89  FXThreadPool &operator=(const FXThreadPool&);
90 public:
91 
95  FXThreadPool(FXuint sz=256);
96 
98  FXbool active() const { return running==1; }
99 
101  FXbool setSize(FXuint sz);
102 
104  FXuint getSize() const { return queue.getSize(); }
105 
107  FXuint getRunningTasks() const { return tasks.count(); }
108 
110  FXuint getRunningThreads() const { return threads.count(); }
111 
113  FXbool setMinimumThreads(FXuint n);
114 
116  FXuint getMinimumThreads() const { return minimum; }
117 
119  FXbool setMaximumThreads(FXuint n);
120 
122  FXuint getMaximumThreads() const { return maximum; }
123 
125  FXbool setExpiration(FXTime ns=forever);
126 
128  FXTime getExpiration() const { return expiration; }
129 
131  FXbool setStackSize(FXuval sz);
132 
134  FXuval getStackSize() const { return stacksize; }
135 
137  static FXThreadPool* instance();
138 
140  static void instance(FXThreadPool *pool);
141 
149  FXuint start(FXuint count=0);
150 
159  FXbool execute(FXRunnable* task,FXTime blocking=forever);
160 
170  FXbool executeAndWait(FXRunnable* task,FXTime blocking=forever);
171 
181  FXbool executeAndWaitFor(FXRunnable* task,FXCompletion& comp,FXTime blocking=forever);
182 
189  FXbool wait();
190 
198  FXbool waitFor(FXCompletion& comp);
199 
208  FXbool stop();
209 
213  virtual ~FXThreadPool();
214  };
215 
216 
217 }
218 
219 #endif

Copyright © 1997-2016 Jeroen van der Zijp