forked from idealvin/coost
-
Notifications
You must be signed in to change notification settings - Fork 0
/
thread_test.cc
81 lines (67 loc) · 1.66 KB
/
thread_test.cc
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
80
81
#include "base/unitest.h"
#include "base/thread.h"
namespace test {
#ifdef _WIN32
#define MUTEX_REENTRANT true
#else
#define MUTEX_REENTRANT false
#endif
void thread_run(int* x, SyncEvent* ev) {
*x += 1;
if (ev != NULL) ev->signal();
}
DEF_test(thread) {
DEF_case(sync_event) {
SyncEvent ev;
EXPECT_EQ(ev.wait(0), false);
ev.signal();
EXPECT_EQ(ev.wait(0), true);
EXPECT_EQ(ev.wait(0), false);
SyncEvent em(true, false); // manual-reset
EXPECT_EQ(em.wait(0), false);
em.signal();
EXPECT_EQ(em.wait(0), true);
EXPECT_EQ(em.wait(0), true);
em.reset();
EXPECT_EQ(em.wait(1), false);
}
DEF_case(thread) {
int v = 0;
SyncEvent ev;
Thread(std::bind(thread_run, &v, &ev)).detach();
ev.wait();
EXPECT_EQ(v, 1);
}
DEF_case(task_sched) {
int v = 0;
TaskSched s;
s.run_every(std::bind(thread_run, &v, (SyncEvent*)NULL), 1);
s.stop();
EXPECT_EQ(v, 0);
}
DEF_case(mutex) {
Mutex mtx;
{
MutexGuard g(mtx);
EXPECT_EQ(mtx.try_lock(), MUTEX_REENTRANT);
}
EXPECT_EQ(mtx.try_lock(), true);
EXPECT_EQ(mtx.try_lock(), MUTEX_REENTRANT);
mtx.unlock();
}
DEF_case(gettid) {
EXPECT_NE(current_thread_id(), -1);
}
DEF_case(thread_ptr) {
thread_ptr<int> pi;
EXPECT(!pi);
EXPECT(pi == NULL);
pi.reset(new int(7));
EXPECT_EQ(*pi, 7);
EXPECT_EQ(*pi.get(), 7);
pi = new int(3);
EXPECT_EQ(*pi, 3);
pi.reset();
}
}
} // namespace test