]> git.sesse.net Git - vlc/blobdiff - src/misc/pthread.c
vlc_detach: releases a thread handle asynchronously
[vlc] / src / misc / pthread.c
index bca5afd99ca9ccd08d3951b382cb90825e462d8f..d3f1dbd1e8d827878127d7aaca779b1093b38888 100644 (file)
@@ -356,7 +356,7 @@ void vlc_cond_wait (vlc_cond_t *p_condvar, vlc_mutex_t *p_mutex)
 int vlc_cond_timedwait (vlc_cond_t *p_condvar, vlc_mutex_t *p_mutex,
                         mtime_t deadline)
 {
-#ifdef __APPLE__
+#if defined(__APPLE__) && !defined(__powerpc__) && !defined( __ppc__ ) && !defined( __ppc64__ )
     /* mdate() is mac_absolute_time on OSX, which we must convert to do
      * the same base than gettimeofday() which pthread_cond_timedwait
      * relies on. */
@@ -375,6 +375,51 @@ int vlc_cond_timedwait (vlc_cond_t *p_condvar, vlc_mutex_t *p_mutex,
     return val;
 }
 
+/**
+ * Initializes a read/write lock.
+ */
+void vlc_rwlock_init (vlc_rwlock_t *lock)
+{
+    if (pthread_rwlock_init (lock, NULL))
+        abort ();
+}
+
+/**
+ * Destroys an initialized unused read/write lock.
+ */
+void vlc_rwlock_destroy (vlc_rwlock_t *lock)
+{
+    int val = pthread_rwlock_destroy (lock);
+    VLC_THREAD_ASSERT ("destroying R/W lock");
+}
+
+/**
+ * Acquires a read/write lock for reading. Recursion is allowed.
+ */
+void vlc_rwlock_rdlock (vlc_rwlock_t *lock)
+{
+    int val = pthread_rwlock_rdlock (lock);
+    VLC_THREAD_ASSERT ("acquiring R/W lock for reading");
+}
+
+/**
+ * Acquires a read/write lock for writing. Recursion is not allowed.
+ */
+void vlc_rwlock_wrlock (vlc_rwlock_t *lock)
+{
+    int val = pthread_rwlock_wrlock (lock);
+    VLC_THREAD_ASSERT ("acquiring R/W lock for writing");
+}
+
+/**
+ * Releases a read/write lock.
+ */
+void vlc_rwlock_unlock (vlc_rwlock_t *lock)
+{
+    int val = pthread_rwlock_unlock (lock);
+    VLC_THREAD_ASSERT ("releasing R/W lock");
+}
+
 /**
  * Allocates a thread-specific variable.
  * @param key where to store the thread-specific variable handle
@@ -416,6 +461,31 @@ void *vlc_threadvar_get (vlc_threadvar_t key)
     return pthread_getspecific (key);
 }
 
+static bool rt_priorities = false;
+static int rt_offset;
+
+void vlc_threads_setup (libvlc_int_t *p_libvlc)
+{
+    static vlc_mutex_t lock = VLC_STATIC_MUTEX;
+    static bool initialized = false;
+
+    vlc_mutex_lock (&lock);
+    /* Initializes real-time priorities before any thread is created,
+     * just once per process. */
+    if (!initialized)
+    {
+#ifndef __APPLE__
+        if (config_GetInt (p_libvlc, "rt-priority"))
+#endif
+        {
+            rt_offset = config_GetInt (p_libvlc, "rt-offset");
+            rt_priorities = true;
+        }
+        initialized = true;
+    }
+    vlc_mutex_unlock (&lock);
+}
+
 /**
  * Creates and starts new thread.
  *
@@ -459,8 +529,9 @@ int vlc_clone (vlc_thread_t *p_handle, void * (*entry) (void *), void *data,
 #if defined (_POSIX_PRIORITY_SCHEDULING) && (_POSIX_PRIORITY_SCHEDULING >= 0) \
  && defined (_POSIX_THREAD_PRIORITY_SCHEDULING) \
  && (_POSIX_THREAD_PRIORITY_SCHEDULING >= 0)
+    if (rt_priorities)
     {
-        struct sched_param sp = { .sched_priority = priority, };
+        struct sched_param sp = { .sched_priority = priority + rt_offset, };
         int policy;
 
         if (sp.sched_priority <= 0)
@@ -513,13 +584,15 @@ void vlc_cancel (vlc_thread_t thread_id)
 }
 
 /**
- * Waits for a thread to complete (if needed), and destroys it.
+ * Waits for a thread to complete (if needed), then destroys it.
  * This is a cancellation point; in case of cancellation, the join does _not_
  * occur.
+ * @warning
+ * A thread cannot join itself (normally VLC will abort if this is attempted).
+ * Also, a detached thread <b>cannot</b> be joined.
  *
  * @param handle thread handle
  * @param p_result [OUT] pointer to write the thread return value or NULL
- * @return 0 on success, a standard error code otherwise.
  */
 void vlc_join (vlc_thread_t handle, void **result)
 {
@@ -527,6 +600,28 @@ void vlc_join (vlc_thread_t handle, void **result)
     VLC_THREAD_ASSERT ("joining thread");
 }
 
