-
Notifications
You must be signed in to change notification settings - Fork 1
/
atomic.hh
78 lines (63 loc) · 1.3 KB
/
atomic.hh
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
#ifndef __YAAL_ATOMIC__
#define __YAAL_ATOMIC__ 1
#include "requirements.hh"
#ifdef __YAAL__
#ifdef __AVR__
#include <avr/interrupt.h> // SREG, sei, cli
namespace yaal {
/* Make block atomic, and restore interrupt state after.
{
Atomic block;
...
}
is same as
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
...
}
*/
class Atomic {
uint8_t state;
public:
YAAL_INLINE("Atomic")
Atomic() {
state = AVR_STATUS_REG; // SREG
cli();
}
YAAL_INLINE("~Atomic")
~Atomic() {
AVR_STATUS_REG = state; // SREG
}
};
/* Make block nonatomic/interruptable
{
Interruptable block;
...
}
is same as
NONATOMIC_BLOCK(NONATOMIC_RESTORESTATE) {
...
}
*/
class Interruptable {
uint8_t state;
public:
YAAL_INLINE("Interruptable")
Interruptable() {
state = AVR_STATUS_REG; // SREG
sei();
}
YAAL_INLINE("~Interruptable")
~Interruptable() {
AVR_STATUS_REG = state; // SREG
}
};
}
#else
// Dummy implementation for tests
namespace yaal {
class Atomic {};
class Interruptable {};
}
#endif
#endif
#endif