aboutsummaryrefslogtreecommitdiff
path: root/src/std/thread.c
blob: 9b9b76421acb4cebba05a409032912cc27a77bbc (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
#include "../../include/std/thread.h"
#include "../../include/std/log.h"
#include <stdio.h>
#include <assert.h>
#include <errno.h>
#include <string.h>
#include <sys/sysinfo.h>
#include <sys/types.h>
#include <sys/syscall.h>
#include <unistd.h>

static int thread_type_to_system_thread_type(amal_thread_type thread_type) {
    switch(thread_type) {
        case AMAL_THREAD_JOINABLE:    return PTHREAD_CREATE_JOINABLE;
        case AMAL_THREAD_DETACHED:    return PTHREAD_CREATE_DETACHED;
    }
    assert(bool_false);
    return PTHREAD_CREATE_JOINABLE;
}

int amal_thread_create(amal_thread *self, amal_thread_type thread_type, const char *name, AmalThreadCallbackFunc callback_func, void *userdata) {
    int result;
    self->name = name;
    self->thread_id = 0;
    self->cancellable = bool_false;
    self->destroyable = bool_false;
    if((result = pthread_attr_init(&self->thread_attr)) != 0) {
        perror("amal_thread_create");
        return result;
    }
    self->destroyable = bool_true;
    if((result = pthread_attr_setdetachstate(&self->thread_attr, thread_type_to_system_thread_type(thread_type))) != 0) {
        perror("amal_thread_create");
        return result;
    }
    if((result = pthread_create(&self->thread_id, NULL, callback_func, userdata)) != 0) {
        perror("amal_thread_create");
        return result;
    }
    self->cancellable = bool_true;
    return 0;
}

int amal_thread_deinit(amal_thread *self) {
    int r1;
    int r2;
    r1 = 0;
    r2 = 0;

    if(self->cancellable) {
        r1 = pthread_cancel(self->thread_id);
        /* thread deinit on a thread that has not been started shouldn't be an error */
        if(r1 == ESRCH)
            r1 = 0;
        if(r1 != 0)
            amal_log_error("amal_thread_deinit: failed to cancel thread, error: %s", strerror(r1));
        self->cancellable = bool_false;
    }

    if(self->destroyable) {
        r2 = pthread_attr_destroy(&self->thread_attr);
        if(r2 != 0)
            amal_log_error("amal_thread_deinit: failed to destroy thread attributes, error: %s", strerror(r2));
        self->destroyable = bool_false;
    }

    return r1 != 0 ? r1 : r2;
}

int amal_thread_detach(amal_thread *self) {
    int result_err;
    int thread_type;
    if((result_err = pthread_attr_getdetachstate(&self->thread_attr, &thread_type)) != 0)
        return result_err;
    if(thread_type != PTHREAD_CREATE_DETACHED)
        return AMAL_THREAD_ERR;
    if((result_err = pthread_detach(self->thread_id)) != 0)
        return result_err;
    self->cancellable = bool_false;
    return 0;
}

int amal_thread_join(amal_thread *self, void **result) {
    int result_err;
    int thread_type;

    if((result_err = pthread_attr_getdetachstate(&self->thread_attr, &thread_type)) != 0) {
        amal_log_error("amal_thread_join: failed to get detach state, error: %d", result_err);
        return result_err;
    }

    if(thread_type != PTHREAD_CREATE_JOINABLE) {
        amal_log_error("amal_thread_join: thread not joinable type");
        return AMAL_THREAD_NOT_JOINABLE;
    }

    if((result_err = pthread_join(self->thread_id, result)) != 0) {
        /* Joining a thread that is not joinable (has already finished) shouldn't be an error */
        if(result_err == ESRCH)
            goto cleanup;
        amal_log_error("amal_thread_join: failed to join thread %llu, error: %s", self->thread_id, strerror(result_err));
        return result_err == EINVAL ? AMAL_THREAD_NOT_JOINABLE : result_err;
    }

    cleanup:
    self->cancellable = bool_false;
    return 0;
}

int amal_mutex_init(amal_mutex *self) {
    #ifdef AMAL_MUTEX_DEBUG
    self->lock_identifier = NULL;
    #endif
    self->locked = bool_false;
    self->owner_thread = 0;
    return pthread_mutex_init(&self->mutex, NULL);
}

void amal_mutex_deinit(amal_mutex *self) {
    pthread_mutex_destroy(&self->mutex);
}

static long amal_process_get_id(void) {
    return getpid();
}

static long amal_thread_get_id(void) {
    return syscall(SYS_gettid);
}

int amal_mutex_lock(amal_mutex *self, const char *lock_identifier) {
    int result;
    result = pthread_mutex_lock(&self->mutex);
    self->owner_thread = pthread_self();
    self->locked = bool_true;
    (void)lock_identifier;
    #ifdef AMAL_MUTEX_DEBUG
    self->lock_identifier = lock_identifier;
    if(result == 0 && self->lock_identifier) {
        amal_log_debug("amal_mutex_lock: mutex locked by thread %lu (%s), identification: %s", 
            amal_thread_get_id(),
            amal_thread_is_main() ? "main" : "not main",
            self->lock_identifier ? self->lock_identifier : "none");
    }
    #endif
    return result;
}

int amal_mutex_unlock(amal_mutex *self) {
    int result;
    #ifdef AMAL_MUTEX_DEBUG
    const char *identifier;
    identifier = self->lock_identifier;
    #endif
    /* No-op if mutex isn't locked or if another thread owns the mutex lock */
    if(!self->locked || pthread_equal(self->owner_thread, pthread_self()) == 0)
        return 0;
    self->locked = bool_false;
    self->owner_thread = 0;
    result = pthread_mutex_unlock(&self->mutex);
    #ifdef AMAL_MUTEX_DEBUG
    if(result == 0 && identifier) {
        amal_log_debug("amal_mutex_unlock: mutex unlocked by thread %lu (%s), identification: %s", 
            amal_thread_get_id(),
            amal_thread_is_main() ? "main" : "not main",
            identifier ? identifier : "none");
    }
    #endif
    return result;
}

void amal_mutex_tryunlock(amal_mutex *self) {
    ignore_result_int(amal_mutex_unlock(self));
}

bool amal_thread_is_main(void) {
    /* TODO: This only works for linux, use equivalent functions on other platforms */
    return amal_thread_get_id() == amal_process_get_id();
}

int amal_get_usable_thread_count(void) {
    return get_nprocs();
}