mirror of
https://github.com/love2d/megasource.git
synced 2026-08-19 04:05:09 +02:00
Update OpenAL Soft to commit 414b56edec5441211dc924fef365c54267c04f1c
This commit is contained in:
+103
-140
@@ -64,6 +64,22 @@ extern inline int altss_set(altss_t tss_id, void *val);
|
||||
#include <mmsystem.h>
|
||||
|
||||
|
||||
/* An associative map of uint:void* pairs. The key is the unique Thread ID and
|
||||
* the value is the thread HANDLE. The thread ID is passed around as the
|
||||
* althrd_t since there is only one ID per thread, whereas a thread may be
|
||||
* referenced by multiple different HANDLEs. This map allows retrieving the
|
||||
* original handle which is needed to join the thread and get its return value.
|
||||
*/
|
||||
static UIntMap ThrdIdHandle = UINTMAP_STATIC_INITIALIZE;
|
||||
|
||||
/* An associative map of uint:void* pairs. The key is the TLS index (given by
|
||||
* TlsAlloc), and the value is the altss_dtor_t callback. When a thread exits,
|
||||
* we iterate over the TLS indices for their thread-local value and call the
|
||||
* destructor function with it if they're both not NULL.
|
||||
*/
|
||||
static UIntMap TlsDestructors = UINTMAP_STATIC_INITIALIZE;
|
||||
|
||||
|
||||
void althrd_setname(althrd_t thr, const char *name)
|
||||
{
|
||||
#if defined(_MSC_VER)
|
||||
@@ -94,23 +110,6 @@ void althrd_setname(althrd_t thr, const char *name)
|
||||
}
|
||||
|
||||
|
||||
static UIntMap ThrdIdHandle = UINTMAP_STATIC_INITIALIZE;
|
||||
|
||||
static void NTAPI althrd_callback(void* UNUSED(handle), DWORD reason, void* UNUSED(reserved))
|
||||
{
|
||||
if(reason == DLL_PROCESS_DETACH)
|
||||
ResetUIntMap(&ThrdIdHandle);
|
||||
}
|
||||
#ifdef _MSC_VER
|
||||
#pragma section(".CRT$XLC",read)
|
||||
__declspec(allocate(".CRT$XLC")) PIMAGE_TLS_CALLBACK althrd_callback_ = althrd_callback;
|
||||
#elif defined(__GNUC__)
|
||||
PIMAGE_TLS_CALLBACK althrd_callback_ __attribute__((section(".CRT$XLC"))) = althrd_callback;
|
||||
#else
|
||||
PIMAGE_TLS_CALLBACK althrd_callback_ = althrd_callback;
|
||||
#endif
|
||||
|
||||
|
||||
typedef struct thread_cntr {
|
||||
althrd_start_t func;
|
||||
void *arg;
|
||||
@@ -208,12 +207,6 @@ void almtx_destroy(almtx_t *mtx)
|
||||
DeleteCriticalSection(mtx);
|
||||
}
|
||||
|
||||
int almtx_timedlock(almtx_t* UNUSED(mtx), const struct timespec* UNUSED(ts))
|
||||
{
|
||||
/* Windows CRITICAL_SECTIONs don't seem to have a timedlock method. */
|
||||
return althrd_error;
|
||||
}
|
||||
|
||||
#if defined(_WIN32_WINNT) && _WIN32_WINNT >= 0x0600
|
||||
int alcnd_init(alcnd_t *cond)
|
||||
{
|
||||
@@ -240,30 +233,6 @@ int alcnd_wait(alcnd_t *cond, almtx_t *mtx)
|
||||
return althrd_error;
|
||||
}
|
||||
|
||||
int alcnd_timedwait(alcnd_t *cond, almtx_t *mtx, const struct timespec *time_point)
|
||||
{
|
||||
struct timespec curtime;
|
||||
DWORD sleeptime;
|
||||
|
||||
if(altimespec_get(&curtime, AL_TIME_UTC) != AL_TIME_UTC)
|
||||
return althrd_error;
|
||||
|
||||
if(curtime.tv_sec > time_point->tv_sec || (curtime.tv_sec == time_point->tv_sec &&
|
||||
curtime.tv_nsec >= time_point->tv_nsec))
|
||||
{
|
||||
if(SleepConditionVariableCS(cond, mtx, 0) != 0)
|
||||
return althrd_success;
|
||||
}
|
||||
else
|
||||
{
|
||||
sleeptime = (time_point->tv_nsec - curtime.tv_nsec + 999999)/1000000;
|
||||
sleeptime += (DWORD)(time_point->tv_sec - curtime.tv_sec)*1000;
|
||||
if(SleepConditionVariableCS(cond, mtx, sleeptime) != 0)
|
||||
return althrd_success;
|
||||
}
|
||||
return (GetLastError()==ERROR_TIMEOUT) ? althrd_timedout : althrd_error;
|
||||
}
|
||||
|
||||
void alcnd_destroy(alcnd_t* UNUSED(cond))
|
||||
{
|
||||
/* Nothing to delete? */
|
||||
@@ -348,37 +317,6 @@ int alcnd_wait(alcnd_t *cond, almtx_t *mtx)
|
||||
return althrd_success;
|
||||
}
|
||||
|
||||
int alcnd_timedwait(alcnd_t *cond, almtx_t *mtx, const struct timespec *time_point)
|
||||
{
|
||||
_int_alcnd_t *icond = cond->Ptr;
|
||||
struct timespec curtime;
|
||||
DWORD sleeptime;
|
||||
int res;
|
||||
|
||||
if(altimespec_get(&curtime, AL_TIME_UTC) != AL_TIME_UTC)
|
||||
return althrd_error;
|
||||
|
||||
if(curtime.tv_sec > time_point->tv_sec || (curtime.tv_sec == time_point->tv_sec &&
|
||||
curtime.tv_nsec >= time_point->tv_nsec))
|
||||
sleeptime = 0;
|
||||
else
|
||||
{
|
||||
sleeptime = (time_point->tv_nsec - curtime.tv_nsec + 999999)/1000000;
|
||||
sleeptime += (DWORD)(time_point->tv_sec - curtime.tv_sec)*1000;
|
||||
}
|
||||
|
||||
IncrementRef(&icond->wait_count);
|
||||
LeaveCriticalSection(mtx);
|
||||
|
||||
res = WaitForMultipleObjects(2, icond->events, FALSE, sleeptime);
|
||||
|
||||
if(DecrementRef(&icond->wait_count) == 0 && res == WAIT_OBJECT_0+BROADCAST)
|
||||
ResetEvent(icond->events[BROADCAST]);
|
||||
EnterCriticalSection(mtx);
|
||||
|
||||
return (res == WAIT_TIMEOUT) ? althrd_timedout : althrd_success;
|
||||
}
|
||||
|
||||
void alcnd_destroy(alcnd_t *cond)
|
||||
{
|
||||
_int_alcnd_t *icond = cond->Ptr;
|
||||
@@ -389,46 +327,40 @@ void alcnd_destroy(alcnd_t *cond)
|
||||
#endif /* defined(_WIN32_WINNT) && _WIN32_WINNT >= 0x0600 */
|
||||
|
||||
|
||||
/* An associative map of uint:void* pairs. The key is the TLS index (given by
|
||||
* TlsAlloc), and the value is the altss_dtor_t callback. When a thread exits,
|
||||
* we iterate over the TLS indices for their thread-local value and call the
|
||||
* destructor function with it if they're both not NULL. To avoid using
|
||||
* DllMain, a PIMAGE_TLS_CALLBACK function pointer is placed in a ".CRT$XLx"
|
||||
* section (where x is a character A to Z) which will be called by the CRT.
|
||||
*/
|
||||
static UIntMap TlsDestructors = UINTMAP_STATIC_INITIALIZE;
|
||||
|
||||
static void NTAPI altss_callback(void* UNUSED(handle), DWORD reason, void* UNUSED(reserved))
|
||||
int alsem_init(alsem_t *sem, unsigned int initial)
|
||||
{
|
||||
ALsizei i;
|
||||
|
||||
if(reason == DLL_PROCESS_DETACH)
|
||||
{
|
||||
ResetUIntMap(&TlsDestructors);
|
||||
return;
|
||||
}
|
||||
if(reason != DLL_THREAD_DETACH)
|
||||
return;
|
||||
|
||||
LockUIntMapRead(&TlsDestructors);
|
||||
for(i = 0;i < TlsDestructors.size;i++)
|
||||
{
|
||||
void *ptr = altss_get(TlsDestructors.keys[i]);
|
||||
altss_dtor_t callback = (altss_dtor_t)TlsDestructors.values[i];
|
||||
if(ptr && callback)
|
||||
callback(ptr);
|
||||
}
|
||||
UnlockUIntMapRead(&TlsDestructors);
|
||||
*sem = CreateSemaphore(NULL, initial, INT_MAX, NULL);
|
||||
if(*sem != NULL) return althrd_success;
|
||||
return althrd_error;
|
||||
}
|
||||
#ifdef _MSC_VER
|
||||
#pragma section(".CRT$XLB",read)
|
||||
__declspec(allocate(".CRT$XLB")) PIMAGE_TLS_CALLBACK altss_callback_ = altss_callback;
|
||||
#elif defined(__GNUC__)
|
||||
PIMAGE_TLS_CALLBACK altss_callback_ __attribute__((section(".CRT$XLB"))) = altss_callback;
|
||||
#else
|
||||
#warning "No TLS callback support, thread-local contexts may leak references on poorly written applications."
|
||||
PIMAGE_TLS_CALLBACK altss_callback_ = altss_callback;
|
||||
#endif
|
||||
|
||||
void alsem_destroy(alsem_t *sem)
|
||||
{
|
||||
CloseHandle(*sem);
|
||||
}
|
||||
|
||||
int alsem_post(alsem_t *sem)
|
||||
{
|
||||
DWORD ret = ReleaseSemaphore(*sem, 1, NULL);
|
||||
if(ret) return althrd_success;
|
||||
return althrd_error;
|
||||
}
|
||||
|
||||
int alsem_wait(alsem_t *sem)
|
||||
{
|
||||
DWORD ret = WaitForSingleObject(*sem, INFINITE);
|
||||
if(ret == WAIT_OBJECT_0) return althrd_success;
|
||||
return althrd_error;
|
||||
}
|
||||
|
||||
int alsem_trywait(alsem_t *sem)
|
||||
{
|
||||
DWORD ret = WaitForSingleObject(*sem, 0);
|
||||
if(ret == WAIT_OBJECT_0) return althrd_success;
|
||||
if(ret == WAIT_TIMEOUT) return althrd_busy;
|
||||
return althrd_error;
|
||||
}
|
||||
|
||||
|
||||
int altss_create(altss_t *tss_id, altss_dtor_t callback)
|
||||
{
|
||||
@@ -480,6 +412,27 @@ void alcall_once(alonce_flag *once, void (*callback)(void))
|
||||
InterlockedExchange(once, 2);
|
||||
}
|
||||
|
||||
|
||||
void althrd_deinit(void)
|
||||
{
|
||||
ResetUIntMap(&ThrdIdHandle);
|
||||
ResetUIntMap(&TlsDestructors);
|
||||
}
|
||||
|
||||
void althrd_thread_detach(void)
|
||||
{
|
||||
ALsizei i;
|
||||
|
||||
LockUIntMapRead(&TlsDestructors);
|
||||
for(i = 0;i < TlsDestructors.size;i++)
|
||||
{
|
||||
void *ptr = altss_get(TlsDestructors.keys[i]);
|
||||
altss_dtor_t callback = (altss_dtor_t)TlsDestructors.values[i];
|
||||
if(ptr && callback) callback(ptr);
|
||||
}
|
||||
UnlockUIntMapRead(&TlsDestructors);
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
#include <sys/time.h>
|
||||
@@ -493,6 +446,8 @@ void alcall_once(alonce_flag *once, void (*callback)(void))
|
||||
extern inline int althrd_sleep(const struct timespec *ts, struct timespec *rem);
|
||||
extern inline void alcall_once(alonce_flag *once, void (*callback)(void));
|
||||
|
||||
extern inline void althrd_deinit(void);
|
||||
extern inline void althrd_thread_detach(void);
|
||||
|
||||
void althrd_setname(althrd_t thr, const char *name)
|
||||
{
|
||||
@@ -604,15 +559,9 @@ int almtx_init(almtx_t *mtx, int type)
|
||||
int ret;
|
||||
|
||||
if(!mtx) return althrd_error;
|
||||
#ifdef HAVE_PTHREAD_MUTEX_TIMEDLOCK
|
||||
if((type&~(almtx_recursive|almtx_timed)) != 0)
|
||||
return althrd_error;
|
||||
#else
|
||||
if((type&~almtx_recursive) != 0)
|
||||
return althrd_error;
|
||||
#endif
|
||||
|
||||
type &= ~almtx_timed;
|
||||
if(type == almtx_plain)
|
||||
ret = pthread_mutex_init(mtx, NULL);
|
||||
else
|
||||
@@ -644,20 +593,6 @@ void almtx_destroy(almtx_t *mtx)
|
||||
pthread_mutex_destroy(mtx);
|
||||
}
|
||||
|
||||
int almtx_timedlock(almtx_t *mtx, const struct timespec *ts)
|
||||
{
|
||||
#ifdef HAVE_PTHREAD_MUTEX_TIMEDLOCK
|
||||
int ret = pthread_mutex_timedlock(mtx, ts);
|
||||
switch(ret)
|
||||
{
|
||||
case 0: return althrd_success;
|
||||
case ETIMEDOUT: return althrd_timedout;
|
||||
case EBUSY: return althrd_busy;
|
||||
}
|
||||
#endif
|
||||
return althrd_error;
|
||||
}
|
||||
|
||||
int alcnd_init(alcnd_t *cond)
|
||||
{
|
||||
if(pthread_cond_init(cond, NULL) == 0)
|
||||
@@ -686,16 +621,44 @@ int alcnd_wait(alcnd_t *cond, almtx_t *mtx)
|
||||
return althrd_error;
|
||||
}
|
||||
|
||||
int alcnd_timedwait(alcnd_t *cond, almtx_t *mtx, const struct timespec *time_point)
|
||||
void alcnd_destroy(alcnd_t *cond)
|
||||
{
|
||||
if(pthread_cond_timedwait(cond, mtx, time_point) == 0)
|
||||
pthread_cond_destroy(cond);
|
||||
}
|
||||
|
||||
|
||||
int alsem_init(alsem_t *sem, unsigned int initial)
|
||||
{
|
||||
if(sem_init(sem, 0, initial) == 0)
|
||||
return althrd_success;
|
||||
return althrd_error;
|
||||
}
|
||||
|
||||
void alcnd_destroy(alcnd_t *cond)
|
||||
void alsem_destroy(alsem_t *sem)
|
||||
{
|
||||
pthread_cond_destroy(cond);
|
||||
sem_destroy(sem);
|
||||
}
|
||||
|
||||
int alsem_post(alsem_t *sem)
|
||||
{
|
||||
if(sem_post(sem) == 0)
|
||||
return althrd_success;
|
||||
return althrd_error;
|
||||
}
|
||||
|
||||
int alsem_wait(alsem_t *sem)
|
||||
{
|
||||
if(sem_wait(sem) == 0) return althrd_success;
|
||||
if(errno == EINTR) return -2;
|
||||
return althrd_error;
|
||||
}
|
||||
|
||||
int alsem_trywait(alsem_t *sem)
|
||||
{
|
||||
if(sem_trywait(sem) == 0) return althrd_success;
|
||||
if(errno == EWOULDBLOCK) return althrd_busy;
|
||||
if(errno == EINTR) return -2;
|
||||
return althrd_error;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user