aboutsummaryrefslogtreecommitdiff
path: root/src/std/thread_pool.c
blob: 8f6e18094715f3926266d70ef3fefd1bdeb520ad (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
#include "../../include/std/thread_pool.h"
#include "../../include/std/alloc.h"
#include "../../include/std/mem.h"
#include "../../include/std/log.h"

/* Sets @result to NULL if there are no available tasks */
static CHECK_RESULT int thread_pool_take_task(amal_thread_pool *self, amal_thread_pool_task **result) {
    *result = NULL;
    cleanup_if_error(amal_mutex_lock(&self->task_select_mutex, "thread_pool_take_task"));
    if(self->num_finished_queued_tasks < (int)buffer_get_size(&self->queued_tasks, amal_thread_pool_task) && !self->dead)
        *result = buffer_get(&self->queued_tasks, self->num_finished_queued_tasks, sizeof(amal_thread_pool_task));
    cleanup:
    amal_mutex_tryunlock(&self->task_select_mutex);
    return 0;
}

static void* thread_pool_thread_callback(void *userdata) {
    amal_thread_pool_callback_data *thread_pool_data = userdata;
    if(thread_pool_data->thread_pool->dead)
        goto cleanup;

    if(thread_pool_data->callback(thread_pool_data->userdata) != 0) {
        thread_pool_mark_dead(thread_pool_data->thread_pool);
        goto cleanup;
    }

    for(;;) {
        amal_thread_pool_task *new_task;
        cleanup_if_error(thread_pool_take_task(thread_pool_data->thread_pool, &new_task));
        if(!new_task)
            break;

        if(new_task->callback(new_task->userdata) != 0) {
            thread_pool_mark_dead(thread_pool_data->thread_pool);
            goto cleanup;
        }
    }

    cleanup:
    thread_pool_data->thread_pool_thread->status = THREAD_POOL_THREAD_STATUS_IDLE;
    am_free(thread_pool_data);
    return NULL;
}

static void thread_pool_thread_init(amal_thread_pool_thread *self) {
    am_memset(&self->thread, 0, sizeof(self->thread));
    self->status = THREAD_POOL_THREAD_STATUS_NEW;
}

static void thread_pool_thread_deinit(amal_thread_pool_thread *self) {
    ignore_result_int(amal_thread_deinit(&self->thread));
}

static CHECK_RESULT int thread_pool_thread_start(amal_thread_pool_thread *self, amal_thread_pool *thread_pool, amal_thread_job_callback callback, void *userdata) {
    amal_thread_pool_callback_data *callback_data;
    return_if_error(am_malloc(sizeof(amal_thread_pool_callback_data), (void**)&callback_data));
    callback_data->thread_pool = thread_pool;
    callback_data->thread_pool_thread = self;
    callback_data->callback = callback;
    callback_data->userdata = userdata;

    return_if_error(amal_thread_deinit(&self->thread));
    return_if_error(amal_thread_create(&self->thread, AMAL_THREAD_JOINABLE, "thread_pool_thread_start", thread_pool_thread_callback, callback_data));
    self->status = THREAD_POOL_THREAD_STATUS_RUNNING;
    return 0;
}

static CHECK_RESULT int thread_pool_thread_join(amal_thread_pool_thread *self) {
    if(self->status == THREAD_POOL_THREAD_STATUS_NEW)
        return 0;
    return amal_thread_join(&self->thread, NULL);
}

int thread_pool_init(amal_thread_pool *self, int num_threads) {
    int i;
    self->num_threads = num_threads != 0 ? num_threads : amal_get_usable_thread_count();
    if(self->num_threads == 0) {
        amal_log_warning("Unable to get the number of threads available on the system, using 1 thread.");
        self->num_threads = 1;
    }

    self->dead = bool_false;
    self->num_finished_queued_tasks = 0;
    self->threads = NULL;

    ignore_result_int(buffer_init(&self->queued_tasks, NULL));
    return_if_error(amal_mutex_init(&self->task_select_mutex));
    cleanup_if_error(am_malloc(self->num_threads * sizeof(amal_thread_pool_thread), (void**)&self->threads));
    for(i = 0; i < self->num_threads; ++i)
        thread_pool_thread_init(&self->threads[i]);
    return 0;

    cleanup:
    am_free(self->threads);
    self->num_threads = 0;
    return -1;
}

void thread_pool_deinit(amal_thread_pool *self) {
    if(self->threads) {
        int i;
        for(i = 0; i < self->num_threads; ++i)
            thread_pool_thread_deinit(&self->threads[i]);
        am_free(self->threads);
    }
    amal_mutex_deinit(&self->task_select_mutex);
    buffer_deinit(&self->queued_tasks);
}

int thread_pool_add_task(amal_thread_pool *self, amal_thread_job_callback callback, void *userdata) {
    int i;
    bool found_available_thread = bool_false;
    int result = -1;

    if(self->dead)
        return result;

    cleanup_if_error(amal_mutex_lock(&self->task_select_mutex, "thread_pool_add_task"));
    for(i = 0; i < self->num_threads; ++i) {
        amal_thread_pool_thread *thread = &self->threads[i];
        if(thread->status != THREAD_POOL_THREAD_STATUS_RUNNING) {
            cleanup_if_error(thread_pool_thread_start(thread, self, callback, userdata));
            found_available_thread = bool_true;
            break;
        }
    }

    if(!found_available_thread) {
        amal_thread_pool_task task;
        task.callback = callback;
        task.userdata = userdata;
        cleanup_if_error(buffer_append(&self->queued_tasks, &task, sizeof(task)));
    }

    result = 0;
    cleanup:
    amal_mutex_tryunlock(&self->task_select_mutex);
    return result;
}

bool thread_pool_join_all_tasks(amal_thread_pool *self) {
    bool died;
    for(;;) {
        /*
            Joining running threads. After checking one running thread another one might start up,
            so this is mostly to wait for threads to finish and to sleep without doing work.
            The check after that (thread_pool_check_threads_finished) check that all threads have finished correctly
        */
        int i;
        bool finished = bool_true;
        for(i = 0; i < self->num_threads; ++i) {
            amal_thread_pool_thread_status thread_status;
            if(amal_mutex_lock(&self->task_select_mutex, "thread_pool_join_all_tasks") != 0)
                thread_pool_mark_dead(self);
            thread_status = self->threads[i].status;
            amal_mutex_tryunlock(&self->task_select_mutex);
            /* TODO: What to do if join fails? */
            switch(thread_status) {
                case THREAD_POOL_THREAD_STATUS_NEW:
                    break;
                case THREAD_POOL_THREAD_STATUS_RUNNING:
                    finished = bool_false;
                    /* fallthrough */
                case THREAD_POOL_THREAD_STATUS_IDLE:
                    ignore_result_int(thread_pool_thread_join(&self->threads[i]));
                    break;
            }
        }

        if(finished)
            break;
    }

    died = self->dead;
    self->dead = bool_false;
    buffer_clear(&self->queued_tasks);
    self->num_finished_queued_tasks = 0;
    return !died;
}

void thread_pool_mark_dead(amal_thread_pool *self) {
    self->dead = bool_true;
}

BufferView thread_pool_get_threads(amal_thread_pool *self) {
    return create_buffer_view((const char*)self->threads, self->num_threads * sizeof(amal_thread_pool_thread));
}