blob: f2a59c23b0b99d313657d2d0b3f938ffaa7aaf8a (
plain)
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
|
#include <BH/Thread.h>
#include <BH/Unit.h>
#include <time.h>
BH_UNIT_TEST(Mutex)
{
BH_Mutex *mutex;
BH_VERIFY((mutex = BH_MutexNew()) != NULL);
BH_VERIFY(BH_MutexLock(mutex) == BH_OK);
BH_VERIFY(BH_MutexTryLock(mutex) != BH_OK);
BH_VERIFY(BH_MutexUnlock(mutex) == BH_OK);
BH_VERIFY(BH_MutexTryLock(mutex) == BH_OK);
BH_VERIFY(BH_MutexUnlock(mutex) == BH_OK);
BH_MutexFree(mutex);
return 0;
}
BH_UNIT_TEST(Semaphore)
{
BH_Semaphore *semaphore;
time_t start;
start = time(NULL);
BH_VERIFY((semaphore = BH_SemaphoreNew(1)) != NULL);
BH_VERIFY(BH_SemaphoreWait(semaphore) == BH_OK);
BH_VERIFY(BH_SemaphoreTryWait(semaphore) != BH_OK);
BH_VERIFY(BH_SemaphoreWaitFor(semaphore, 5000) == BH_TIMEOUT);
BH_VERIFY(BH_SemaphorePost(semaphore) == BH_OK);
BH_VERIFY(BH_SemaphoreTryWait(semaphore) == BH_OK);
BH_VERIFY(BH_SemaphorePost(semaphore) == BH_OK);
BH_VERIFY(BH_SemaphoreWaitFor(semaphore, 5000) == BH_OK);
BH_VERIFY(BH_SemaphorePost(semaphore) == BH_OK);
BH_VERIFY(time(NULL) - start >= 5);
BH_SemaphoreFree(semaphore);
return 0;
}
BH_UNIT_TEST(Condition)
{
BH_Condition *condition;
BH_Mutex *mutex;
time_t start;
start = time(NULL);
BH_VERIFY((condition = BH_ConditionNew()) != NULL);
BH_VERIFY((mutex = BH_MutexNew()) != NULL);
BH_VERIFY(BH_MutexLock(mutex) == BH_OK);
BH_VERIFY(BH_ConditionWaitFor(condition, mutex, 5000) == BH_TIMEOUT);
BH_VERIFY(BH_ConditionSignal(condition) == BH_OK);
BH_VERIFY(BH_ConditionBroadcast(condition) == BH_OK);
BH_VERIFY(BH_MutexUnlock(mutex) == BH_OK);
BH_VERIFY(time(NULL) - start >= 5);
BH_ConditionFree(condition);
BH_MutexFree(mutex);
return 0;
}
static int ThreadCallback(void *data)
{
*(int *)data = 12345;
return 0;
}
BH_UNIT_TEST(Thread)
{
BH_Thread *thread;
int data = 0;
BH_VERIFY((thread = BH_ThreadNew(0, ThreadCallback, &data)) != NULL);
BH_VERIFY(BH_ThreadJoin(thread) == BH_OK);
BH_VERIFY(data == 12345);
return 0;
}
BH_UNIT_TEST(Spinlock)
{
int lock = 0;
BH_SpinlockLock(&lock);
BH_VERIFY(BH_SpinlockTryLock(&lock) != BH_OK);
BH_SpinlockUnlock(&lock);
BH_VERIFY(BH_SpinlockTryLock(&lock) == BH_OK);
return 0;
}
int main(int argc, char **argv)
{
(void)argc;
(void)argv;
BH_UNIT_ADD(Mutex);
BH_UNIT_ADD(Semaphore);
BH_UNIT_ADD(Condition);
BH_UNIT_ADD(Thread);
BH_UNIT_ADD(Spinlock);
return BH_UnitRun();
}
|