aboutsummaryrefslogtreecommitdiff
path: root/src/std/thread.c
blob: 64a7f1b604161d20b7485246fc6eda0f6b3cabd5 (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
#include "../../include/std/thread.h"
#include "../../include/std/log.h"
#include <stdio.h>
#include <assert.h>
#include <errno.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);
        self->cancellable = bool_false;
    }
    if(self->destroyable) {
        r2 = pthread_attr_destroy(&self->thread_attr);
        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)
        return result_err;
    if(thread_type != PTHREAD_CREATE_JOINABLE)
        return AMAL_THREAD_NOT_JOINABLE;
    if((result_err = pthread_join(self->thread_id, result)) != 0)
        return result_err == EINVAL ? AMAL_THREAD_NOT_JOINABLE : result_err;
    self->cancellable = bool_false;
    return 0;
}

void amal_mutex_init(amal_mutex *self) {
    pthread_mutex_init(&self->mutex, NULL);
    /* TODO: pthread_mutex_destroy in amal_mutex_deinit */
    self->lock_identifier = NULL;
}

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

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

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

int amal_mutex_lock(amal_mutex *self, const char *lock_identifier) {
    int result;
    result = pthread_mutex_lock(&self->mutex);
    self->lock_identifier = lock_identifier;
    #ifdef AMAL_MUTEX_DEBUG
    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
    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() {
    /* 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() {
    return get_nprocs();
}