demux: heif: refactor pic setup
[vlc.git] / src / android / thread.c
blob0cce89e7491475fdad8eb1826628ecf3183946b4
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>
33 #include "libvlc.h"
34 #include <signal.h>
35 #include <errno.h>
36 #include <stdatomic.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 void vlc_mutex_lock (vlc_mutex_t *p_mutex)
118 int val = pthread_mutex_lock( p_mutex );
119 VLC_THREAD_ASSERT ("locking mutex");
120 vlc_mutex_mark(p_mutex);
123 int vlc_mutex_trylock (vlc_mutex_t *p_mutex)
125 int val = pthread_mutex_trylock( p_mutex );
127 if (val != EBUSY) {
128 VLC_THREAD_ASSERT ("locking mutex");
129 vlc_mutex_mark(p_mutex);
131 return val;
134 void vlc_mutex_unlock (vlc_mutex_t *p_mutex)
136 int val = pthread_mutex_unlock( p_mutex );
137 VLC_THREAD_ASSERT ("unlocking mutex");
138 vlc_mutex_unmark(p_mutex);
141 void vlc_once(vlc_once_t *once, void (*cb)(void))
143 int val = pthread_once(once, cb);
144 VLC_THREAD_ASSERT("initializing once");
147 struct vlc_thread
149 pthread_t thread;
150 vlc_sem_t finished;
152 void *(*entry)(void*);
153 void *data;
155 struct
157 void *addr; /// Non-null if waiting on futex
158 vlc_mutex_t lock ; /// Protects futex address
159 } wait;
161 atomic_bool killed;
162 bool killable;
165 static thread_local struct vlc_thread *thread = NULL;
167 vlc_thread_t vlc_thread_self (void)
169 return thread;
172 void vlc_threads_setup (libvlc_int_t *p_libvlc)
174 (void)p_libvlc;
177 /* pthread */
178 static void clean_detached_thread(void *data)
180 struct vlc_thread *th = data;
182 /* release thread handle */
183 vlc_mutex_destroy(&th->wait.lock);
184 free(th);
187 static void *detached_thread(void *data)
189 vlc_thread_t th = data;
191 thread = th;
193 vlc_cleanup_push(clean_detached_thread, th);
194 th->entry(th->data);
195 vlc_cleanup_pop();
196 clean_detached_thread(th);
197 return NULL;
200 static void finish_joinable_thread(void *data)
202 vlc_thread_t th = data;
204 vlc_sem_post(&th->finished);
207 static void *joinable_thread(void *data)
209 vlc_thread_t th = data;
210 void *ret;
212 vlc_cleanup_push(finish_joinable_thread, th);
213 thread = th;
214 ret = th->entry(th->data);
215 vlc_cleanup_pop();
216 vlc_sem_post(&th->finished);
218 return ret;
221 static int vlc_clone_attr (vlc_thread_t *th, void *(*entry) (void *),
222 void *data, bool detach)
224 vlc_thread_t thread = malloc (sizeof (*thread));
225 if (unlikely(thread == NULL))
226 return ENOMEM;
228 int ret;
230 sigset_t oldset;
232 sigset_t set;
233 sigemptyset (&set);
234 sigdelset (&set, SIGHUP);
235 sigaddset (&set, SIGINT);
236 sigaddset (&set, SIGQUIT);
237 sigaddset (&set, SIGTERM);
239 sigaddset (&set, SIGPIPE); /* We don't want this one, really! */
240 pthread_sigmask (SIG_BLOCK, &set, &oldset);
243 if (!detach)
244 vlc_sem_init(&thread->finished, 0);
245 atomic_store(&thread->killed, false);
246 thread->killable = true;
247 thread->entry = entry;
248 thread->data = data;
249 thread->wait.addr = NULL;
250 vlc_mutex_init(&thread->wait.lock);
252 pthread_attr_t attr;
253 pthread_attr_init (&attr);
254 pthread_attr_setdetachstate (&attr, detach ? PTHREAD_CREATE_DETACHED
255 : PTHREAD_CREATE_JOINABLE);
257 ret = pthread_create (&thread->thread, &attr,
258 detach ? detached_thread : joinable_thread, thread);
259 pthread_attr_destroy (&attr);
261 pthread_sigmask (SIG_SETMASK, &oldset, NULL);
262 *th = thread;
263 return ret;
266 int vlc_clone (vlc_thread_t *th, void *(*entry) (void *), void *data,
267 int priority)
269 (void) priority;
270 return vlc_clone_attr (th, entry, data, false);
273 void vlc_join (vlc_thread_t handle, void **result)
275 vlc_sem_wait (&handle->finished);
276 vlc_sem_destroy (&handle->finished);
278 int val = pthread_join (handle->thread, result);
279 VLC_THREAD_ASSERT ("joining thread");
280 clean_detached_thread(handle);
283 int vlc_clone_detach (vlc_thread_t *th, void *(*entry) (void *), void *data,
284 int priority)
286 vlc_thread_t dummy;
287 if (th == NULL)
288 th = &dummy;
290 (void) priority;
291 return vlc_clone_attr (th, entry, data, true);
294 int vlc_set_priority (vlc_thread_t th, int priority)
296 (void) th; (void) priority;
297 return VLC_SUCCESS;
300 void vlc_cancel (vlc_thread_t thread_id)
302 atomic_int *addr;
304 atomic_store(&thread_id->killed, true);
306 vlc_mutex_lock(&thread_id->wait.lock);
307 addr = thread_id->wait.addr;
308 if (addr != NULL)
310 atomic_fetch_or_explicit(addr, 1, memory_order_relaxed);
311 vlc_addr_broadcast(addr);
313 vlc_mutex_unlock(&thread_id->wait.lock);
316 int vlc_savecancel (void)
318 if (!thread) /* not created by VLC, can't be cancelled */
319 return true;
321 int oldstate = thread->killable;
322 thread->killable = false;
323 return oldstate;
326 void vlc_restorecancel (int state)
328 if (!thread) /* not created by VLC, can't be cancelled */
329 return;
331 thread->killable = state;
334 void vlc_testcancel (void)
336 if (!thread) /* not created by VLC, can't be cancelled */
337 return;
338 if (!thread->killable)
339 return;
340 if (!atomic_load(&thread->killed))
341 return;
343 pthread_exit(NULL);
346 void vlc_control_cancel(int cmd, ...)
348 vlc_thread_t th = vlc_thread_self();
349 va_list ap;
351 if (th == NULL)
352 return;
354 va_start(ap, cmd);
355 switch (cmd)
357 case VLC_CANCEL_ADDR_SET:
359 void *addr = va_arg(ap, void *);
361 vlc_mutex_lock(&th->wait.lock);
362 assert(th->wait.addr == NULL);
363 th->wait.addr = addr;
364 vlc_mutex_unlock(&th->wait.lock);
365 break;
368 case VLC_CANCEL_ADDR_CLEAR:
370 void *addr = va_arg(ap, void *);
372 vlc_mutex_lock(&th->wait.lock);
373 assert(th->wait.addr == addr);
374 th->wait.addr = NULL;
375 (void) addr;
376 vlc_mutex_unlock(&th->wait.lock);
377 break;
380 default:
381 vlc_assert_unreachable ();
383 va_end(ap);
386 /* threadvar */
388 int vlc_threadvar_create (vlc_threadvar_t *key, void (*destr) (void *))
390 return pthread_key_create (key, destr);
393 void vlc_threadvar_delete (vlc_threadvar_t *p_tls)
395 pthread_key_delete (*p_tls);
398 int vlc_threadvar_set (vlc_threadvar_t key, void *value)
400 return pthread_setspecific (key, value);
403 void *vlc_threadvar_get (vlc_threadvar_t key)
405 return pthread_getspecific (key);
408 /* time */
409 vlc_tick_t vlc_tick_now (void)
411 struct timespec ts;
413 if (unlikely(clock_gettime (CLOCK_MONOTONIC, &ts) != 0))
414 abort ();
416 return vlc_tick_from_timespec( &ts );
419 /* cpu */
421 unsigned vlc_GetCPUCount(void)
423 return sysconf(_SC_NPROCESSORS_ONLN);