2 * Copyright (c) 2014 Nicolas George
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 License
8 * 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
14 * GNU Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with FFmpeg; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "threadmessage.h"
27 #include "compat/w32pthreads.h"
29 #include "compat/os2threads.h"
31 #error "Unknown threads implementation"
35 struct AVThreadMessageQueue {
48 int av_thread_message_queue_alloc(AVThreadMessageQueue **mq,
53 AVThreadMessageQueue *rmq;
56 if (nelem > INT_MAX / elsize)
57 return AVERROR(EINVAL);
58 if (!(rmq = av_mallocz(sizeof(*rmq))))
59 return AVERROR(ENOMEM);
60 if ((ret = pthread_mutex_init(&rmq->lock, NULL))) {
64 if ((ret = pthread_cond_init(&rmq->cond, NULL))) {
65 pthread_mutex_destroy(&rmq->lock);
69 if (!(rmq->fifo = av_fifo_alloc(elsize * nelem))) {
70 pthread_cond_destroy(&rmq->cond);
71 pthread_mutex_destroy(&rmq->lock);
80 return AVERROR(ENOSYS);
81 #endif /* HAVE_THREADS */
84 void av_thread_message_queue_free(AVThreadMessageQueue **mq)
88 av_fifo_freep(&(*mq)->fifo);
89 pthread_cond_destroy(&(*mq)->cond);
90 pthread_mutex_destroy(&(*mq)->lock);
98 static int av_thread_message_queue_send_locked(AVThreadMessageQueue *mq,
102 while (!mq->err_send && av_fifo_space(mq->fifo) < mq->elsize) {
103 if ((flags & AV_THREAD_MESSAGE_NONBLOCK))
104 return AVERROR(EAGAIN);
105 pthread_cond_wait(&mq->cond, &mq->lock);
109 av_fifo_generic_write(mq->fifo, msg, mq->elsize, NULL);
110 pthread_cond_signal(&mq->cond);
114 static int av_thread_message_queue_recv_locked(AVThreadMessageQueue *mq,
118 while (!mq->err_recv && av_fifo_size(mq->fifo) < mq->elsize) {
119 if ((flags & AV_THREAD_MESSAGE_NONBLOCK))
120 return AVERROR(EAGAIN);
121 pthread_cond_wait(&mq->cond, &mq->lock);
123 if (av_fifo_size(mq->fifo) < mq->elsize)
125 av_fifo_generic_read(mq->fifo, msg, mq->elsize, NULL);
126 pthread_cond_signal(&mq->cond);
130 #endif /* HAVE_THREADS */
132 int av_thread_message_queue_send(AVThreadMessageQueue *mq,
139 pthread_mutex_lock(&mq->lock);
140 ret = av_thread_message_queue_send_locked(mq, msg, flags);
141 pthread_mutex_unlock(&mq->lock);
144 return AVERROR(ENOSYS);
145 #endif /* HAVE_THREADS */
148 int av_thread_message_queue_recv(AVThreadMessageQueue *mq,
155 pthread_mutex_lock(&mq->lock);
156 ret = av_thread_message_queue_recv_locked(mq, msg, flags);
157 pthread_mutex_unlock(&mq->lock);
160 return AVERROR(ENOSYS);
161 #endif /* HAVE_THREADS */
164 void av_thread_message_queue_set_err_send(AVThreadMessageQueue *mq,
168 pthread_mutex_lock(&mq->lock);
170 pthread_cond_broadcast(&mq->cond);
171 pthread_mutex_unlock(&mq->lock);
172 #endif /* HAVE_THREADS */
175 void av_thread_message_queue_set_err_recv(AVThreadMessageQueue *mq,
179 pthread_mutex_lock(&mq->lock);
181 pthread_cond_broadcast(&mq->cond);
182 pthread_mutex_unlock(&mq->lock);
183 #endif /* HAVE_THREADS */