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
23 #include "safe_queue.h"
24 #include "libavutil/mem.h"
25 #include "libavutil/avassert.h"
26 #include "libavutil/thread.h"
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
36 static inline int dnn_cond_init(DNNCond *cond, const void *attr) { return 0; }
37 static inline int dnn_cond_destroy(DNNCond *cond) { return 0; }
38 static inline int dnn_cond_signal(DNNCond *cond) { return 0; }
39 static inline int dnn_cond_wait(DNNCond *cond, AVMutex *mutex)
41 av_assert0(!"should not reach here");
52 SafeQueue *ff_safe_queue_create(void)
54 SafeQueue *sq = av_malloc(sizeof(*sq));
58 sq->q = ff_queue_create();
64 ff_mutex_init(&sq->mutex, NULL);
65 dnn_cond_init(&sq->cond, NULL);
69 void ff_safe_queue_destroy(SafeQueue *sq)
74 ff_queue_destroy(sq->q);
75 ff_mutex_destroy(&sq->mutex);
76 dnn_cond_destroy(&sq->cond);
80 size_t ff_safe_queue_size(SafeQueue *sq)
82 return sq ? ff_queue_size(sq->q) : 0;
85 int ff_safe_queue_push_front(SafeQueue *sq, void *v)
88 ff_mutex_lock(&sq->mutex);
89 ret = ff_queue_push_front(sq->q, v);
90 dnn_cond_signal(&sq->cond);
91 ff_mutex_unlock(&sq->mutex);
95 int ff_safe_queue_push_back(SafeQueue *sq, void *v)
98 ff_mutex_lock(&sq->mutex);
99 ret = ff_queue_push_back(sq->q, v);
100 dnn_cond_signal(&sq->cond);
101 ff_mutex_unlock(&sq->mutex);
105 void *ff_safe_queue_pop_front(SafeQueue *sq)
108 ff_mutex_lock(&sq->mutex);
109 while (ff_queue_size(sq->q) == 0) {
110 dnn_cond_wait(&sq->cond, &sq->mutex);
112 value = ff_queue_pop_front(sq->q);
113 dnn_cond_signal(&sq->cond);
114 ff_mutex_unlock(&sq->mutex);