FFmpeg
Loading...
Searching...
No Matches
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 */
28typedef 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 */
49
50/**
51 * @brief Wait until queue length reaches at least min_size.
52 *
53 * @param sq pointer to the SafeQueue
54 * @param min_size target queue length
55 */
56void ff_safe_queue_wait_for_size(SafeQueue *sq, size_t min_size);
57
58/**
59 * @brief Add data to the head of queue in the
60 * SafeQueue after locking mutex. After adding
61 * the data, it signals the condition variable
62 * and unlocks the mutex. It increases the length
63 * of queue in the SafeQueue by one.
64 *
65 * @param sq pointer to the SafeQueue
66 * @param v data to be added
67 * @return The length of the queue
68 * @retval 0 if the queue is not initialized
69 * @retval -1 if new entry cannot be created
70 */
71int ff_safe_queue_push_front(SafeQueue *sq, void *v);
72
73/**
74 * @brief Add data to the tail of queue in the
75 * SafeQueue after locking mutex. After adding
76 * the data, it signals the condition variable
77 * and unlocks the mutex. It increases the length
78 * of queue in the SafeQueue by one.
79 *
80 * @param sq pointer to the SafeQueue
81 * @param v data to be added
82 * @return The length of the queue
83 * @retval 0 if the queue is not initialized
84 * @retval -1 if new entry cannot be created
85 */
86int ff_safe_queue_push_back(SafeQueue *sq, void *v);
87
88/**
89 * @brief Remove and free first element from
90 * the queue in SafeQueue. Before removing, it
91 * waits for the condition variable to signal and
92 * acquires the mutex. Finally, it signals the
93 * condition and unlocks the mutex.
94 * It shrinks the length of queue in the SafeQueue
95 * by one.
96 *
97 * @param sq pointer to the SafeQueue.
98 * @return The value of first element as void.
99 * If a null pointer or empty queue is passed,
100 * it returns NULL
101 */
103
104#endif
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:106
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:96
void ff_safe_queue_wait_for_size(SafeQueue *sq, size_t min_size)
Wait until queue length reaches at least min_size.
Definition safe_queue.c:85
void * ff_safe_queue_pop_front(SafeQueue *sq)
Remove and free first element from the queue in SafeQueue.
Definition safe_queue.c:116
size_t ff_safe_queue_size(SafeQueue *sq)
Return the length of the SafeQueue.
Definition safe_queue.c:80
SafeQueue * ff_safe_queue_create(void)
Create and initialize a SafeQueue instance.
Definition safe_queue.c:52
void ff_safe_queue_destroy(SafeQueue *sq)
Destroy the SafeQueue instance.
Definition safe_queue.c:69
Double-ended queue with mutex locks ensuring data consistency while multithreading.
Definition safe_queue.c:46