+
+
+struct vlc_timer
+{
+ vlc_thread_t thread;
+ vlc_mutex_t lock;
+ vlc_cond_t wait;
+ void (*func) (void *);
+ void *data;
+ mtime_t value, interval;
+ unsigned users;
+ unsigned overruns;
+};
+
+static void *vlc_timer_do (void *data)
+{
+ struct vlc_timer *timer = data;
+
+ timer->func (timer->data);
+
+ vlc_mutex_lock (&timer->lock);
+ assert (timer->users > 0);
+ if (--timer->users == 0)
+ vlc_cond_signal (&timer->wait);
+ vlc_mutex_unlock (&timer->lock);
+ return NULL;
+}
+
+static void *vlc_timer_thread (void *data)
+{
+ struct vlc_timer *timer = data;
+ mtime_t value, interval;
+
+ vlc_mutex_lock (&timer->lock);
+ value = timer->value;
+ interval = timer->interval;
+ vlc_mutex_unlock (&timer->lock);
+
+ for (;;)
+ {
+ vlc_thread_t th;
+
+ mwait (value);
+
+ vlc_mutex_lock (&timer->lock);
+ if (vlc_clone (&th, vlc_timer_do, timer, VLC_THREAD_PRIORITY_INPUT))
+ timer->overruns++;
+ else
+ {
+ vlc_detach (th);
+ timer->users++;
+ }
+ vlc_mutex_unlock (&timer->lock);
+
+ if (interval == 0)
+ return NULL;
+
+ value += interval;
+ }
+}
+
+/**
+ * Initializes an asynchronous timer.
+ * @warning Asynchronous timers are processed from an unspecified thread.
+ * Also, multiple occurences of an interval timer can run concurrently.
+ *
+ * @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)
+{
+ struct vlc_timer *timer = malloc (sizeof (*timer));
+
+ if (unlikely(timer == NULL))
+ return ENOMEM;
+ vlc_mutex_init (&timer->lock);
+ vlc_cond_init (&timer->wait);
+ assert (func);
+ timer->func = func;
+ timer->data = data;
+ timer->value = 0;
+ timer->interval = 0;
+ timer->users = 0;
+ timer->overruns = 0;
+ *id = timer;
+ return 0;
+}
+
+/**
+ * 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 timer to destroy
+ */
+void vlc_timer_destroy (vlc_timer_t timer)
+{
+ vlc_timer_schedule (timer, false, 0, 0);
+ vlc_mutex_lock (&timer->lock);
+ while (timer->users != 0)
+ vlc_cond_wait (&timer->wait, &timer->lock);
+ vlc_mutex_unlock (&timer->lock);
+
+ vlc_cond_destroy (&timer->wait);
+ vlc_mutex_destroy (&timer->lock);
+ free (timer);
+}
+
+/**
+ * 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 timer initialized timer
+ * @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 timer, bool absolute,
+ mtime_t value, mtime_t interval)
+{
+ static vlc_mutex_t lock = VLC_STATIC_MUTEX;
+
+ vlc_mutex_lock (&lock);
+ vlc_mutex_lock (&timer->lock);
+ if (timer->value)
+ {
+ vlc_mutex_unlock (&timer->lock);
+ vlc_cancel (timer->thread);
+ vlc_join (timer->thread, NULL);
+ vlc_mutex_lock (&timer->lock);
+ timer->value = 0;
+ }
+ if ((value != 0)
+ && (vlc_clone (&timer->thread, vlc_timer_thread, timer,
+ VLC_THREAD_PRIORITY_INPUT) == 0))
+ {
+ timer->value = (absolute ? 0 : mdate ()) + value;
+ timer->interval = interval;
+ }
+ vlc_mutex_unlock (&timer->lock);
+ vlc_mutex_unlock (&lock);
+}
+
+/**
+ * Fetch and reset the overrun counter for a timer.
+ * @param timer initialized timer
+ * @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 (vlc_timer_t timer)
+{
+ unsigned ret;
+
+ vlc_mutex_lock (&timer->lock);
+ ret = timer->overruns;
+ timer->overruns = 0;
+ vlc_mutex_unlock (&timer->lock);
+ return ret;
+}