asx: remove useless test
[vlc.git] / src / android / thread.c
blob606f40ad12976eb1a538ddfb2d1c90e272f6bd38
1 /*****************************************************************************
2 * thread.c : android pthread back-end for LibVLC
3 *****************************************************************************
4 * Copyright (C) 1999-2016 VLC authors and VideoLAN
6 * Authors: Jean-Marc Dressler <polux@via.ecp.fr>
7 * Samuel Hocevar <sam@zoy.org>
8 * Gildas Bazin <gbazin@netcourrier.com>
9 * Clément Sténac
10 * Rémi Denis-Courmont
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU Lesser General Public License as published by
14 * the Free Software Foundation; either version 2.1 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public License
23 * along with this program; if not, write to the Free Software Foundation,
24 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25 *****************************************************************************/
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
31 #include <vlc_common.h>
32 #include <vlc_atomic.h>
34 #include "libvlc.h"
35 #include <signal.h>
36 #include <errno.h>
37 #include <time.h>
38 #include <assert.h>
40 #include <sys/types.h>
41 #include <unistd.h> /* fsync() */
42 #include <pthread.h>
43 #include <sched.h>
45 /* debug */
47 #ifndef NDEBUG
48 static void
49 vlc_thread_fatal_print (const char *action, int error,
50 const char *function, const char *file, unsigned line)
52 char buf[1000];
53 const char *msg;
55 switch (strerror_r (error, buf, sizeof (buf)))
57 case 0:
58 msg = buf;
59 break;
60 case ERANGE: /* should never happen */
61 msg = "unknown (too big to display)";
62 break;
63 default:
64 msg = "unknown (invalid error number)";
65 break;
68 fprintf(stderr, "LibVLC fatal error %s (%d) in thread %lu "
69 "at %s:%u in %s\n Error message: %s\n",
70 action, error, vlc_thread_id (), file, line, function, msg);
71 fflush (stderr);
74 # define VLC_THREAD_ASSERT( action ) do { \
75 if (unlikely(val)) { \
76 vlc_thread_fatal_print (action, val, __func__, __FILE__, __LINE__); \
77 assert (!action); \
78 } \
79 } while(0)
80 #else
81 # define VLC_THREAD_ASSERT( action ) ((void)val)
82 #endif
84 /* mutexes */
85 void vlc_mutex_init( vlc_mutex_t *p_mutex )
87 pthread_mutexattr_t attr;
89 pthread_mutexattr_init (&attr);
90 #ifdef NDEBUG
91 pthread_mutexattr_settype (&attr, PTHREAD_MUTEX_DEFAULT);
92 #else
93 pthread_mutexattr_settype (&attr, PTHREAD_MUTEX_ERRORCHECK);
94 #endif
95 pthread_mutex_init (p_mutex, &attr);
96 pthread_mutexattr_destroy( &attr );
99 void vlc_mutex_init_recursive( vlc_mutex_t *p_mutex )
101 pthread_mutexattr_t attr;
103 pthread_mutexattr_init (&attr);
104 pthread_mutexattr_settype (&attr, PTHREAD_MUTEX_RECURSIVE);
105 pthread_mutex_init (p_mutex, &attr);
106 pthread_mutexattr_destroy( &attr );
110 void vlc_mutex_destroy (vlc_mutex_t *p_mutex)
112 int val = pthread_mutex_destroy( p_mutex );
113 VLC_THREAD_ASSERT ("destroying mutex");
116 #ifndef NDEBUG
117 void vlc_assert_locked (vlc_mutex_t *p_mutex)
119 assert (pthread_mutex_lock (p_mutex) == EDEADLK);
121 #endif
123 void vlc_mutex_lock (vlc_mutex_t *p_mutex)
125 int val = pthread_mutex_lock( p_mutex );
126 VLC_THREAD_ASSERT ("locking mutex");
129 int vlc_mutex_trylock (vlc_mutex_t *p_mutex)
131 int val = pthread_mutex_trylock( p_mutex );
133 if (val != EBUSY)
134 VLC_THREAD_ASSERT ("locking mutex");
135 return val;
138 void vlc_mutex_unlock (vlc_mutex_t *p_mutex)
140 int val = pthread_mutex_unlock( p_mutex );
141 VLC_THREAD_ASSERT ("unlocking mutex");
144 struct vlc_thread
146 pthread_t thread;
147 vlc_sem_t finished;
149 void *(*entry)(void*);
150 void *data;
152 struct
154 void *addr; /// Non-null if waiting on futex
155 vlc_mutex_t lock ; /// Protects futex address
156 } wait;
158 atomic_bool killed;
159 bool killable;
162 static thread_local struct vlc_thread *thread = NULL;
164 vlc_thread_t vlc_thread_self (void)
166 return thread;
169 void vlc_threads_setup (libvlc_int_t *p_libvlc)
171 (void)p_libvlc;
174 /* pthread */
175 static void clean_detached_thread(void *data)
177 struct vlc_thread *th = data;
179 /* release thread handle */
180 vlc_mutex_destroy(&th->wait.lock);
181 free(th);
184 static void *detached_thread(void *data)
186 vlc_thread_t th = data;
188 thread = th;
190 vlc_cleanup_push(clean_detached_thread, th);
191 th->entry(th->data);
192 vlc_cleanup_pop();
193 clean_detached_thread(th);
194 return NULL;
197 static void finish_joinable_thread(void *data)
199 vlc_thread_t th = data;
201 vlc_sem_post(&th->finished);
204 static void *joinable_thread(void *data)
206 vlc_thread_t th = data;
207 void *ret;
209 vlc_cleanup_push(finish_joinable_thread, th);
210 thread = th;
211 ret = th->entry(th->data);
212 vlc_cleanup_pop();
213 vlc_sem_post(&th->finished);
215 return ret;
218 static int vlc_clone_attr (vlc_thread_t *th, void *(*entry) (void *),
219 void *data, bool detach)
221 vlc_thread_t thread = malloc (sizeof (*thread));
222 if (unlikely(thread == NULL))
223 return ENOMEM;
225 int ret;
227 sigset_t oldset;
229 sigset_t set;
230 sigemptyset (&set);
231 sigdelset (&set, SIGHUP);
232 sigaddset (&set, SIGINT);
233 sigaddset (&set, SIGQUIT);
234 sigaddset (&set, SIGTERM);
236 sigaddset (&set, SIGPIPE); /* We don't want this one, really! */
237 pthread_sigmask (SIG_BLOCK, &set, &oldset);
240 if (!detach)
241 vlc_sem_init(&thread->finished, 0);
242 atomic_store(&thread->killed, false);
243 thread->killable = true;
244 thread->entry = entry;
245 thread->data = data;
246 thread->wait.addr = NULL;
247 vlc_mutex_init(&thread->wait.lock);
249 pthread_attr_t attr;
250 pthread_attr_init (&attr);
251 pthread_attr_setdetachstate (&attr, detach ? PTHREAD_CREATE_DETACHED
252 : PTHREAD_CREATE_JOINABLE);
254 ret = pthread_create (&thread->thread, &attr,
255 detach ? detached_thread : joinable_thread, thread);
256 pthread_attr_destroy (&attr);
258 pthread_sigmask (SIG_SETMASK, &oldset, NULL);
259 *th = thread;
260 return ret;
263 int vlc_clone (vlc_thread_t *th, void *(*entry) (void *), void *data,
264 int priority)
266 (void) priority;
267 return vlc_clone_attr (th, entry, data, false);
270 void vlc_join (vlc_thread_t handle, void **result)
272 vlc_sem_wait (&handle->finished);
273 vlc_sem_destroy (&handle->finished);
275 int val = pthread_join (handle->thread, result);
276 VLC_THREAD_ASSERT ("joining thread");
277 clean_detached_thread(handle);
280 int vlc_clone_detach (vlc_thread_t *th, void *(*entry) (void *), void *data,
281 int priority)
283 vlc_thread_t dummy;
284 if (th == NULL)
285 th = &dummy;
287 (void) priority;
288 return vlc_clone_attr (th, entry, data, true);
291 int vlc_set_priority (vlc_thread_t th, int priority)
293 (void) th; (void) priority;
294 return VLC_SUCCESS;
297 void vlc_cancel (vlc_thread_t thread_id)
299 atomic_int *addr;
301 atomic_store(&thread_id->killed, true);
303 vlc_mutex_lock(&thread_id->wait.lock);
304 addr = thread_id->wait.addr;
305 if (addr != NULL)
307 atomic_fetch_or_explicit(addr, 1, memory_order_relaxed);
308 vlc_addr_broadcast(addr);
310 vlc_mutex_unlock(&thread_id->wait.lock);
313 int vlc_savecancel (void)
315 if (!thread) /* not created by VLC, can't be cancelled */
316 return true;
318 int oldstate = thread->killable;
319 thread->killable = false;
320 return oldstate;
323 void vlc_restorecancel (int state)
325 if (!thread) /* not created by VLC, can't be cancelled */
326 return;
328 thread->killable = state;
331 void vlc_testcancel (void)
333 if (!thread) /* not created by VLC, can't be cancelled */
334 return;
335 if (!thread->killable)
336 return;
337 if (!atomic_load(&thread->killed))
338 return;
340 pthread_exit(NULL);
343 void vlc_control_cancel(int cmd, ...)
345 vlc_thread_t th = vlc_thread_self();
346 va_list ap;
348 if (th == NULL)
349 return;
351 va_start(ap, cmd);
352 switch (cmd)
354 case VLC_CANCEL_ADDR_SET:
356 void *addr = va_arg(ap, void *);
358 vlc_mutex_lock(&th->wait.lock);
359 assert(th->wait.addr == NULL);
360 th->wait.addr = addr;
361 vlc_mutex_unlock(&th->wait.lock);
362 break;
365 case VLC_CANCEL_ADDR_CLEAR:
367 void *addr = va_arg(ap, void *);
369 vlc_mutex_lock(&th->wait.lock);
370 assert(th->wait.addr == addr);
371 th->wait.addr = NULL;
372 (void) addr;
373 vlc_mutex_unlock(&th->wait.lock);
374 break;
377 default:
378 vlc_assert_unreachable ();
380 va_end(ap);
383 /* threadvar */
385 int vlc_threadvar_create (vlc_threadvar_t *key, void (*destr) (void *))
387 return pthread_key_create (key, destr);
390 void vlc_threadvar_delete (vlc_threadvar_t *p_tls)
392 pthread_key_delete (*p_tls);
395 int vlc_threadvar_set (vlc_threadvar_t key, void *value)
397 return pthread_setspecific (key, value);
400 void *vlc_threadvar_get (vlc_threadvar_t key)
402 return pthread_getspecific (key);
405 /* time */
406 mtime_t mdate (void)
408 struct timespec ts;
410 if (unlikely(clock_gettime (CLOCK_MONOTONIC, &ts) != 0))
411 abort ();
413 return (INT64_C(1000000) * ts.tv_sec) + (ts.tv_nsec / 1000);
416 /* cpu */
418 unsigned vlc_GetCPUCount(void)
420 return sysconf(_SC_NPROCESSORS_ONLN);