4 * This file is part of FFmpeg.
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 #ifndef AVFILTER_DNN_SAFE_QUEUE_H
22 #define AVFILTER_DNN_SAFE_QUEUE_H
25 * Double-ended queue with mutex locks ensuring
26 * data consistency while multithreading.
28 typedef struct SafeQueue SafeQueue;
31 * @brief Create and initialize a SafeQueue instance.
33 * @return Pointer to the SafeQueue
34 * @retval NULL if initialization fails
36 SafeQueue *ff_safe_queue_create(void);
39 * @brief Destroy the SafeQueue instance.
40 * It also frees all elements of the queue,
41 * destroys the mutex and condition variable.
43 void ff_safe_queue_destroy(SafeQueue *sq);
46 * @brief Return the length of the SafeQueue
48 size_t ff_safe_queue_size(SafeQueue *sq);
51 * @brief Add data to the head of queue in the
52 * SafeQueue after locking mutex. After adding
53 * the data, it signals the condition variable
54 * and unlocks the mutex. It increases the length
55 * of queue in the SafeQueue by one.
57 * @param sq pointer to the SafeQueue
58 * @param v data to be added
59 * @return The length of the queue
60 * @retval 0 if the queue is not initialized
61 * @retval -1 if new entry cannot be created
63 int ff_safe_queue_push_front(SafeQueue *sq, void *v);
66 * @brief Add data to the tail of queue in the
67 * SafeQueue after locking mutex. After adding
68 * the data, it signals the condition variable
69 * and unlocks the mutex. It increases the length
70 * of queue in the SafeQueue by one.
72 * @param sq pointer to the SafeQueue
73 * @param v data to be added
74 * @return The length of the queue
75 * @retval 0 if the queue is not initialized
76 * @retval -1 if new entry cannot be created
78 int ff_safe_queue_push_back(SafeQueue *sq, void *v);
81 * @brief Remove and free first element from
82 * the queue in SafeQueue. Before removing, it
83 * waits for the condition variable to signal and
84 * acquires the mutex. Finally, it signals the
85 * condition and unlocks the mutex.
86 * It shrinks the length of queue in the SafeQueue
89 * @param sq pointer to the SafeQueue.
90 * @return The value of first element as void.
91 * If a null pointer or empty queue is passed,
94 void *ff_safe_queue_pop_front(SafeQueue *sq);