Don't feed the HRTF mix to the click removal and pending click buffers
[openal-soft.git] / Alc / threads.c
blob64586ae90e46b1b244e55d6d95bab79dc55775cd
1 /**
2 * OpenAL cross platform audio library
3 * Copyright (C) 1999-2007 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 "threads.h"
25 #include <stdlib.h>
26 #include <errno.h>
28 #include "alMain.h"
29 #include "alThunk.h"
31 #define THREAD_STACK_SIZE (1*1024*1024) /* 1MB */
33 #ifdef _WIN32
35 #define WIN32_LEAN_AND_MEAN
36 #include <windows.h>
38 typedef struct althread_info {
39 ALuint (*func)(ALvoid*);
40 ALvoid *ptr;
41 HANDLE hdl;
42 } althread_info;
44 static DWORD CALLBACK StarterFunc(void *ptr)
46 althread_info *inf = (althread_info*)ptr;
47 ALuint ret;
49 ret = inf->func(inf->ptr);
50 ExitThread((DWORD)ret);
52 return (DWORD)ret;
56 ALboolean StartThread(althread_t *thread, ALuint (*func)(ALvoid*), ALvoid *ptr)
58 althread_info *info;
59 DWORD dummy;
61 info = malloc(sizeof(*info));
62 if(!info) return AL_FALSE;
64 info->func = func;
65 info->ptr = ptr;
67 info->hdl = CreateThread(NULL, THREAD_STACK_SIZE, StarterFunc, info, 0, &dummy);
68 if(!info->hdl)
70 free(info);
71 return AL_FALSE;
74 *thread = info;
75 return AL_TRUE;
78 ALuint StopThread(althread_t thread)
80 DWORD ret = 0;
82 WaitForSingleObject(thread->hdl, INFINITE);
83 GetExitCodeThread(thread->hdl, &ret);
84 CloseHandle(thread->hdl);
86 free(thread);
88 return (ALuint)ret;
92 void SetThreadName(const char *name)
94 #if defined(_MSC_VER)
95 #define MS_VC_EXCEPTION 0x406D1388
96 struct {
97 DWORD dwType; // Must be 0x1000.
98 LPCSTR szName; // Pointer to name (in user addr space).
99 DWORD dwThreadID; // Thread ID (-1=caller thread).
100 DWORD dwFlags; // Reserved for future use, must be zero.
101 } info;
102 info.dwType = 0x1000;
103 info.szName = name;
104 info.dwThreadID = -1;
105 info.dwFlags = 0;
107 __try {
108 RaiseException(MS_VC_EXCEPTION, 0, sizeof(info)/sizeof(DWORD), (ULONG_PTR*)&info);
110 __except(EXCEPTION_CONTINUE_EXECUTION) {
112 #undef MS_VC_EXCEPTION
113 #else
114 TRACE("Can't set thread %04lx name to \"%s\"\n", GetCurrentThreadId(), name);
115 #endif
118 #else
120 #include <pthread.h>
122 typedef struct althread_info {
123 ALuint (*func)(ALvoid*);
124 ALvoid *ptr;
125 ALuint ret;
126 pthread_t hdl;
127 } althread_info;
129 static void *StarterFunc(void *ptr)
131 althread_info *inf = (althread_info*)ptr;
132 inf->ret = inf->func(inf->ptr);
133 return NULL;
137 ALboolean StartThread(althread_t *thread, ALuint (*func)(ALvoid*), ALvoid *ptr)
139 pthread_attr_t attr;
140 althread_info *info;
142 info = malloc(sizeof(*info));
143 if(!info) return AL_FALSE;
145 if(pthread_attr_init(&attr) != 0)
147 free(info);
148 return AL_FALSE;
150 if(pthread_attr_setstacksize(&attr, THREAD_STACK_SIZE) != 0)
152 pthread_attr_destroy(&attr);
153 free(info);
154 return AL_FALSE;
157 info->func = func;
158 info->ptr = ptr;
159 if(pthread_create(&info->hdl, &attr, StarterFunc, info) != 0)
161 pthread_attr_destroy(&attr);
162 free(info);
163 return AL_FALSE;
165 pthread_attr_destroy(&attr);
167 *thread = info;
168 return AL_TRUE;
171 ALuint StopThread(althread_t thread)
173 ALuint ret;
175 pthread_join(thread->hdl, NULL);
176 ret = thread->ret;
178 free(thread);
180 return ret;
184 void SetThreadName(const char *name)
186 #if defined(HAVE_PTHREAD_SETNAME_NP)
187 #if defined(__GNUC__)
188 if(pthread_setname_np(pthread_self(), name) != 0)
189 #elif defined(__APPLE__)
190 if(pthread_setname_np(name) != 0)
191 #endif
192 WARN("Failed to set thread name to \"%s\": %s\n", name, strerror(errno));
193 #elif defined(HAVE_PTHREAD_SET_NAME_NP)
194 pthread_set_name_np(pthread_self(), name);
195 #else
196 TRACE("Can't set thread name to \"%s\"\n", name);
197 #endif
200 #endif