[FFmpeg-cvslog] lavu: add thread message API.

Nicolas George git at videolan.org
Mon May 26 16:58:21 CEST 2014


ffmpeg | branch: master | Nicolas George <george at nsup.org> | Thu Feb 20 14:14:34 2014 +0100| [55cc60cd6d9429270f5b3479c45a1abada72c05f] | committer: Nicolas George

lavu: add thread message API.

> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=55cc60cd6d9429270f5b3479c45a1abada72c05f
---

 doc/APIchanges            |    3 +
 libavutil/Makefile        |    2 +
 libavutil/threadmessage.c |  184 +++++++++++++++++++++++++++++++++++++++++++++
 libavutil/threadmessage.h |   91 ++++++++++++++++++++++
 libavutil/version.h       |    2 +-
 5 files changed, 281 insertions(+), 1 deletion(-)

diff --git a/doc/APIchanges b/doc/APIchanges
index 4a47291..395083c 100644
--- a/doc/APIchanges
+++ b/doc/APIchanges
@@ -15,6 +15,9 @@ libavutil:     2012-10-22
 
 API changes, most recent first:
 
+2014-05-26 - xxxxxxx - lavu 52.87.100 - threadmessage.h
+  Add thread message queue API.
+
 2014-05-26 - c37d179 - lavf 55.41.100 - avformat.h
   Add format_probesize to AVFormatContext.
 
diff --git a/libavutil/Makefile b/libavutil/Makefile
index 8832715..5cc978b 100644
--- a/libavutil/Makefile
+++ b/libavutil/Makefile
@@ -52,6 +52,7 @@ HEADERS = adler32.h                                                     \
           sha.h                                                         \
           sha512.h                                                      \
           stereo3d.h                                                    \
+          threadmessage.h                                               \
           time.h                                                        \
           timecode.h                                                    \
           timestamp.h                                                   \
@@ -119,6 +120,7 @@ OBJS = adler32.o                                                        \
        sha.o                                                            \
        sha512.o                                                         \
        stereo3d.o                                                       \
+       threadmessage.o                                                  \
        time.o                                                           \
        timecode.o                                                       \
        tree.o                                                           \
