1 /*------------------------------------------------------------------------
2 Junction: Concurrent data structures in C++
3 Copyright (c) 2016 Jeff Preshing
5 Distributed under the Simplified BSD License.
6 Original location: https://github.com/preshing/junction
8 This software is distributed WITHOUT ANY WARRANTY; without even the
9 implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 See the LICENSE file for more information.
11 ------------------------------------------------------------------------*/
13 #ifndef JUNCTION_SIMPLEJOBCOORDINATOR_H
14 #define JUNCTION_SIMPLEJOBCOORDINATOR_H
16 #include <junction/Core.h>
17 #include <junction/striped/ConditionBank.h>
21 // It's safe to call everything here from within a Job itself.
22 // In particular, you're allowed to particpate() recursively.
23 // We actually do this in ConcurrentMap_Grampa::publish() when migrating a new flattree.
24 class SimpleJobCoordinator {
29 virtual void run() = 0;
33 JUNCTION_STRIPED_CONDITIONBANK_DEFINE_MEMBER()
34 turf::Atomic<uptr> m_job;
37 SimpleJobCoordinator() : m_job(uptr(NULL)) {
40 Job* loadConsume() const {
41 return (Job*) m_job.load(turf::Consume);
44 void storeRelease(Job* job) {
45 junction::striped::ConditionPair& pair = JUNCTION_STRIPED_CONDITIONBANK_GET(this);
47 turf::LockGuard<turf::Mutex> guard(pair.mutex);
48 m_job.store(uptr(job), turf::Release);
50 pair.condVar.wakeAll();
54 junction::striped::ConditionPair& pair = JUNCTION_STRIPED_CONDITIONBANK_GET(this);
55 uptr prevJob = uptr(NULL);
57 uptr job = m_job.load(turf::Consume);
59 turf::LockGuard<turf::Mutex> guard(pair.mutex);
61 job = m_job.loadNonatomic(); // No concurrent writes inside lock
64 pair.condVar.wait(guard);
69 reinterpret_cast<Job*>(job)->run();
74 void runOne(Job* job) {
75 TURF_ASSERT(job != (Job*) m_job.load(turf::Relaxed));
81 junction::striped::ConditionPair& pair = JUNCTION_STRIPED_CONDITIONBANK_GET(this);
83 turf::LockGuard<turf::Mutex> guard(pair.mutex);
84 m_job.store(1, turf::Release);
86 pair.condVar.wakeAll();
90 } // namespace junction
92 #endif // JUNCTION_SIMPLEJOBCOORDINATOR_H