forked from xcore/tool_axe
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSynchroniser.h
More file actions
80 lines (67 loc) · 1.7 KB
/
Synchroniser.h
File metadata and controls
80 lines (67 loc) · 1.7 KB
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
// Copyright (c) 2011, Richard Osborne, All rights reserved
// This software is freely distributable under a derivative of the
// University of Illinois/NCSA Open Source License posted in
// LICENSE.txt and at <http://github.xcore.com/>
#ifndef _Synchroniser_h_
#define _Synchroniser_h_
#include "Resource.h"
#include <cassert>
class Synchroniser : public Resource {
public:
enum SyncResult {
SYNC_CONTINUE,
SYNC_DESCHEDULE,
SYNC_KILL
};
private:
/// Number of threads.
unsigned NumThreads;
/// List of threads. The master thread is always at the first index.
Thread *threads[NUM_THREADS];
/// Number of paused threads
unsigned NumPaused;
bool join;
/// Returns the time of thread with the latest time.
ticks_t MaxThreadTime() const;
SyncResult sync(Thread &thread, bool isMaster);
public:
Synchroniser() : Resource(RES_TYPE_SYNC) {}
bool alloc(Thread &master)
{
assert(!isInUse() && "Trying to allocate in use synchroniser");
setInUse(true);
NumThreads = 1;
threads[0] = &master;
NumPaused = 0;
join = false;
return true;
}
void addChild(Thread &thread)
{
assert(NumThreads + 1 <= NUM_THREADS && "Too many threads");
threads[NumThreads++] = &thread;
NumPaused++;
}
bool free()
{
setInUse(false);
return true;
}
unsigned getNumThreads() const
{
return NumThreads;
}
unsigned getNumPaused() const
{
return NumPaused;
}
Thread &master()
{
return *threads[0];
}
SyncResult ssync(Thread &thread) { return sync(thread, false); }
SyncResult msync(Thread &thread) { return sync(thread, true); }
SyncResult mjoin(Thread &thread);
void cancel();
};
#endif // _Synchroniser_h_