comparison ext/openal-soft/Alc/alcThread.c @ 0:4a0efb7baf70

* Datasets becomes the new trunk and retires after that :-)
author mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
date Sun, 29 Jun 2008 18:44:17 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:4a0efb7baf70
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 #include <windows.h>
32
33 typedef struct {
34 ALuint (*func)(ALvoid*);
35 ALvoid *ptr;
36 HANDLE thread;
37 } ThreadInfo;
38
39 static DWORD CALLBACK StarterFunc(void *ptr)
40 {
41 ThreadInfo *inf = (ThreadInfo*)ptr;
42 ALint ret;
43
44 ret = inf->func(inf->ptr);
45 ExitThread((DWORD)ret);
46
47 return (DWORD)ret;
48 }
49
50 ALvoid *StartThread(ALuint (*func)(ALvoid*), ALvoid *ptr)
51 {
52 ThreadInfo *inf = malloc(sizeof(ThreadInfo));
53 if(!inf) return 0;
54
55 inf->func = func;
56 inf->ptr = ptr;
57
58 inf->thread = CreateThread(NULL, 0, StarterFunc, inf, 0, NULL);
59 if(!inf->thread)
60 {
61 free(inf);
62 return NULL;
63 }
64
65 return inf;
66 }
67
68 ALuint StopThread(ALvoid *thread)
69 {
70 ThreadInfo *inf = thread;
71 DWORD ret = 0;
72
73 WaitForSingleObject(inf->thread, INFINITE);
74 GetExitCodeThread(inf->thread, &ret);
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 pthread_t thread;
89 } ThreadInfo;
90
91 static void *StarterFunc(void *ptr)
92 {
93 ThreadInfo *inf = (ThreadInfo*)ptr;
94 ALint ret;
95
96 ret = inf->func(inf->ptr);
97 return (void*)ret;
98 }
99
100 ALvoid *StartThread(ALuint (*func)(ALvoid*), ALvoid *ptr)
101 {
102 ThreadInfo *inf = malloc(sizeof(ThreadInfo));
103 if(!inf) return 0;
104
105 inf->func = func;
106 inf->ptr = ptr;
107 if(pthread_create(&inf->thread, NULL, StarterFunc, inf) != 0)
108 {
109 free(inf);
110 return NULL;
111 }
112
113 return inf;
114 }
115
116 ALuint StopThread(ALvoid *thread)
117 {
118 ThreadInfo *inf = thread;
119 void *ret;
120
121 pthread_join(inf->thread, &ret);
122 free(inf);
123
124 return (ALuint)ret;
125 }
126
127 #endif