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>
37 #include <stdnoreturn.h>
41 #include <sys/types.h>
42 #include <unistd.h> /* fsync() */
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
);
60 # define VLC_THREAD_ASSERT( action ) do { \
61 if (unlikely(val)) { \
62 vlc_thread_fatal_print (action, val, __func__, __FILE__, __LINE__); \
67 # define VLC_THREAD_ASSERT( action ) ((void)val)
70 void vlc_once(vlc_once_t
*once
, void (*cb
)(void))
72 int val
= pthread_once(once
, cb
);
73 VLC_THREAD_ASSERT("initializing once");
80 void *(*entry
)(void*);
85 atomic_uint
*addr
; /// Non-null if waiting on futex
86 vlc_mutex_t lock
; /// Protects futex address
93 static thread_local
struct vlc_thread
*thread
= NULL
;
95 void vlc_threads_setup (libvlc_int_t
*p_libvlc
)
101 static void clean_detached_thread(void *data
)
103 struct vlc_thread
*th
= data
;
105 /* release thread handle */
109 static void *detached_thread(void *data
)
111 vlc_thread_t th
= data
;
115 vlc_cleanup_push(clean_detached_thread
, th
);
118 clean_detached_thread(th
);
122 static void *joinable_thread(void *data
)
124 vlc_thread_t th
= data
;
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
))
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
;
156 thread
->wait
.addr
= NULL
;
157 vlc_mutex_init(&thread
->wait
.lock
);
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
);
173 int vlc_clone (vlc_thread_t
*th
, void *(*entry
) (void *), void *data
,
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
,
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
;
204 void vlc_cancel (vlc_thread_t thread_id
)
208 atomic_store(&thread_id
->killed
, true);
210 vlc_mutex_lock(&thread_id
->wait
.lock
);
211 addr
= thread_id
->wait
.addr
;
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 */
225 int oldstate
= thread
->killable
;
226 thread
->killable
= false;
230 void vlc_restorecancel (int state
)
232 if (!thread
) /* not created by VLC, can't be cancelled */
235 thread
->killable
= state
;
238 void vlc_testcancel (void)
240 if (!thread
) /* not created by VLC, can't be cancelled */
242 if (!thread
->killable
)
244 if (!atomic_load(&thread
->killed
))
250 void vlc_cancel_addr_set(atomic_uint
*addr
)
252 vlc_thread_t th
= thread
;
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
;
268 vlc_mutex_lock(&th
->wait
.lock
);
269 assert(th
->wait
.addr
== addr
);
270 th
->wait
.addr
= NULL
;
272 vlc_mutex_unlock(&th
->wait
.lock
);
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
);
298 vlc_tick_t
vlc_tick_now (void)
302 if (unlikely(clock_gettime (CLOCK_MONOTONIC
, &ts
) != 0))
305 return vlc_tick_from_timespec( &ts
);
310 unsigned vlc_GetCPUCount(void)
312 return sysconf(_SC_NPROCESSORS_ONLN
);