Store thread return value in the struct to avoid void*-to-uint casting
[openal-soft/openal-hmr.git] / Alc / alcThread.c
blob2d49a468a7c8c77b69eba2836c6256700e9fd5be
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"
29 #ifdef _WIN32
31 typedef struct {
32 ALuint (*func)(ALvoid*);
33 ALvoid *ptr;
34 HANDLE thread;
35 } ThreadInfo;
37 static DWORD CALLBACK StarterFunc(void *ptr)
39 ThreadInfo *inf = (ThreadInfo*)ptr;
40 ALint ret;
42 ret = inf->func(inf->ptr);
43 ExitThread((DWORD)ret);
45 return (DWORD)ret;
48 ALvoid *StartThread(ALuint (*func)(ALvoid*), ALvoid *ptr)
50 ThreadInfo *inf = malloc(sizeof(ThreadInfo));
51 if(!inf) return 0;
53 inf->func = func;
54 inf->ptr = ptr;
56 inf->thread = CreateThread(NULL, 0, StarterFunc, inf, 0, NULL);
57 if(!inf->thread)
59 free(inf);
60 return NULL;
63 return inf;
66 ALuint StopThread(ALvoid *thread)
68 ThreadInfo *inf = thread;
69 DWORD ret = 0;
71 WaitForSingleObject(inf->thread, INFINITE);
72 GetExitCodeThread(inf->thread, &ret);
74 free(inf);
76 return (ALuint)ret;
79 #else
81 #include <pthread.h>
83 typedef struct {
84 ALuint (*func)(ALvoid*);
85 ALvoid *ptr;
86 ALuint ret;
87 pthread_t thread;
88 } ThreadInfo;
90 static void *StarterFunc(void *ptr)
92 ThreadInfo *inf = (ThreadInfo*)ptr;
93 inf->ret = inf->func(inf->ptr);
94 return NULL;
97 ALvoid *StartThread(ALuint (*func)(ALvoid*), ALvoid *ptr)
99 ThreadInfo *inf = malloc(sizeof(ThreadInfo));
100 if(!inf) return NULL;
102 inf->func = func;
103 inf->ptr = ptr;
104 if(pthread_create(&inf->thread, NULL, StarterFunc, inf) != 0)
106 free(inf);
107 return NULL;
110 return inf;
113 ALuint StopThread(ALvoid *thread)
115 ThreadInfo *inf = thread;
116 ALuint ret;
118 pthread_join(inf->thread, NULL);
119 ret = inf->ret;
121 free(inf);
123 return ret;
126 #endif