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
28 #if defined(HAVE_GUIDDEF_H) || defined(HAVE_INITGUID_H)
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);
52 void pthread_once(pthread_once_t
*once
, void (*callback
)(void))
55 while((ret
=InterlockedExchange(once
, 1)) == 1)
59 InterlockedExchange(once
, 2);
63 int pthread_key_create(pthread_key_t
*key
, void (*callback
)(void*))
67 InsertUIntMapEntry(&TlsDestructor
, *key
, callback
);
71 int pthread_key_delete(pthread_key_t key
)
73 InsertUIntMapEntry(&TlsDestructor
, key
, NULL
);
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
);
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
)
96 ret
= (void*)GetProcAddress((HANDLE
)handle
, name
);
98 ERR("Failed to load %s\n", name
);
104 void InitializeCriticalSection(CRITICAL_SECTION
*cs
)
106 pthread_mutexattr_t attrib
;
109 ret
= pthread_mutexattr_init(&attrib
);
112 ret
= pthread_mutexattr_settype(&attrib
, PTHREAD_MUTEX_RECURSIVE
);
113 #ifdef HAVE_PTHREAD_NP_H
115 ret
= pthread_mutexattr_setkind_np(&attrib
, PTHREAD_MUTEX_RECURSIVE
);
118 ret
= pthread_mutex_init(cs
, &attrib
);
121 pthread_mutexattr_destroy(&attrib
);
123 void DeleteCriticalSection(CRITICAL_SECTION
*cs
)
126 ret
= pthread_mutex_destroy(cs
);
129 void EnterCriticalSection(CRITICAL_SECTION
*cs
)
132 ret
= pthread_mutex_lock(cs
);
135 void LeaveCriticalSection(CRITICAL_SECTION
*cs
)
138 ret
= pthread_mutex_unlock(cs
);
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
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))
158 ret
= clock_gettime(CLOCK_MONOTONIC
, &ts
);
161 ret
= clock_gettime(CLOCK_REALTIME
, &ts
);
164 return ts
.tv_nsec
/1000000 + ts
.tv_sec
*1000;
169 ret
= gettimeofday(&tv
, NULL
);
172 return tv
.tv_usec
/1000 + tv
.tv_sec
*1000;
178 struct timespec tv
, rem
;
179 tv
.tv_nsec
= (t
*1000000)%1000000000;
182 while(nanosleep(&tv
, &rem
) == -1 && errno
== EINTR
)
188 void *LoadLib(const char *name
)
194 handle
= dlopen(name
, RTLD_NOW
);
195 if((err
=dlerror()) != NULL
)
199 void CloseLib(void *handle
)
201 void *GetSymbol(void *handle
, const char *name
)
207 sym
= dlsym(handle
, name
);
208 if((err
=dlerror()) != NULL
)
210 WARN("Failed to load %s: %s\n", name
, err
);
220 void al_print(const char *func
, const char *fmt
, ...)
225 i
= snprintf(str
, sizeof(str
), "AL lib: %s: ", func
);
226 if(i
< (int)sizeof(str
) && i
> 0)
230 vsnprintf(str
+i
, sizeof(str
)-i
, fmt
, ap
);
233 str
[sizeof(str
)-1] = 0;
235 fprintf(LogFile
, "%s", str
);
240 void SetRTPriority(void)
242 ALboolean failed
= AL_FALSE
;
246 failed
= !SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL
);
247 #elif defined(HAVE_PTHREAD_SETSCHEDPARAM) && !defined(__OpenBSD__)
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
, ¶m
);
257 /* Real-time priority not available */
258 failed
= (RTPrioLevel
>0);
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
)
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
)
322 RWLockInit(&map
->lock
);
325 void ResetUIntMap(UIntMap
*map
)
327 WriteLock(&map
->lock
);
332 WriteUnlock(&map
->lock
);
335 ALenum
InsertUIntMapEntry(UIntMap
*map
, ALuint key
, ALvoid
*value
)
339 WriteLock(&map
->lock
);
343 ALsizei high
= map
->size
- 1;
346 ALsizei mid
= low
+ (high
-low
)/2;
347 if(map
->array
[mid
].key
< key
)
352 if(map
->array
[low
].key
< key
)
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
)
370 newsize
= (map
->maxsize
? (map
->maxsize
<<1) : 4);
371 if(newsize
>= map
->maxsize
)
372 temp
= realloc(map
->array
, newsize
*sizeof(map
->array
[0]));
375 WriteUnlock(&map
->lock
);
376 return AL_OUT_OF_MEMORY
;
379 map
->maxsize
= newsize
;
383 memmove(&map
->array
[pos
+1], &map
->array
[pos
],
384 (map
->size
-pos
)*sizeof(map
->array
[0]));
387 map
->array
[pos
].key
= key
;
388 map
->array
[pos
].value
= value
;
389 WriteUnlock(&map
->lock
);
394 ALvoid
*RemoveUIntMapKey(UIntMap
*map
, ALuint key
)
397 WriteLock(&map
->lock
);
401 ALsizei high
= map
->size
- 1;
404 ALsizei mid
= low
+ (high
-low
)/2;
405 if(map
->array
[mid
].key
< key
)
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]));
419 WriteUnlock(&map
->lock
);
423 ALvoid
*LookupUIntMapKey(UIntMap
*map
, ALuint key
)
426 ReadLock(&map
->lock
);
430 ALsizei high
= map
->size
- 1;
433 ALsizei mid
= low
+ (high
-low
)/2;
434 if(map
->array
[mid
].key
< key
)
439 if(map
->array
[low
].key
== key
)
440 ptr
= map
->array
[low
].value
;
442 ReadUnlock(&map
->lock
);