rlm@0: /** rlm@0: * OpenAL cross platform audio library rlm@0: * Copyright (C) 1999-2007 by authors. rlm@0: * This library is free software; you can redistribute it and/or rlm@0: * modify it under the terms of the GNU Library General Public rlm@0: * License as published by the Free Software Foundation; either rlm@0: * version 2 of the License, or (at your option) any later version. rlm@0: * rlm@0: * This library is distributed in the hope that it will be useful, rlm@0: * but WITHOUT ANY WARRANTY; without even the implied warranty of rlm@0: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU rlm@0: * Library General Public License for more details. rlm@0: * rlm@0: * You should have received a copy of the GNU Library General Public rlm@0: * License along with this library; if not, write to the rlm@0: * Free Software Foundation, Inc., 59 Temple Place - Suite 330, rlm@0: * Boston, MA 02111-1307, USA. rlm@0: * Or go to http://www.gnu.org/copyleft/lgpl.html rlm@0: */ rlm@0: rlm@0: #include "config.h" rlm@0: rlm@0: #include rlm@0: rlm@0: #include "alMain.h" rlm@0: #include "alThunk.h" rlm@0: rlm@0: rlm@0: #ifdef _WIN32 rlm@0: rlm@0: typedef struct { rlm@0: ALuint (*func)(ALvoid*); rlm@0: ALvoid *ptr; rlm@0: HANDLE thread; rlm@0: } ThreadInfo; rlm@0: rlm@0: static DWORD CALLBACK StarterFunc(void *ptr) rlm@0: { rlm@0: ThreadInfo *inf = (ThreadInfo*)ptr; rlm@0: ALint ret; rlm@0: rlm@0: ret = inf->func(inf->ptr); rlm@0: ExitThread((DWORD)ret); rlm@0: rlm@0: return (DWORD)ret; rlm@0: } rlm@0: rlm@0: ALvoid *StartThread(ALuint (*func)(ALvoid*), ALvoid *ptr) rlm@0: { rlm@0: DWORD dummy; rlm@0: ThreadInfo *inf = malloc(sizeof(ThreadInfo)); rlm@0: if(!inf) return 0; rlm@0: rlm@0: inf->func = func; rlm@0: inf->ptr = ptr; rlm@0: rlm@0: inf->thread = CreateThread(NULL, 0, StarterFunc, inf, 0, &dummy); rlm@0: if(!inf->thread) rlm@0: { rlm@0: free(inf); rlm@0: return NULL; rlm@0: } rlm@0: rlm@0: return inf; rlm@0: } rlm@0: rlm@0: ALuint StopThread(ALvoid *thread) rlm@0: { rlm@0: ThreadInfo *inf = thread; rlm@0: DWORD ret = 0; rlm@0: rlm@0: WaitForSingleObject(inf->thread, INFINITE); rlm@0: GetExitCodeThread(inf->thread, &ret); rlm@0: CloseHandle(inf->thread); rlm@0: rlm@0: free(inf); rlm@0: rlm@0: return (ALuint)ret; rlm@0: } rlm@0: rlm@0: #else rlm@0: rlm@0: #include rlm@0: rlm@0: typedef struct { rlm@0: ALuint (*func)(ALvoid*); rlm@0: ALvoid *ptr; rlm@0: ALuint ret; rlm@0: pthread_t thread; rlm@0: } ThreadInfo; rlm@0: rlm@0: static void *StarterFunc(void *ptr) rlm@0: { rlm@0: ThreadInfo *inf = (ThreadInfo*)ptr; rlm@0: inf->ret = inf->func(inf->ptr); rlm@0: return NULL; rlm@0: } rlm@0: rlm@0: ALvoid *StartThread(ALuint (*func)(ALvoid*), ALvoid *ptr) rlm@0: { rlm@0: ThreadInfo *inf = malloc(sizeof(ThreadInfo)); rlm@0: if(!inf) return NULL; rlm@0: rlm@0: inf->func = func; rlm@0: inf->ptr = ptr; rlm@0: if(pthread_create(&inf->thread, NULL, StarterFunc, inf) != 0) rlm@0: { rlm@0: free(inf); rlm@0: return NULL; rlm@0: } rlm@0: rlm@0: return inf; rlm@0: } rlm@0: rlm@0: ALuint StopThread(ALvoid *thread) rlm@0: { rlm@0: ThreadInfo *inf = thread; rlm@0: ALuint ret; rlm@0: rlm@0: pthread_join(inf->thread, NULL); rlm@0: ret = inf->ret; rlm@0: rlm@0: free(inf); rlm@0: rlm@0: return ret; rlm@0: } rlm@0: rlm@0: #endif