Mercurial > audio-send
comparison Alc/alcThread.c @ 0:f9476ff7637e
initial forking of open-al to create multiple listeners
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Tue, 25 Oct 2011 13:02:31 -0700 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:f9476ff7637e |
---|---|
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. | |
8 * | |
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. | |
13 * | |
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 | |
19 */ | |
20 | |
21 #include "config.h" | |
22 | |
23 #include <stdlib.h> | |
24 | |
25 #include "alMain.h" | |
26 #include "alThunk.h" | |
27 | |
28 | |
29 #ifdef _WIN32 | |
30 | |
31 typedef struct { | |
32 ALuint (*func)(ALvoid*); | |
33 ALvoid *ptr; | |
34 HANDLE thread; | |
35 } ThreadInfo; | |
36 | |
37 static DWORD CALLBACK StarterFunc(void *ptr) | |
38 { | |
39 ThreadInfo *inf = (ThreadInfo*)ptr; | |
40 ALint ret; | |
41 | |
42 ret = inf->func(inf->ptr); | |
43 ExitThread((DWORD)ret); | |
44 | |
45 return (DWORD)ret; | |
46 } | |
47 | |
48 ALvoid *StartThread(ALuint (*func)(ALvoid*), ALvoid *ptr) | |
49 { | |
50 DWORD dummy; | |
51 ThreadInfo *inf = malloc(sizeof(ThreadInfo)); | |
52 if(!inf) return 0; | |
53 | |
54 inf->func = func; | |
55 inf->ptr = ptr; | |
56 | |
57 inf->thread = CreateThread(NULL, 0, StarterFunc, inf, 0, &dummy); | |
58 if(!inf->thread) | |
59 { | |
60 free(inf); | |
61 return NULL; | |
62 } | |
63 | |
64 return inf; | |
65 } | |
66 | |
67 ALuint StopThread(ALvoid *thread) | |
68 { | |
69 ThreadInfo *inf = thread; | |
70 DWORD ret = 0; | |
71 | |
72 WaitForSingleObject(inf->thread, INFINITE); | |
73 GetExitCodeThread(inf->thread, &ret); | |
74 CloseHandle(inf->thread); | |
75 | |
76 free(inf); | |
77 | |
78 return (ALuint)ret; | |
79 } | |
80 | |
81 #else | |
82 | |
83 #include <pthread.h> | |
84 | |
85 typedef struct { | |
86 ALuint (*func)(ALvoid*); | |
87 ALvoid *ptr; | |
88 ALuint ret; | |
89 pthread_t thread; | |
90 } ThreadInfo; | |
91 | |
92 static void *StarterFunc(void *ptr) | |
93 { | |
94 ThreadInfo *inf = (ThreadInfo*)ptr; | |
95 inf->ret = inf->func(inf->ptr); | |
96 return NULL; | |
97 } | |
98 | |
99 ALvoid *StartThread(ALuint (*func)(ALvoid*), ALvoid *ptr) | |
100 { | |
101 ThreadInfo *inf = malloc(sizeof(ThreadInfo)); | |
102 if(!inf) return NULL; | |
103 | |
104 inf->func = func; | |
105 inf->ptr = ptr; | |
106 if(pthread_create(&inf->thread, NULL, StarterFunc, inf) != 0) | |
107 { | |
108 free(inf); | |
109 return NULL; | |
110 } | |
111 | |
112 return inf; | |
113 } | |
114 | |
115 ALuint StopThread(ALvoid *thread) | |
116 { | |
117 ThreadInfo *inf = thread; | |
118 ALuint ret; | |
119 | |
120 pthread_join(inf->thread, NULL); | |
121 ret = inf->ret; | |
122 | |
123 free(inf); | |
124 | |
125 return ret; | |
126 } | |
127 | |
128 #endif |