summaryrefslogtreecommitdiff
path: root/Tests/Kernel/pthread-cond-timedwait-example.cpp
blob: c2bd2b62c584db2b861f322e7a3d80abd8ccfd88 (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
/*
 * Copyright (c) 2018-2020, the SerenityOS developers.
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <cassert>
#include <cstring>
#include <ctime>
#include <errno.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

struct worker_t {
    char const* name;
    int count;
    pthread_t thread;
    pthread_mutex_t lock;
    pthread_cond_t cond;
    long int wait_time;
};

static void* run_worker(void* args)
{
    struct timespec time_to_wait = { 0, 0 };
    worker_t* worker = (worker_t*)args;
    worker->count = 0;

    while (worker->count < 25) {
        time_to_wait.tv_sec = time(nullptr) + worker->wait_time;
        pthread_mutex_lock(&worker->lock);
        int rc = pthread_cond_timedwait(&worker->cond, &worker->lock, &time_to_wait);

        // Validate return code is always timed out.
        assert(rc == -1);
        assert(errno == ETIMEDOUT);

        worker->count++;
        printf("Increase worker[%s] count to [%d]\n", worker->name, worker->count);
        pthread_mutex_unlock(&worker->lock);
    }

    return nullptr;
}

static void init_worker(worker_t* worker, char const* name, long int wait_time)
{
    worker->name = name;
    worker->wait_time = wait_time;

    pthread_attr_t attr;
    pthread_attr_init(&attr);
    pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);

    pthread_mutex_init(&worker->lock, nullptr);
    pthread_cond_init(&worker->cond, nullptr);
    pthread_create(&worker->thread, &attr, &run_worker, (void*)worker);

    pthread_attr_destroy(&attr);
}

int main()
{
    worker_t worker_a;
    init_worker(&worker_a, "A", 2L);

    worker_t worker_b;
    init_worker(&worker_b, "B", 4L);

    pthread_join(worker_a.thread, nullptr);
    pthread_join(worker_b.thread, nullptr);

    return EXIT_SUCCESS;
}