Attempted fix for the coreaudio backend
[openal-soft.git] / Alc / helpers.c
blob00bbe7edff34031a7428d7453740b3359ee80ae4
1 /**
2 * OpenAL cross platform audio library
3 * Copyright (C) 2011 by authors.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
18 * Or go to http://www.gnu.org/copyleft/lgpl.html
21 #include "config.h"
23 #include <stdlib.h>
24 #ifdef HAVE_DLFCN_H
25 #include <dlfcn.h>
26 #endif
28 #if defined(HAVE_GUIDDEF_H) || defined(HAVE_INITGUID_H)
29 #define INITGUID
30 #include <windows.h>
31 #ifdef HAVE_GUIDDEF_H
32 #include <guiddef.h>
33 #else
34 #include <initguid.h>
35 #endif
37 DEFINE_GUID(KSDATAFORMAT_SUBTYPE_PCM, 0x00000001, 0x0000, 0x0010, 0x80,0x00, 0x00,0xaa,0x00,0x38,0x9b,0x71);
38 DEFINE_GUID(KSDATAFORMAT_SUBTYPE_IEEE_FLOAT, 0x00000003, 0x0000, 0x0010, 0x80,0x00, 0x00,0xaa,0x00,0x38,0x9b,0x71);
40 DEFINE_GUID(IID_IDirectSoundNotify, 0xb0210783, 0x89cd, 0x11d0, 0xaf,0x08, 0x00,0xa0,0xc9,0x25,0xcd,0x16);
42 DEFINE_GUID(CLSID_MMDeviceEnumerator, 0xbcde0395, 0xe52f, 0x467c, 0x8e,0x3d, 0xc4,0x57,0x92,0x91,0x69,0x2e);
43 DEFINE_GUID(IID_IMMDeviceEnumerator, 0xa95664d2, 0x9614, 0x4f35, 0xa7,0x46, 0xde,0x8d,0xb6,0x36,0x17,0xe6);
44 DEFINE_GUID(IID_IAudioClient, 0x1cb9ad4c, 0xdbfa, 0x4c32, 0xb1,0x78, 0xc2,0xf5,0x68,0xa7,0x03,0xb2);
45 DEFINE_GUID(IID_IAudioRenderClient, 0xf294acfc, 0x3146, 0x4483, 0xa7,0xbf, 0xad,0xdc,0xa7,0xc2,0x60,0xe2);
47 #ifdef HAVE_MMDEVAPI
48 #include <devpropdef.h>
50 DEFINE_DEVPROPKEY(DEVPKEY_Device_FriendlyName, 0xa45c254e, 0xdf1c, 0x4efd, 0x80,0x20, 0x67,0xd1,0x46,0xa8,0x50,0xe0, 14);
51 #endif
53 #endif
55 #include "alMain.h"
57 #ifdef _WIN32
58 void pthread_once(pthread_once_t *once, void (*callback)(void))
60 LONG ret;
61 while((ret=InterlockedExchange(once, 1)) == 1)
62 sched_yield();
63 if(ret == 0)
64 callback();
65 InterlockedExchange(once, 2);
69 int pthread_key_create(pthread_key_t *key, void (*callback)(void*))
71 *key = TlsAlloc();
72 if(callback)
73 InsertUIntMapEntry(&TlsDestructor, *key, callback);
74 return 0;
77 int pthread_key_delete(pthread_key_t key)
79 InsertUIntMapEntry(&TlsDestructor, key, NULL);
80 TlsFree(key);
81 return 0;
84 void *pthread_getspecific(pthread_key_t key)
85 { return TlsGetValue(key); }
87 int pthread_setspecific(pthread_key_t key, void *val)
89 TlsSetValue(key, val);
90 return 0;
94 void *LoadLib(const char *name)
95 { return LoadLibraryA(name); }
96 void CloseLib(void *handle)
97 { FreeLibrary((HANDLE)handle); }
98 void *GetSymbol(void *handle, const char *name)
100 void *ret;
102 ret = (void*)GetProcAddress((HANDLE)handle, name);
103 if(ret == NULL)
104 ERR("Failed to load %s\n", name);
105 return ret;
108 WCHAR *strdupW(const WCHAR *str)
110 const WCHAR *n;
111 WCHAR *ret;
112 size_t len;
114 n = str;
115 while(*n) n++;
116 len = n - str;
118 ret = calloc(sizeof(WCHAR), len+1);
119 if(ret != NULL)
120 memcpy(ret, str, sizeof(WCHAR)*len);
121 return ret;
124 #else
126 void InitializeCriticalSection(CRITICAL_SECTION *cs)
128 pthread_mutexattr_t attrib;
129 int ret;
131 ret = pthread_mutexattr_init(&attrib);
132 assert(ret == 0);
134 ret = pthread_mutexattr_settype(&attrib, PTHREAD_MUTEX_RECURSIVE);
135 #ifdef HAVE_PTHREAD_NP_H
136 if(ret != 0)
137 ret = pthread_mutexattr_setkind_np(&attrib, PTHREAD_MUTEX_RECURSIVE);
138 #endif
139 assert(ret == 0);
140 ret = pthread_mutex_init(cs, &attrib);
141 assert(ret == 0);
143 pthread_mutexattr_destroy(&attrib);
145 void DeleteCriticalSection(CRITICAL_SECTION *cs)
147 int ret;
148 ret = pthread_mutex_destroy(cs);
149 assert(ret == 0);
151 void EnterCriticalSection(CRITICAL_SECTION *cs)
153 int ret;
154 ret = pthread_mutex_lock(cs);
155 assert(ret == 0);
157 void LeaveCriticalSection(CRITICAL_SECTION *cs)
159 int ret;
160 ret = pthread_mutex_unlock(cs);
161 assert(ret == 0);
164 /* NOTE: This wrapper isn't quite accurate as it returns an ALuint, as opposed
165 * to the expected DWORD. Both are defined as unsigned 32-bit types, however.
166 * Additionally, Win32 is supposed to measure the time since Windows started,
167 * as opposed to the actual time. */
168 ALuint timeGetTime(void)
170 #if _POSIX_TIMERS > 0
171 struct timespec ts;
172 int ret = -1;
174 #if defined(_POSIX_MONOTONIC_CLOCK) && (_POSIX_MONOTONIC_CLOCK >= 0)
175 #if _POSIX_MONOTONIC_CLOCK == 0
176 static int hasmono = 0;
177 if(hasmono > 0 || (hasmono == 0 &&
178 (hasmono=sysconf(_SC_MONOTONIC_CLOCK)) > 0))
179 #endif
180 ret = clock_gettime(CLOCK_MONOTONIC, &ts);
181 #endif
182 if(ret != 0)
183 ret = clock_gettime(CLOCK_REALTIME, &ts);
184 assert(ret == 0);
186 return ts.tv_nsec/1000000 + ts.tv_sec*1000;
187 #else
188 struct timeval tv;
189 int ret;
191 ret = gettimeofday(&tv, NULL);
192 assert(ret == 0);
194 return tv.tv_usec/1000 + tv.tv_sec*1000;
195 #endif
198 void Sleep(ALuint t)
200 struct timespec tv, rem;
201 tv.tv_nsec = (t*1000000)%1000000000;
202 tv.tv_sec = t/1000;
204 while(nanosleep(&tv, &rem) == -1 && errno == EINTR)
205 tv = rem;
208 #ifdef HAVE_DLFCN_H
210 void *LoadLib(const char *name)
212 const char *err;
213 void *handle;
215 dlerror();
216 handle = dlopen(name, RTLD_NOW);
217 if((err=dlerror()) != NULL)
218 handle = NULL;
219 return handle;
221 void CloseLib(void *handle)
222 { dlclose(handle); }
223 void *GetSymbol(void *handle, const char *name)
225 const char *err;
226 void *sym;
228 dlerror();
229 sym = dlsym(handle, name);
230 if((err=dlerror()) != NULL)
232 WARN("Failed to load %s: %s\n", name, err);
233 sym = NULL;
235 return sym;
238 #endif
239 #endif
242 void al_print(const char *func, const char *fmt, ...)
244 char str[256];
245 int i;
247 i = snprintf(str, sizeof(str), "AL lib: %s: ", func);
248 if(i < (int)sizeof(str) && i > 0)
250 va_list ap;
251 va_start(ap, fmt);
252 vsnprintf(str+i, sizeof(str)-i, fmt, ap);
253 va_end(ap);
255 str[sizeof(str)-1] = 0;
257 fprintf(LogFile, "%s", str);
258 fflush(LogFile);
262 void SetRTPriority(void)
264 ALboolean failed = AL_FALSE;
266 #ifdef _WIN32
267 if(RTPrioLevel > 0)
268 failed = !SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL);
269 #elif defined(HAVE_PTHREAD_SETSCHEDPARAM) && !defined(__OpenBSD__)
270 if(RTPrioLevel > 0)
272 struct sched_param param;
273 /* Use the minimum real-time priority possible for now (on Linux this
274 * should be 1 for SCHED_RR) */
275 param.sched_priority = sched_get_priority_min(SCHED_RR);
276 failed = !!pthread_setschedparam(pthread_self(), SCHED_RR, &param);
278 #else
279 /* Real-time priority not available */
280 failed = (RTPrioLevel>0);
281 #endif
282 if(failed)
283 ERR("Failed to set priority level for thread\n");
287 static void Lock(volatile ALenum *l)
289 while(ExchangeInt(l, AL_TRUE) == AL_TRUE)
290 sched_yield();
293 static void Unlock(volatile ALenum *l)
295 ExchangeInt(l, AL_FALSE);
298 void RWLockInit(RWLock *lock)
300 lock->read_count = 0;
301 lock->write_count = 0;
302 lock->read_lock = AL_FALSE;
303 lock->read_entry_lock = AL_FALSE;
304 lock->write_lock = AL_FALSE;
307 void ReadLock(RWLock *lock)
309 Lock(&lock->read_entry_lock);
310 Lock(&lock->read_lock);
311 if(IncrementRef(&lock->read_count) == 1)
312 Lock(&lock->write_lock);
313 Unlock(&lock->read_lock);
314 Unlock(&lock->read_entry_lock);
317 void ReadUnlock(RWLock *lock)
319 if(DecrementRef(&lock->read_count) == 0)
320 Unlock(&lock->write_lock);
323 void WriteLock(RWLock *lock)
325 if(IncrementRef(&lock->write_count) == 1)
326 Lock(&lock->read_lock);
327 Lock(&lock->write_lock);
330 void WriteUnlock(RWLock *lock)
332 Unlock(&lock->write_lock);
333 if(DecrementRef(&lock->write_count) == 0)
334 Unlock(&lock->read_lock);
338 void InitUIntMap(UIntMap *map, ALsizei limit)
340 map->array = NULL;
341 map->size = 0;
342 map->maxsize = 0;
343 map->limit = limit;
344 RWLockInit(&map->lock);
347 void ResetUIntMap(UIntMap *map)
349 WriteLock(&map->lock);
350 free(map->array);
351 map->array = NULL;
352 map->size = 0;
353 map->maxsize = 0;
354 WriteUnlock(&map->lock);
357 ALenum InsertUIntMapEntry(UIntMap *map, ALuint key, ALvoid *value)
359 ALsizei pos = 0;
361 WriteLock(&map->lock);
362 if(map->size > 0)
364 ALsizei low = 0;
365 ALsizei high = map->size - 1;
366 while(low < high)
368 ALsizei mid = low + (high-low)/2;
369 if(map->array[mid].key < key)
370 low = mid + 1;
371 else
372 high = mid;
374 if(map->array[low].key < key)
375 low++;
376 pos = low;
379 if(pos == map->size || map->array[pos].key != key)
381 if(map->size == map->limit)
383 WriteUnlock(&map->lock);
384 return AL_OUT_OF_MEMORY;
387 if(map->size == map->maxsize)
389 ALvoid *temp = NULL;
390 ALsizei newsize;
392 newsize = (map->maxsize ? (map->maxsize<<1) : 4);
393 if(newsize >= map->maxsize)
394 temp = realloc(map->array, newsize*sizeof(map->array[0]));
395 if(!temp)
397 WriteUnlock(&map->lock);
398 return AL_OUT_OF_MEMORY;
400 map->array = temp;
401 map->maxsize = newsize;
404 if(pos < map->size)
405 memmove(&map->array[pos+1], &map->array[pos],
406 (map->size-pos)*sizeof(map->array[0]));
407 map->size++;
409 map->array[pos].key = key;
410 map->array[pos].value = value;
411 WriteUnlock(&map->lock);
413 return AL_NO_ERROR;
416 ALvoid *RemoveUIntMapKey(UIntMap *map, ALuint key)
418 ALvoid *ptr = NULL;
419 WriteLock(&map->lock);
420 if(map->size > 0)
422 ALsizei low = 0;
423 ALsizei high = map->size - 1;
424 while(low < high)
426 ALsizei mid = low + (high-low)/2;
427 if(map->array[mid].key < key)
428 low = mid + 1;
429 else
430 high = mid;
432 if(map->array[low].key == key)
434 ptr = map->array[low].value;
435 if(low < map->size-1)
436 memmove(&map->array[low], &map->array[low+1],
437 (map->size-1-low)*sizeof(map->array[0]));
438 map->size--;
441 WriteUnlock(&map->lock);
442 return ptr;
445 ALvoid *LookupUIntMapKey(UIntMap *map, ALuint key)
447 ALvoid *ptr = NULL;
448 ReadLock(&map->lock);
449 if(map->size > 0)
451 ALsizei low = 0;
452 ALsizei high = map->size - 1;
453 while(low < high)
455 ALsizei mid = low + (high-low)/2;
456 if(map->array[mid].key < key)
457 low = mid + 1;
458 else
459 high = mid;
461 if(map->array[low].key == key)
462 ptr = map->array[low].value;
464 ReadUnlock(&map->lock);
465 return ptr;