ultimatepp/uppsrc/Core/CoWork.h
cxl 7ece28ae38 Core: CoWork refactoring finished, RichText: Fixed issue with begin/infinite height text
git-svn-id: svn://ultimatepp.org/upp/trunk@10153 f0d560ea-af0d-0410-9eb7-867de7ffcac7
2016-08-08 07:31:41 +00:00

97 lines
2.3 KiB
C++

#ifdef _MULTITHREADED
class CoWork : NoCopy {
struct MJob : Moveable<MJob>, Link<MJob, 2> {
Function<void ()> fn;
CoWork *work = NULL;
};
enum { SCHEDULED_MAX = 2048 };
public:
struct Pool {
MJob *free;
Link<MJob, 2> jobs;
MJob slot[SCHEDULED_MAX];
int waiting_threads;
Array<Thread> threads;
bool quit;
Mutex lock;
ConditionVariable waitforjob;
void Free(MJob& m);
void DoJob(MJob& m);
void PushJob(Function<void ()>&& fn, CoWork *work);
void InitThreads(int nthreads);
void ExitThreads();
Pool();
~Pool();
static thread__ bool finlock;
bool DoJob();
static void ThreadRun(int tno);
};
friend struct Pool;
static Pool& GetPool();
static thread_local bool is_worker;
// byte magic[sizeof(ConditionVariable)];
ConditionVariable waitforfinish;
Link<MJob, 2> jobs;
int todo;
Mutex stepmutex;
Array<BiVector<Function<void ()>>> step;
Vector<bool> steprunning;
public:
static bool TrySchedule(Function<void ()>&& fn);
static bool TrySchedule(const Function<void ()>& fn) { return TrySchedule(clone(fn)); }
static void Schedule(Function<void ()>&& fn);
static void Schedule(const Function<void ()>& fn) { return Schedule(clone(fn)); }
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); // experimental
static void FinLock();
void Finish();
bool IsFinished();
static bool IsWorker() { return is_worker; }
static void SetPoolSize(int n);
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