FFmpeg
Loading...
Searching...
No Matches
safe_queue.c
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#include <stdio.h>
22#include "queue.h"
23#include "safe_queue.h"
24#include "libavutil/mem.h"
25#include "libavutil/avassert.h"
26#include "libavutil/thread.h"
27
28#if HAVE_PTHREAD_CANCEL
29#define DNNCond pthread_cond_t
30#define dnn_cond_init pthread_cond_init
31#define dnn_cond_destroy pthread_cond_destroy
32#define dnn_cond_signal pthread_cond_signal
33#define dnn_cond_wait pthread_cond_wait
34#else
35#define DNNCond char
36static inline int dnn_cond_init(DNNCond *cond, const void *attr) { return 0; }
37static inline int dnn_cond_destroy(DNNCond *cond) { return 0; }
38static inline int dnn_cond_signal(DNNCond *cond) { return 0; }
39static inline int dnn_cond_wait(DNNCond *cond, AVMutex *mutex)
40{
41 av_assert0(!"should not reach here");
42 return 0;
43}
44#endif
45
51
53{
54 SafeQueue *sq = av_malloc(sizeof(*sq));
55 if (!sq)
56 return NULL;
57
58 sq->q = ff_queue_create();
59 if (!sq->q) {
60 av_freep(&sq);
61 return NULL;
62 }
63
65 dnn_cond_init(&sq->cond, NULL);
66 return sq;
67}
68
70{
71 if (!sq)
72 return;
73
77 av_freep(&sq);
78}
79
81{
82 return sq ? ff_queue_size(sq->q) : 0;
83}
84
85void ff_safe_queue_wait_for_size(SafeQueue *sq, size_t min_size)
86{
87 if (!sq)
88 return;
89
90 ff_mutex_lock(&sq->mutex);
91 while (ff_queue_size(sq->q) < min_size)
92 dnn_cond_wait(&sq->cond, &sq->mutex);
94}
95
97{
98 int ret;
99 ff_mutex_lock(&sq->mutex);
100 ret = ff_queue_push_front(sq->q, v);
101 dnn_cond_signal(&sq->cond);
103 return ret;
104}
105
107{
108 int ret;
109 ff_mutex_lock(&sq->mutex);
110 ret = ff_queue_push_back(sq->q, v);
111 dnn_cond_signal(&sq->cond);
113 return ret;
114}
115
117{
118 void *value;
119 ff_mutex_lock(&sq->mutex);
120 while (ff_queue_size(sq->q) == 0) {
121 dnn_cond_wait(&sq->cond, &sq->mutex);
122 }
124 dnn_cond_signal(&sq->cond);
126 return value;
127}
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition avassert.h:42
#define NULL
Definition coverity.c:32
double value
Definition eval.c:102
static int ff_mutex_unlock(AVMutex *mutex)
Definition thread.h:189
static int ff_mutex_lock(AVMutex *mutex)
Definition thread.h:188
static int ff_mutex_destroy(AVMutex *mutex)
Definition thread.h:190
#define AVMutex
Definition thread.h:184
static int ff_mutex_init(AVMutex *mutex, const void *attr)
Definition thread.h:187
Memory handling functions.
#define av_malloc(s)
Definition ops_static.c:52
int(* cond)(enum AVPixelFormat pix_fmt)
void ff_queue_destroy(Queue *q)
Destroy the Queue instance.
Definition queue.c:72
int ff_queue_push_front(Queue *q, void *v)
Add data to the head of the queue.
Definition queue.c:109
void * ff_queue_pop_front(Queue *q)
Remove and free first element from the Queue.
Definition queue.c:151
int ff_queue_push_back(Queue *q, void *v)
Add data to the tail of the queue.
Definition queue.c:130
size_t ff_queue_size(Queue *q)
Return the length of the Queue.
Definition queue.c:88
Queue * ff_queue_create(void)
Create a Queue instance.
Definition queue.c:47
static AVMutex mutex
Definition resman.c:61
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
static int dnn_cond_init(DNNCond *cond, const void *attr)
Definition safe_queue.c:36
static int dnn_cond_destroy(DNNCond *cond)
Definition safe_queue.c:37
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
#define DNNCond
Definition safe_queue.c:35
SafeQueue * ff_safe_queue_create(void)
Create and initialize a SafeQueue instance.
Definition safe_queue.c:52
static int dnn_cond_signal(DNNCond *cond)
Definition safe_queue.c:38
static int dnn_cond_wait(DNNCond *cond, AVMutex *mutex)
Definition safe_queue.c:39
void ff_safe_queue_destroy(SafeQueue *sq)
Destroy the SafeQueue instance.
Definition safe_queue.c:69
Linear double-ended data structure.
Definition executor.c:51
Double-ended queue with mutex locks ensuring data consistency while multithreading.
Definition safe_queue.c:46
AVMutex mutex
Definition safe_queue.c:48
Queue * q
Definition safe_queue.c:47
DNNCond cond
Definition safe_queue.c:49
#define av_freep(p)