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
|
/********************************************************************************
* *
* T a s k G r o u p C l a s s *
* *
*********************************************************************************
* Copyright (C) 2012,2022 by Jeroen van der Zijp. All Rights Reserved. *
*********************************************************************************
* This library is free software; you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as published by *
* the Free Software Foundation; either version 3 of the License, or *
* (at your option) any later version. *
* *
* This library is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/> *
********************************************************************************/
#ifndef FXTASKGROUP_H
#define FXTASKGROUP_H
namespace FX {
class FXThreadPool;
/**
* An FXTaskGroup manages a number of tasks, executing on the associated FXThreadPool.
* In a typical use, an FXTaskGroup is constructed on the stack of the calling function.
* If an explicit FXThreadPool is passed to the constructor of the FXTaskGroup, this is
* the FXThreadPool that will be used to execute the tasks. Otherwise, an instance of
* FXThreadPool will be located through a thread-local variable from the calling thread.
* This thread-local variable will be set if the calling thread is a worker thread from
* the FXThreadPool, or is the thread that called start() on the FXThreadPool.
* Tasks managed by the FXTaskGroup may be started at any time during the FXTaskGroup's
* lifetime. However, the FXTaskGroup can not be destroyed until the last task in the
* group has finished execution: the FXTaskGroup's destructor will wait until all tasks
* (if any) have finished executing.
* The calling thread can enter the task-processing loop in the FXThreadPool; it returns
* when either the tasks from this FXTaskGroup have been completed (count reaching zero),
* or when the task-queue is empty (in this case another thread may still be working on
* a task from the FXTaskGroup). Thus, the calling thread should block on the completion
* semaphore to ensure all tasks are completed.
*/
class FXAPI FXTaskGroup {
private:
class Task : public FXRunnable {
private:
FXTaskGroup *taskgroup; // Backlink to taskgroup
FXRunnable *runnable; // Wrapped runnable
private:
Task(const Task&);
Task &operator=(const Task&);
public:
Task(FXTaskGroup* g,FXRunnable *r);
virtual FXint run();
virtual ~Task();
};
private:
FXThreadPool *threadpool; // Thread pool used by task group
FXCompletion completion; // Completion counter
private:
FXTaskGroup(const FXTaskGroup&);
FXTaskGroup &operator=(const FXTaskGroup&);
public:
/**
* Create new task group, using the calling thread's associated
* thread pool.
*/
FXTaskGroup();
/**
* Create new task group, using the given thread pool.
*/
FXTaskGroup(FXThreadPool* p);
/**
* Return threadpool.
*/
FXThreadPool* getThreadPool() const { return threadpool; }
/**
* Return number of tasks.
*/
FXuint getRunningTasks() const { return completion.count(); }
/**
* Start a task in this task group.
*/
FXbool execute(FXRunnable* task);
/**
* Start task in this task group, and then enter the task-processing
* loop, returning when all tasks have been completed.
* Return false if unable to start the task.
*/
FXbool executeAndWait(FXRunnable* task);
/**
* Wait until all tasks of this group have finished executing, then return.
* The completion semaphore is reset after being signaled by the last completed
* task.
*/
FXbool wait();
/**
* Wait for the semaphore to be signaled, then destroy the task group.
*/
virtual ~FXTaskGroup();
};
}
#endif
|