Rework threading functions

This commit is contained in:
Chris Robinson
2013-10-27 08:14:13 -07:00
parent b9e30f7604
commit 8ceb800def
15 changed files with 112 additions and 91 deletions
+6 -4
View File
@@ -26,6 +26,7 @@
#include "alMain.h"
#include "alu.h"
#include "threads.h"
#include <alsa/asoundlib.h>
@@ -293,7 +294,7 @@ typedef struct {
snd_pcm_sframes_t last_avail;
volatile int killNow;
ALvoid *thread;
althread_t thread;
} alsa_data;
typedef struct {
@@ -849,6 +850,7 @@ error:
static ALCboolean alsa_start_playback(ALCdevice *device)
{
alsa_data *data = (alsa_data*)device->ExtraData;
ALuint (*thread_func)(ALvoid*) = NULL;
snd_pcm_hw_params_t *hp = NULL;
snd_pcm_access_t access;
const char *funcerr;
@@ -872,7 +874,7 @@ static ALCboolean alsa_start_playback(ALCdevice *device)
ERR("buffer malloc failed\n");
return ALC_FALSE;
}
data->thread = StartThread(ALSANoMMapProc, device);
thread_func = ALSANoMMapProc;
}
else
{
@@ -882,9 +884,9 @@ static ALCboolean alsa_start_playback(ALCdevice *device)
ERR("snd_pcm_prepare(data->pcmHandle) failed: %s\n", snd_strerror(err));
return ALC_FALSE;
}
data->thread = StartThread(ALSAProc, device);
thread_func = ALSAProc;
}
if(data->thread == NULL)
if(!StartThread(&data->thread, thread_func, device))
{
ERR("Could not create playback thread\n");
free(data->buffer);
+3 -3
View File
@@ -34,6 +34,7 @@
#include "alMain.h"
#include "alu.h"
#include "threads.h"
#ifndef DSSPEAKER_5POINT1
# define DSSPEAKER_5POINT1 0x00000006
@@ -74,7 +75,7 @@ typedef struct {
HANDLE NotifyEvent;
volatile int killNow;
ALvoid *thread;
althread_t thread;
} DSoundPlaybackData;
typedef struct {
@@ -632,8 +633,7 @@ static ALCboolean DSoundStartPlayback(ALCdevice *device)
{
DSoundPlaybackData *data = (DSoundPlaybackData*)device->ExtraData;
data->thread = StartThread(DSoundPlaybackProc, device);
if(data->thread == NULL)
if(!StartThread(&data->thread, DSoundPlaybackProc, device))
return ALC_FALSE;
return ALC_TRUE;
+3 -3
View File
@@ -40,6 +40,7 @@
#include "alMain.h"
#include "alu.h"
#include "threads.h"
DEFINE_GUID(KSDATAFORMAT_SUBTYPE_PCM, 0x00000001, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71);
@@ -69,7 +70,7 @@ typedef struct {
volatile UINT32 Padding;
volatile int killNow;
ALvoid *thread;
althread_t thread;
} MMDevApiData;
@@ -677,8 +678,7 @@ static DWORD CALLBACK MMDevApiMsgProc(void *ptr)
if(SUCCEEDED(hr))
{
data->render = ptr;
data->thread = StartThread(MMDevApiProc, device);
if(!data->thread)
if(!StartThread(&data->thread, MMDevApiProc, device))
{
if(data->render)
IAudioRenderClient_Release(data->render);
+3 -3
View File
@@ -27,11 +27,12 @@
#include "alMain.h"
#include "alu.h"
#include "threads.h"
typedef struct {
volatile int killNow;
ALvoid *thread;
althread_t thread;
} null_data;
@@ -112,8 +113,7 @@ static ALCboolean null_start_playback(ALCdevice *device)
{
null_data *data = (null_data*)device->ExtraData;
data->thread = StartThread(NullProc, device);
if(data->thread == NULL)
if(!StartThread(&data->thread, NullProc, device))
return ALC_FALSE;
return ALC_TRUE;
+6 -6
View File
@@ -33,6 +33,7 @@
#include "alMain.h"
#include "alu.h"
#include "threads.h"
#include <sys/soundcard.h>
@@ -54,14 +55,15 @@ static const char *oss_capture = "/dev/dsp";
typedef struct {
int fd;
volatile int killNow;
ALvoid *thread;
ALubyte *mix_data;
int data_size;
RingBuffer *ring;
int doCapture;
volatile int killNow;
althread_t thread;
} oss_data;
@@ -286,8 +288,7 @@ static ALCboolean oss_start_playback(ALCdevice *device)
data->data_size = device->UpdateSize * FrameSizeFromDevFmt(device->FmtChans, device->FmtType);
data->mix_data = calloc(1, data->data_size);
data->thread = StartThread(OSSProc, device);
if(data->thread == NULL)
if(!StartThread(&data->thread, OSSProc, device))
{
free(data->mix_data);
data->mix_data = NULL;
@@ -428,8 +429,7 @@ static ALCenum oss_open_capture(ALCdevice *device, const ALCchar *deviceName)
data->mix_data = calloc(1, data->data_size);
device->ExtraData = data;
data->thread = StartThread(OSSCaptureProc, device);
if(data->thread == NULL)
if(!StartThread(&data->thread, OSSCaptureProc, device))
{
device->ExtraData = NULL;
free(data->mix_data);
+5 -5
View File
@@ -25,6 +25,7 @@
#include "alMain.h"
#include "alu.h"
#include "threads.h"
#include <pulse/pulseaudio.h>
@@ -205,11 +206,11 @@ typedef struct {
pa_threaded_mainloop *loop;
ALvoid *thread;
volatile ALboolean killNow;
pa_stream *stream;
pa_context *context;
volatile ALboolean killNow;
althread_t thread;
} pulse_data;
typedef struct {
@@ -1151,8 +1152,7 @@ static ALCboolean pulse_start_playback(ALCdevice *device)
{
pulse_data *data = device->ExtraData;
data->thread = StartThread(PulseProc, device);
if(!data->thread)
if(!StartThread(&data->thread, PulseProc, device))
return ALC_FALSE;
return ALC_TRUE;
+8 -9
View File
@@ -31,20 +31,22 @@
#include "alMain.h"
#include "alu.h"
#include "threads.h"
typedef struct
{
snd_pcm_t* pcmHandle;
int audio_fd;
snd_pcm_channel_setup_t csetup;
snd_pcm_channel_params_t cparams;
ALvoid* buffer;
ALsizei size;
volatile int killNow;
ALvoid* thread;
snd_pcm_channel_setup_t csetup;
snd_pcm_channel_params_t cparams;
althread_t thread;
} qsa_data;
typedef struct
@@ -619,13 +621,10 @@ static ALCboolean qsa_reset_playback(ALCdevice* device)
static ALCboolean qsa_start_playback(ALCdevice* device)
{
qsa_data* data=(qsa_data*)device->ExtraData;
qsa_data *data = (qsa_data*)device->ExtraData;
data->thread=StartThread(qsa_proc_playback, device);
if (data->thread==NULL)
{
if(!StartThread(&data->thread, qsa_proc_playback, device))
return ALC_FALSE;
}
return ALC_TRUE;
}
+3 -3
View File
@@ -26,6 +26,7 @@
#include "alMain.h"
#include "alu.h"
#include "threads.h"
#include <sndio.h>
@@ -46,7 +47,7 @@ typedef struct {
ALsizei data_size;
volatile int killNow;
ALvoid *thread;
althread_t thread;
} sndio_data;
@@ -223,8 +224,7 @@ static ALCboolean sndio_start_playback(ALCdevice *device)
data->data_size = device->UpdateSize * FrameSizeFromDevFmt(device->FmtChans, device->FmtType);
data->mix_data = calloc(1, data->data_size);
data->thread = StartThread(sndio_proc, device);
if(data->thread == NULL)
if(!StartThread(&data->thread, sndio_proc, device))
{
sio_stop(data->sndHandle);
free(data->mix_data);
+5 -4
View File
@@ -33,6 +33,7 @@
#include "alMain.h"
#include "alu.h"
#include "threads.h"
#include <sys/audioio.h>
@@ -43,11 +44,12 @@ static const char *solaris_driver = "/dev/audio";
typedef struct {
int fd;
volatile int killNow;
ALvoid *thread;
ALubyte *mix_data;
int data_size;
volatile int killNow;
althread_t thread;
} solaris_data;
@@ -208,8 +210,7 @@ static ALCboolean solaris_start_playback(ALCdevice *device)
data->data_size = device->UpdateSize * FrameSizeFromDevFmt(device->FmtChans, device->FmtType);
data->mix_data = calloc(1, data->data_size);
data->thread = StartThread(SolarisProc, device);
if(data->thread == NULL)
if(!StartThread(&data->thread, SolarisProc, device))
{
free(data->mix_data);
data->mix_data = NULL;
+3 -3
View File
@@ -29,6 +29,7 @@
#include "alMain.h"
#include "alu.h"
#include "threads.h"
typedef struct {
@@ -39,7 +40,7 @@ typedef struct {
ALuint size;
volatile int killNow;
ALvoid *thread;
althread_t thread;
} wave_data;
@@ -288,8 +289,7 @@ static ALCboolean wave_start_playback(ALCdevice *device)
return ALC_FALSE;
}
data->thread = StartThread(WaveProc, device);
if(data->thread == NULL)
if(!StartThread(&data->thread, WaveProc, device))
{
free(data->buffer);
data->buffer = NULL;
+1
View File
@@ -29,6 +29,7 @@
#include "alMain.h"
#include "alu.h"
#include "threads.h"
#ifndef WAVE_FORMAT_IEEE_FLOAT
#define WAVE_FORMAT_IEEE_FLOAT 0x0003
+51 -43
View File
@@ -20,6 +20,8 @@
#include "config.h"
#include "threads.h"
#include <stdlib.h>
#include "alMain.h"
@@ -29,16 +31,16 @@
#ifdef _WIN32
typedef struct {
typedef struct althread_info {
ALuint (*func)(ALvoid*);
ALvoid *ptr;
HANDLE thread;
} ThreadInfo;
HANDLE hdl;
} althread_info;
static DWORD CALLBACK StarterFunc(void *ptr)
{
ThreadInfo *inf = (ThreadInfo*)ptr;
ALint ret;
althread_info *inf = (althread_info*)ptr;
ALuint ret;
ret = inf->func(inf->ptr);
ExitThread((DWORD)ret);
@@ -46,35 +48,38 @@ static DWORD CALLBACK StarterFunc(void *ptr)
return (DWORD)ret;
}
ALvoid *StartThread(ALuint (*func)(ALvoid*), ALvoid *ptr)
ALboolean StartThread(althread_t *thread, ALuint (*func)(ALvoid*), ALvoid *ptr)
{
althread_info *info;
DWORD dummy;
ThreadInfo *inf = malloc(sizeof(ThreadInfo));
if(!inf) return 0;
inf->func = func;
inf->ptr = ptr;
info = malloc(sizeof(*info));
if(!info) return AL_FALSE;
inf->thread = CreateThread(NULL, THREAD_STACK_SIZE, StarterFunc, inf, 0, &dummy);
if(!inf->thread)
info->func = func;
info->ptr = ptr;
info->hdl = CreateThread(NULL, THREAD_STACK_SIZE, StarterFunc, info, 0, &dummy);
if(!info->hdl)
{
free(inf);
return NULL;
free(info);
return AL_FALSE;
}
return inf;
*thread = info;
return AL_TRUE;
}
ALuint StopThread(ALvoid *thread)
ALuint StopThread(althread_t thread)
{
ThreadInfo *inf = thread;
DWORD ret = 0;
WaitForSingleObject(inf->thread, INFINITE);
GetExitCodeThread(inf->thread, &ret);
CloseHandle(inf->thread);
WaitForSingleObject(thread->hdl, INFINITE);
GetExitCodeThread(thread->hdl, &ret);
CloseHandle(thread->hdl);
free(inf);
free(thread);
return (ALuint)ret;
}
@@ -83,60 +88,63 @@ ALuint StopThread(ALvoid *thread)
#include <pthread.h>
typedef struct {
typedef struct althread_info {
ALuint (*func)(ALvoid*);
ALvoid *ptr;
ALuint ret;
pthread_t thread;
} ThreadInfo;
pthread_t hdl;
} althread_info;
static void *StarterFunc(void *ptr)
{
ThreadInfo *inf = (ThreadInfo*)ptr;
althread_info *inf = (althread_info*)ptr;
inf->ret = inf->func(inf->ptr);
return NULL;
}
ALvoid *StartThread(ALuint (*func)(ALvoid*), ALvoid *ptr)
ALboolean StartThread(althread_t *thread, ALuint (*func)(ALvoid*), ALvoid *ptr)
{
pthread_attr_t attr;
ThreadInfo *inf = malloc(sizeof(ThreadInfo));
if(!inf) return NULL;
althread_info *info;
info = malloc(sizeof(*info));
if(!info) return AL_FALSE;
if(pthread_attr_init(&attr) != 0)
{
free(inf);
return NULL;
free(info);
return AL_FALSE;
}
if(pthread_attr_setstacksize(&attr, THREAD_STACK_SIZE) != 0)
{
pthread_attr_destroy(&attr);
free(inf);
return NULL;
free(info);
return AL_FALSE;
}
inf->func = func;
inf->ptr = ptr;
if(pthread_create(&inf->thread, &attr, StarterFunc, inf) != 0)
info->func = func;
info->ptr = ptr;
if(pthread_create(&info->hdl, &attr, StarterFunc, info) != 0)
{
pthread_attr_destroy(&attr);
free(inf);
return NULL;
free(info);
return AL_FALSE;
}
pthread_attr_destroy(&attr);
return inf;
*thread = info;
return AL_TRUE;
}
ALuint StopThread(ALvoid *thread)
ALuint StopThread(althread_t thread)
{
ThreadInfo *inf = thread;
ALuint ret;
pthread_join(inf->thread, NULL);
ret = inf->ret;
pthread_join(thread->hdl, NULL);
ret = thread->ret;
free(inf);
free(thread);
return ret;
}
+1 -1
View File
@@ -489,7 +489,6 @@ SET(ALC_OBJS Alc/ALc.c
Alc/ALu.c
Alc/alcConfig.c
Alc/alcRing.c
Alc/alcThread.c
Alc/bs2b.c
Alc/effects/autowah.c
Alc/effects/chorus.c
@@ -505,6 +504,7 @@ SET(ALC_OBJS Alc/ALc.c
Alc/helpers.c
Alc/hrtf.c
Alc/panning.c
Alc/threads.c
Alc/mixer.c
Alc/mixer_c.c
)
-4
View File
@@ -815,10 +815,6 @@ typedef struct {
void SetMixerFPUMode(FPUCtl *ctl);
void RestoreFPUMode(const FPUCtl *ctl);
ALvoid *StartThread(ALuint (*func)(ALvoid*), ALvoid *ptr);
ALuint StopThread(ALvoid *thread);
void SetThreadName(const char *name);
typedef struct RingBuffer RingBuffer;
RingBuffer *CreateRingBuffer(ALsizei frame_size, ALsizei length);
+14
View File
@@ -0,0 +1,14 @@
#ifndef AL_THREADS_H
#define AL_THREADS_H
#include "alMain.h"
struct althread_info;
typedef struct althread_info* althread_t;
ALboolean StartThread(althread_t *out, ALuint (*func)(ALvoid*), ALvoid *ptr);
ALuint StopThread(althread_t thread);
void SetThreadName(const char *name);
#endif /* AL_THREADS_H */