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>
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>
35 #include <stdatomic.h>
36 #include <stdnoreturn.h>
42 #include <sys/types.h>
43 #include <unistd.h> /* fsync() */
47 #ifdef HAVE_EXECINFO_H
48 # include <execinfo.h>
51 # include <sys/processor.h>
52 # include <sys/pset.h>
55 static unsigned vlc_clock_prec
;
57 static void vlc_clock_setup_once (void)
60 if (unlikely(clock_getres(CLOCK_MONOTONIC
, &res
) != 0 || res
.tv_sec
!= 0))
62 vlc_clock_prec
= (res
.tv_nsec
+ 500) / 1000;
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 */
74 int len
= backtrace (stack
, ARRAY_SIZE (stack
) );
75 backtrace_symbols_fd (stack
, len
, 2);
82 * Reports a fatal error from the threading layer, for debugging purposes.
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");
95 vlc_restorecancel (canc
);
99 # define VLC_THREAD_ASSERT( action ) \
101 vlc_thread_fatal (action, val, __func__, __FILE__, __LINE__)
103 # define VLC_THREAD_ASSERT( action ) ((void)val)
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
)
131 static int vlc_clone_attr (vlc_thread_t
*th
, pthread_attr_t
*attr
,
132 void *(*entry
) (void *), void *data
, int priority
)
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.
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)
174 ret
= pthread_attr_setstacksize (attr
, VLC_STACKSIZE
);
175 assert (ret
== 0); /* fails iif VLC_STACKSIZE is invalid */
178 ret
= pthread_create(&th
->handle
, attr
, entry
, data
);
179 pthread_sigmask (SIG_SETMASK
, &oldset
, NULL
);
180 pthread_attr_destroy (attr
);
185 int vlc_clone (vlc_thread_t
*th
, void *(*entry
) (void *), void *data
,
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)
205 int vlc_set_priority (vlc_thread_t th
, int priority
)
207 (void) th
; (void) priority
;
211 void vlc_cancel(vlc_thread_t th
)
213 pthread_cancel(th
.handle
);
216 int vlc_savecancel (void)
219 int val
= pthread_setcancelstate (PTHREAD_CANCEL_DISABLE
, &state
);
221 VLC_THREAD_ASSERT ("saving cancellation");
225 void vlc_restorecancel (int state
)
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__
);
238 pthread_setcancelstate (state
, NULL
);
242 void vlc_testcancel (void)
244 pthread_testcancel ();
247 vlc_tick_t
vlc_tick_now (void)
251 if (unlikely(clock_gettime(CLOCK_MONOTONIC
, &ts
) != 0))
254 return vlc_tick_from_timespec( &ts
);
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)
286 if (sched_getaffinity (0, sizeof (cpu
), &cpu
) < 0)
289 return CPU_COUNT (&cpu
);
291 #elif defined(__SunOS)
295 processor_info_t cpuinfo
;
297 processorid_t
*cpulist
= vlc_alloc (sysconf(_SC_NPROCESSORS_MAX
), sizeof (*cpulist
));
298 if (unlikely(cpulist
== NULL
))
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
);
308 count
= sysconf (_SC_NPROCESSORS_ONLN
);
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
);
316 # warning "vlc_GetCPUCount is not implemented for your platform"