+/**
+ * Detaches a thread. When the specified thread completes, it will be
+ * automatically destroyed (in particular, its stack will be reclaimed),
+ * instead of waiting for another thread to call vlc_join(). If the thread has
+ * already completed, it will be destroyed immediately.
+ *
+ * When a thread performs some work asynchronously and may complete much
+ * earlier than it can be joined, detaching the thread can save memory.
+ * However, care must be taken that any resources used by a detached thread
+ * remains valid until the thread completes. This will typically involve some
+ * kind of thread-safe signaling.
+ *
+ * A thread may detach itself.
+ *
+ * @param handle thread handle
+ */
+void vlc_detach (vlc_thread_t handle)
+{
+    int val = pthread_detach (handle);
+    VLC_THREAD_ASSERT ("detaching thread");
+}
+
 /**
  * Save the current cancellation state (enabled or disabled), then disable
  * cancellation for the calling thread.
@@ -583,3 +678,140 @@ void vlc_control_cancel (int cmd, ...)
     (void) cmd;
     assert (0);
 }
+
+#ifndef HAVE_POSIX_TIMER
+/* We have no fallback currently. We'll just crash on timer API usage. */
+static void timer_not_supported(void)
+{
+    fprintf(stderr, "*** Error: Timer API is not supported on this platform.\n");
+    abort();
+}
+#endif
+
+static void vlc_timer_do (union sigval val)
+{
+    vlc_timer_t *id = val.sival_ptr;
+    id->func (id->data);
+}
+
+/**
+ * Initializes an asynchronous timer.
+ * @warning Asynchronous timers are processed from an unspecified thread, and
+ * a timer is only serialized against itself.
+ *
+ * @param id pointer to timer to be initialized
+ * @param func function that the timer will call
+ * @param data parameter for the timer function
+ * @return 0 on success, a system error code otherwise.
+ */
+int vlc_timer_create (vlc_timer_t *id, void (*func) (void *), void *data)
+{
+#ifdef HAVE_POSIX_TIMER
+    struct sigevent ev;
+
+    memset (&ev, 0, sizeof (ev));
+    ev.sigev_notify = SIGEV_THREAD;
+    ev.sigev_value.sival_ptr = id;
+    ev.sigev_notify_function = vlc_timer_do;
+    ev.sigev_notify_attributes = NULL;
+    id->func = func;
+    id->data = data;
+
+#if (_POSIX_CLOCK_SELECTION >= 0)
+    if (timer_create (CLOCK_MONOTONIC, &ev, &id->handle))
+#else
+    if (timer_create (CLOCK_REALTIME, &ev, &id->handle))
+#endif
+        return errno;
+
+    return 0;
+#else
+    timer_not_supported();
+    return 0;
+#endif
+}
+
+/**
+ * Destroys an initialized timer. If needed, the timer is first disarmed.
+ * This function is undefined if the specified timer is not initialized.
+ *
+ * @warning This function <b>must</b> be called before the timer data can be
+ * freed and before the timer callback function can be unloaded.
+ *
+ * @param timer to destroy
+ */
+void vlc_timer_destroy (vlc_timer_t *id)
+{
+#ifdef HAVE_POSIX_TIMER
+    int val = timer_delete (id->handle);
+    VLC_THREAD_ASSERT ("deleting timer");
+#else
+    timer_not_supported();
+#endif
+}
+
+/**
+ * Arm or disarm an initialized timer.
+ * This functions overrides any previous call to itself.
+ *
+ * @note A timer can fire later than requested due to system scheduling
+ * limitations. An interval timer can fail to trigger sometimes, either because
+ * the system is busy or suspended, or because a previous iteration of the
+ * timer is still running. See also vlc_timer_getoverrun().
+ *
+ * @param id initialized timer pointer
+ * @param absolute the timer value origin is the same as mdate() if true,
+ *                 the timer value is relative to now if false.
+ * @param value zero to disarm the timer, otherwise the initial time to wait
+ *              before firing the timer.
+ * @param interval zero to fire the timer just once, otherwise the timer
+ *                 repetition interval.
+ */
+void vlc_timer_schedule (vlc_timer_t *id, bool absolute,
+                         mtime_t value, mtime_t interval)
+{
+#ifdef HAVE_POSIX_TIMER
+    lldiv_t vad = lldiv (value, CLOCK_FREQ);
+    lldiv_t itd = lldiv (interval, CLOCK_FREQ);
+    struct itimerspec it = {
+        .it_interval = {
+            .tv_sec = itd.quot,
+            .tv_nsec = (1000000000 / CLOCK_FREQ) * itd.rem,
+        },
+        .it_value = {
+            .tv_sec = vad.quot,
+            .tv_nsec = (1000000000 / CLOCK_FREQ) * vad.rem,
+        },
+    };
+    int flags = absolute ? TIMER_ABSTIME : 0;
+
+    int val = timer_settime (id->handle, flags, &it, NULL);
+    VLC_THREAD_ASSERT ("scheduling timer");
+#else
+    timer_not_supported();
+#endif
+}
+
+/**
+ * @param id initialized timer pointer
+ * @return the timer overrun counter, i.e. the number of times that the timer
+ * should have run but did not since the last actual run. If all is well, this
+ * is zero.
+ */
+unsigned vlc_timer_getoverrun (const vlc_timer_t *id)
+{
+#ifdef HAVE_POSIX_TIMER
+    int val = timer_getoverrun (id->handle);
+#ifndef NDEBUG
+    if (val == -1)
+    {
+        val = errno;
+        VLC_THREAD_ASSERT ("fetching timer overrun counter");
+    }
+#endif
+    return val;
+#else
+    timer_not_supported();
+    return 0;
+#endif
+}