[vlc-devel] [PATCH 2/2] thread: remove redundant darwin thread code

Marvin Scholz epirat07 at gmail.com
Sat Feb 22 03:23:39 CET 2020


A lot of the code for darwin threading was exactly the same as the
posix one, so instead of having a copy of the code that needs to be
maintained, use the posix thread code
---
 include/vlc_threads.h |  10 +-
 src/Makefile.am       |   3 +-
 src/darwin/thread.c   | 230 ------------------------------------------
 src/posix/thread.c    |   2 +
 4 files changed, 10 insertions(+), 235 deletions(-)

diff --git a/include/vlc_threads.h b/include/vlc_threads.h
index d04da93629..bfcf3e21c9 100644
--- a/include/vlc_threads.h
+++ b/include/vlc_threads.h
@@ -186,12 +186,14 @@ static inline int vlc_poll (struct pollfd *fds, unsigned nfds, int timeout)
 # define _APPLE_C_SOURCE    1 /* Proper pthread semantics on OSX */
 # include <unistd.h>
 # include <pthread.h>
-/* Unnamed POSIX semaphores not supported on Mac OS X */
-# include <mach/semaphore.h>
-# include <mach/task.h>
+
 # define LIBVLC_USE_PTHREAD_CLEANUP   1
 
-typedef pthread_t       vlc_thread_t;
+typedef struct
+{
+    pthread_t handle;
+} vlc_thread_t;
+
 #define VLC_THREAD_CANCELED PTHREAD_CANCELED
 typedef pthread_t       vlc_osthread_t;
 #define vlc_thread_equal(a,b) pthread_equal(a,b)
diff --git a/src/Makefile.am b/src/Makefile.am
index bf91159fcc..3fbe22253b 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -441,7 +441,8 @@ if HAVE_DARWIN
 libvlccore_la_SOURCES += \
 	darwin/error.c \
 	darwin/specific.c \
-	darwin/thread.c
+	darwin/thread.c \
+	posix/thread.c
 libvlccore_objc_la_SOURCES = \
 	darwin/dirs.m \
 	darwin/netconf.m
diff --git a/src/darwin/thread.c b/src/darwin/thread.c
index 3c796fc06e..774ff1ebaf 100644
--- a/src/darwin/thread.c
+++ b/src/darwin/thread.c
@@ -64,231 +64,6 @@ static pthread_once_t vlc_clock_once = PTHREAD_ONCE_INIT;
 #define vlc_clock_setup() \
     pthread_once(&vlc_clock_once, vlc_clock_setup_once)
 
