Skip to content

Commit

Permalink
Only force threads from the CUI
Browse files Browse the repository at this point in the history
My hack to force threads leaked through to some libav components. By
making it a #define instead of just using __EMSCRIPTEN__, I can control
it much more precisely.
  • Loading branch information
Yahweasel committed Sep 27, 2024
1 parent 3bfc0d4 commit 98439b5
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 460 deletions.
44 changes: 39 additions & 5 deletions patches/ffmpeg/08-fftools.diff
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,15 @@ Index: ffmpeg-7.0.2/fftools/ffmpeg.c
===================================================================
--- ffmpeg-7.0.2.orig/fftools/ffmpeg.c
+++ ffmpeg-7.0.2/fftools/ffmpeg.c
@@ -337,22 +337,29 @@ static void ffmpeg_cleanup(int ret)
@@ -24,6 +24,7 @@
*/

#include "config.h"
+#define FF_FORCE_PTHREADS

#include <errno.h>
#include <limits.h>
@@ -337,22 +338,29 @@ static void ffmpeg_cleanup(int ret)
for (int i = 0; i < nb_filtergraphs; i++)
fg_free(&filtergraphs[i]);
av_freep(&filtergraphs);
Expand Down Expand Up @@ -32,7 +40,7 @@ Index: ffmpeg-7.0.2/fftools/ffmpeg.c
}
av_freep(&vstats_filename);
of_enc_stats_close();
@@ -927,7 +934,12 @@ static int64_t getmaxrss(void)
@@ -927,7 +935,12 @@ static int64_t getmaxrss(void)
#endif
}

