Debian Codegraph @

src/unix/uxthread.c

raw
1 /* ______ ___ ___
2 * /\ _ \ /\_ \ /\_ \
3 * \ \ \L\ \\//\ \ \//\ \ __ __ _ __ ___
4 * \ \ __ \ \ \ \ \ \ \ /'__`\ /'_ `\/\`'__\/ __`\
5 * \ \ \/\ \ \_\ \_ \_\ \_/\ __//\ \L\ \ \ \//\ \L\ \
6 * \ \_\ \_\/\____\/\____\ \____\ \____ \ \_\\ \____/
7 * \/_/\/_/\/____/\/____/\/____/\/___L\ \/_/ \/___/
8 * /\____/
9 * \_/__/
10 *
11 * Internal cross-platform threading API for Unix.
12 *
13 * By Peter Wang.
14 *
15 * See readme.txt for copyright information.
16 */
17
18
19 #define _XOPEN_SOURCE 500 /* for Unix98 recursive mutexes */
20 /* XXX: added configure test */
21
22 #include <sys/time.h>
23
24 #include "allegro5/allegro.h"
25 #include "allegro5/internal/aintern.h"
26 #include "allegro5/internal/aintern_thread.h"
27 #include "allegro5/platform/aintunix.h"
28
29 #ifdef ALLEGRO_ANDROID
30 #include "allegro5/internal/aintern_android.h"
31 #endif
32
33
34
35 /* threads */
36
37 static void *thread_proc_trampoline(void *data)
38 {
39 _AL_THREAD *thread = data;
40 /* Android is special and needs to attach/detach threads with Java */
41 #ifdef ALLEGRO_ANDROID
42 _al_android_thread_created();
43 #endif
44 (*thread->proc)(thread, thread->arg);
45 #ifdef ALLEGRO_ANDROID
46 _al_android_thread_ended();
47 #endif
48 return NULL;
49 }
50
51
52 void _al_thread_create(_AL_THREAD *thread, void (*proc)(_AL_THREAD*, void*), void *arg)
53 {
54 ASSERT(thread);
55 ASSERT(proc);
56 {
57 int status;
58
59 pthread_mutex_init(&thread->mutex, NULL);
60
61 thread->should_stop = false;
62 thread->proc = proc;
63 thread->arg = arg;
64
65 status = pthread_create(&thread->thread, NULL, thread_proc_trampoline, thread);
66 ASSERT(status == 0);
67 if (status != 0)
68 abort();
69 }
70 }
71
72
73 void _al_thread_create_with_stacksize(_AL_THREAD* thread, void (*proc)(_AL_THREAD*, void*), void *arg, size_t stacksize)
74 {
75 #ifndef __GNU__
76 ASSERT(stacksize >= PTHREAD_STACK_MIN);
77 #endif
78 ASSERT(thread);
79 ASSERT(proc);
80 {
81 int status;
82
83 pthread_mutex_init(&thread->mutex, NULL);
84
85 thread->should_stop = false;
86 thread->proc = proc;
87 thread->arg = arg;
88
89 pthread_attr_t thread_attr;
90 int result = 0;
91 result = pthread_attr_init(&thread_attr);
92 ASSERT(result == 0);
93
94 // On some systems, pthread_attr_setstacksize() can fail
95 // if stacksize is not a multiple of the system page size.
96 result = pthread_attr_setstacksize(&thread_attr, stacksize);
97 ASSERT(result == 0);
98
99 status = pthread_create(&thread->thread, &thread_attr, thread_proc_trampoline, thread);
100 ASSERT(status == 0);
101 if (status != 0)
102 abort();
103 }
104 }
105
106
107 void _al_thread_set_should_stop(_AL_THREAD *thread)
108 {
109 ASSERT(thread);
110
111 pthread_mutex_lock(&thread->mutex);
112 {
113 thread->should_stop = true;
114 }
115 pthread_mutex_unlock(&thread->mutex);
116 }
117
118
119
120 void _al_thread_join(_AL_THREAD *thread)
121 {
122 ASSERT(thread);
123
124 _al_thread_set_should_stop(thread);
125 pthread_join(thread->thread, NULL);
126
127 pthread_mutex_destroy(&thread->mutex);
128 }
129
130
131 void _al_thread_detach(_AL_THREAD *thread)
132 {
133 ASSERT(thread);
134 pthread_mutex_destroy(&thread->mutex);
135 pthread_detach(thread->thread);
136 }
137
138
139 /* mutexes */
140
141 void _al_mutex_init(_AL_MUTEX *mutex)
142 {
143 ASSERT(mutex);
144
145 pthread_mutex_init(&mutex->mutex, NULL);
146 mutex->inited = true;
147 }
148
149
150 void _al_mutex_init_recursive(_AL_MUTEX *mutex)
151 {
152 pthread_mutexattr_t attr;
153
154 ASSERT(mutex);
155
156 pthread_mutexattr_init(&attr);
157 if (pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE) == EINVAL) {
158 pthread_mutexattr_destroy(&attr);
159 abort(); /* XXX */
160 }
161
162 pthread_mutex_init(&mutex->mutex, &attr);
163 mutex->inited = true;
164
165 pthread_mutexattr_destroy(&attr);
166 }
167
168
169 void _al_mutex_destroy(_AL_MUTEX *mutex)
170 {
171 ASSERT(mutex);
172
173 if (mutex->inited) {
174 pthread_mutex_destroy(&mutex->mutex);
175 mutex->inited = false;
176 }
177 }
178
179
180 /* condition variables */
181 /* most of the condition variable implementation is actually inline */
182
183 int _al_cond_timedwait(_AL_COND *cond, _AL_MUTEX *mutex,
184 const ALLEGRO_TIMEOUT *timeout)
185 {
186 ALLEGRO_TIMEOUT_UNIX *unix_timeout = (ALLEGRO_TIMEOUT_UNIX *) timeout;
187 int retcode;
188
189 retcode = pthread_cond_timedwait(&cond->cond, &mutex->mutex,
190 &unix_timeout->abstime);
191
192 return (retcode == ETIMEDOUT) ? -1 : 0;
193 }
194
195
196 /*
197 * Local Variables:
198 * c-basic-offset: 3
199 * indent-tabs-mode: nil
200 * End:
201 */