forked from xcore/tool_axe
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSynchroniser.cpp
More file actions
75 lines (70 loc) · 1.66 KB
/
Synchroniser.cpp
File metadata and controls
75 lines (70 loc) · 1.66 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
// 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/>
#include "Synchroniser.h"
#include "Core.h"
ticks_t Synchroniser::MaxThreadTime() const
{
ticks_t time = threads[0]->time;
for (unsigned i = 1; i < NumThreads; i++) {
if (threads[i]->time > time)
time = threads[i]->time;
}
return time;
}
Synchroniser::SyncResult Synchroniser::
sync(Thread &thread, bool isMaster)
{
if (getNumPaused() + 1 < getNumThreads()) {
// Pause the current thread
NumPaused++;
if (!isMaster)
thread.setSSync(true);
return SYNC_DESCHEDULE;
}
// Wakeup / kill threads
ticks_t newTime = MaxThreadTime();
NumPaused = 0;
if (!join) {
for (unsigned i = 0; i < NumThreads; i++) {
threads[i]->time = newTime;
if (threads[i] != &thread) {
if (i > 0)
threads[i]->setSSync(false);
threads[i]->pc++;
threads[i]->schedule();
}
}
return SYNC_CONTINUE;
}
SyncResult result;
threads[0]->time = newTime;
if (isMaster) {
result = SYNC_CONTINUE;
for (unsigned i = 1; i < NumThreads; i++) {
threads[i]->free();
}
} else {
master().schedule();
result = SYNC_KILL;
for (unsigned i = 1; i < NumThreads; i++) {
if (threads[i] != &thread) {
threads[i]->free();
}
}
}
join = false;
NumThreads = 1;
return result;
}
Synchroniser::SyncResult Synchroniser::
mjoin(Thread &thread)
{
join = true;
return sync(thread, true);
}
void Synchroniser::cancel()
{
NumPaused--;
}