ultimatepp/uppsrc/Core/CoWork.h
cxl 4f15b187dd Core: CoWork now accepts Function<void ()> only, callback per operator
git-svn-id: svn://ultimatepp.org/upp/trunk@9946 f0d560ea-af0d-0410-9eb7-867de7ffcac7
2016-06-15 18:30:07 +00:00

86 lines
1.8 KiB
C++

#ifdef _MULTITHREADED
class CoWork : NoCopy {
typedef StaticCriticalSection Lock;
struct MJob : Moveable<MJob> {
Function<void ()> fn;
CoWork *work;
};
enum { SCHEDULED_MAX = 2048 };
public:
struct Pool {
int scheduled;
MJob jobs[SCHEDULED_MAX];
int waiting_threads;
Array<Thread> threads;
Mutex lock;
Semaphore waitforjob;
Pool(int nthreads);
~Pool();
static thread__ bool finlock;
static bool DoJob();
static void ThreadRun(int tno);
};
friend struct Pool;
static Pool& GetPool(int n);
static Pool& GetPool();
static thread_local bool is_worker;
static thread_local Pool *pool;
Semaphore waitforfinish;
int todo;
Mutex stepmutex;
Array<BiVector<Function<void ()>>> step;
Vector<bool> steprunning;
public:
void Do(Function<void ()>&& fn);
void Do(const Function<void ()>& fn) { Do(clone(fn)); }
CoWork& operator&(const Function<void ()>& fn) { Do(fn); return *this; }
CoWork& operator&(Function<void ()>&& fn) { Do(pick(fn)); return *this; }
void Pipe(int stepi, Function<void ()>&& lambda);
static void FinLock();
void Finish();
bool IsFinished();
static bool IsWorker() { return is_worker; }
static void StartPool(int n);
static void ShutdownPool();
CoWork();
~CoWork();
};
#else
class CoWork : NoCopy {
public:
void Do(Callback cb) { cb(); }
CoWork& operator&(Callback cb) { cb(); return *this; }
void Finish() {}
bool IsFinished() { return true; }
static void FinLock() {}
static bool IsWorker() { return false; }
static void StartPool(int n) {}
static void ShutdownPool() {}
};
#endif