[vlc-commits] queue: helpers for killable queues

Rémi Denis-Courmont git at videolan.org
Mon Apr 13 13:05:21 CEST 2020


vlc | branch: master | Rémi Denis-Courmont <remi at remlab.net> | Sat Apr 11 22:06:20 2020 +0300| [04cf487945a8d988c1be1da6aa54227768cb5127] | committer: Rémi Denis-Courmont

queue: helpers for killable queues

This adds two functions to simplify the common case of a thread waiting on
a queue until it is terminated.

> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=04cf487945a8d988c1be1da6aa54227768cb5127
---

 include/vlc_queue.h | 41 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 41 insertions(+)

diff --git a/include/vlc_queue.h b/include/vlc_queue.h
index 44222c1ae0..c651217396 100644
--- a/include/vlc_queue.h
+++ b/include/vlc_queue.h
@@ -213,5 +213,46 @@ VLC_API void *vlc_queue_Dequeue(vlc_queue_t *) VLC_USED;
  */
 VLC_API void *vlc_queue_DequeueAll(vlc_queue_t *) VLC_USED;
 
+/**
+ * @defgroup queue_killable Killable queues
+ *
+ * Thread-safe queues with an end flag.
+ *
+ * @{
+ */
+
+/**
+ * Marks a queue ended.
+ */
+static inline void vlc_queue_Kill(vlc_queue_t *q,
+                                  bool *restrict tombstone)
+{
+    vlc_queue_Lock(q);
+    *tombstone = true;
+    vlc_queue_Signal(q);
+    vlc_queue_Unlock(q);
+}
+
+/**
+ * Dequeues one entry from a killable queue.
+ *
+ * @return an entry, or NULL if the queue is empty and has been ended.
+ */
+static inline void *vlc_queue_DequeueKillable(vlc_queue_t *q,
+                                              bool *restrict tombstone)
+{
+    void *entry;
+
+    vlc_queue_Lock(q);
+    while (vlc_queue_IsEmpty(q) && !*tombstone)
+        vlc_queue_Wait(q);
+
+    entry = vlc_queue_DequeueUnlocked(q);
+    vlc_queue_Unlock(q);
+    return entry;
+}
+
+/** @} */
+
 /** @} */
 #endif



More information about the vlc-commits mailing list