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)
74 void *(*entry
)(void*);
79 atomic_uint
*addr
; /// Non-null if waiting on futex
80 vlc_mutex_t lock
; /// Protects futex address
87 static thread_local
struct vlc_thread
*thread
= NULL
;
89 void vlc_threads_setup (libvlc_int_t
*p_libvlc
)
95 static void clean_detached_thread(void *data
)
97 struct vlc_thread
*th
= data
;
99 /* release thread handle */
103 static void *detached_thread(void *data
)
105 vlc_thread_t th
= data
;
109 vlc_cleanup_push(clean_detached_thread
, th
);
112 clean_detached_thread(th
);
116 static void *joinable_thread(void *data
)
118 vlc_thread_t th
= data
;
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
))
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
;
150 thread
->wait
.addr
= NULL
;
151 vlc_mutex_init(&thread
->wait
.lock
);
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
);
167 int vlc_clone (vlc_thread_t
*th
, void *(*entry
) (void *), void *data
,
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
,
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
;
198 void vlc_cancel (vlc_thread_t thread_id
)
202 atomic_store(&thread_id
->killed
, true);
204 vlc_mutex_lock(&thread_id
->wait
.lock
);
205 addr
= thread_id
->wait
.addr
;
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 */
219 int oldstate
= thread
->killable
;
220 thread
->killable
= false;
224 void vlc_restorecancel (int state
)
226 if (!thread
) /* not created by VLC, can't be cancelled */
229 thread
->killable
= state
;
232 void vlc_testcancel (void)
234 if (!thread
) /* not created by VLC, can't be cancelled */
236 if (!thread
->killable
)
238 if (!atomic_load(&thread
->killed
))
244 void vlc_cancel_addr_set(atomic_uint
*addr
)
246 vlc_thread_t th
= thread
;
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
;
262 vlc_mutex_lock(&th
->wait
.lock
);
263 assert(th
->wait
.addr
== addr
);
264 th
->wait
.addr
= NULL
;
266 vlc_mutex_unlock(&th
->wait
.lock
);
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
);
292 vlc_tick_t
vlc_tick_now (void)
296 if (unlikely(clock_gettime (CLOCK_MONOTONIC
, &ts
) != 0))
299 return vlc_tick_from_timespec( &ts
);
304 unsigned vlc_GetCPUCount(void)
306 return sysconf(_SC_NPROCESSORS_ONLN
);