-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy paththread.c
223 lines (188 loc) · 5.85 KB
/
thread.c
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/* -*- coding: utf-8 -*-
* ----------------------------------------------------------------------
* Copyright © 2013-2015, RedJack, LLC.
* All rights reserved.
*
* Please see the COPYING file in this distribution for license details.
* ----------------------------------------------------------------------
*/
#if defined(__linux)
/* This is needed on Linux to get the pthread_setname_np function. */
#if !defined(_GNU_SOURCE)
#define _GNU_SOURCE 1
#endif
#endif
#include <assert.h>
#include <string.h>
#include <pthread.h>
#include "libcork/core/allocator.h"
#include "libcork/core/error.h"
#include "libcork/core/types.h"
#include "libcork/ds/buffer.h"
#include "libcork/threads/basics.h"
/*-----------------------------------------------------------------------
* Current thread
*/
static volatile cork_thread_id last_thread_descriptor = 0;
struct cork_thread {
const char *name;
cork_thread_id id;
pthread_t thread_id;
void *user_data;
cork_free_f free_user_data;
cork_run_f run;
cork_error error_code;
struct cork_buffer error_message;
bool started;
bool joined;
};
struct cork_thread_descriptor {
struct cork_thread *current_thread;
cork_thread_id id;
};
cork_tls(struct cork_thread_descriptor, cork_thread_descriptor);
struct cork_thread *
cork_current_thread_get(void)
{
struct cork_thread_descriptor *desc = cork_thread_descriptor_get();
return desc->current_thread;
}
cork_thread_id
cork_current_thread_get_id(void)
{
struct cork_thread_descriptor *desc = cork_thread_descriptor_get();
if (CORK_UNLIKELY(desc->id == 0)) {
if (desc->current_thread == NULL) {
desc->id = cork_uint_atomic_add(&last_thread_descriptor, 1);
} else {
desc->id = desc->current_thread->id;
}
}
return desc->id;
}
/*-----------------------------------------------------------------------
* Threads
*/
struct cork_thread *
cork_thread_new(const char *name,
void *user_data, cork_free_f free_user_data,
cork_run_f run)
{
struct cork_thread *self = cork_new(struct cork_thread);
self->name = cork_strdup(name);
self->id = cork_uint_atomic_add(&last_thread_descriptor, 1);
self->user_data = user_data;
self->free_user_data = free_user_data;
self->run = run;
self->error_code = CORK_ERROR_NONE;
cork_buffer_init(&self->error_message);
self->started = false;
self->joined = false;
return self;
}
static void
cork_thread_free_private(struct cork_thread *self)
{
cork_strfree(self->name);
cork_free_user_data(self);
cork_buffer_done(&self->error_message);
cork_delete(struct cork_thread, self);
}
void
cork_thread_free(struct cork_thread *self)
{
assert(!self->started);
cork_thread_free_private(self);
}
const char *
cork_thread_get_name(struct cork_thread *self)
{
return self->name;
}
cork_thread_id
cork_thread_get_id(struct cork_thread *self)
{
return self->id;
}
#define PTHREADS_MAX_THREAD_NAME_LENGTH 16
static void *
cork_thread_pthread_run(void *vself)
{
int rc;
struct cork_thread *self = vself;
struct cork_thread_descriptor *desc = cork_thread_descriptor_get();
#if defined(__APPLE__) && defined(__MACH__)
char thread_name[PTHREADS_MAX_THREAD_NAME_LENGTH];
#endif
desc->current_thread = self;
desc->id = self->id;
rc = self->run(self->user_data);
#if defined(__APPLE__) && defined(__MACH__)
/* On Mac OS X, we set the name of the current thread, not of an arbitrary
* thread of our choosing. */
strncpy(thread_name, self->name, PTHREADS_MAX_THREAD_NAME_LENGTH);
thread_name[PTHREADS_MAX_THREAD_NAME_LENGTH - 1] = '\0';
pthread_setname_np(thread_name);
#endif
/* If an error occurred in the body of the thread, save the error into the
* cork_thread object so that we can propagate that error when some calls
* cork_thread_join. */
if (CORK_UNLIKELY(rc != 0)) {
if (CORK_LIKELY(cork_error_occurred())) {
self->error_code = cork_error_code();
cork_buffer_set_string(&self->error_message, cork_error_message());
} else {
self->error_code = CORK_UNKNOWN_ERROR;
cork_buffer_set_string(&self->error_message, "Unknown error");
}
}
return NULL;
}
int
cork_thread_start(struct cork_thread *self)
{
int rc;
pthread_t thread_id;
#if defined(__linux) && ((__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 12))
char thread_name[PTHREADS_MAX_THREAD_NAME_LENGTH];
#endif
assert(!self->started);
rc = pthread_create(&thread_id, NULL, cork_thread_pthread_run, self);
if (CORK_UNLIKELY(rc != 0)) {
cork_system_error_set_explicit(rc);
return -1;
}
#if defined(__linux) && ((__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 12))
/* On Linux we choose which thread to name via an explicit thread ID.
* However, pthread_setname_np() isn't supported on versions of glibc
* earlier than 2.12. So we need to check for a MINOR version of 12 or
* higher. */
strncpy(thread_name, self->name, PTHREADS_MAX_THREAD_NAME_LENGTH);
thread_name[PTHREADS_MAX_THREAD_NAME_LENGTH - 1] = '\0';
pthread_setname_np(thread_id, thread_name);
#endif
self->thread_id = thread_id;
self->started = true;
return 0;
}
int
cork_thread_join(struct cork_thread *self)
{
int rc;
assert(self->started && !self->joined);
rc = pthread_join(self->thread_id, NULL);
if (CORK_UNLIKELY(rc != 0)) {
cork_system_error_set_explicit(rc);
cork_thread_free_private(self);
return -1;
}
if (CORK_UNLIKELY(self->error_code != CORK_ERROR_NONE)) {
cork_error_set_printf
(self->error_code, "Error from thread %s: %s",
self->name, (char *) self->error_message.buf);
cork_thread_free_private(self);
return -1;
}
cork_thread_free_private(self);
return 0;
}