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/thread.h"
26
32
34{
35 SafeQueue *sq = av_malloc(sizeof(*sq));
36 if (!sq)
37 return NULL;
38
39 sq->q = ff_queue_create();
40 if (!sq->q) {
41 av_freep(&sq);
42 return NULL;
43 }
44
46 ff_cond_init(&sq->cond, NULL);
47 return sq;
48}
49
51{
52 if (!sq)
53 return;
54
58 av_freep(&sq);
59}
60
62{
63 return sq ? ff_queue_size(sq->q) : 0;
64}
65
66void ff_safe_queue_wait_for_size(SafeQueue *sq, size_t min_size)
67{
68 if (!sq)
69 return;
70
71 ff_mutex_lock(&sq->mutex);
72 while (ff_queue_size(sq->q) < min_size)
73 ff_cond_wait(&sq->cond, &sq->mutex);
75}
76
78{
79 int ret;
80 ff_mutex_lock(&sq->mutex);
81 ret = ff_queue_push_front(sq->q, v);
82 ff_cond_signal(&sq->cond);
84 return ret;
85}
86
88{
89 int ret;
90 ff_mutex_lock(&sq->mutex);
91 ret = ff_queue_push_back(sq->q, v);
92 ff_cond_signal(&sq->cond);
94 return ret;
95}
96
98{
99 void *value;
100 ff_mutex_lock(&sq->mutex);
101 while (ff_queue_size(sq->q) == 0) {
102 ff_cond_wait(&sq->cond, &sq->mutex);
103 }
105 ff_cond_signal(&sq->cond);
107 return value;
108}
#define NULL
Definition coverity.c:32
double value
Definition eval.c:102
static int ff_cond_wait(AVCond *cond, AVMutex *mutex)
Definition thread.h:198
static int ff_mutex_unlock(AVMutex *mutex)
Definition thread.h:189
static int ff_cond_destroy(AVCond *cond)
Definition thread.h:195
static int ff_mutex_lock(AVMutex *mutex)
Definition thread.h:188
static int ff_mutex_destroy(AVMutex *mutex)
Definition thread.h:190
static int ff_cond_init(AVCond *cond, const void *attr)
Definition thread.h:194
#define AVCond
Definition thread.h:192
static int ff_cond_signal(AVCond *cond)
Definition thread.h:196
#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
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
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:87
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:77
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:66
void * ff_safe_queue_pop_front(SafeQueue *sq)
Remove and free first element from the queue in SafeQueue.
Definition safe_queue.c:97
size_t ff_safe_queue_size(SafeQueue *sq)
Return the length of the SafeQueue.
Definition safe_queue.c:61
SafeQueue * ff_safe_queue_create(void)
Create and initialize a SafeQueue instance.
Definition safe_queue.c:33
void ff_safe_queue_destroy(SafeQueue *sq)
Destroy the SafeQueue instance.
Definition safe_queue.c:50
Linear double-ended data structure.
Definition executor.c:51
Double-ended queue with mutex locks ensuring data consistency while multithreading.
Definition safe_queue.c:27
AVMutex mutex
Definition safe_queue.c:29
AVCond cond
Definition safe_queue.c:30
Queue * q
Definition safe_queue.c:28
#define av_freep(p)