Don't bother handling MMX since we don't use it
[openal-soft.git] / Alc / helpers.c
blobb5a8363e76a0821e2528f12b9f118f563a58d02f
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 #include <time.h>
25 #include <errno.h>
26 #include <stdarg.h>
27 #ifdef HAVE_DLFCN_H
28 #include <dlfcn.h>
29 #endif
30 #ifdef HAVE_CPUID_H
31 #include <cpuid.h>
32 #endif
34 #if defined(HAVE_GUIDDEF_H) || defined(HAVE_INITGUID_H)
35 #define INITGUID
36 #include <windows.h>
37 #ifdef HAVE_GUIDDEF_H
38 #include <guiddef.h>
39 #else
40 #include <initguid.h>
41 #endif
43 DEFINE_GUID(KSDATAFORMAT_SUBTYPE_PCM, 0x00000001, 0x0000, 0x0010, 0x80,0x00, 0x00,0xaa,0x00,0x38,0x9b,0x71);
44 DEFINE_GUID(KSDATAFORMAT_SUBTYPE_IEEE_FLOAT, 0x00000003, 0x0000, 0x0010, 0x80,0x00, 0x00,0xaa,0x00,0x38,0x9b,0x71);
46 DEFINE_GUID(IID_IDirectSoundNotify, 0xb0210783, 0x89cd, 0x11d0, 0xaf,0x08, 0x00,0xa0,0xc9,0x25,0xcd,0x16);
48 DEFINE_GUID(CLSID_MMDeviceEnumerator, 0xbcde0395, 0xe52f, 0x467c, 0x8e,0x3d, 0xc4,0x57,0x92,0x91,0x69,0x2e);
49 DEFINE_GUID(IID_IMMDeviceEnumerator, 0xa95664d2, 0x9614, 0x4f35, 0xa7,0x46, 0xde,0x8d,0xb6,0x36,0x17,0xe6);
50 DEFINE_GUID(IID_IAudioClient, 0x1cb9ad4c, 0xdbfa, 0x4c32, 0xb1,0x78, 0xc2,0xf5,0x68,0xa7,0x03,0xb2);
51 DEFINE_GUID(IID_IAudioRenderClient, 0xf294acfc, 0x3146, 0x4483, 0xa7,0xbf, 0xad,0xdc,0xa7,0xc2,0x60,0xe2);
53 #ifdef HAVE_MMDEVAPI
54 #include <devpropdef.h>
56 DEFINE_DEVPROPKEY(DEVPKEY_Device_FriendlyName, 0xa45c254e, 0xdf1c, 0x4efd, 0x80,0x20, 0x67,0xd1,0x46,0xa8,0x50,0xe0, 14);
57 #endif
59 #endif
61 #include "alMain.h"
63 ALuint CPUCapFlags = 0;
66 void FillCPUCaps(ALuint capfilter)
68 ALuint caps = 0;
70 #if defined(__i386__) || defined(__x86_64__) || defined(_M_IX86) || defined(_M_X64)
71 /* FIXME: We really should get this for all available CPUs in case different
72 * CPUs have different caps (is that possible on one machine?). */
73 #ifdef HAVE_CPUID_H
74 union {
75 unsigned int regs[4];
76 char str[sizeof(unsigned int[4])];
77 } cpuinf[3];
79 if(!__get_cpuid(0, &cpuinf[0].regs[0], &cpuinf[0].regs[1], &cpuinf[0].regs[2], &cpuinf[0].regs[3]))
80 ERR("Failed to get CPUID\n");
81 else
83 TRACE("Vendor ID: \"%.4s%.4s%.4s\"\n", cpuinf[0].str+4, cpuinf[0].str+12, cpuinf[0].str+8);
84 if(__get_cpuid(0x80000002, &cpuinf[0].regs[0], &cpuinf[0].regs[1], &cpuinf[0].regs[2], &cpuinf[0].regs[3]) &&
85 __get_cpuid(0x80000003, &cpuinf[1].regs[0], &cpuinf[1].regs[1], &cpuinf[1].regs[2], &cpuinf[1].regs[3]) &&
86 __get_cpuid(0x80000004, &cpuinf[2].regs[0], &cpuinf[2].regs[1], &cpuinf[2].regs[2], &cpuinf[2].regs[3]))
87 TRACE("Name: \"%.16s%.16s%.16s\"\n", cpuinf[0].str, cpuinf[1].str, cpuinf[2].str);
89 if(!__get_cpuid(1, &cpuinf[0].regs[0], &cpuinf[0].regs[1], &cpuinf[0].regs[2], &cpuinf[0].regs[3]))
90 ERR("Failed to get CPU features\n");
91 else
93 #ifdef bit_SSE
94 if((cpuinf[0].regs[3]&bit_SSE))
95 caps |= CPU_CAP_SSE;
96 #endif
99 #endif
100 #endif
101 #ifdef HAVE_ARM_NEON_H
102 /* Assume Neon support if compiled with it */
103 caps |= CPU_CAP_NEON;
104 #endif
106 TRACE("Got caps:%s%s%s\n", ((caps&CPU_CAP_SSE)?((capfilter&CPU_CAP_SSE)?" SSE":" (SSE)"):""),
107 ((caps&CPU_CAP_NEON)?((capfilter&CPU_CAP_NEON)?" Neon":" (Neon)"):""),
108 ((!caps)?" -none-":""));
109 CPUCapFlags = caps & capfilter;
113 #ifdef _WIN32
114 void pthread_once(pthread_once_t *once, void (*callback)(void))
116 LONG ret;
117 while((ret=InterlockedExchange(once, 1)) == 1)
118 sched_yield();
119 if(ret == 0)
120 callback();
121 InterlockedExchange(once, 2);
125 int pthread_key_create(pthread_key_t *key, void (*callback)(void*))
127 *key = TlsAlloc();
128 if(callback)
129 InsertUIntMapEntry(&TlsDestructor, *key, callback);
130 return 0;
133 int pthread_key_delete(pthread_key_t key)
135 InsertUIntMapEntry(&TlsDestructor, key, NULL);
136 TlsFree(key);
137 return 0;
140 void *pthread_getspecific(pthread_key_t key)
141 { return TlsGetValue(key); }
143 int pthread_setspecific(pthread_key_t key, void *val)
145 TlsSetValue(key, val);
146 return 0;
150 void *LoadLib(const char *name)
151 { return LoadLibraryA(name); }
152 void CloseLib(void *handle)
153 { FreeLibrary((HANDLE)handle); }
154 void *GetSymbol(void *handle, const char *name)
156 void *ret;
158 ret = (void*)GetProcAddress((HANDLE)handle, name);
159 if(ret == NULL)
160 ERR("Failed to load %s\n", name);
161 return ret;
164 WCHAR *strdupW(const WCHAR *str)
166 const WCHAR *n;
167 WCHAR *ret;
168 size_t len;
170 n = str;
171 while(*n) n++;
172 len = n - str;
174 ret = calloc(sizeof(WCHAR), len+1);
175 if(ret != NULL)
176 memcpy(ret, str, sizeof(WCHAR)*len);
177 return ret;
180 #else
182 #include <pthread.h>
183 #ifdef HAVE_PTHREAD_NP_H
184 #include <pthread_np.h>
185 #endif
186 #include <sched.h>
188 void InitializeCriticalSection(CRITICAL_SECTION *cs)
190 pthread_mutexattr_t attrib;
191 int ret;
193 ret = pthread_mutexattr_init(&attrib);
194 assert(ret == 0);
196 ret = pthread_mutexattr_settype(&attrib, PTHREAD_MUTEX_RECURSIVE);
197 #ifdef HAVE_PTHREAD_NP_H
198 if(ret != 0)
199 ret = pthread_mutexattr_setkind_np(&attrib, PTHREAD_MUTEX_RECURSIVE);
200 #endif
201 assert(ret == 0);
202 ret = pthread_mutex_init(cs, &attrib);
203 assert(ret == 0);
205 pthread_mutexattr_destroy(&attrib);
207 void DeleteCriticalSection(CRITICAL_SECTION *cs)
209 int ret;
210 ret = pthread_mutex_destroy(cs);
211 assert(ret == 0);
213 void EnterCriticalSection(CRITICAL_SECTION *cs)
215 int ret;
216 ret = pthread_mutex_lock(cs);
217 assert(ret == 0);
219 void LeaveCriticalSection(CRITICAL_SECTION *cs)
221 int ret;
222 ret = pthread_mutex_unlock(cs);
223 assert(ret == 0);
226 /* NOTE: This wrapper isn't quite accurate as it returns an ALuint, as opposed
227 * to the expected DWORD. Both are defined as unsigned 32-bit types, however.
228 * Additionally, Win32 is supposed to measure the time since Windows started,
229 * as opposed to the actual time. */
230 ALuint timeGetTime(void)
232 #if _POSIX_TIMERS > 0
233 struct timespec ts;
234 int ret = -1;
236 #if defined(_POSIX_MONOTONIC_CLOCK) && (_POSIX_MONOTONIC_CLOCK >= 0)
237 #if _POSIX_MONOTONIC_CLOCK == 0
238 static int hasmono = 0;
239 if(hasmono > 0 || (hasmono == 0 &&
240 (hasmono=sysconf(_SC_MONOTONIC_CLOCK)) > 0))
241 #endif
242 ret = clock_gettime(CLOCK_MONOTONIC, &ts);
243 #endif
244 if(ret != 0)
245 ret = clock_gettime(CLOCK_REALTIME, &ts);
246 assert(ret == 0);
248 return ts.tv_nsec/1000000 + ts.tv_sec*1000;
249 #else
250 struct timeval tv;
251 int ret;
253 ret = gettimeofday(&tv, NULL);
254 assert(ret == 0);
256 return tv.tv_usec/1000 + tv.tv_sec*1000;
257 #endif
260 void Sleep(ALuint t)
262 struct timespec tv, rem;
263 tv.tv_nsec = (t*1000000)%1000000000;
264 tv.tv_sec = t/1000;
266 while(nanosleep(&tv, &rem) == -1 && errno == EINTR)
267 tv = rem;
270 #ifdef HAVE_DLFCN_H
272 void *LoadLib(const char *name)
274 const char *err;
275 void *handle;
277 dlerror();
278 handle = dlopen(name, RTLD_NOW);
279 if((err=dlerror()) != NULL)
280 handle = NULL;
281 return handle;
283 void CloseLib(void *handle)
284 { dlclose(handle); }
285 void *GetSymbol(void *handle, const char *name)
287 const char *err;
288 void *sym;
290 dlerror();
291 sym = dlsym(handle, name);
292 if((err=dlerror()) != NULL)
294 WARN("Failed to load %s: %s\n", name, err);
295 sym = NULL;
297 return sym;
300 #endif
301 #endif
304 void al_print(const char *func, const char *fmt, ...)
306 char str[256];
307 int i;
309 i = snprintf(str, sizeof(str), "AL lib: %s: ", func);
310 if(i < (int)sizeof(str) && i > 0)
312 va_list ap;
313 va_start(ap, fmt);
314 vsnprintf(str+i, sizeof(str)-i, fmt, ap);
315 va_end(ap);
317 str[sizeof(str)-1] = 0;
319 fprintf(LogFile, "%s", str);
320 fflush(LogFile);
324 void SetRTPriority(void)
326 ALboolean failed = AL_FALSE;
328 #ifdef _WIN32
329 if(RTPrioLevel > 0)
330 failed = !SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL);
331 #elif defined(HAVE_PTHREAD_SETSCHEDPARAM) && !defined(__OpenBSD__)
332 if(RTPrioLevel > 0)
334 struct sched_param param;
335 /* Use the minimum real-time priority possible for now (on Linux this
336 * should be 1 for SCHED_RR) */
337 param.sched_priority = sched_get_priority_min(SCHED_RR);
338 failed = !!pthread_setschedparam(pthread_self(), SCHED_RR, &param);
340 #else
341 /* Real-time priority not available */
342 failed = (RTPrioLevel>0);
343 #endif
344 if(failed)
345 ERR("Failed to set priority level for thread\n");
349 static void Lock(volatile ALenum *l)
351 while(ExchangeInt(l, AL_TRUE) == AL_TRUE)
352 sched_yield();
355 static void Unlock(volatile ALenum *l)
357 ExchangeInt(l, AL_FALSE);
360 void RWLockInit(RWLock *lock)
362 lock->read_count = 0;
363 lock->write_count = 0;
364 lock->read_lock = AL_FALSE;
365 lock->read_entry_lock = AL_FALSE;
366 lock->write_lock = AL_FALSE;
369 void ReadLock(RWLock *lock)
371 Lock(&lock->read_entry_lock);
372 Lock(&lock->read_lock);
373 if(IncrementRef(&lock->read_count) == 1)
374 Lock(&lock->write_lock);
375 Unlock(&lock->read_lock);
376 Unlock(&lock->read_entry_lock);
379 void ReadUnlock(RWLock *lock)
381 if(DecrementRef(&lock->read_count) == 0)
382 Unlock(&lock->write_lock);
385 void WriteLock(RWLock *lock)
387 if(IncrementRef(&lock->write_count) == 1)
388 Lock(&lock->read_lock);
389 Lock(&lock->write_lock);
392 void WriteUnlock(RWLock *lock)
394 Unlock(&lock->write_lock);
395 if(DecrementRef(&lock->write_count) == 0)
396 Unlock(&lock->read_lock);
400 void InitUIntMap(UIntMap *map, ALsizei limit)
402 map->array = NULL;
403 map->size = 0;
404 map->maxsize = 0;
405 map->limit = limit;
406 RWLockInit(&map->lock);
409 void ResetUIntMap(UIntMap *map)
411 WriteLock(&map->lock);
412 free(map->array);
413 map->array = NULL;
414 map->size = 0;
415 map->maxsize = 0;
416 WriteUnlock(&map->lock);
419 ALenum InsertUIntMapEntry(UIntMap *map, ALuint key, ALvoid *value)
421 ALsizei pos = 0;
423 WriteLock(&map->lock);
424 if(map->size > 0)
426 ALsizei low = 0;
427 ALsizei high = map->size - 1;
428 while(low < high)
430 ALsizei mid = low + (high-low)/2;
431 if(map->array[mid].key < key)
432 low = mid + 1;
433 else
434 high = mid;
436 if(map->array[low].key < key)
437 low++;
438 pos = low;
441 if(pos == map->size || map->array[pos].key != key)
443 if(map->size == map->limit)
445 WriteUnlock(&map->lock);
446 return AL_OUT_OF_MEMORY;
449 if(map->size == map->maxsize)
451 ALvoid *temp = NULL;
452 ALsizei newsize;
454 newsize = (map->maxsize ? (map->maxsize<<1) : 4);
455 if(newsize >= map->maxsize)
456 temp = realloc(map->array, newsize*sizeof(map->array[0]));
457 if(!temp)
459 WriteUnlock(&map->lock);
460 return AL_OUT_OF_MEMORY;
462 map->array = temp;
463 map->maxsize = newsize;
466 if(pos < map->size)
467 memmove(&map->array[pos+1], &map->array[pos],
468 (map->size-pos)*sizeof(map->array[0]));
469 map->size++;
471 map->array[pos].key = key;
472 map->array[pos].value = value;
473 WriteUnlock(&map->lock);
475 return AL_NO_ERROR;
478 ALvoid *RemoveUIntMapKey(UIntMap *map, ALuint key)
480 ALvoid *ptr = NULL;
481 WriteLock(&map->lock);
482 if(map->size > 0)
484 ALsizei low = 0;
485 ALsizei high = map->size - 1;
486 while(low < high)
488 ALsizei mid = low + (high-low)/2;
489 if(map->array[mid].key < key)
490 low = mid + 1;
491 else
492 high = mid;
494 if(map->array[low].key == key)
496 ptr = map->array[low].value;
497 if(low < map->size-1)
498 memmove(&map->array[low], &map->array[low+1],
499 (map->size-1-low)*sizeof(map->array[0]));
500 map->size--;
503 WriteUnlock(&map->lock);
504 return ptr;
507 ALvoid *LookupUIntMapKey(UIntMap *map, ALuint key)
509 ALvoid *ptr = NULL;
510 ReadLock(&map->lock);
511 if(map->size > 0)
513 ALsizei low = 0;
514 ALsizei high = map->size - 1;
515 while(low < high)
517 ALsizei mid = low + (high-low)/2;
518 if(map->array[mid].key < key)
519 low = mid + 1;
520 else
521 high = mid;
523 if(map->array[low].key == key)
524 ptr = map->array[low].value;
526 ReadUnlock(&map->lock);
527 return ptr;