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>
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 *****************************************************************************/
31 #include <vlc_common.h>
36 #include <stdatomic.h>
40 #include <sys/types.h>
41 #include <unistd.h> /* fsync() */
49 vlc_thread_fatal_print (const char *action
, int error
,
50 const char *function
, const char *file
, unsigned line
)
55 switch (strerror_r (error
, buf
, sizeof (buf
)))
60 case ERANGE
: /* should never happen */
61 msg
= "unknown (too big to display)";
64 msg
= "unknown (invalid error number)";
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
);
74 # define VLC_THREAD_ASSERT( action ) do { \
75 if (unlikely(val)) { \
76 vlc_thread_fatal_print (action, val, __func__, __FILE__, __LINE__); \
81 # define VLC_THREAD_ASSERT( action ) ((void)val)
85 void vlc_mutex_init( vlc_mutex_t
*p_mutex
)
87 pthread_mutexattr_t attr
;
89 pthread_mutexattr_init (&attr
);
91 pthread_mutexattr_settype (&attr
, PTHREAD_MUTEX_DEFAULT
);
93 pthread_mutexattr_settype (&attr
, PTHREAD_MUTEX_ERRORCHECK
);
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
);
128 VLC_THREAD_ASSERT ("locking mutex");
129 vlc_mutex_mark(p_mutex
);
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");
152 void *(*entry
)(void*);
157 void *addr
; /// Non-null if waiting on futex
158 vlc_mutex_t lock
; /// Protects futex address
165 static thread_local
struct vlc_thread
*thread
= NULL
;
167 vlc_thread_t
vlc_thread_self (void)
172 void vlc_threads_setup (libvlc_int_t
*p_libvlc
)
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
);
187 static void *detached_thread(void *data
)
189 vlc_thread_t th
= data
;
193 vlc_cleanup_push(clean_detached_thread
, th
);
196 clean_detached_thread(th
);
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
;
212 vlc_cleanup_push(finish_joinable_thread
, th
);
214 ret
= th
->entry(th
->data
);
216 vlc_sem_post(&th
->finished
);
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
))
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
);
244 vlc_sem_init(&thread
->finished
, 0);
245 atomic_store(&thread
->killed
, false);
246 thread
->killable
= true;
247 thread
->entry
= entry
;
249 thread
->wait
.addr
= NULL
;
250 vlc_mutex_init(&thread
->wait
.lock
);
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
);
266 int vlc_clone (vlc_thread_t
*th
, void *(*entry
) (void *), void *data
,
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
,
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
;
300 void vlc_cancel (vlc_thread_t thread_id
)
304 atomic_store(&thread_id
->killed
, true);
306 vlc_mutex_lock(&thread_id
->wait
.lock
);
307 addr
= thread_id
->wait
.addr
;
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 */
321 int oldstate
= thread
->killable
;
322 thread
->killable
= false;
326 void vlc_restorecancel (int state
)
328 if (!thread
) /* not created by VLC, can't be cancelled */
331 thread
->killable
= state
;
334 void vlc_testcancel (void)
336 if (!thread
) /* not created by VLC, can't be cancelled */
338 if (!thread
->killable
)
340 if (!atomic_load(&thread
->killed
))
346 void vlc_control_cancel(int cmd
, ...)
348 vlc_thread_t th
= vlc_thread_self();
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
);
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
;
376 vlc_mutex_unlock(&th
->wait
.lock
);
381 vlc_assert_unreachable ();
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
);
409 vlc_tick_t
vlc_tick_now (void)
413 if (unlikely(clock_gettime (CLOCK_MONOTONIC
, &ts
) != 0))
416 return vlc_tick_from_timespec( &ts
);
421 unsigned vlc_GetCPUCount(void)
423 return sysconf(_SC_NPROCESSORS_ONLN
);