Remove an unnecessary else statement
[openal-soft/openal-hmr.git] / Alc / helpers.c
blobc5e2be6c7c2642448b29c891dadae764d778986f
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 #endif
49 #include "alMain.h"
51 #ifdef _WIN32
52 void pthread_once(pthread_once_t *once, void (*callback)(void))
54 LONG ret;
55 while((ret=InterlockedExchange(once, 1)) == 1)
56 sched_yield();
57 if(ret == 0)
58 callback();
59 InterlockedExchange(once, 2);
63 int pthread_key_create(pthread_key_t *key, void (*callback)(void*))
65 *key = TlsAlloc();
66 if(callback)
67 InsertUIntMapEntry(&TlsDestructor, *key, callback);
68 return 0;
71 int pthread_key_delete(pthread_key_t key)
73 InsertUIntMapEntry(&TlsDestructor, key, NULL);
74 TlsFree(key);
75 return 0;
78 void *pthread_getspecific(pthread_key_t key)
79 { return TlsGetValue(key); }
81 int pthread_setspecific(pthread_key_t key, void *val)
83 TlsSetValue(key, val);
84 return 0;
88 void *LoadLib(const char *name)
89 { return LoadLibraryA(name); }
90 void CloseLib(void *handle)
91 { FreeLibrary((HANDLE)handle); }
92 void *GetSymbol(void *handle, const char *name)
94 void *ret;
96 ret = (void*)GetProcAddress((HANDLE)handle, name);
97 if(ret == NULL)
98 ERR("Failed to load %s\n", name);
99 return ret;
102 #else
104 void InitializeCriticalSection(CRITICAL_SECTION *cs)
106 pthread_mutexattr_t attrib;
107 int ret;
109 ret = pthread_mutexattr_init(&attrib);
110 assert(ret == 0);
112 ret = pthread_mutexattr_settype(&attrib, PTHREAD_MUTEX_RECURSIVE);
113 #ifdef HAVE_PTHREAD_NP_H
114 if(ret != 0)
115 ret = pthread_mutexattr_setkind_np(&attrib, PTHREAD_MUTEX_RECURSIVE);
116 #endif
117 assert(ret == 0);
118 ret = pthread_mutex_init(cs, &attrib);
119 assert(ret == 0);
121 pthread_mutexattr_destroy(&attrib);
123 void DeleteCriticalSection(CRITICAL_SECTION *cs)
125 int ret;
126 ret = pthread_mutex_destroy(cs);
127 assert(ret == 0);
129 void EnterCriticalSection(CRITICAL_SECTION *cs)
131 int ret;
132 ret = pthread_mutex_lock(cs);
133 assert(ret == 0);
135 void LeaveCriticalSection(CRITICAL_SECTION *cs)
137 int ret;
138 ret = pthread_mutex_unlock(cs);
139 assert(ret == 0);
142 /* NOTE: This wrapper isn't quite accurate as it returns an ALuint, as opposed
143 * to the expected DWORD. Both are defined as unsigned 32-bit types, however.
144 * Additionally, Win32 is supposed to measure the time since Windows started,
145 * as opposed to the actual time. */
146 ALuint timeGetTime(void)
148 #if _POSIX_TIMERS > 0
149 struct timespec ts;
150 int ret = -1;
152 #if defined(_POSIX_MONOTONIC_CLOCK) && (_POSIX_MONOTONIC_CLOCK >= 0)
153 #if _POSIX_MONOTONIC_CLOCK == 0
154 static int hasmono = 0;
155 if(hasmono > 0 || (hasmono == 0 &&
156 (hasmono=sysconf(_SC_MONOTONIC_CLOCK)) > 0))
157 #endif
158 ret = clock_gettime(CLOCK_MONOTONIC, &ts);
159 #endif
160 if(ret != 0)
161 ret = clock_gettime(CLOCK_REALTIME, &ts);
162 assert(ret == 0);
164 return ts.tv_nsec/1000000 + ts.tv_sec*1000;
165 #else
166 struct timeval tv;
167 int ret;
169 ret = gettimeofday(&tv, NULL);
170 assert(ret == 0);
172 return tv.tv_usec/1000 + tv.tv_sec*1000;
173 #endif
176 void Sleep(ALuint t)
178 struct timespec tv, rem;
179 tv.tv_nsec = (t*1000000)%1000000000;
180 tv.tv_sec = t/1000;
182 while(nanosleep(&tv, &rem) == -1 && errno == EINTR)
183 tv = rem;
186 #ifdef HAVE_DLFCN_H
188 void *LoadLib(const char *name)
190 const char *err;
191 void *handle;
193 dlerror();
194 handle = dlopen(name, RTLD_NOW);
195 if((err=dlerror()) != NULL)
196 handle = NULL;
197 return handle;
199 void CloseLib(void *handle)
200 { dlclose(handle); }
201 void *GetSymbol(void *handle, const char *name)
203 const char *err;
204 void *sym;
206 dlerror();
207 sym = dlsym(handle, name);
208 if((err=dlerror()) != NULL)
210 WARN("Failed to load %s: %s\n", name, err);
211 sym = NULL;
213 return sym;
216 #endif
217 #endif
220 void al_print(const char *func, const char *fmt, ...)
222 char str[256];
223 int i;
225 i = snprintf(str, sizeof(str), "AL lib: %s: ", func);
226 if(i < (int)sizeof(str) && i > 0)
228 va_list ap;
229 va_start(ap, fmt);
230 vsnprintf(str+i, sizeof(str)-i, fmt, ap);
231 va_end(ap);
233 str[sizeof(str)-1] = 0;
235 fprintf(LogFile, "%s", str);
236 fflush(LogFile);
240 void SetRTPriority(void)
242 ALboolean failed = AL_FALSE;
244 #ifdef _WIN32
245 if(RTPrioLevel > 0)
246 failed = !SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL);
247 #elif defined(HAVE_PTHREAD_SETSCHEDPARAM) && !defined(__OpenBSD__)
248 if(RTPrioLevel > 0)
250 struct sched_param param;
251 /* Use the minimum real-time priority possible for now (on Linux this
252 * should be 1 for SCHED_RR) */
253 param.sched_priority = sched_get_priority_min(SCHED_RR);
254 failed = !!pthread_setschedparam(pthread_self(), SCHED_RR, &param);
256 #else
257 /* Real-time priority not available */
258 failed = (RTPrioLevel>0);
259 #endif
260 if(failed)
261 ERR("Failed to set priority level for thread\n");
265 static void Lock(volatile ALenum *l)
267 while(ExchangeInt(l, AL_TRUE) == AL_TRUE)
268 sched_yield();
271 static void Unlock(volatile ALenum *l)
273 ExchangeInt(l, AL_FALSE);
276 void RWLockInit(RWLock *lock)
278 lock->read_count = 0;
279 lock->write_count = 0;
280 lock->read_lock = AL_FALSE;
281 lock->read_entry_lock = AL_FALSE;
282 lock->write_lock = AL_FALSE;
285 void ReadLock(RWLock *lock)
287 Lock(&lock->read_entry_lock);
288 Lock(&lock->read_lock);
289 if(IncrementRef(&lock->read_count) == 1)
290 Lock(&lock->write_lock);
291 Unlock(&lock->read_lock);
292 Unlock(&lock->read_entry_lock);
295 void ReadUnlock(RWLock *lock)
297 if(DecrementRef(&lock->read_count) == 0)
298 Unlock(&lock->write_lock);
301 void WriteLock(RWLock *lock)
303 if(IncrementRef(&lock->write_count) == 1)
304 Lock(&lock->read_lock);
305 Lock(&lock->write_lock);
308 void WriteUnlock(RWLock *lock)
310 Unlock(&lock->write_lock);
311 if(DecrementRef(&lock->write_count) == 0)
312 Unlock(&lock->read_lock);
316 void InitUIntMap(UIntMap *map, ALsizei limit)
318 map->array = NULL;
319 map->size = 0;
320 map->maxsize = 0;
321 map->limit = limit;
322 RWLockInit(&map->lock);
325 void ResetUIntMap(UIntMap *map)
327 WriteLock(&map->lock);
328 free(map->array);
329 map->array = NULL;
330 map->size = 0;
331 map->maxsize = 0;
332 WriteUnlock(&map->lock);
335 ALenum InsertUIntMapEntry(UIntMap *map, ALuint key, ALvoid *value)
337 ALsizei pos = 0;
339 WriteLock(&map->lock);
340 if(map->size > 0)
342 ALsizei low = 0;
343 ALsizei high = map->size - 1;
344 while(low < high)
346 ALsizei mid = low + (high-low)/2;
347 if(map->array[mid].key < key)
348 low = mid + 1;
349 else
350 high = mid;
352 if(map->array[low].key < key)
353 low++;
354 pos = low;
357 if(pos == map->size || map->array[pos].key != key)
359 if(map->size == map->limit)
361 WriteUnlock(&map->lock);
362 return AL_OUT_OF_MEMORY;
365 if(map->size == map->maxsize)
367 ALvoid *temp = NULL;
368 ALsizei newsize;
370 newsize = (map->maxsize ? (map->maxsize<<1) : 4);
371 if(newsize >= map->maxsize)
372 temp = realloc(map->array, newsize*sizeof(map->array[0]));
373 if(!temp)
375 WriteUnlock(&map->lock);
376 return AL_OUT_OF_MEMORY;
378 map->array = temp;
379 map->maxsize = newsize;
382 if(pos < map->size)
383 memmove(&map->array[pos+1], &map->array[pos],
384 (map->size-pos)*sizeof(map->array[0]));
385 map->size++;
387 map->array[pos].key = key;
388 map->array[pos].value = value;
389 WriteUnlock(&map->lock);
391 return AL_NO_ERROR;
394 ALvoid *RemoveUIntMapKey(UIntMap *map, ALuint key)
396 ALvoid *ptr = NULL;
397 WriteLock(&map->lock);
398 if(map->size > 0)
400 ALsizei low = 0;
401 ALsizei high = map->size - 1;
402 while(low < high)
404 ALsizei mid = low + (high-low)/2;
405 if(map->array[mid].key < key)
406 low = mid + 1;
407 else
408 high = mid;
410 if(map->array[low].key == key)
412 ptr = map->array[low].value;
413 if(low < map->size-1)
414 memmove(&map->array[low], &map->array[low+1],
415 (map->size-1-low)*sizeof(map->array[0]));
416 map->size--;
419 WriteUnlock(&map->lock);
420 return ptr;
423 ALvoid *LookupUIntMapKey(UIntMap *map, ALuint key)
425 ALvoid *ptr = NULL;
426 ReadLock(&map->lock);
427 if(map->size > 0)
429 ALsizei low = 0;
430 ALsizei high = map->size - 1;
431 while(low < high)
433 ALsizei mid = low + (high-low)/2;
434 if(map->array[mid].key < key)
435 low = mid + 1;
436 else
437 high = mid;
439 if(map->array[low].key == key)
440 ptr = map->array[low].value;
442 ReadUnlock(&map->lock);
443 return ptr;