demux: wav: skip header in ChunkFind
[vlc.git] / src / android / thread.c
blobf831f5b5c11d7e0ace04caddd231ab1a3bad2181
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 void vlc_once(vlc_once_t *once, void (*cb)(void))
72 int val = pthread_once(once, cb);
73 VLC_THREAD_ASSERT("initializing once");
76 struct vlc_thread
78 pthread_t thread;
80 void *(*entry)(void*);
81 void *data;
83 struct
85 atomic_uint *addr; /// Non-null if waiting on futex
86 vlc_mutex_t lock ; /// Protects futex address
87 } wait;
89 atomic_bool killed;
90 bool killable;
93 static thread_local struct vlc_thread *thread = NULL;
95 void vlc_threads_setup (libvlc_int_t *p_libvlc)
97 (void)p_libvlc;
100 /* pthread */
101 static void clean_detached_thread(void *data)
103 struct vlc_thread *th = data;
105 /* release thread handle */
106 free(th);
109 static void *detached_thread(void *data)
111 vlc_thread_t th = data;
113 thread = th;
115 vlc_cleanup_push(clean_detached_thread, th);
116 th->entry(th->data);
117 vlc_cleanup_pop();
118 clean_detached_thread(th);
119 return NULL;
122 static void *joinable_thread(void *data)
124 vlc_thread_t th = data;
126 thread = th;
127 return th->entry(th->data);
130 static int vlc_clone_attr (vlc_thread_t *th, void *(*entry) (void *),
131 void *data, bool detach)
133 vlc_thread_t thread = malloc (sizeof (*thread));
134 if (unlikely(thread == NULL))
135 return ENOMEM;
137 int ret;
139 sigset_t oldset;
141 sigset_t set;
142 sigemptyset (&set);
143 sigdelset (&set, SIGHUP);
144 sigaddset (&set, SIGINT);
145 sigaddset (&set, SIGQUIT);
146 sigaddset (&set, SIGTERM);
148 sigaddset (&set, SIGPIPE); /* We don't want this one, really! */
149 pthread_sigmask (SIG_BLOCK, &set, &oldset);
152 atomic_store(&thread->killed, false);
153 thread->killable = true;
154 thread->entry = entry;
155 thread->data = data;
156 thread->wait.addr = NULL;
157 vlc_mutex_init(&thread->wait.lock);
159 pthread_attr_t attr;
160 pthread_attr_init (&attr);
161 pthread_attr_setdetachstate (&attr, detach ? PTHREAD_CREATE_DETACHED
162 : PTHREAD_CREATE_JOINABLE);
164 ret = pthread_create (&thread->thread, &attr,
165 detach ? detached_thread : joinable_thread, thread);
166 pthread_attr_destroy (&attr);
168 pthread_sigmask (SIG_SETMASK, &oldset, NULL);
169 *th = thread;
170 return ret;
173 int vlc_clone (vlc_thread_t *th, void *(*entry) (void *), void *data,
174 int priority)
176 (void) priority;
177 return vlc_clone_attr (th, entry, data, false);
180 void vlc_join (vlc_thread_t handle, void **result)
182 int val = pthread_join (handle->thread, result);
183 VLC_THREAD_ASSERT ("joining thread");
184 clean_detached_thread(handle);
187 int vlc_clone_detach (vlc_thread_t *th, void *(*entry) (void *), void *data,
188 int priority)
190 vlc_thread_t dummy;
191 if (th == NULL)
192 th = &dummy;
194 (void) priority;
195 return vlc_clone_attr (th, entry, data, true);
198 int vlc_set_priority (vlc_thread_t th, int priority)
200 (void) th; (void) priority;
201 return VLC_SUCCESS;
204 void vlc_cancel (vlc_thread_t thread_id)
206 atomic_uint *addr;
208 atomic_store(&thread_id->killed, true);
210 vlc_mutex_lock(&thread_id->wait.lock);
211 addr = thread_id->wait.addr;
212 if (addr != NULL)
214 atomic_fetch_or_explicit(addr, 1, memory_order_relaxed);
215 vlc_atomic_notify_all(addr);
217 vlc_mutex_unlock(&thread_id->wait.lock);
220 int vlc_savecancel (void)
222 if (!thread) /* not created by VLC, can't be cancelled */
223 return true;
225 int oldstate = thread->killable;
226 thread->killable = false;
227 return oldstate;
230 void vlc_restorecancel (int state)
232 if (!thread) /* not created by VLC, can't be cancelled */
233 return;
235 thread->killable = state;
238 void vlc_testcancel (void)
240 if (!thread) /* not created by VLC, can't be cancelled */
241 return;
242 if (!thread->killable)
243 return;
244 if (!atomic_load(&thread->killed))
245 return;
247 pthread_exit(NULL);
250 void vlc_cancel_addr_set(atomic_uint *addr)
252 vlc_thread_t th = thread;
253 if (th == NULL)
254 return;
256 vlc_mutex_lock(&th->wait.lock);
257 assert(th->wait.addr == NULL);
258 th->wait.addr = addr;
259 vlc_mutex_unlock(&th->wait.lock);
262 void vlc_cancel_addr_clear(atomic_uint *addr)
264 vlc_thread_t th = thread;
265 if (th == NULL)
266 return;
268 vlc_mutex_lock(&th->wait.lock);
269 assert(th->wait.addr == addr);
270 th->wait.addr = NULL;
271 (void) addr;
272 vlc_mutex_unlock(&th->wait.lock);
275 /* threadvar */
277 int vlc_threadvar_create (vlc_threadvar_t *key, void (*destr) (void *))
279 return pthread_key_create (key, destr);
282 void vlc_threadvar_delete (vlc_threadvar_t *p_tls)
284 pthread_key_delete (*p_tls);
287 int vlc_threadvar_set (vlc_threadvar_t key, void *value)
289 return pthread_setspecific (key, value);
292 void *vlc_threadvar_get (vlc_threadvar_t key)
294 return pthread_getspecific (key);
297 /* time */
298 vlc_tick_t vlc_tick_now (void)
300 struct timespec ts;
302 if (unlikely(clock_gettime (CLOCK_MONOTONIC, &ts) != 0))
303 abort ();
305 return vlc_tick_from_timespec( &ts );
308 /* cpu */
310 unsigned vlc_GetCPUCount(void)
312 return sysconf(_SC_NPROCESSORS_ONLN);