-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquem.hpp
71 lines (67 loc) · 937 Bytes
/
quem.hpp
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
#include "thread.hpp"
namespace doot{
//threadsafe queue
tplt class quem{
mutex mut;
cons<T*>* car;
public:
void push(T* e){
if(!e){
bad("quem::push( NULL )");
re;
}
auto* n= new cons<T*>{e,{null}};
mut.lock();
if(auto* t= tail(car))
t->b= n;
else
car= n;
mut.locknt();
}
maybe<T> pull(){
mut.lock();
if(!car){
mut.locknt();
nope;
}
may<T > a= {car->a};
cons<T*>* b= car->b;
delete car;
car= b;
mut.locknt();
re a;
}
bool peek(){//1+
bool r;
mut.lock();
r= !!car;
mut.locknt();
retr;
}
bool peek_next(){//2+
bool r;
mut.lock();
r= car && car->b;
mut.locknt();
retr;
}
maybe<T> dump(){//discards all and returns last
mut.lock();
if(!car){
mut.locknt();
nope;
}
while(ato* n= car->b){//next
delete car->a;
delete car;
car= n;
}
T* r= car->a;//disown
ass(r);
delete car;
car= null;
mut.locknt();
re {r};
}
};
}