qt: playlist: use item title if available
[vlc.git] / src / posix / thread.c
blob28975b1f934e7dd2f890148fd9f68fbdc16968ec
1 /*****************************************************************************
2 * thread.c : pthread back-end for LibVLC
3 *****************************************************************************
4 * Copyright (C) 1999-2009 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 <stdarg.h>
35 #include <stdatomic.h>
36 #include <stdnoreturn.h>
37 #include <signal.h>
38 #include <errno.h>
39 #include <time.h>
40 #include <assert.h>
42 #include <sys/types.h>
43 #include <unistd.h> /* fsync() */
44 #include <pthread.h>
45 #include <sched.h>
47 #ifdef HAVE_EXECINFO_H
48 # include <execinfo.h>
49 #endif
50 #if defined(__SunOS)
51 # include <sys/processor.h>
52 # include <sys/pset.h>
53 #endif
55 static unsigned vlc_clock_prec;
57 static void vlc_clock_setup_once (void)
59 struct timespec res;
60 if (unlikely(clock_getres(CLOCK_MONOTONIC, &res) != 0 || res.tv_sec != 0))
61 abort ();
62 vlc_clock_prec = (res.tv_nsec + 500) / 1000;
65 /**
66 * Print a backtrace to the standard error for debugging purpose.
68 void vlc_trace (const char *fn, const char *file, unsigned line)
70 fprintf (stderr, "at %s:%u in %s\n", file, line, fn);
71 fflush (stderr); /* needed before switch to low-level I/O */
72 #ifdef HAVE_BACKTRACE
73 void *stack[20];
74 int len = backtrace (stack, ARRAY_SIZE (stack) );
75 backtrace_symbols_fd (stack, len, 2);
76 #endif
77 fsync (2);
80 #ifndef NDEBUG
81 /**
82 * Reports a fatal error from the threading layer, for debugging purposes.
84 static void
85 vlc_thread_fatal (const char *action, int error,
86 const char *function, const char *file, unsigned line)
88 int canc = vlc_savecancel ();
89 fprintf (stderr, "LibVLC fatal error %s (%d) in thread %lu ",
90 action, error, vlc_thread_id ());
91 vlc_trace (function, file, line);
92 perror ("Thread error");
93 fflush (stderr);
95 vlc_restorecancel (canc);
96 abort ();
99 # define VLC_THREAD_ASSERT( action ) \
100 if (unlikely(val)) \
101 vlc_thread_fatal (action, val, __func__, __FILE__, __LINE__)
102 #else
103 # define VLC_THREAD_ASSERT( action ) ((void)val)
104 #endif
106 int vlc_threadvar_create (vlc_threadvar_t *key, void (*destr) (void *))
108 return pthread_key_create (key, destr);
111 void vlc_threadvar_delete (vlc_threadvar_t *p_tls)
113 pthread_key_delete (*p_tls);
116 int vlc_threadvar_set (vlc_threadvar_t key, void *value)
118 return pthread_setspecific (key, value);
121 void *vlc_threadvar_get (vlc_threadvar_t key)
123 return pthread_getspecific (key);
126 void vlc_threads_setup (libvlc_int_t *p_libvlc)
128 (void) p_libvlc;
131 static int vlc_clone_attr (vlc_thread_t *th, pthread_attr_t *attr,
132 void *(*entry) (void *), void *data, int priority)
134 int ret;
136 /* Block the signals that signals interface plugin handles.
137 * If the LibVLC caller wants to handle some signals by itself, it should
138 * block these before whenever invoking LibVLC. And it must obviously not
139 * start the VLC signals interface plugin.
141 * LibVLC will normally ignore any interruption caused by an asynchronous
142 * signal during a system call. But there may well be some buggy cases
143 * where it fails to handle EINTR (bug reports welcome). Some underlying
144 * libraries might also not handle EINTR properly.
146 sigset_t oldset;
148 sigset_t set;
149 sigemptyset (&set);
150 sigdelset (&set, SIGHUP);
151 sigaddset (&set, SIGINT);
152 sigaddset (&set, SIGQUIT);
153 sigaddset (&set, SIGTERM);
155 sigaddset (&set, SIGPIPE); /* We don't want this one, really! */
156 pthread_sigmask (SIG_BLOCK, &set, &oldset);
159 /* The thread stack size.
160 * The lower the value, the less address space per thread, the highest
161 * maximum simultaneous threads per process. Too low values will cause
162 * stack overflows and weird crashes. Set with caution. Also keep in mind
163 * that 64-bits platforms consume more stack than 32-bits one.
165 * Thanks to on-demand paging, thread stack size only affects address space
166 * consumption. In terms of memory, threads only use what they need
167 * (rounded up to the page boundary).
169 * For example, on Linux i386, the default is 2 mega-bytes, which supports
170 * about 320 threads per processes. */
171 #define VLC_STACKSIZE (128 * sizeof (void *) * 1024)
173 #ifdef VLC_STACKSIZE
174 ret = pthread_attr_setstacksize (attr, VLC_STACKSIZE);
175 assert (ret == 0); /* fails iif VLC_STACKSIZE is invalid */
176 #endif
178 ret = pthread_create(&th->handle, attr, entry, data);
179 pthread_sigmask (SIG_SETMASK, &oldset, NULL);
180 pthread_attr_destroy (attr);
181 (void) priority;
182 return ret;
185 int vlc_clone (vlc_thread_t *th, void *(*entry) (void *), void *data,
186 int priority)
188 pthread_attr_t attr;
190 pthread_attr_init (&attr);
191 return vlc_clone_attr (th, &attr, entry, data, priority);
194 void vlc_join(vlc_thread_t th, void **result)
196 int val = pthread_join(th.handle, result);
197 VLC_THREAD_ASSERT ("joining thread");
200 VLC_WEAK unsigned long vlc_thread_id(void)
202 return -1;
205 int vlc_set_priority (vlc_thread_t th, int priority)
207 (void) th; (void) priority;
208 return VLC_SUCCESS;
211 void vlc_cancel(vlc_thread_t th)
213 pthread_cancel(th.handle);
216 int vlc_savecancel (void)
218 int state;
219 int val = pthread_setcancelstate (PTHREAD_CANCEL_DISABLE, &state);
221 VLC_THREAD_ASSERT ("saving cancellation");
222 return state;
225 void vlc_restorecancel (int state)
227 #ifndef NDEBUG
228 int oldstate, val;
230 val = pthread_setcancelstate (state, &oldstate);
231 /* This should fail if an invalid value for given for state */
232 VLC_THREAD_ASSERT ("restoring cancellation");
234 if (unlikely(oldstate != PTHREAD_CANCEL_DISABLE))
235 vlc_thread_fatal ("restoring cancellation while not disabled", EINVAL,
236 __func__, __FILE__, __LINE__);
237 #else
238 pthread_setcancelstate (state, NULL);
239 #endif
242 void vlc_testcancel (void)
244 pthread_testcancel ();
247 vlc_tick_t vlc_tick_now (void)
249 struct timespec ts;
251 if (unlikely(clock_gettime(CLOCK_MONOTONIC, &ts) != 0))
252 abort ();
254 return vlc_tick_from_timespec( &ts );
257 #undef vlc_tick_wait
258 void vlc_tick_wait (vlc_tick_t deadline)
260 static pthread_once_t vlc_clock_once = PTHREAD_ONCE_INIT;
262 /* If the deadline is already elapsed, or within the clock precision,
263 * do not even bother the system timer. */
264 pthread_once(&vlc_clock_once, vlc_clock_setup_once);
265 deadline -= vlc_clock_prec;
267 struct timespec ts = timespec_from_vlc_tick (deadline);
269 while (clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &ts, NULL) == EINTR);
272 #undef vlc_tick_sleep
273 void vlc_tick_sleep (vlc_tick_t delay)
275 struct timespec ts = timespec_from_vlc_tick (delay);
277 while (clock_nanosleep(CLOCK_MONOTONIC, 0, &ts, &ts) == EINTR);
280 unsigned vlc_GetCPUCount(void)
282 #if defined(HAVE_SCHED_GETAFFINITY)
283 cpu_set_t cpu;
285 CPU_ZERO(&cpu);
286 if (sched_getaffinity (0, sizeof (cpu), &cpu) < 0)
287 return 1;
289 return CPU_COUNT (&cpu);
291 #elif defined(__SunOS)
292 unsigned count = 0;
293 int type;
294 u_int numcpus;
295 processor_info_t cpuinfo;
297 processorid_t *cpulist = vlc_alloc (sysconf(_SC_NPROCESSORS_MAX), sizeof (*cpulist));
298 if (unlikely(cpulist == NULL))
299 return 1;
301 if (pset_info(PS_MYID, &type, &numcpus, cpulist) == 0)
303 for (u_int i = 0; i < numcpus; i++)
304 if (processor_info (cpulist[i], &cpuinfo) == 0)
305 count += (cpuinfo.pi_state == P_ONLINE);
307 else
308 count = sysconf (_SC_NPROCESSORS_ONLN);
309 free (cpulist);
310 return count ? count : 1;
311 #elif defined(_SC_NPROCESSORS_ONLN)
312 return sysconf(_SC_NPROCESSORS_ONLN);
313 #elif defined(_SC_NPROCESSORS_CONF)
314 return sysconf(_SC_NPROCESSORS_CONF);
315 #else
316 # warning "vlc_GetCPUCount is not implemented for your platform"
317 return 1;
318 #endif