lib: media_player: fix aout callbacks having no effects
[vlc.git] / src / android / thread.c
blob79132be2e46401ac975986d0e9cce6b581828ca9
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 <stdnoreturn.h>
38 #include <time.h>
39 #include <assert.h>
41 #include <sys/types.h>
42 #include <unistd.h> /* fsync() */
43 #include <pthread.h>
44 #include <sched.h>
46 /* debug */
48 #ifndef NDEBUG
49 static void
50 vlc_thread_fatal_print (const char *action, int error,
51 const char *function, const char *file, unsigned line)
53 const char *msg = vlc_strerror_c(error);
54 fprintf(stderr, "LibVLC fatal error %s (%d) in thread %lu "
55 "at %s:%u in %s\n Error message: %s\n",
56 action, error, vlc_thread_id (), file, line, function, msg);
57 fflush (stderr);
60 # define VLC_THREAD_ASSERT( action ) do { \
61 if (unlikely(val)) { \
62 vlc_thread_fatal_print (action, val, __func__, __FILE__, __LINE__); \
63 assert (!action); \
64 } \
65 } while(0)
66 #else
67 # define VLC_THREAD_ASSERT( action ) ((void)val)
68 #endif
70 struct vlc_thread
72 pthread_t thread;
74 void *(*entry)(void*);
75 void *data;
77 struct
79 atomic_uint *addr; /// Non-null if waiting on futex
80 vlc_mutex_t lock ; /// Protects futex address
81 } wait;
83 atomic_bool killed;
84 bool killable;
87 static thread_local struct vlc_thread *thread = NULL;
89 void vlc_threads_setup (libvlc_int_t *p_libvlc)
91 (void)p_libvlc;
94 /* pthread */
95 static void clean_detached_thread(void *data)
97 struct vlc_thread *th = data;
99 /* release thread handle */
100 free(th);
103 static void *detached_thread(void *data)
105 vlc_thread_t th = data;
107 thread = th;
109 vlc_cleanup_push(clean_detached_thread, th);
110 th->entry(th->data);
111 vlc_cleanup_pop();
112 clean_detached_thread(th);
113 return NULL;
116 static void *joinable_thread(void *data)
118 vlc_thread_t th = data;
120 thread = th;
121 return th->entry(th->data);
124 static int vlc_clone_attr (vlc_thread_t *th, void *(*entry) (void *),
125 void *data, bool detach)
127 vlc_thread_t thread = malloc (sizeof (*thread));
128 if (unlikely(thread == NULL))
129 return ENOMEM;
131 int ret;
133 sigset_t oldset;
135 sigset_t set;
136 sigemptyset (&set);
137 sigdelset (&set, SIGHUP);
138 sigaddset (&set, SIGINT);
139 sigaddset (&set, SIGQUIT);
140 sigaddset (&set, SIGTERM);
142 sigaddset (&set, SIGPIPE); /* We don't want this one, really! */
143 pthread_sigmask (SIG_BLOCK, &set, &oldset);
146 atomic_store(&thread->killed, false);
147 thread->killable = true;
148 thread->entry = entry;
149 thread->data = data;
150 thread->wait.addr = NULL;
151 vlc_mutex_init(&thread->wait.lock);
153 pthread_attr_t attr;
154 pthread_attr_init (&attr);
155 pthread_attr_setdetachstate (&attr, detach ? PTHREAD_CREATE_DETACHED
156 : PTHREAD_CREATE_JOINABLE);
158 ret = pthread_create (&thread->thread, &attr,
159 detach ? detached_thread : joinable_thread, thread);
160 pthread_attr_destroy (&attr);
162 pthread_sigmask (SIG_SETMASK, &oldset, NULL);
163 *th = thread;
164 return ret;
167 int vlc_clone (vlc_thread_t *th, void *(*entry) (void *), void *data,
168 int priority)
170 (void) priority;
171 return vlc_clone_attr (th, entry, data, false);
174 void vlc_join (vlc_thread_t handle, void **result)
176 int val = pthread_join (handle->thread, result);
177 VLC_THREAD_ASSERT ("joining thread");
178 clean_detached_thread(handle);
181 int vlc_clone_detach (vlc_thread_t *th, void *(*entry) (void *), void *data,
182 int priority)
184 vlc_thread_t dummy;
185 if (th == NULL)
186 th = &dummy;
188 (void) priority;
189 return vlc_clone_attr (th, entry, data, true);
192 int vlc_set_priority (vlc_thread_t th, int priority)
194 (void) th; (void) priority;
195 return VLC_SUCCESS;
198 void vlc_cancel (vlc_thread_t thread_id)
200 atomic_uint *addr;
202 atomic_store(&thread_id->killed, true);
204 vlc_mutex_lock(&thread_id->wait.lock);
205 addr = thread_id->wait.addr;
206 if (addr != NULL)
208 atomic_fetch_or_explicit(addr, 1, memory_order_relaxed);
209 vlc_atomic_notify_all(addr);
211 vlc_mutex_unlock(&thread_id->wait.lock);
214 int vlc_savecancel (void)
216 if (!thread) /* not created by VLC, can't be cancelled */
217 return true;
219 int oldstate = thread->killable;
220 thread->killable = false;
221 return oldstate;
224 void vlc_restorecancel (int state)
226 if (!thread) /* not created by VLC, can't be cancelled */
227 return;
229 thread->killable = state;
232 void vlc_testcancel (void)
234 if (!thread) /* not created by VLC, can't be cancelled */
235 return;
236 if (!thread->killable)
237 return;
238 if (!atomic_load(&thread->killed))
239 return;
241 pthread_exit(NULL);
244 void vlc_cancel_addr_set(atomic_uint *addr)
246 vlc_thread_t th = thread;
247 if (th == NULL)
248 return;
250 vlc_mutex_lock(&th->wait.lock);
251 assert(th->wait.addr == NULL);
252 th->wait.addr = addr;
253 vlc_mutex_unlock(&th->wait.lock);
256 void vlc_cancel_addr_clear(atomic_uint *addr)
258 vlc_thread_t th = thread;
259 if (th == NULL)
260 return;
262 vlc_mutex_lock(&th->wait.lock);
263 assert(th->wait.addr == addr);
264 th->wait.addr = NULL;
265 (void) addr;
266 vlc_mutex_unlock(&th->wait.lock);
269 /* threadvar */
271 int vlc_threadvar_create (vlc_threadvar_t *key, void (*destr) (void *))
273 return pthread_key_create (key, destr);
276 void vlc_threadvar_delete (vlc_threadvar_t *p_tls)
278 pthread_key_delete (*p_tls);
281 int vlc_threadvar_set (vlc_threadvar_t key, void *value)
283 return pthread_setspecific (key, value);
286 void *vlc_threadvar_get (vlc_threadvar_t key)
288 return pthread_getspecific (key);
291 /* time */
292 vlc_tick_t vlc_tick_now (void)
294 struct timespec ts;
296 if (unlikely(clock_gettime (CLOCK_MONOTONIC, &ts) != 0))
297 abort ();
299 return vlc_tick_from_timespec( &ts );
302 /* cpu */
304 unsigned vlc_GetCPUCount(void)
306 return sysconf(_SC_NPROCESSORS_ONLN);