-/* Print a backtrace to the standard error for debugging purpose. */
-void vlc_trace (const char *fn, const char *file, unsigned line)
-{
-     fprintf (stderr, "at %s:%u in %s\n", file, line, fn);
-     fflush (stderr); /* needed before switch to low-level I/O */
-     void *stack[20];
-     int len = backtrace (stack, sizeof (stack) / sizeof (stack[0]));
-     backtrace_symbols_fd (stack, len, 2);
-     fsync (2);
-}
-
-#ifndef NDEBUG
-/* Reports a fatal error from the threading layer, for debugging purposes. */
-static void
-vlc_thread_fatal (const char *action, int error,
-                  const char *function, const char *file, unsigned line)
-{
-    int canc = vlc_savecancel ();
-    fprintf (stderr, "LibVLC fatal error %s (%d) in thread %lu ",
-             action, error, vlc_thread_id ());
-    vlc_trace (function, file, line);
-
-    char buf[1000];
-    const char *msg;
-
-    switch (strerror_r (error, buf, sizeof (buf)))
-    {
-        case 0:
-            msg = buf;
-            break;
-        case ERANGE: /* should never happen */
-            msg = "unknown (too big to display)";
-            break;
-        default:
-            msg = "unknown (invalid error number)";
-            break;
-    }
-    fprintf (stderr, " Error message: %s\n", msg);
-    fflush (stderr);
-
-    vlc_restorecancel (canc);
-    abort ();
-}
-
-# define VLC_THREAD_ASSERT( action ) \
-    if (unlikely(val)) \
-        vlc_thread_fatal (action, val, __func__, __FILE__, __LINE__)
-#else
-# define VLC_THREAD_ASSERT( action ) ((void)val)
-#endif
-
-void vlc_rwlock_init (vlc_rwlock_t *lock)
-{
-    if (unlikely(pthread_rwlock_init (lock, NULL)))
-        abort ();
-}
-
-void vlc_rwlock_destroy (vlc_rwlock_t *lock)
-{
-    int val = pthread_rwlock_destroy (lock);
-    VLC_THREAD_ASSERT ("destroying R/W lock");
-}
-
-void vlc_rwlock_rdlock (vlc_rwlock_t *lock)
-{
-    int val = pthread_rwlock_rdlock (lock);
-    VLC_THREAD_ASSERT ("acquiring R/W lock for reading");
-}
-
-void vlc_rwlock_wrlock (vlc_rwlock_t *lock)
-{
-    int val = pthread_rwlock_wrlock (lock);
-    VLC_THREAD_ASSERT ("acquiring R/W lock for writing");
-}
-
-void vlc_rwlock_unlock (vlc_rwlock_t *lock)
-{
-    int val = pthread_rwlock_unlock (lock);
-    VLC_THREAD_ASSERT ("releasing R/W lock");
-}
-
-void vlc_once(vlc_once_t *once, void (*cb)(void))
-{
-    int val = pthread_once(once, cb);
-    VLC_THREAD_ASSERT("initializing once");
-}
-
-int vlc_threadvar_create (vlc_threadvar_t *key, void (*destr) (void *))
-{
-    return pthread_key_create (key, destr);
-}
-
-void vlc_threadvar_delete (vlc_threadvar_t *p_tls)
-{
-    pthread_key_delete (*p_tls);
-}
-
-int vlc_threadvar_set (vlc_threadvar_t key, void *value)
-{
-    return pthread_setspecific (key, value);
-}
-
-void *vlc_threadvar_get (vlc_threadvar_t key)
-{
-    return pthread_getspecific (key);
-}
-
-void vlc_threads_setup (libvlc_int_t *p_libvlc)
-{
-    (void) p_libvlc;
-}
-
-static int vlc_clone_attr (vlc_thread_t *th, pthread_attr_t *attr,
-                           void *(*entry) (void *), void *data, int priority)
-{
-    int ret;
-
-    sigset_t oldset;
-    {
-        sigset_t set;
-        sigemptyset (&set);
-        sigdelset (&set, SIGHUP);
-        sigaddset (&set, SIGINT);
-        sigaddset (&set, SIGQUIT);
-        sigaddset (&set, SIGTERM);
-
-        sigaddset (&set, SIGPIPE); /* We don't want this one, really! */
-        pthread_sigmask (SIG_BLOCK, &set, &oldset);
-    }
-
-    (void) priority;
-
-#define VLC_STACKSIZE (128 * sizeof (void *) * 1024)
-
-#ifdef VLC_STACKSIZE
-    ret = pthread_attr_setstacksize (attr, VLC_STACKSIZE);
-    assert (ret == 0); /* fails iif VLC_STACKSIZE is invalid */
-#endif
-
-    ret = pthread_create (th, attr, entry, data);
-    pthread_sigmask (SIG_SETMASK, &oldset, NULL);
-    pthread_attr_destroy (attr);
-    return ret;
-}
-
-int vlc_clone (vlc_thread_t *th, void *(*entry) (void *), void *data,
-               int priority)
-{
-    pthread_attr_t attr;
-
-    pthread_attr_init (&attr);
-    return vlc_clone_attr (th, &attr, entry, data, priority);
-}
-
-void vlc_join (vlc_thread_t handle, void **result)
-{
-    int val = pthread_join (handle, result);
-    VLC_THREAD_ASSERT ("joining thread");
-}
-
-int vlc_clone_detach (vlc_thread_t *th, void *(*entry) (void *), void *data,
-                      int priority)
-{
-    vlc_thread_t dummy;
-    pthread_attr_t attr;
-
-    if (th == NULL)
-        th = &dummy;
-
-    pthread_attr_init (&attr);
-    pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
-    return vlc_clone_attr (th, &attr, entry, data, priority);
-}
-
-vlc_thread_t vlc_thread_self (void)
-{
-    return pthread_self ();
-}
-
-unsigned long vlc_thread_id (void)
-{
-    return -1;
-}
-
-int vlc_set_priority (vlc_thread_t th, int priority)
-{
-    (void) th; (void) priority;
-    return VLC_SUCCESS;
-}
-
-void vlc_cancel (vlc_thread_t thread_id)
-{
-    pthread_cancel (thread_id);
-}
-
-int vlc_savecancel (void)
-{
-    int state;
-    int val = pthread_setcancelstate (PTHREAD_CANCEL_DISABLE, &state);
-
-    VLC_THREAD_ASSERT ("saving cancellation");
-    return state;
-}
-
-void vlc_restorecancel (int state)
-{
-#ifndef NDEBUG
-    int oldstate, val;
-
-    val = pthread_setcancelstate (state, &oldstate);
-    VLC_THREAD_ASSERT ("restoring cancellation");
-
-    if (unlikely(oldstate != PTHREAD_CANCEL_DISABLE))
-         vlc_thread_fatal ("restoring cancellation while not disabled", EINVAL,
-                           __func__, __FILE__, __LINE__);
-#else
-    pthread_setcancelstate (state, NULL);
-#endif
-}
-
-void vlc_testcancel (void)
-{
-    pthread_testcancel ();
-}
-
 vlc_tick_t vlc_tick_now (void)
 {
     vlc_clock_setup();
@@ -317,8 +92,3 @@ void vlc_tick_sleep (vlc_tick_t delay)
     while (nanosleep (&ts, &ts) == -1)
         assert (errno == EINTR);
 }
-
-unsigned vlc_GetCPUCount(void)
-{
-    return sysconf(_SC_NPROCESSORS_CONF);
-}
diff --git a/src/posix/thread.c b/src/posix/thread.c
index 32d48b5cc2..85407702c1 100644
--- a/src/posix/thread.c
+++ b/src/posix/thread.c
@@ -386,6 +386,7 @@ void vlc_testcancel (void)
     pthread_testcancel ();
 }
 
+#if !defined(__APPLE__)
 vlc_tick_t vlc_tick_now (void)
 {
     struct timespec ts;
@@ -418,6 +419,7 @@ void vlc_tick_sleep (vlc_tick_t delay)
 
     while (clock_nanosleep(CLOCK_MONOTONIC, 0, &ts, &ts) == EINTR);
 }
+#endif
 
 unsigned vlc_GetCPUCount(void)
 {
-- 
2.21.1 (Apple Git-122.3)



More information about the vlc-devel mailing list