From 64eed5bc02c0a4e09423aa67f118a2a7c8bc69b0 Mon Sep 17 00:00:00 2001 From: Vito Caputo Date: Wed, 28 Apr 2021 19:19:34 -0700 Subject: threads: handle pthread_create() errors This doesn't try to cleanup anything on the failures, basically assuming they're always fatal and the process will exit. Previously no pthread_create() errors were noticed at all, which could result in some very strange failure modes. --- src/rmd.c | 3 +- src/rmd_threads.c | 85 ++++++++++++++++++++++++++++++++----------------------- src/rmd_threads.h | 3 +- 3 files changed, 54 insertions(+), 37 deletions(-) (limited to 'src') diff --git a/src/rmd.c b/src/rmd.c index 64addd4..b3a1af2 100644 --- a/src/rmd.c +++ b/src/rmd.c @@ -150,7 +150,8 @@ int main(int argc, char **argv) } //this is where the capturing happens. - rmdThreads(&pdata); + if (rmdThreads(&pdata)) + return 1; XCloseDisplay(pdata.dpy); fprintf(stderr, ".\n"); diff --git a/src/rmd_threads.c b/src/rmd_threads.c index b3b207e..bfcdf75 100644 --- a/src/rmd_threads.c +++ b/src/rmd_threads.c @@ -46,7 +46,16 @@ #include #include -void rmdThreads(ProgData *pdata) + +int rmdThread(pthread_t *thread, void *(*func)(ProgData *), ProgData *pdata) +{ + if (pthread_create(thread, NULL, (void *)func, (void *)pdata) != 0) + return 1; + + return 0; +} + +int rmdThreads(ProgData *pdata) { pthread_t image_capture_t, image_encode_t, @@ -62,46 +71,50 @@ void rmdThreads(ProgData *pdata) } /*start threads*/ - pthread_create( &image_capture_t, - NULL, - (void *)rmdGetFrames, - (void *)pdata); + if (rmdThread(&image_capture_t, rmdGetFrames, pdata)) { + fprintf(stderr, "Error creating rmdGetFrames thread!!!\n"); + return 1; + } - if (pdata->args.encOnTheFly) - pthread_create( &image_encode_t, - NULL, - (void *)rmdEncodeImageBuffers, - (void *)pdata); - else - pthread_create( &image_cache_t, - NULL, - (void *)rmdCacheImageBuffer, - (void *)pdata); + if (pdata->args.encOnTheFly) { + if (rmdThread(&image_encode_t, rmdEncodeImageBuffers, pdata)) { + fprintf(stderr, "Error creating rmdEncodeImageBuffers thread!!!\n"); + return 1; + } + } else { + if (rmdThread(&image_cache_t, rmdCacheImageBuffer, pdata)) { + fprintf(stderr, "Error creating rmdCacheImageBuffer thread!!!\n"); + return 1; + } + } if (!pdata->args.nosound) { - if (!pdata->args.use_jack) - pthread_create( &sound_capture_t, - NULL, - (void *)rmdCaptureAudio, - (void *)pdata); + if (!pdata->args.use_jack) { + if (rmdThread(&sound_capture_t, rmdCaptureAudio, pdata)) { + fprintf(stderr, "Error creating rmdCaptureAudio thread!!!\n"); + return 1; + } + } - if (pdata->args.encOnTheFly) - pthread_create( &sound_encode_t, - NULL, - (void *)rmdEncodeAudioBuffers, - (void *)pdata); - else - pthread_create( &sound_cache_t, - NULL, - (void *)rmdCacheAudioBuffer, - (void *)pdata); + if (pdata->args.encOnTheFly) { + if (rmdThread(&sound_encode_t, rmdEncodeAudioBuffers, pdata)) { + fprintf(stderr, "Error creating rmdEncodeAudioBuffers thread!!!\n"); + return 1; + } + } else { + if (rmdThread(&sound_cache_t, rmdCacheAudioBuffer, pdata)) { + fprintf(stderr, "Error creating rmdCacheAudioBuffer thread!!!\n"); + return 1; + } + } } - if (pdata->args.encOnTheFly) - pthread_create( &flush_to_ogg_t, - NULL, - (void *)rmdFlushToOgg, - (void *)pdata); + if (pdata->args.encOnTheFly) { + if (rmdThread(&flush_to_ogg_t, rmdFlushToOgg, pdata)) { + fprintf(stderr, "Error creating rmdFlushToOgg thread!!!\n"); + return 1; + } + } rmdRegisterCallbacks(pdata); fprintf(stderr,"Capturing!\n"); @@ -158,6 +171,8 @@ void rmdThreads(ProgData *pdata) fprintf(stderr,".."); fprintf(stderr,"."); + + return 0; } void rmdThreadsSetName(const char *name) diff --git a/src/rmd_threads.h b/src/rmd_threads.h index 6ca2dbd..d80985f 100644 --- a/src/rmd_threads.h +++ b/src/rmd_threads.h @@ -30,6 +30,7 @@ #include "rmd_types.h" +int rmdThread(pthread_t *thread, void *(*func)(ProgData *), ProgData *pdata); /** * Launch and wait capture threads. * Also creates and waits the encoding threads when @@ -37,7 +38,7 @@ * * \param pdata ProgData struct containing all program data */ -void rmdThreads(ProgData *pdata); +int rmdThreads(ProgData *pdata); void rmdThreadsSetName(const char *name); -- cgit v1.2.1