FFmpeg
safe_queue.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2020
3  *
4  * This file is part of FFmpeg.
5  *
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.
10  *
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.
15  *
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
19  */
20 
21 #ifndef AVFILTER_DNN_SAFE_QUEUE_H
22 #define AVFILTER_DNN_SAFE_QUEUE_H
23 
24 /**
25  * Double-ended queue with mutex locks ensuring
26  * data consistency while multithreading.
27  */
28 typedef struct SafeQueue SafeQueue;
29 
30 /**
31  * @brief Create and initialize a SafeQueue instance.
32  *
33  * @return Pointer to the SafeQueue
34  * @retval NULL if initialization fails
35  */
37 
38 /**
39  * @brief Destroy the SafeQueue instance.
40  * It also frees all elements of the queue,
41  * destroys the mutex and condition variable.
42  */
44 
45 /**
46  * @brief Return the length of the SafeQueue
47  */
48 size_t ff_safe_queue_size(SafeQueue *sq);
49 
50 /**
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.
56  *
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
62  */
63 int ff_safe_queue_push_front(SafeQueue *sq, void *v);
64 
65 /**
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.
71  *
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
77  */
78 int ff_safe_queue_push_back(SafeQueue *sq, void *v);
79 
80 /**
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
87  * by one.
88  *
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,
92  * it returns NULL
93  */
95 
96 #endif
ff_safe_queue_destroy
void ff_safe_queue_destroy(SafeQueue *sq)
Destroy the SafeQueue instance.
Definition: safe_queue.c:69
SafeQueue
Double-ended queue with mutex locks ensuring data consistency while multithreading.
Definition: safe_queue.c:46
ff_safe_queue_size
size_t ff_safe_queue_size(SafeQueue *sq)
Return the length of the SafeQueue.
Definition: safe_queue.c:80
ff_safe_queue_create
SafeQueue * ff_safe_queue_create(void)
Create and initialize a SafeQueue instance.
Definition: safe_queue.c:52
ff_safe_queue_pop_front
void * ff_safe_queue_pop_front(SafeQueue *sq)
Remove and free first element from the queue in SafeQueue.
Definition: safe_queue.c:105
ff_safe_queue_push_front
int ff_safe_queue_push_front(SafeQueue *sq, void *v)
Add data to the head of queue in the SafeQueue after locking mutex.
Definition: safe_queue.c:85
ff_safe_queue_push_back
int ff_safe_queue_push_back(SafeQueue *sq, void *v)
Add data to the tail of queue in the SafeQueue after locking mutex.
Definition: safe_queue.c:95