Add a special resampler for matching sample rates
[openal-soft.git] / Alc / alcThread.c
blobc2f38031e3aa7c52e92893242e11c5722c332bf4
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 <stdlib.h>
25 #include "alMain.h"
26 #include "alThunk.h"
28 #define THREAD_STACK_SIZE (1*1024*1024) /* 1MB */
30 #ifdef _WIN32
32 typedef struct {
33 ALuint (*func)(ALvoid*);
34 ALvoid *ptr;
35 HANDLE thread;
36 } ThreadInfo;
38 static DWORD CALLBACK StarterFunc(void *ptr)
40 ThreadInfo *inf = (ThreadInfo*)ptr;
41 ALint ret;
43 ret = inf->func(inf->ptr);
44 ExitThread((DWORD)ret);
46 return (DWORD)ret;
49 ALvoid *StartThread(ALuint (*func)(ALvoid*), ALvoid *ptr)
51 DWORD dummy;
52 ThreadInfo *inf = malloc(sizeof(ThreadInfo));
53 if(!inf) return 0;
55 inf->func = func;
56 inf->ptr = ptr;
58 inf->thread = CreateThread(NULL, THREAD_STACK_SIZE, StarterFunc, inf, 0, &dummy);
59 if(!inf->thread)
61 free(inf);
62 return NULL;
65 return inf;
68 ALuint StopThread(ALvoid *thread)
70 ThreadInfo *inf = thread;
71 DWORD ret = 0;
73 WaitForSingleObject(inf->thread, INFINITE);
74 GetExitCodeThread(inf->thread, &ret);
75 CloseHandle(inf->thread);
77 free(inf);
79 return (ALuint)ret;
82 #else
84 #include <pthread.h>
86 typedef struct {
87 ALuint (*func)(ALvoid*);
88 ALvoid *ptr;
89 ALuint ret;
90 pthread_t thread;
91 } ThreadInfo;
93 static void *StarterFunc(void *ptr)
95 ThreadInfo *inf = (ThreadInfo*)ptr;
96 inf->ret = inf->func(inf->ptr);
97 return NULL;
100 ALvoid *StartThread(ALuint (*func)(ALvoid*), ALvoid *ptr)
102 pthread_attr_t attr;
103 ThreadInfo *inf = malloc(sizeof(ThreadInfo));
104 if(!inf) return NULL;
106 if(pthread_attr_init(&attr) != 0)
108 free(inf);
109 return NULL;
111 if(pthread_attr_setstacksize(&attr, THREAD_STACK_SIZE) != 0)
113 pthread_attr_destroy(&attr);
114 free(inf);
115 return NULL;
118 inf->func = func;
119 inf->ptr = ptr;
120 if(pthread_create(&inf->thread, &attr, StarterFunc, inf) != 0)
122 pthread_attr_destroy(&attr);
123 free(inf);
124 return NULL;
126 pthread_attr_destroy(&attr);
128 return inf;
131 ALuint StopThread(ALvoid *thread)
133 ThreadInfo *inf = thread;
134 ALuint ret;
136 pthread_join(inf->thread, NULL);
137 ret = inf->ret;
139 free(inf);
141 return ret;
144 #endif