forked from xcore/tool_axe
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Lock.h
44 lines (36 loc) · 1005 Bytes
/
Lock.h
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
// 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 _Lock_h_
#define _Lock_h_
#include <queue>
#include "Resource.h"
class Lock : public Resource {
private:
/// Is the lock currently held by a thread?
bool held;
/// Paused threads.
std::queue<Thread *> threads;
public:
Lock() : Resource(RES_TYPE_LOCK) {}
bool alloc(Thread &master)
{
assert(!isInUse() && "Trying to allocate in use lock");
setInUse(true);
held = false;
while (!threads.empty()) {
threads.pop();
}
return true;
}
bool free()
{
// TODO what if there are still threads paused on the lock?
setInUse(false);
return true;
}
ResOpResult in(Thread &thread, ticks_t time, uint32_t &value);
ResOpResult out(Thread &thread, uint32_t value, ticks_t time);
};
#endif // _Lock_h_