-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathbackground-thread.h
155 lines (142 loc) · 4.39 KB
/
background-thread.h
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
#ifndef BACKGROUND_THREAD_H
#define BACKGROUND_THREAD_H
#include <pthread.h>
#include <stdlib.h>
#include <string.h>
#include "assert.h"
#include "debug.h"
enum {
GC_BACKGROUND_TASK_START = 0,
GC_BACKGROUND_TASK_MIDDLE = 100,
GC_BACKGROUND_TASK_END = 200
};
struct gc_background_task {
int id;
int priority;
void (*run)(void *data);
void *data;
};
enum gc_background_thread_state {
GC_BACKGROUND_THREAD_STARTING,
GC_BACKGROUND_THREAD_RUNNING,
GC_BACKGROUND_THREAD_STOPPING
};
struct gc_background_thread {
size_t count;
size_t capacity;
struct gc_background_task *tasks;
int next_id;
enum gc_background_thread_state state;
pthread_t thread;
pthread_mutex_t lock;
pthread_cond_t cond;
};
static void*
gc_background_thread(void *data) {
struct gc_background_thread *thread = data;
pthread_mutex_lock(&thread->lock);
while (thread->state == GC_BACKGROUND_THREAD_STARTING)
pthread_cond_wait(&thread->cond, &thread->lock);
struct timespec ts;
if (clock_gettime(CLOCK_REALTIME, &ts)) {
perror("background thread: failed to get time!");
return NULL;
}
while (thread->state == GC_BACKGROUND_THREAD_RUNNING) {
ts.tv_sec += 1;
pthread_cond_timedwait(&thread->cond, &thread->lock, &ts);
if (thread->state == GC_BACKGROUND_THREAD_RUNNING)
for (size_t i = 0; i < thread->count; i++)
thread->tasks[i].run(thread->tasks[i].data);
}
pthread_mutex_unlock(&thread->lock);
return NULL;
}
static struct gc_background_thread*
gc_make_background_thread(void) {
struct gc_background_thread *thread;
thread = malloc(sizeof(*thread));
if (!thread)
GC_CRASH();
memset(thread, 0, sizeof(*thread));
thread->tasks = NULL;
thread->count = 0;
thread->capacity = 0;
thread->state = GC_BACKGROUND_THREAD_STARTING;
pthread_mutex_init(&thread->lock, NULL);
pthread_cond_init(&thread->cond, NULL);
if (pthread_create(&thread->thread, NULL, gc_background_thread, thread)) {
perror("spawning background thread failed");
GC_CRASH();
}
return thread;
}
static void
gc_background_thread_start(struct gc_background_thread *thread) {
pthread_mutex_lock(&thread->lock);
GC_ASSERT_EQ(thread->state, GC_BACKGROUND_THREAD_STARTING);
thread->state = GC_BACKGROUND_THREAD_RUNNING;
pthread_mutex_unlock(&thread->lock);
pthread_cond_signal(&thread->cond);
}
static int
gc_background_thread_add_task(struct gc_background_thread *thread,
int priority, void (*run)(void *data),
void *data) {
pthread_mutex_lock(&thread->lock);
if (thread->count == thread->capacity) {
size_t new_capacity = thread->capacity * 2 + 1;
struct gc_background_task *new_tasks =
realloc(thread->tasks, sizeof(struct gc_background_task) * new_capacity);
if (!new_tasks) {
perror("ran out of space for background tasks!");
GC_CRASH();
}
thread->capacity = new_capacity;
thread->tasks = new_tasks;
}
size_t insert = 0;
for (; insert < thread->count; insert++) {
if (priority < thread->tasks[insert].priority)
break;
}
size_t bytes_to_move =
(thread->count - insert) * sizeof(struct gc_background_task);
memmove(&thread->tasks[insert + 1], &thread->tasks[insert], bytes_to_move);
int id = thread->next_id++;
thread->tasks[insert].id = id;
thread->tasks[insert].priority = priority;
thread->tasks[insert].run = run;
thread->tasks[insert].data = data;
thread->count++;
pthread_mutex_unlock(&thread->lock);
return id;
}
static void
gc_background_thread_remove_task(struct gc_background_thread *thread,
int id) {
pthread_mutex_lock(&thread->lock);
size_t remove = 0;
for (; remove < thread->count; remove++) {
if (thread->tasks[remove].id == id)
break;
}
if (remove == thread->count)
GC_CRASH();
size_t bytes_to_move =
(thread->count - (remove + 1)) * sizeof(struct gc_background_task);
memmove(&thread->tasks[remove], &thread->tasks[remove + 1], bytes_to_move);
pthread_mutex_unlock(&thread->lock);
}
static void
gc_destroy_background_thread(struct gc_background_thread *thread) {
pthread_mutex_lock(&thread->lock);
GC_ASSERT(thread->state == GC_BACKGROUND_THREAD_RUNNING);
thread->state = GC_BACKGROUND_THREAD_STOPPING;
pthread_mutex_unlock(&thread->lock);
pthread_cond_signal(&thread->cond);
pthread_join(thread->thread, NULL);
free(thread->tasks);
free(thread);
}
#endif // BACKGROUND_THREAD_H