forked from includeos/IncludeOS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathservice.cpp
More file actions
79 lines (62 loc) · 1.71 KB
/
service.cpp
File metadata and controls
79 lines (62 loc) · 1.71 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
76
77
78
79
#include <os>
#include <cassert>
#include <smp>
void Service::start()
{
INFO("service", "Testing spinlocks!");
Spinlock s;
INFO2("Basic lock/unlock tests");
if (s.is_locked()) {
os::panic("Lock was locked at init");
}
s.lock();
if (!s.is_locked()) {
os::panic("Lock was not locked after call to lock()");
}
s.unlock();
if (s.is_locked()) {
os::panic("Lock was locked after call to unlock()");
}
INFO2("Testing try_lock*");
if (!s.try_lock()) {
os::panic("try_lock() didn't succeed");
}
// This will timeout
INFO2("Waiting for try_lock_for to timeout");
{
auto t0 = std::chrono::high_resolution_clock::now();
if (s.try_lock_for(std::chrono::milliseconds(250))) {
os::panic("try_lock_for() got lock, expected timeout");
}
auto t1 = std::chrono::high_resolution_clock::now();
if (t1 - t0 < std::chrono::milliseconds(250)) {
os::panic("try_lock_for() returned earlier than timeout");
}
}
s.unlock();
// We should get this immediately
INFO2("Testing try_lock_for on unlocked lock");
{
auto t0 = std::chrono::high_resolution_clock::now();
if (!s.try_lock_for(std::chrono::seconds(3))) {
os::panic("try_lock_for didn't get lock");
}
auto t1 = std::chrono::high_resolution_clock::now();
if (t1 - t0 > std::chrono::milliseconds(250)) {
os::panic("try_lock_for() didn't return immediately when lock was unlocked");
}
}
s.unlock();
// std::lock_guard test
INFO2("Testing std::lock_guard");
{
std::lock_guard<Spinlock> lock(s);
if (!s.is_locked()) {
os::panic("std::lock_guard didn't lock");
}
}
if (s.is_locked()) {
os::panic("std::lock_guard didn't unlock");
}
exit(0);
}