Remove the last remaining uses of althrd_t

This commit is contained in:
Chris Robinson 2018-11-26 21:19:20 -08:00
parent 7860a11ae2
commit b108d0acfd
5 changed files with 67 additions and 143 deletions

View File

@ -28,6 +28,7 @@
#include <errno.h>
#include <memory.h>
#include <thread>
#include <memory>
#include <algorithm>
@ -53,7 +54,7 @@ struct qsa_data {
ALsizei size{0};
std::atomic<ALenum> 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();
}

View File

@ -26,6 +26,8 @@
#include <stdlib.h>
#include <string.h>
#include <thread>
#include "alMain.h"
#include "alu.h"
#include "threads.h"
@ -44,10 +46,10 @@ struct SndioPlayback final : public ALCbackend {
ALsizei data_size{0};
std::atomic<ALenum> 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<SndioPlayback*>(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<ALenum> 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<SndioCapture*>(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");

View File

@ -34,6 +34,8 @@
#include <errno.h>
#include <math.h>
#include <thread>
#include "alMain.h"
#include "alu.h"
#include "alconfig.h"
@ -50,10 +52,10 @@ struct ALCsolarisBackend final : public ALCbackend {
int data_size{0};
std::atomic<ALenum> 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<ALCsolarisBackend*>(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));

View File

@ -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<thread_cntr*>(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;

View File

@ -70,7 +70,6 @@ inline int almtx_unlock(almtx_t *mtx)
#include <semaphore.h>
#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);