Use pthread_mutexattr_setkind_np as a fallback to set a recursive mutex type
[openal-soft.git] / OpenAL32 / Include / alMain.h
blob6bc5fd4854d48bccb8813369c0d40213c150c29a
1 #ifndef AL_MAIN_H
2 #define AL_MAIN_H
4 #include <string.h>
5 #include <stdio.h>
7 #include "alu.h"
9 #ifdef _WIN32
11 #ifndef _WIN32_WINNT
12 #define _WIN32_WINNT 0x0500
13 #endif
14 #include <windows.h>
16 #else
18 #include <assert.h>
19 #include <pthread.h>
20 #ifdef HAVE_PTHREAD_NP_H
21 #include <pthread_np.h>
22 #endif
23 #include <sys/time.h>
24 #include <time.h>
25 #include <errno.h>
27 #define IsBadWritePtr(a,b) (0)
29 typedef pthread_mutex_t CRITICAL_SECTION;
30 static inline void EnterCriticalSection(CRITICAL_SECTION *cs)
32 int ret;
33 ret = pthread_mutex_lock(cs);
34 assert(ret == 0);
36 static inline void LeaveCriticalSection(CRITICAL_SECTION *cs)
38 int ret;
39 ret = pthread_mutex_unlock(cs);
40 assert(ret == 0);
42 static inline void InitializeCriticalSection(CRITICAL_SECTION *cs)
44 pthread_mutexattr_t attrib;
45 int ret;
47 ret = pthread_mutexattr_init(&attrib);
48 assert(ret == 0);
50 ret = pthread_mutexattr_settype(&attrib, PTHREAD_MUTEX_RECURSIVE);
51 #ifdef HAVE_PTHREAD_NP_H
52 if(ret != 0)
53 ret = pthread_mutexattr_setkind_np(&attrib, PTHREAD_MUTEX_RECURSIVE);
54 #endif
55 assert(ret == 0);
56 ret = pthread_mutex_init(cs, &attrib);
57 assert(ret == 0);
59 pthread_mutexattr_destroy(&attrib);
62 static inline void DeleteCriticalSection(CRITICAL_SECTION *cs)
64 int ret;
65 ret = pthread_mutex_destroy(cs);
66 assert(ret == 0);
69 /* NOTE: This wrapper isn't quite accurate as it returns an ALuint, as opposed
70 * to the expected DWORD. Both are defined as unsigned 32-bit types, however.
71 * Additionally, Win32 is supposed to measure the time since Windows started,
72 * as opposed to the actual time. */
73 static inline ALuint timeGetTime(void)
75 struct timeval tv;
76 int ret;
78 ret = gettimeofday(&tv, NULL);
79 assert(ret == 0);
81 return tv.tv_usec/1000 + tv.tv_sec*1000;
84 static inline void Sleep(ALuint t)
86 struct timespec tv, rem;
87 tv.tv_nsec = (t*1000000)%1000000000;
88 tv.tv_sec = t/1000;
90 while(nanosleep(&tv, &rem) == -1 && errno == EINTR)
91 tv = rem;
93 #define min(x,y) (((x)<(y))?(x):(y))
94 #define max(x,y) (((x)>(y))?(x):(y))
95 #endif
97 #include "AL/al.h"
98 #include "AL/alc.h"
99 #include "AL/alext.h"
100 #include "alListener.h"
102 #ifdef __cplusplus
103 extern "C" {
104 #endif
106 extern CRITICAL_SECTION _alMutex;
108 extern char _alDebug[256];
110 #define AL_PRINT(...) do { \
111 int _al_print_i; \
112 const char *_al_print_fn = strrchr(__FILE__, '/'); \
113 if(!_al_print_fn) _al_print_fn = __FILE__; \
114 else _al_print_fn += 1; \
115 _al_print_i = snprintf(_alDebug, sizeof(_alDebug), "AL lib: %s:%d: ", _al_print_fn, __LINE__); \
116 if(_al_print_i < (int)sizeof(_alDebug) && _al_print_i > 0) \
117 snprintf(_alDebug+_al_print_i, sizeof(_alDebug)-_al_print_i, __VA_ARGS__); \
118 _alDebug[sizeof(_alDebug)-1] = 0; \
119 fprintf(stderr, "%s", _alDebug); \
120 } while(0)
123 #define SWMIXER_OUTPUT_RATE 44100
125 #define SPEEDOFSOUNDMETRESPERSEC (343.3f)
126 #define AIRABSORBGAINHF (0.994f)
128 typedef struct {
129 ALCboolean (*OpenPlayback)(ALCdevice*, const ALCchar*);
130 void (*ClosePlayback)(ALCdevice*);
132 ALCboolean (*OpenCapture)(ALCdevice*, const ALCchar*, ALCuint, ALCenum, ALCsizei);
133 void (*CloseCapture)(ALCdevice*);
134 void (*StartCapture)(ALCdevice*);
135 void (*StopCapture)(ALCdevice*);
136 void (*CaptureSamples)(ALCdevice*, void*, ALCuint);
137 ALCuint (*AvailableSamples)(ALCdevice*);
138 } BackendFuncs;
140 void alc_alsa_init(BackendFuncs *func_list);
141 void alc_oss_init(BackendFuncs *func_list);
142 void alcDSoundInit(BackendFuncs *func_list);
143 void alcWinMMInit(BackendFuncs *FuncList);
144 void alc_wave_init(BackendFuncs *func_list);
147 struct ALCdevice_struct
149 ALboolean IsCaptureDevice;
151 ALuint Frequency;
152 ALuint UpdateSize;
153 ALenum Format;
155 ALCchar *szDeviceName;
157 // Maximum number of sources that can be created
158 ALuint MaxNoOfSources;
160 // Context created on this device
161 ALCcontext *Context;
163 BackendFuncs *Funcs;
164 void *ExtraData; // For the backend's use
166 ALCdevice *next;
169 #define ALCdevice_OpenPlayback(a,b) ((a)->Funcs->OpenPlayback((a), (b)))
170 #define ALCdevice_ClosePlayback(a) ((a)->Funcs->ClosePlayback((a)))
171 #define ALCdevice_OpenCapture(a,b,c,d,e) ((a)->Funcs->OpenCapture((a), (b), (c), (d), (e)))
172 #define ALCdevice_CloseCapture(a) ((a)->Funcs->CloseCapture((a)))
173 #define ALCdevice_StartCapture(a) ((a)->Funcs->StartCapture((a)))
174 #define ALCdevice_StopCapture(a) ((a)->Funcs->StopCapture((a)))
175 #define ALCdevice_CaptureSamples(a,b,c) ((a)->Funcs->CaptureSamples((a), (b), (c)))
176 #define ALCdevice_AvailableSamples(a) ((a)->Funcs->AvailableSamples((a)))
178 struct ALCcontext_struct
180 ALlistener Listener;
182 struct ALsource *Source;
183 ALuint SourceCount;
185 struct ALeffectslot *AuxiliaryEffectSlot;
186 ALuint AuxiliaryEffectSlotCount;
188 ALenum LastError;
189 ALboolean InUse;
191 ALuint Frequency;
193 ALenum DistanceModel;
195 ALfloat DopplerFactor;
196 ALfloat DopplerVelocity;
197 ALfloat flSpeedOfSound;
199 ALint lNumMonoSources;
200 ALint lNumStereoSources;
202 ALCdevice *Device;
203 ALCchar ExtensionList[1024];
205 struct bs2b *bs2b;
207 ALCcontext *next;
210 ALCvoid ReleaseALC(ALCvoid);
212 ALCchar *AppendDeviceList(char *name);
213 ALCchar *AppendAllDeviceList(char *name);
214 ALCchar *AppendCaptureDeviceList(char *name);
216 ALCvoid SetALCError(ALenum errorCode);
218 ALCvoid SuspendContext(ALCcontext *context);
219 ALCvoid ProcessContext(ALCcontext *context);
221 ALvoid *StartThread(ALuint (*func)(ALvoid*), ALvoid *ptr);
222 ALuint StopThread(ALvoid *thread);
224 typedef struct RingBuffer RingBuffer;
225 RingBuffer *CreateRingBuffer(ALsizei frame_size, ALsizei length);
226 void DestroyRingBuffer(RingBuffer *ring);
227 ALsizei RingBufferSize(RingBuffer *ring);
228 void WriteRingBuffer(RingBuffer *ring, const ALubyte *data, ALsizei len);
229 void ReadRingBuffer(RingBuffer *ring, ALubyte *data, ALsizei len);
231 void ReadALConfig(void);
232 void FreeALConfig(void);
233 const char *GetConfigValue(const char *blockName, const char *keyName, const char *def);
234 int GetConfigValueInt(const char *blockName, const char *keyName, int def);
235 float GetConfigValueFloat(const char *blockName, const char *keyName, float def);
237 #ifdef __cplusplus
239 #endif
241 #endif