diff --git a/libavutil/threadmessage.c b/libavutil/threadmessage.c
new file mode 100644
index 0000000..b7fcbe2
--- /dev/null
+++ b/libavutil/threadmessage.c
@@ -0,0 +1,184 @@
+/*
+ * Copyright (c) 2014 Nicolas George
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License
+ * as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with FFmpeg; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include "fifo.h"
+#include "threadmessage.h"
+#if HAVE_THREADS
+#if HAVE_PTHREADS
+#include <pthread.h>
+#elif HAVE_W32THREADS
+#include "compat/w32pthreads.h"
+#elif HAVE_OS2THREADS
+#include "compat/os2threads.h"
+#else
+#error "Unknown threads implementation"
+#endif
+#endif
+
+struct AVThreadMessageQueue {
+#if HAVE_THREADS
+    AVFifoBuffer *fifo;
+    pthread_mutex_t lock;
+    pthread_cond_t cond;
+    int err_send;
+    int err_recv;
+    unsigned elsize;
+#else
+    int dummy;
+#endif
+};
+
+int av_thread_message_queue_alloc(AVThreadMessageQueue **mq,
+                                  unsigned nelem,
+                                  unsigned elsize)
+{
+#if HAVE_THREADS
+    AVThreadMessageQueue *rmq;
+    int ret = 0;
+
+    if (nelem > INT_MAX / elsize)
+        return AVERROR(EINVAL);
+    if (!(rmq = av_mallocz(sizeof(*rmq))))
+        return AVERROR(ENOMEM);
+    if ((ret = pthread_mutex_init(&rmq->lock, NULL))) {
+        av_free(rmq);
+        return AVERROR(ret);
+    }
+    if ((ret = pthread_cond_init(&rmq->cond, NULL))) {
+        pthread_mutex_destroy(&rmq->lock);
+        av_free(rmq);
+        return AVERROR(ret);
+    }
+    if (!(rmq->fifo = av_fifo_alloc(elsize * nelem))) {
+        pthread_cond_destroy(&rmq->cond);
+        pthread_mutex_destroy(&rmq->lock);
+        av_free(rmq);
+        return AVERROR(ret);
+    }
+    rmq->elsize = elsize;
+    *mq = rmq;
+    return 0;
+#else
+    *mq = NULL;
+    return AVERROR(ENOSYS);
+#endif /* HAVE_THREADS */
+}
+
+void av_thread_message_queue_free(AVThreadMessageQueue **mq)
+{
+#if HAVE_THREADS
+    if (*mq) {
+        av_fifo_freep(&(*mq)->fifo);
+        pthread_cond_destroy(&(*mq)->cond);
+        pthread_mutex_destroy(&(*mq)->lock);
+        av_freep(mq);
+    }
+#endif
+}
+
+#if HAVE_THREADS
+
+static int av_thread_message_queue_send_locked(AVThreadMessageQueue *mq,
+                                               void *msg,
+                                               unsigned flags)
+{
+    while (!mq->err_send && av_fifo_space(mq->fifo) < mq->elsize) {
+        if ((flags & AV_THREAD_MESSAGE_NONBLOCK))
+            return AVERROR(EAGAIN);
+        pthread_cond_wait(&mq->cond, &mq->lock);
+    }
+    if (mq->err_send)
+        return mq->err_send;
+    av_fifo_generic_write(mq->fifo, msg, mq->elsize, NULL);
+    pthread_cond_signal(&mq->cond);
+    return 0;
+}
+
+static int av_thread_message_queue_recv_locked(AVThreadMessageQueue *mq,
+                                               void *msg,
+                                               unsigned flags)
+{
+    while (!mq->err_recv && av_fifo_size(mq->fifo) < mq->elsize) {
+        if ((flags & AV_THREAD_MESSAGE_NONBLOCK))
+            return AVERROR(EAGAIN);
+        pthread_cond_wait(&mq->cond, &mq->lock);
+    }
+    if (av_fifo_size(mq->fifo) < mq->elsize)
+        return mq->err_recv;
+    av_fifo_generic_read(mq->fifo, msg, mq->elsize, NULL);
+    pthread_cond_signal(&mq->cond);
+    return 0;
+}
+
+#endif /* HAVE_THREADS */
+
+int av_thread_message_queue_send(AVThreadMessageQueue *mq,
+                                 void *msg,
+                                 unsigned flags)
+{
+#if HAVE_THREADS
+    int ret;
+
+    pthread_mutex_lock(&mq->lock);
+    ret = av_thread_message_queue_send_locked(mq, msg, flags);
+    pthread_mutex_unlock(&mq->lock);
+    return ret;
+#else
+    return AVERROR(ENOSYS);
+#endif /* HAVE_THREADS */
+}
+
+int av_thread_message_queue_recv(AVThreadMessageQueue *mq,
+                                 void *msg,
+                                 unsigned flags)
+{
+#if HAVE_THREADS
+    int ret;
+
+    pthread_mutex_lock(&mq->lock);
+    ret = av_thread_message_queue_recv_locked(mq, msg, flags);
+    pthread_mutex_unlock(&mq->lock);
+    return ret;
+#else
+    return AVERROR(ENOSYS);
+#endif /* HAVE_THREADS */
+}
+
+void av_thread_message_queue_set_err_send(AVThreadMessageQueue *mq,
+                                          int err)
+{
+#if HAVE_THREADS
+    pthread_mutex_lock(&mq->lock);
+    mq->err_send = err;
+    pthread_cond_broadcast(&mq->cond);
+    pthread_mutex_unlock(&mq->lock);
+#endif /* HAVE_THREADS */
+}
+
+void av_thread_message_queue_set_err_recv(AVThreadMessageQueue *mq,
+                                          int err)
+{
+#if HAVE_THREADS
+    pthread_mutex_lock(&mq->lock);
+    mq->err_recv = err;
+    pthread_cond_broadcast(&mq->cond);
+    pthread_mutex_unlock(&mq->lock);
+#endif /* HAVE_THREADS */
+}
diff --git a/libavutil/threadmessage.h b/libavutil/threadmessage.h
new file mode 100644
index 0000000..a8481d8
--- /dev/null
+++ b/libavutil/threadmessage.h
@@ -0,0 +1,91 @@
+/*
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License
+ * as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with FFmpeg; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#ifndef AVUTIL_THREADMESSAGE_H
+#define AVUTIL_THREADMESSAGE_H
+
+typedef struct AVThreadMessageQueue AVThreadMessageQueue;
+
+typedef enum AVThreadMessageFlags {
+
+    /**
+     * Perform non-blocking operation.
+     * If this flag is set, send and recv operations are non-blocking and
+     * return AVERROR(EAGAIN) immediately if they can not proceed.
+     */
+    AV_THREAD_MESSAGE_NONBLOCK = 1,
+
+} AVThreadMessageFlags;
+
+/**
+ * Allocate a new message queue.
+ *
+ * @param mq      pointer to the message queue
+ * @param nelem   maximum number of elements in the queue
+ * @param elsize  size of each element in the queue
+ * @return  >=0 for success; <0 for error, in particular AVERROR(ENOSYS) if
+ *          lavu was built without thread support
+ */
+int av_thread_message_queue_alloc(AVThreadMessageQueue **mq,
+                                  unsigned nelem,
+                                  unsigned elsize);
+
+/**
+ * Free a message queue.
+ *
+ * The message queue must no longer be in use by another thread.
+ */
+void av_thread_message_queue_free(AVThreadMessageQueue **mq);
+
+/**
+ * Send a message on the queue.
+ */
+int av_thread_message_queue_send(AVThreadMessageQueue *mq,
+                                 void *msg,
+                                 unsigned flags);
+
+/**
+ * Receive a message from the queue.
+ */
+int av_thread_message_queue_recv(AVThreadMessageQueue *mq,
+                                 void *msg,
+                                 unsigned flags);
+
+/**
+ * Set the sending error code.
+ *
+ * If the error code is set to non-zero, av_thread_message_queue_recv() will
+ * return it immediately when there are no longer available messages.
+ * Conventional values, such as AVERROR_EOF or AVERROR(EAGAIN), can be used
+ * to cause the receiving thread to stop or suspend its operation.
+ */
+void av_thread_message_queue_set_err_send(AVThreadMessageQueue *mq,
+                                          int err);
+
+/**
+ * Set the receiving error code.
+ *
+ * If the error code is set to non-zero, av_thread_message_queue_send() will
+ * return it immediately. Conventional values, such as AVERROR_EOF or
+ * AVERROR(EAGAIN), can be used to cause the sending thread to stop or
+ * suspend its operation.
+ */
+void av_thread_message_queue_set_err_recv(AVThreadMessageQueue *mq,
+                                          int err);
+
+#endif /* AVUTIL_THREADMESSAGE_H */
diff --git a/libavutil/version.h b/libavutil/version.h
index b134615..4dad89a 100644
--- a/libavutil/version.h
+++ b/libavutil/version.h
@@ -56,7 +56,7 @@
  */
 
 #define LIBAVUTIL_VERSION_MAJOR  52
-#define LIBAVUTIL_VERSION_MINOR  86
+#define LIBAVUTIL_VERSION_MINOR  87
 #define LIBAVUTIL_VERSION_MICRO 100
 
 #define LIBAVUTIL_VERSION_INT   AV_VERSION_INT(LIBAVUTIL_VERSION_MAJOR, \



More information about the ffmpeg-cvslog mailing list