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>
32 #include <vlc_atomic.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");
117 void vlc_assert_locked (vlc_mutex_t
*p_mutex
)
119 assert (pthread_mutex_lock (p_mutex
) == EDEADLK
);
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
);
134 VLC_THREAD_ASSERT ("locking mutex");
138 void vlc_mutex_unlock (vlc_mutex_t
*p_mutex
)
140 int val
= pthread_mutex_unlock( p_mutex
);
141 VLC_THREAD_ASSERT ("unlocking mutex");
149 void *(*entry
)(void*);
154 void *addr
; /// Non-null if waiting on futex
155 vlc_mutex_t lock
; /// Protects futex address
162 static __thread
struct vlc_thread
*thread
= NULL
;
164 vlc_thread_t
vlc_thread_self (void)
169 void vlc_threads_setup (libvlc_int_t
*p_libvlc
)
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
);
184 static void *detached_thread(void *data
)
186 vlc_thread_t th
= data
;
190 vlc_cleanup_push(clean_detached_thread
, th
);
193 clean_detached_thread(th
);
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
;
209 vlc_cleanup_push(finish_joinable_thread
, th
);
211 ret
= th
->entry(th
->data
);
213 vlc_sem_post(&th
->finished
);
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
))
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
);
241 vlc_sem_init(&thread
->finished
, 0);
242 atomic_store(&thread
->killed
, false);
243 thread
->killable
= true;
244 thread
->entry
= entry
;
246 thread
->wait
.addr
= NULL
;
247 vlc_mutex_init(&thread
->wait
.lock
);
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
);
263 int vlc_clone (vlc_thread_t
*th
, void *(*entry
) (void *), void *data
,
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
,
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
;
297 void vlc_cancel (vlc_thread_t thread_id
)
301 atomic_store(&thread_id
->killed
, true);
303 vlc_mutex_lock(&thread_id
->wait
.lock
);
304 addr
= thread_id
->wait
.addr
;
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 */
318 int oldstate
= thread
->killable
;
319 thread
->killable
= false;
323 void vlc_restorecancel (int state
)
325 if (!thread
) /* not created by VLC, can't be cancelled */
328 thread
->killable
= state
;
331 void vlc_testcancel (void)
333 if (!thread
) /* not created by VLC, can't be cancelled */
335 if (!thread
->killable
)
337 if (!atomic_load(&thread
->killed
))
343 void vlc_control_cancel(int cmd
, ...)
345 vlc_thread_t th
= vlc_thread_self();
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
);
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
;
373 vlc_mutex_unlock(&th
->wait
.lock
);
378 vlc_assert_unreachable ();
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
);
410 if (unlikely(clock_gettime (CLOCK_MONOTONIC
, &ts
) != 0))
413 return (INT64_C(1000000) * ts
.tv_sec
) + (ts
.tv_nsec
/ 1000);
418 unsigned vlc_GetCPUCount(void)
420 return sysconf(_SC_NPROCESSORS_ONLN
);