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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
|
/*
* Copyright (c) 2021, Brian Gianforcaro <bgianf@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibPthread/pthread.h>
#include <LibTest/TestCase.h>
#include <errno.h>
#include <unistd.h>
TEST_CASE(spin_init_process_scope)
{
{
pthread_spinlock_t lock {};
auto result = pthread_spin_init(&lock, PTHREAD_SCOPE_PROCESS);
EXPECT_EQ(0, result);
result = pthread_spin_destroy(&lock);
EXPECT_EQ(0, result);
}
{
pthread_spinlock_t garbage_lock { 0x1337 };
auto result = pthread_spin_init(&garbage_lock, PTHREAD_SCOPE_PROCESS);
EXPECT_EQ(0, result);
result = pthread_spin_destroy(&garbage_lock);
EXPECT_EQ(0, result);
}
}
TEST_CASE(spin_init_system_scope)
{
pthread_spinlock_t lock {};
auto result = pthread_spin_init(&lock, PTHREAD_SCOPE_SYSTEM);
EXPECT_EQ(0, result);
pthread_spinlock_t garbage_lock { 0x99999 };
result = pthread_spin_init(&garbage_lock, PTHREAD_SCOPE_PROCESS);
EXPECT_EQ(0, result);
}
TEST_CASE(spin_lock)
{
pthread_spinlock_t lock {};
auto result = pthread_spin_lock(&lock);
EXPECT_EQ(0, result);
// We should detect that this thread already holds this lock.
result = pthread_spin_lock(&lock);
EXPECT_EQ(EDEADLK, result);
}
TEST_CASE(spin_try_lock)
{
{
pthread_spinlock_t lock {};
auto result = pthread_spin_trylock(&lock);
EXPECT_EQ(0, result);
result = pthread_spin_unlock(&lock);
EXPECT_EQ(0, result);
}
{
pthread_spinlock_t lock {};
auto result = pthread_spin_trylock(&lock);
EXPECT_EQ(0, result);
// We should detect that this thread already holds the lock.
result = pthread_spin_trylock(&lock);
EXPECT_EQ(EBUSY, result);
}
}
static void lock_from_different_thread(pthread_spinlock_t* lock)
{
pthread_t thread_id {};
auto result = pthread_create(
&thread_id, nullptr, [](void* param) -> void* {
auto lock = (pthread_spinlock_t*)param;
pthread_spin_lock(lock);
return nullptr;
},
lock);
EXPECT_EQ(0, result);
result = pthread_join(thread_id, nullptr);
EXPECT_EQ(0, result);
}
TEST_CASE(spin_unlock)
{
{
pthread_spinlock_t lock {};
auto result = pthread_spin_lock(&lock);
EXPECT_EQ(0, result);
result = pthread_spin_unlock(&lock);
EXPECT_EQ(0, result);
}
{
pthread_spinlock_t lock {};
lock_from_different_thread(&lock);
auto result = pthread_spin_unlock(&lock);
EXPECT_EQ(EPERM, result);
}
}
TEST_CASE(spin_destroy)
{
{
pthread_spinlock_t lock {};
auto result = pthread_spin_lock(&lock);
EXPECT_EQ(0, result);
result = pthread_spin_destroy(&lock);
EXPECT_EQ(EBUSY, result);
result = pthread_spin_unlock(&lock);
EXPECT_EQ(0, result);
}
{
pthread_spinlock_t lock {};
lock_from_different_thread(&lock);
auto result = pthread_spin_destroy(&lock);
EXPECT_EQ(EBUSY, result);
}
}
|