Expand All @@ -45,7 +53,7 @@ Index: ffmpeg-7.0.2/fftools/ffmpeg.c
{
Scheduler *sch = NULL;

@@ -993,8 +1005,12 @@ finish:
@@ -993,8 +1006,12 @@ finish:
ret = 0;

ffmpeg_cleanup(ret);
Expand Down Expand Up @@ -215,10 +223,36 @@ Index: ffmpeg-7.0.2/libavutil/thread.h
#include "error.h"

-#if HAVE_PTHREADS || HAVE_W32THREADS || HAVE_OS2THREADS
+#if HAVE_PTHREADS || HAVE_W32THREADS || HAVE_OS2THREADS || defined(__EMSCRIPTEN__)
+#if HAVE_PTHREADS || HAVE_W32THREADS || HAVE_OS2THREADS || defined(FF_FORCE_PTHREADS)

-#if HAVE_PTHREADS
+#if HAVE_PTHREADS || defined(__EMSCRIPTEN__)
+#if HAVE_PTHREADS || defined(FF_FORCE_PTHREADS)
#include <pthread.h>

#if defined(ASSERT_LEVEL) && ASSERT_LEVEL > 1
Index: ffmpeg-7.0.2/fftools/ffmpeg_sched.c
===================================================================
--- ffmpeg-7.0.2.orig/fftools/ffmpeg_sched.c
+++ ffmpeg-7.0.2/fftools/ffmpeg_sched.c
@@ -23,6 +23,8 @@
#include <stddef.h>
#include <stdint.h>

+#define FF_FORCE_PTHREADS
+
#include "cmdutils.h"
#include "ffmpeg_sched.h"
#include "ffmpeg_utils.h"
Index: ffmpeg-7.0.2/fftools/thread_queue.c
===================================================================
--- ffmpeg-7.0.2.orig/fftools/thread_queue.c
+++ ffmpeg-7.0.2/fftools/thread_queue.c
@@ -19,6 +19,8 @@
#include <stdint.h>
#include <string.h>

+#define FF_FORCE_PTHREADS
+
#include "libavutil/avassert.h"
#include "libavutil/error.h"
#include "libavutil/fifo.h"
160 changes: 9 additions & 151 deletions patches/ffmpeg/4/08-fftools.diff
Original file line number Diff line number Diff line change
Expand Up @@ -189,165 +189,23 @@ Index: ffmpeg-4.4.5/libavutil/thread.h
#include "config.h"

-#if HAVE_PTHREADS || HAVE_W32THREADS || HAVE_OS2THREADS
+#if HAVE_PTHREADS || HAVE_W32THREADS || HAVE_OS2THREADS || defined(__EMSCRIPTEN__)
+#if HAVE_PTHREADS || HAVE_W32THREADS || HAVE_OS2THREADS || defined(FF_FORCE_PTHREADS)

-#if HAVE_PTHREADS
+#if HAVE_PTHREADS || defined(__EMSCRIPTEN__)
+#if HAVE_PTHREADS || defined(FF_FORCE_PTHREADS)
#include <pthread.h>

#if defined(ASSERT_LEVEL) && ASSERT_LEVEL > 1
Index: ffmpeg-4.4.5/libavutil/threadmessage.c
===================================================================
--- ffmpeg-4.4.5.orig/libavutil/threadmessage.c
+++ ffmpeg-4.4.5/libavutil/threadmessage.c
@@ -22,8 +22,10 @@
#include "threadmessage.h"
#include "thread.h"
@@ -18,6 +18,8 @@
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/

+#define HAVE_THREAD_MESSAGES (HAVE_THREADS || defined(__EMSCRIPTEN__))
+#define FF_FORCE_PTHREADS
+
struct AVThreadMessageQueue {
-#if HAVE_THREADS
+#if HAVE_THREAD_MESSAGES
AVFifoBuffer *fifo;
pthread_mutex_t lock;
pthread_cond_t cond_recv;
@@ -41,7 +43,7 @@ int av_thread_message_queue_alloc(AVThre
unsigned nelem,
unsigned elsize)
{
-#if HAVE_THREADS
+#if HAVE_THREAD_MESSAGES
AVThreadMessageQueue *rmq;
int ret = 0;

@@ -77,20 +79,20 @@ int av_thread_message_queue_alloc(AVThre
#else
*mq = NULL;
return AVERROR(ENOSYS);
-#endif /* HAVE_THREADS */
+#endif /* HAVE_THREAD_MESSAGES */
}

void av_thread_message_queue_set_free_func(AVThreadMessageQueue *mq,
void (*free_func)(void *msg))
{
-#if HAVE_THREADS
+#if HAVE_THREAD_MESSAGES
mq->free_func = free_func;
#endif
}

void av_thread_message_queue_free(AVThreadMessageQueue **mq)
{
-#if HAVE_THREADS
+#if HAVE_THREAD_MESSAGES
if (*mq) {
av_thread_message_flush(*mq);
av_fifo_freep(&(*mq)->fifo);
@@ -104,7 +106,7 @@ void av_thread_message_queue_free(AVThre

int av_thread_message_queue_nb_elems(AVThreadMessageQueue *mq)
{
-#if HAVE_THREADS
+#if HAVE_THREAD_MESSAGES
int ret;
pthread_mutex_lock(&mq->lock);
ret = av_fifo_size(mq->fifo);
@@ -115,7 +117,7 @@ int av_thread_message_queue_nb_elems(AVT
#endif
}

-#if HAVE_THREADS
+#if HAVE_THREAD_MESSAGES

static int av_thread_message_queue_send_locked(AVThreadMessageQueue *mq,
void *msg,
@@ -151,13 +153,13 @@ static int av_thread_message_queue_recv_
return 0;
}

-#endif /* HAVE_THREADS */
+#endif /* HAVE_THREAD_MESSAGES */

int av_thread_message_queue_send(AVThreadMessageQueue *mq,
void *msg,
unsigned flags)
{
-#if HAVE_THREADS
+#if HAVE_THREAD_MESSAGES
int ret;

pthread_mutex_lock(&mq->lock);
@@ -166,14 +168,14 @@ int av_thread_message_queue_send(AVThrea
return ret;
#else
return AVERROR(ENOSYS);
-#endif /* HAVE_THREADS */
+#endif /* HAVE_THREAD_MESSAGES */
}

int av_thread_message_queue_recv(AVThreadMessageQueue *mq,
void *msg,
unsigned flags)
{
-#if HAVE_THREADS
+#if HAVE_THREAD_MESSAGES
int ret;

pthread_mutex_lock(&mq->lock);
@@ -182,32 +184,32 @@ int av_thread_message_queue_recv(AVThrea
return ret;
#else
return AVERROR(ENOSYS);
-#endif /* HAVE_THREADS */
+#endif /* HAVE_THREAD_MESSAGES */
}

void av_thread_message_queue_set_err_send(AVThreadMessageQueue *mq,
int err)
{
-#if HAVE_THREADS
+#if HAVE_THREAD_MESSAGES
pthread_mutex_lock(&mq->lock);
mq->err_send = err;
pthread_cond_broadcast(&mq->cond_send);
pthread_mutex_unlock(&mq->lock);
-#endif /* HAVE_THREADS */
+#endif /* HAVE_THREAD_MESSAGES */
}

void av_thread_message_queue_set_err_recv(AVThreadMessageQueue *mq,
int err)
{
-#if HAVE_THREADS
+#if HAVE_THREAD_MESSAGES
pthread_mutex_lock(&mq->lock);
mq->err_recv = err;
pthread_cond_broadcast(&mq->cond_recv);
pthread_mutex_unlock(&mq->lock);
-#endif /* HAVE_THREADS */
+#endif /* HAVE_THREAD_MESSAGES */
}

-#if HAVE_THREADS
+#if HAVE_THREAD_MESSAGES
static void free_func_wrap(void *arg, void *msg, int size)
{
AVThreadMessageQueue *mq = arg;
@@ -217,7 +219,7 @@ static void free_func_wrap(void *arg, vo

void av_thread_message_flush(AVThreadMessageQueue *mq)
{
-#if HAVE_THREADS
+#if HAVE_THREAD_MESSAGES
int used, off;
void *free_func = mq->free_func;

@@ -231,5 +233,5 @@ void av_thread_message_flush(AVThreadMes
* is nothing to read */
pthread_cond_broadcast(&mq->cond_send);
pthread_mutex_unlock(&mq->lock);
-#endif /* HAVE_THREADS */
+#endif /* HAVE_THREAD_MESSAGES */
}
#include "fifo.h"
#include "threadmessage.h"
#include "thread.h"
Loading

0 comments on commit 98439b5

Please sign in to comment.