Skip to content

Commit ac516f5

Browse files
committed
extensions: Implemented ALC_EXT_thread_local_context.
Reference Issue #36.
1 parent 500c3a9 commit ac516f5

File tree

1 file changed

+26
-5
lines changed

1 file changed

+26
-5
lines changed

mojoal.c

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1890,7 +1890,8 @@ static ALCenum null_device_error = ALC_NO_ERROR;
18901890
#define ALC_EXTENSION_ITEMS \
18911891
ALC_EXTENSION_ITEM(ALC_ENUMERATION_EXT) \
18921892
ALC_EXTENSION_ITEM(ALC_EXT_CAPTURE) \
1893-
ALC_EXTENSION_ITEM(ALC_EXT_DISCONNECT)
1893+
ALC_EXTENSION_ITEM(ALC_EXT_DISCONNECT) \
1894+
ALC_EXTENSION_ITEM(ALC_EXT_thread_local_context)
18941895

18951896
#define AL_EXTENSION_ITEMS \
18961897
AL_EXTENSION_ITEM(AL_EXT_FLOAT32) \
@@ -2278,18 +2279,36 @@ static ALCcontext *_alcCreateContext(ALCdevice *device, const ALCint* attrlist)
22782279
}
22792280
ENTRYPOINT(ALCcontext *,alcCreateContext,(ALCdevice *device, const ALCint* attrlist),(device,attrlist))
22802281

2281-
2282+
static SDL_AtomicInt tls_ctx_used;
2283+
static SDL_TLSID tlsid_current_ctx;
22822284
static SDL_INLINE ALCcontext *get_current_context(void)
22832285
{
2284-
return (ALCcontext *) SDL_GetAtomicPointer(&current_context);
2286+
ALCcontext *tlsctx = (ALCcontext *) SDL_GetTLS(&tlsid_current_ctx);
2287+
return tlsctx ? tlsctx : (ALCcontext *) SDL_GetAtomicPointer(&current_context);
2288+
}
2289+
2290+
// no api lock; it's thread-local.
2291+
ALCboolean alcSetThreadContext(ALCcontext *context)
2292+
{
2293+
SDL_SetAtomicInt(&tls_ctx_used, 1);
2294+
return SDL_SetTLS(&tlsid_current_ctx, context, NULL);
2295+
}
2296+
2297+
// no api lock; it's thread-local.
2298+
ALCcontext *alcGetThreadContext(void)
2299+
{
2300+
return (ALCcontext *) SDL_GetTLS(&tlsid_current_ctx);
22852301
}
22862302

22872303
// no api lock; it just sets an atomic pointer at the moment
22882304
ALCboolean alcMakeContextCurrent(ALCcontext *ctx)
22892305
{
22902306
FIXME("maybe just use the api lock");
22912307
SDL_SetAtomicPointer(&current_context, ctx);
2292-
FIXME("any reason this might return ALC_FALSE?");
2308+
FIXME("any reason this might return ALC_FALSE?"); // technically this could return false for an invalid non-NULL context but we don't keep a list of created contexts.
2309+
if (SDL_GetAtomicInt(&tls_ctx_used)) { // we don't want to create all the TLS infrastructure until someone has actually used ALC_EXT_thread_local_context, so query first.
2310+
SDL_SetTLS(&tlsid_current_ctx, ctx, NULL);
2311+
}
22932312
return ALC_TRUE;
22942313
}
22952314

@@ -2371,7 +2390,7 @@ static void _alcDestroyContext(ALCcontext *ctx)
23712390
}
23722391
ENTRYPOINTVOID(alcDestroyContext,(ALCcontext *ctx),(ctx))
23732392

2374-
// no api lock; atomic.
2393+
// no api lock; atomic (if not thread-local).
23752394
ALCcontext *alcGetCurrentContext(void)
23762395
{
23772396
return get_current_context();
@@ -2430,6 +2449,8 @@ void *alcGetProcAddress(ALCdevice *device, const ALCchar *funcname)
24302449
FN_TEST(alcCaptureStart);
24312450
FN_TEST(alcCaptureStop);
24322451
FN_TEST(alcCaptureSamples);
2452+
FN_TEST(alcSetThreadContext);
2453+
FN_TEST(alcGetThreadContext);
24332454
#undef FN_TEST
24342455

24352456
set_alc_error(device, ALC_INVALID_VALUE);

0 commit comments

Comments
 (0)