Remove some unneeded markers
[openal-soft/openal-hmr.git] / Alc / helpers.c
blob693831d163b46cc5f88c5875b8c04200a95c2ff5
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 <propkeydef.h>
49 #include <devpropdef.h>
51 DEFINE_PROPERTYKEY(PKEY_AudioEndpoint_GUID, 0x1da5d803, 0xd492, 0x4edd, 0x8c,0x23, 0xe0,0xc0,0xff,0xee,0x7f,0x0e, 4);
52 DEFINE_DEVPROPKEY(DEVPKEY_Device_FriendlyName, 0xa45c254e, 0xdf1c, 0x4efd, 0x80,0x20, 0x67,0xd1,0x46,0xa8,0x50,0xe0, 14);
53 #endif
55 #endif
57 #include "alMain.h"
59 #ifdef _WIN32
60 void pthread_once(pthread_once_t *once, void (*callback)(void))
62 LONG ret;
63 while((ret=InterlockedExchange(once, 1)) == 1)
64 sched_yield();
65 if(ret == 0)
66 callback();
67 InterlockedExchange(once, 2);
71 int pthread_key_create(pthread_key_t *key, void (*callback)(void*))
73 *key = TlsAlloc();
74 if(callback)
75 InsertUIntMapEntry(&TlsDestructor, *key, callback);
76 return 0;
79 int pthread_key_delete(pthread_key_t key)
81 InsertUIntMapEntry(&TlsDestructor, key, NULL);
82 TlsFree(key);
83 return 0;
86 void *pthread_getspecific(pthread_key_t key)
87 { return TlsGetValue(key); }
89 int pthread_setspecific(pthread_key_t key, void *val)
91 TlsSetValue(key, val);
92 return 0;
96 void *LoadLib(const char *name)
97 { return LoadLibraryA(name); }
98 void CloseLib(void *handle)
99 { FreeLibrary((HANDLE)handle); }
100 void *GetSymbol(void *handle, const char *name)
102 void *ret;
104 ret = (void*)GetProcAddress((HANDLE)handle, name);
105 if(ret == NULL)
106 ERR("Failed to load %s\n", name);
107 return ret;
110 WCHAR *strdupW(const WCHAR *str)
112 const WCHAR *n;
113 WCHAR *ret;
114 size_t len;
116 n = str;
117 while(*n) n++;
118 len = n - str;
120 ret = calloc(sizeof(WCHAR), len+1);
121 if(ret != NULL)
122 memcpy(ret, str, sizeof(WCHAR)*len);
123 return ret;
126 #else
128 void InitializeCriticalSection(CRITICAL_SECTION *cs)
130 pthread_mutexattr_t attrib;
131 int ret;
133 ret = pthread_mutexattr_init(&attrib);
134 assert(ret == 0);
136 ret = pthread_mutexattr_settype(&attrib, PTHREAD_MUTEX_RECURSIVE);
137 #ifdef HAVE_PTHREAD_NP_H
138 if(ret != 0)
139 ret = pthread_mutexattr_setkind_np(&attrib, PTHREAD_MUTEX_RECURSIVE);
140 #endif
141 assert(ret == 0);
142 ret = pthread_mutex_init(cs, &attrib);
143 assert(ret == 0);
145 pthread_mutexattr_destroy(&attrib);
147 void DeleteCriticalSection(CRITICAL_SECTION *cs)
149 int ret;
150 ret = pthread_mutex_destroy(cs);
151 assert(ret == 0);
153 void EnterCriticalSection(CRITICAL_SECTION *cs)
155 int ret;
156 ret = pthread_mutex_lock(cs);
157 assert(ret == 0);
159 void LeaveCriticalSection(CRITICAL_SECTION *cs)
161 int ret;
162 ret = pthread_mutex_unlock(cs);
163 assert(ret == 0);
166 /* NOTE: This wrapper isn't quite accurate as it returns an ALuint, as opposed
167 * to the expected DWORD. Both are defined as unsigned 32-bit types, however.
168 * Additionally, Win32 is supposed to measure the time since Windows started,
169 * as opposed to the actual time. */
170 ALuint timeGetTime(void)
172 #if _POSIX_TIMERS > 0
173 struct timespec ts;
174 int ret = -1;
176 #if defined(_POSIX_MONOTONIC_CLOCK) && (_POSIX_MONOTONIC_CLOCK >= 0)
177 #if _POSIX_MONOTONIC_CLOCK == 0
178 static int hasmono = 0;
179 if(hasmono > 0 || (hasmono == 0 &&
180 (hasmono=sysconf(_SC_MONOTONIC_CLOCK)) > 0))
181 #endif
182 ret = clock_gettime(CLOCK_MONOTONIC, &ts);
183 #endif
184 if(ret != 0)
185 ret = clock_gettime(CLOCK_REALTIME, &ts);
186 assert(ret == 0);
188 return ts.tv_nsec/1000000 + ts.tv_sec*1000;
189 #else
190 struct timeval tv;
191 int ret;
193 ret = gettimeofday(&tv, NULL);
194 assert(ret == 0);
196 return tv.tv_usec/1000 + tv.tv_sec*1000;
197 #endif
200 void Sleep(ALuint t)
202 struct timespec tv, rem;
203 tv.tv_nsec = (t*1000000)%1000000000;
204 tv.tv_sec = t/1000;
206 while(nanosleep(&tv, &rem) == -1 && errno == EINTR)
207 tv = rem;
210 #ifdef HAVE_DLFCN_H
212 void *LoadLib(const char *name)
214 const char *err;
215 void *handle;
217 dlerror();
218 handle = dlopen(name, RTLD_NOW);
219 if((err=dlerror()) != NULL)
220 handle = NULL;
221 return handle;
223 void CloseLib(void *handle)
224 { dlclose(handle); }
225 void *GetSymbol(void *handle, const char *name)
227 const char *err;
228 void *sym;
230 dlerror();
231 sym = dlsym(handle, name);
232 if((err=dlerror()) != NULL)
234 WARN("Failed to load %s: %s\n", name, err);
235 sym = NULL;
237 return sym;
240 #endif
241 #endif
244 void al_print(const char *func, const char *fmt, ...)
246 char str[256];
247 int i;
249 i = snprintf(str, sizeof(str), "AL lib: %s: ", func);
250 if(i < (int)sizeof(str) && i > 0)
252 va_list ap;
253 va_start(ap, fmt);
254 vsnprintf(str+i, sizeof(str)-i, fmt, ap);
255 va_end(ap);
257 str[sizeof(str)-1] = 0;
259 fprintf(LogFile, "%s", str);
260 fflush(LogFile);
264 void SetRTPriority(void)
266 ALboolean failed = AL_FALSE;
268 #ifdef _WIN32
269 if(RTPrioLevel > 0)
270 failed = !SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL);
271 #elif defined(HAVE_PTHREAD_SETSCHEDPARAM) && !defined(__OpenBSD__)
272 if(RTPrioLevel > 0)
274 struct sched_param param;
275 /* Use the minimum real-time priority possible for now (on Linux this
276 * should be 1 for SCHED_RR) */
277 param.sched_priority = sched_get_priority_min(SCHED_RR);
278 failed = !!pthread_setschedparam(pthread_self(), SCHED_RR, &param);
280 #else
281 /* Real-time priority not available */
282 failed = (RTPrioLevel>0);
283 #endif
284 if(failed)
285 ERR("Failed to set priority level for thread\n");
289 static void Lock(volatile ALenum *l)
291 while(ExchangeInt(l, AL_TRUE) == AL_TRUE)
292 sched_yield();
295 static void Unlock(volatile ALenum *l)
297 ExchangeInt(l, AL_FALSE);
300 void RWLockInit(RWLock *lock)
302 lock->read_count = 0;
303 lock->write_count = 0;
304 lock->read_lock = AL_FALSE;
305 lock->read_entry_lock = AL_FALSE;
306 lock->write_lock = AL_FALSE;
309 void ReadLock(RWLock *lock)
311 Lock(&lock->read_entry_lock);
312 Lock(&lock->read_lock);
313 if(IncrementRef(&lock->read_count) == 1)
314 Lock(&lock->write_lock);
315 Unlock(&lock->read_lock);
316 Unlock(&lock->read_entry_lock);
319 void ReadUnlock(RWLock *lock)
321 if(DecrementRef(&lock->read_count) == 0)
322 Unlock(&lock->write_lock);
325 void WriteLock(RWLock *lock)
327 if(IncrementRef(&lock->write_count) == 1)
328 Lock(&lock->read_lock);
329 Lock(&lock->write_lock);
332 void WriteUnlock(RWLock *lock)
334 Unlock(&lock->write_lock);
335 if(DecrementRef(&lock->write_count) == 0)
336 Unlock(&lock->read_lock);
340 void InitUIntMap(UIntMap *map, ALsizei limit)
342 map->array = NULL;
343 map->size = 0;
344 map->maxsize = 0;
345 map->limit = limit;
346 RWLockInit(&map->lock);
349 void ResetUIntMap(UIntMap *map)
351 WriteLock(&map->lock);
352 free(map->array);
353 map->array = NULL;
354 map->size = 0;
355 map->maxsize = 0;
356 WriteUnlock(&map->lock);
359 ALenum InsertUIntMapEntry(UIntMap *map, ALuint key, ALvoid *value)
361 ALsizei pos = 0;
363 WriteLock(&map->lock);
364 if(map->size > 0)
366 ALsizei low = 0;
367 ALsizei high = map->size - 1;
368 while(low < high)
370 ALsizei mid = low + (high-low)/2;
371 if(map->array[mid].key < key)
372 low = mid + 1;
373 else
374 high = mid;
376 if(map->array[low].key < key)
377 low++;
378 pos = low;
381 if(pos == map->size || map->array[pos].key != key)
383 if(map->size == map->limit)
385 WriteUnlock(&map->lock);
386 return AL_OUT_OF_MEMORY;
389 if(map->size == map->maxsize)
391 ALvoid *temp = NULL;
392 ALsizei newsize;
394 newsize = (map->maxsize ? (map->maxsize<<1) : 4);
395 if(newsize >= map->maxsize)
396 temp = realloc(map->array, newsize*sizeof(map->array[0]));
397 if(!temp)
399 WriteUnlock(&map->lock);
400 return AL_OUT_OF_MEMORY;
402 map->array = temp;
403 map->maxsize = newsize;
406 if(pos < map->size)
407 memmove(&map->array[pos+1], &map->array[pos],
408 (map->size-pos)*sizeof(map->array[0]));
409 map->size++;
411 map->array[pos].key = key;
412 map->array[pos].value = value;
413 WriteUnlock(&map->lock);
415 return AL_NO_ERROR;
418 ALvoid *RemoveUIntMapKey(UIntMap *map, ALuint key)
420 ALvoid *ptr = NULL;
421 WriteLock(&map->lock);
422 if(map->size > 0)
424 ALsizei low = 0;
425 ALsizei high = map->size - 1;
426 while(low < high)
428 ALsizei mid = low + (high-low)/2;
429 if(map->array[mid].key < key)
430 low = mid + 1;
431 else
432 high = mid;
434 if(map->array[low].key == key)
436 ptr = map->array[low].value;
437 if(low < map->size-1)
438 memmove(&map->array[low], &map->array[low+1],
439 (map->size-1-low)*sizeof(map->array[0]));
440 map->size--;
443 WriteUnlock(&map->lock);
444 return ptr;
447 ALvoid *LookupUIntMapKey(UIntMap *map, ALuint key)
449 ALvoid *ptr = NULL;
450 ReadLock(&map->lock);
451 if(map->size > 0)
453 ALsizei low = 0;
454 ALsizei high = map->size - 1;
455 while(low < high)
457 ALsizei mid = low + (high-low)/2;
458 if(map->array[mid].key < key)
459 low = mid + 1;
460 else
461 high = mid;
463 if(map->array[low].key == key)
464 ptr = map->array[low].value;
466 ReadUnlock(&map->lock);
467 return ptr;