summaryrefslogtreecommitdiff
path: root/Userland/Utilities/test-pthread.cpp
blob: 2b11604c0de30e624af5c9920eb016ec57a203e6 (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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*
 * Copyright (c) 2020, Sergey Bugaev <bugaevc@serenityos.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <AK/Assertions.h>
#include <LibMain/Main.h>
#include <LibThreading/Thread.h>
#include <errno.h>
#include <pthread.h>
#include <semaphore.h>
#include <unistd.h>

static ErrorOr<void> test_once()
{
    constexpr size_t threads_count = 10;

    static Vector<int> v;
    v.clear();
    pthread_once_t once = PTHREAD_ONCE_INIT;
    Vector<NonnullRefPtr<Threading::Thread>, threads_count> threads;

    for (size_t i = 0; i < threads_count; i++) {
        threads.unchecked_append(TRY(Threading::Thread::try_create([&] {
            return pthread_once(&once, [] {
                v.append(35);
                sleep(1);
            });
        })));
        threads.last()->start();
    }
    // clang-format off
    // It wants to put [[maybe_unused]] on its own line, for some reason.
    for (auto& thread : threads)
        [[maybe_unused]] auto res = thread->join();
    // clang-format on

    VERIFY(v.size() == 1);

    return {};
}

static ErrorOr<void> test_mutex()
{
    constexpr size_t threads_count = 10;
    constexpr size_t num_times = 100;

    Vector<int> v;
    Vector<NonnullRefPtr<Threading::Thread>, threads_count> threads;
    pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;

    for (size_t i = 0; i < threads_count; i++) {
        threads.unchecked_append(TRY(Threading::Thread::try_create([&] {
            for (size_t j = 0; j < num_times; j++) {
                pthread_mutex_lock(&mutex);
                v.append(35);
                sched_yield();
                pthread_mutex_unlock(&mutex);
                sched_yield();
            }
            return 0;
        })));
        threads.last()->start();
    }
    // clang-format off
    // It wants to put [[maybe_unused]] on its own line, for some reason.
    for (auto& thread : threads)
        [[maybe_unused]] auto res = thread->join();
    // clang-format on

    VERIFY(v.size() == threads_count * num_times);
    VERIFY(pthread_mutex_trylock(&mutex) == 0);
    VERIFY(pthread_mutex_trylock(&mutex) == EBUSY);

    return {};
}

static ErrorOr<void> test_semaphore_as_lock()
{
    constexpr size_t threads_count = 10;
    constexpr size_t num_times = 100;

    Vector<int> v;
    Vector<NonnullRefPtr<Threading::Thread>, threads_count> threads;
    sem_t semaphore;
    sem_init(&semaphore, 0, 1);

    for (size_t i = 0; i < threads_count; i++) {
        threads.unchecked_append(TRY(Threading::Thread::try_create([&] {
            for (size_t j = 0; j < num_times; j++) {
                sem_wait(&semaphore);
                v.append(35);
                sched_yield();
                sem_post(&semaphore);
                sched_yield();
            }
            return 0;
        })));
        threads.last()->start();
    }
    // clang-format off
    // It wants to put [[maybe_unused]] on its own line, for some reason.
    for (auto& thread : threads)
        [[maybe_unused]] auto res = thread->join();
    // clang-format on

    VERIFY(v.size() == threads_count * num_times);
    VERIFY(sem_trywait(&semaphore) == 0);
    VERIFY(sem_trywait(&semaphore) == -1);
    VERIFY(errno == EAGAIN);

    return {};
}

static ErrorOr<void> test_semaphore_as_event()
{
    Vector<int> v;
    sem_t semaphore;
    sem_init(&semaphore, 0, 0);

    auto reader = TRY(Threading::Thread::try_create([&] {
        sem_wait(&semaphore);
        VERIFY(v.size() == 1);
        return 0;
    }));
    reader->start();

    auto writer = TRY(Threading::Thread::try_create([&] {
        sched_yield();
        v.append(35);
        sem_post(&semaphore);
        return 0;
    }));
    writer->start();

    [[maybe_unused]] auto r1 = reader->join();
    [[maybe_unused]] auto r2 = writer->join();

    VERIFY(sem_trywait(&semaphore) == -1);
    VERIFY(errno == EAGAIN);

    return {};
}

static ErrorOr<void> test_semaphore_nonbinary()
{
    constexpr size_t num = 5;
    constexpr size_t threads_count = 10;
    constexpr size_t num_times = 100;

    Vector<NonnullRefPtr<Threading::Thread>, threads_count> threads;
    sem_t semaphore;
    sem_init(&semaphore, 0, num);

    Atomic<u32, AK::memory_order_relaxed> value = 0;
    Atomic<bool, AK::memory_order_relaxed> seen_more_than_two = false;

    for (size_t i = 0; i < threads_count; i++) {
        threads.unchecked_append(TRY(Threading::Thread::try_create([&] {
            for (size_t j = 0; j < num_times; j++) {
                sem_wait(&semaphore);
                u32 v = 1 + value.fetch_add(1);
                VERIFY(v <= num);
                if (v > 2)
                    seen_more_than_two.store(true);
                sched_yield();
                value.fetch_sub(1);
                sem_post(&semaphore);
            }
            return 0;
        })));
        threads.last()->start();
    }
    // clang-format off
    // It wants to put [[maybe_unused]] on its own line, for some reason.
    for (auto& thread : threads)
        [[maybe_unused]] auto res = thread->join();
    // clang-format on

    VERIFY(value.load() == 0);
    VERIFY(seen_more_than_two.load());
    for (size_t i = 0; i < num; i++) {
        VERIFY(sem_trywait(&semaphore) == 0);
    }
    VERIFY(sem_trywait(&semaphore) == -1);
    VERIFY(errno == EAGAIN);

    return {};
}

ErrorOr<int> serenity_main(Main::Arguments)
{
    TRY(test_once());
    TRY(test_mutex());

    TRY(test_semaphore_as_lock());
    TRY(test_semaphore_as_event());
    TRY(test_semaphore_nonbinary());

    return 0;
}