-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathversion_tag.h
76 lines (62 loc) · 1.39 KB
/
version_tag.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
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
#ifndef UTIL_VERSION_H__
#define UTIL_VERSION_H__
#include <cassert>
/**
* A version tag to check for changes in multi-threaded environments.
*
* Usage example to prevent dirty reads on a shared variable 'foo':
*
* Thread A:
*
* while (true) {
* tag.lock();
* update(foo);
* tag.unlock();
* }
*
* Thread B:
*
* while (true) {
*
* version_tag::version_type v = tag.get_version()
*
* if (version_tag::is_locked(v))
* continue;
*
* bar = foo;
*
* if (tag.changed(v))
* continue;
*
* // now we know that foo was not changed while we were reading it
* process(bar);
* }
*/
class version_tag {
public:
typedef unsigned char version_type;
version_tag() : _version(0) {}
/**
* Get the current version of the tag.
*/
version_type get_version() { return _version; }
/**
* Mark the version tag as locked.
*/
void lock() { assert(_version%2 == 0); _version++; }
/**
* Unlock the tag and increment the version.
*/
void unlock() { assert(_version%2 == 1); _version++; }
/**
* Check whether the given version is locked.
*/
static bool is_locked(version_type v) { return v%2; }
/**
* Check whether the tag's version changed compared to the given version.
*/
bool changed(version_type v) { return v != _version; }
private:
unsigned char _version;
};
#endif // UTIL_VERSION_H__