annotate Alc/alcThread.c @ 29:cdf320cb5949

updated test suite
author Robert McIntyre <rlm@mit.edu>
date Sat, 10 Dec 2011 21:42:50 -0600
parents f9476ff7637e
children
rev   line source
rlm@0 1 /**
rlm@0 2 * OpenAL cross platform audio library
rlm@0 3 * Copyright (C) 1999-2007 by authors.
rlm@0 4 * This library is free software; you can redistribute it and/or
rlm@0 5 * modify it under the terms of the GNU Library General Public
rlm@0 6 * License as published by the Free Software Foundation; either
rlm@0 7 * version 2 of the License, or (at your option) any later version.
rlm@0 8 *
rlm@0 9 * This library is distributed in the hope that it will be useful,
rlm@0 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
rlm@0 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
rlm@0 12 * Library General Public License for more details.
rlm@0 13 *
rlm@0 14 * You should have received a copy of the GNU Library General Public
rlm@0 15 * License along with this library; if not, write to the
rlm@0 16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
rlm@0 17 * Boston, MA 02111-1307, USA.
rlm@0 18 * Or go to http://www.gnu.org/copyleft/lgpl.html
rlm@0 19 */
rlm@0 20
rlm@0 21 #include "config.h"
rlm@0 22
rlm@0 23 #include <stdlib.h>
rlm@0 24
rlm@0 25 #include "alMain.h"
rlm@0 26 #include "alThunk.h"
rlm@0 27
rlm@0 28
rlm@0 29 #ifdef _WIN32
rlm@0 30
rlm@0 31 typedef struct {
rlm@0 32 ALuint (*func)(ALvoid*);
rlm@0 33 ALvoid *ptr;
rlm@0 34 HANDLE thread;
rlm@0 35 } ThreadInfo;
rlm@0 36
rlm@0 37 static DWORD CALLBACK StarterFunc(void *ptr)
rlm@0 38 {
rlm@0 39 ThreadInfo *inf = (ThreadInfo*)ptr;
rlm@0 40 ALint ret;
rlm@0 41
rlm@0 42 ret = inf->func(inf->ptr);
rlm@0 43 ExitThread((DWORD)ret);
rlm@0 44
rlm@0 45 return (DWORD)ret;
rlm@0 46 }
rlm@0 47
rlm@0 48 ALvoid *StartThread(ALuint (*func)(ALvoid*), ALvoid *ptr)
rlm@0 49 {
rlm@0 50 DWORD dummy;
rlm@0 51 ThreadInfo *inf = malloc(sizeof(ThreadInfo));
rlm@0 52 if(!inf) return 0;
rlm@0 53
rlm@0 54 inf->func = func;
rlm@0 55 inf->ptr = ptr;
rlm@0 56
rlm@0 57 inf->thread = CreateThread(NULL, 0, StarterFunc, inf, 0, &dummy);
rlm@0 58 if(!inf->thread)
rlm@0 59 {
rlm@0 60 free(inf);
rlm@0 61 return NULL;
rlm@0 62 }
rlm@0 63
rlm@0 64 return inf;
rlm@0 65 }
rlm@0 66
rlm@0 67 ALuint StopThread(ALvoid *thread)
rlm@0 68 {
rlm@0 69 ThreadInfo *inf = thread;
rlm@0 70 DWORD ret = 0;
rlm@0 71
rlm@0 72 WaitForSingleObject(inf->thread, INFINITE);
rlm@0 73 GetExitCodeThread(inf->thread, &ret);
rlm@0 74 CloseHandle(inf->thread);
rlm@0 75
rlm@0 76 free(inf);
rlm@0 77
rlm@0 78 return (ALuint)ret;
rlm@0 79 }
rlm@0 80
rlm@0 81 #else
rlm@0 82
rlm@0 83 #include <pthread.h>
rlm@0 84
rlm@0 85 typedef struct {
rlm@0 86 ALuint (*func)(ALvoid*);
rlm@0 87 ALvoid *ptr;
rlm@0 88 ALuint ret;
rlm@0 89 pthread_t thread;
rlm@0 90 } ThreadInfo;
rlm@0 91
rlm@0 92 static void *StarterFunc(void *ptr)
rlm@0 93 {
rlm@0 94 ThreadInfo *inf = (ThreadInfo*)ptr;
rlm@0 95 inf->ret = inf->func(inf->ptr);
rlm@0 96 return NULL;
rlm@0 97 }
rlm@0 98
rlm@0 99 ALvoid *StartThread(ALuint (*func)(ALvoid*), ALvoid *ptr)
rlm@0 100 {
rlm@0 101 ThreadInfo *inf = malloc(sizeof(ThreadInfo));
rlm@0 102 if(!inf) return NULL;
rlm@0 103
rlm@0 104 inf->func = func;
rlm@0 105 inf->ptr = ptr;
rlm@0 106 if(pthread_create(&inf->thread, NULL, StarterFunc, inf) != 0)
rlm@0 107 {
rlm@0 108 free(inf);
rlm@0 109 return NULL;
rlm@0 110 }
rlm@0 111
rlm@0 112 return inf;
rlm@0 113 }
rlm@0 114
rlm@0 115 ALuint StopThread(ALvoid *thread)
rlm@0 116 {
rlm@0 117 ThreadInfo *inf = thread;
rlm@0 118 ALuint ret;
rlm@0 119
rlm@0 120 pthread_join(inf->thread, NULL);
rlm@0 121 ret = inf->ret;
rlm@0 122
rlm@0 123 free(inf);
rlm@0 124
rlm@0 125 return ret;
rlm@0 126 }
rlm@0 127
rlm@0 128 #endif