From b108d0acfd062756da69074242e24ea7b2856b8a Mon Sep 17 00:00:00 2001 From: Chris Robinson Date: Mon, 26 Nov 2018 21:19:20 -0800 Subject: [PATCH] Remove the last remaining uses of althrd_t --- Alc/backends/qsa.cpp | 24 +++++++++----- Alc/backends/sndio.cpp | 64 +++++++++++++++++++----------------- Alc/backends/solaris.cpp | 30 ++++++++++------- common/threads.cpp | 85 ------------------------------------------------ common/threads.h | 7 ---- 5 files changed, 67 insertions(+), 143 deletions(-) diff --git a/Alc/backends/qsa.cpp b/Alc/backends/qsa.cpp index b40b5b6b..3d09a744 100644 --- a/Alc/backends/qsa.cpp +++ b/Alc/backends/qsa.cpp @@ -28,6 +28,7 @@ #include #include +#include #include #include @@ -53,7 +54,7 @@ struct qsa_data { ALsizei size{0}; std::atomic mKillNow{AL_TRUE}; - althrd_t thread; + std::thread mThread; }; struct DevMap { @@ -592,21 +593,26 @@ static ALCboolean qsa_start_playback(PlaybackWrapper *self) { qsa_data *data = self->ExtraData.get(); - data->mKillNow.store(AL_FALSE, std::memory_order_release); - if(althrd_create(&data->thread, qsa_proc_playback, self) != althrd_success) - return ALC_FALSE; - - return ALC_TRUE; + try { + data->mKillNow.store(AL_FALSE, std::memory_order_release); + data->mThread = std::thread(qsa_proc_playback, self); + return ALC_TRUE; + } + catch(std::exception& e) { + ERR("Could not create playback thread: %s\n", e.what()); + } + catch(...) { + } + return ALC_FALSE; } static void qsa_stop_playback(PlaybackWrapper *self) { qsa_data *data = self->ExtraData.get(); - int res; - if(data->mKillNow.exchange(AL_TRUE, std::memory_order_acq_rel)) + if(data->mKillNow.exchange(AL_TRUE, std::memory_order_acq_rel) || !data->mThread.joinable()) return; - althrd_join(data->thread, &res); + data->mThread.join(); } diff --git a/Alc/backends/sndio.cpp b/Alc/backends/sndio.cpp index 2a999bd4..1dabe5ca 100644 --- a/Alc/backends/sndio.cpp +++ b/Alc/backends/sndio.cpp @@ -26,6 +26,8 @@ #include #include +#include + #include "alMain.h" #include "alu.h" #include "threads.h" @@ -44,10 +46,10 @@ struct SndioPlayback final : public ALCbackend { ALsizei data_size{0}; std::atomic mKillNow{AL_TRUE}; - althrd_t thread; + std::thread mThread; }; -static int SndioPlayback_mixerProc(void *ptr); +static int SndioPlayback_mixerProc(SndioPlayback *self); static void SndioPlayback_Construct(SndioPlayback *self, ALCdevice *device); static void SndioPlayback_Destruct(SndioPlayback *self); @@ -86,9 +88,8 @@ static void SndioPlayback_Destruct(SndioPlayback *self) } -static int SndioPlayback_mixerProc(void *ptr) +static int SndioPlayback_mixerProc(SndioPlayback *self) { - SndioPlayback *self = static_cast(ptr); ALCdevice *device = STATIC_CAST(ALCbackend, self)->mDevice; ALsizei frameSize; size_t wrote; @@ -249,23 +250,25 @@ static ALCboolean SndioPlayback_start(SndioPlayback *self) return ALC_FALSE; } - self->mKillNow.store(AL_FALSE, std::memory_order_release); - if(althrd_create(&self->thread, SndioPlayback_mixerProc, self) != althrd_success) - { - sio_stop(self->sndHandle); - return ALC_FALSE; + try { + self->mKillNow.store(AL_FALSE, std::memory_order_release); + self->mThread = std::thread(SndioPlayback_mixerProc, self); + return ALC_TRUE; } - - return ALC_TRUE; + catch(std::exception& e) { + ERR("Could not create playback thread: %s\n", e.what()); + } + catch(...) { + } + sio_stop(self->sndHandle); + return ALC_FALSE; } static void SndioPlayback_stop(SndioPlayback *self) { - int res; - - if(self->mKillNow.exchange(AL_TRUE, std::memory_order_acq_rel)) + if(self->mKillNow.exchange(AL_TRUE, std::memory_order_acq_rel) || !self->mThread.joinable()) return; - althrd_join(self->thread, &res); + self->mThread.join(); if(!sio_stop(self->sndHandle)) ERR("Error stopping device\n"); @@ -281,10 +284,10 @@ struct SndioCapture final : public ALCbackend { ll_ringbuffer_t *ring{nullptr}; std::atomic mKillNow{AL_TRUE}; - althrd_t thread; + std::thread mThread; }; -static int SndioCapture_recordProc(void *ptr); +static int SndioCapture_recordProc(SndioCapture *self); static void SndioCapture_Construct(SndioCapture *self, ALCdevice *device); static void SndioCapture_Destruct(SndioCapture *self); @@ -323,9 +326,8 @@ static void SndioCapture_Destruct(SndioCapture *self) } -static int SndioCapture_recordProc(void* ptr) +static int SndioCapture_recordProc(SndioCapture *self) { - SndioCapture *self = static_cast(ptr); ALCdevice *device = STATIC_CAST(ALCbackend, self)->mDevice; ALsizei frameSize; @@ -490,23 +492,25 @@ static ALCboolean SndioCapture_start(SndioCapture *self) return ALC_FALSE; } - self->mKillNow.store(AL_FALSE, std::memory_order_release); - if(althrd_create(&self->thread, SndioCapture_recordProc, self) != althrd_success) - { - sio_stop(self->sndHandle); - return ALC_FALSE; + try { + self->mKillNow.store(AL_FALSE, std::memory_order_release); + self->mThread = std::thread(SndioCapture_recordProc, self); + return ALC_TRUE; } - - return ALC_TRUE; + catch(std::exception& e) { + ERR("Could not create record thread: %s\n", e.what()); + } + catch(...) { + } + sio_stop(self->sndHandle); + return ALC_FALSE; } static void SndioCapture_stop(SndioCapture *self) { - int res; - - if(self->mKillNow.exchange(AL_TRUE, std::memory_order_acq_rel)) + if(self->mKillNow.exchange(AL_TRUE, std::memory_order_acq_rel) || !self->mThread.joinable()) return; - althrd_join(self->thread, &res); + self->mThread.join(); if(!sio_stop(self->sndHandle)) ERR("Error stopping device\n"); diff --git a/Alc/backends/solaris.cpp b/Alc/backends/solaris.cpp index 0ca7a4ec..941ca015 100644 --- a/Alc/backends/solaris.cpp +++ b/Alc/backends/solaris.cpp @@ -34,6 +34,8 @@ #include #include +#include + #include "alMain.h" #include "alu.h" #include "alconfig.h" @@ -50,10 +52,10 @@ struct ALCsolarisBackend final : public ALCbackend { int data_size{0}; std::atomic mKillNow{AL_TRUE}; - althrd_t thread; + std::thread mThread; }; -static int ALCsolarisBackend_mixerProc(void *ptr); +static int ALCsolarisBackend_mixerProc(ALCsolarisBackend *self); static void ALCsolarisBackend_Construct(ALCsolarisBackend *self, ALCdevice *device); static void ALCsolarisBackend_Destruct(ALCsolarisBackend *self); @@ -98,9 +100,8 @@ static void ALCsolarisBackend_Destruct(ALCsolarisBackend *self) } -static int ALCsolarisBackend_mixerProc(void *ptr) +static int ALCsolarisBackend_mixerProc(ALCsolarisBackend *self) { - ALCsolarisBackend *self = static_cast(ptr); ALCdevice *device = STATIC_CAST(ALCbackend, self)->mDevice; struct timeval timeout; ALubyte *write_ptr; @@ -268,20 +269,25 @@ static ALCboolean ALCsolarisBackend_reset(ALCsolarisBackend *self) static ALCboolean ALCsolarisBackend_start(ALCsolarisBackend *self) { - self->mKillNow.store(AL_FALSE); - if(althrd_create(&self->thread, ALCsolarisBackend_mixerProc, self) != althrd_success) - return ALC_FALSE; - return ALC_TRUE; + try { + self->mKillNow.store(AL_FALSE); + self->mThread = std::thread(ALCsolarisBackend_mixerProc, self); + return ALC_TRUE; + } + catch(std::exception& e) { + ERR("Could not create playback thread: %s\n", e.what()); + } + catch(...) { + } + return ALC_FALSE; } static void ALCsolarisBackend_stop(ALCsolarisBackend *self) { - int res; - - if(self->mKillNow.exchange(AL_TRUE)) + if(self->mKillNow.exchange(AL_TRUE) || !self->mThread.joinable()) return; - althrd_join(self->thread, &res); + self->mThread.join(); if(ioctl(self->fd, AUDIO_DRAIN) < 0) ERR("Error draining device: %s\n", strerror(errno)); diff --git a/common/threads.cpp b/common/threads.cpp index 86fbb7b5..489e5f5b 100644 --- a/common/threads.cpp +++ b/common/threads.cpp @@ -158,91 +158,6 @@ void althrd_setname(const char *name) } -typedef struct thread_cntr { - althrd_start_t func; - void *arg; -} thread_cntr; - -static void *althrd_starter(void *arg) -{ - thread_cntr cntr; - memcpy(&cntr, arg, sizeof(cntr)); - free(arg); - - return (void*)(intptr_t)((*cntr.func)(cntr.arg)); -} - - -int althrd_create(althrd_t *thr, althrd_start_t func, void *arg) -{ - thread_cntr *cntr; - pthread_attr_t attr; - size_t stackmult = 1; - int err; - - cntr = static_cast(malloc(sizeof(*cntr))); - if(!cntr) return althrd_nomem; - - if(pthread_attr_init(&attr) != 0) - { - free(cntr); - return althrd_error; - } -retry_stacksize: - if(pthread_attr_setstacksize(&attr, THREAD_STACK_SIZE*stackmult) != 0) - { - pthread_attr_destroy(&attr); - free(cntr); - return althrd_error; - } - - cntr->func = func; - cntr->arg = arg; - if((err=pthread_create(thr, &attr, althrd_starter, cntr)) == 0) - { - pthread_attr_destroy(&attr); - return althrd_success; - } - - if(err == EINVAL) - { - /* If an invalid stack size, try increasing it (limit x4, 8MB). */ - if(stackmult < 4) - { - stackmult *= 2; - goto retry_stacksize; - } - /* If still nothing, try defaults and hope they're good enough. */ - if(pthread_create(thr, NULL, althrd_starter, cntr) == 0) - { - pthread_attr_destroy(&attr); - return althrd_success; - } - } - pthread_attr_destroy(&attr); - free(cntr); - return althrd_error; -} - -int althrd_detach(althrd_t thr) -{ - if(pthread_detach(thr) != 0) - return althrd_error; - return althrd_success; -} - -int althrd_join(althrd_t thr, int *res) -{ - void *code; - - if(pthread_join(thr, &code) != 0) - return althrd_error; - if(res != NULL) - *res = (int)(intptr_t)code; - return althrd_success; -} - - int almtx_init(almtx_t *mtx, int type) { int ret; diff --git a/common/threads.h b/common/threads.h index a5f6ce45..53b56147 100644 --- a/common/threads.h +++ b/common/threads.h @@ -70,7 +70,6 @@ inline int almtx_unlock(almtx_t *mtx) #include #endif /* __APPLE__ */ -typedef pthread_t althrd_t; typedef pthread_mutex_t almtx_t; #ifdef __APPLE__ typedef dispatch_semaphore_t alsem_t; @@ -78,8 +77,6 @@ typedef dispatch_semaphore_t alsem_t; typedef sem_t alsem_t; #endif /* __APPLE__ */ -typedef int (*althrd_start_t)(void*); - inline void althrd_yield(void) { @@ -101,10 +98,6 @@ inline int almtx_unlock(almtx_t *mtx) return althrd_success; } -int althrd_create(althrd_t *thr, althrd_start_t func, void *arg); -int althrd_detach(althrd_t thr); -int althrd_join(althrd_t thr, int *res); - #endif void althrd_setname(const char *name); -- 2.11.4.GIT