diff 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
line wrap: on
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/Alc/alcThread.c	Tue Oct 25 13:02:31 2011 -0700
     1.3 @@ -0,0 +1,128 @@
     1.4 +/**
     1.5 + * OpenAL cross platform audio library
     1.6 + * Copyright (C) 1999-2007 by authors.
     1.7 + * This library is free software; you can redistribute it and/or
     1.8 + *  modify it under the terms of the GNU Library General Public
     1.9 + *  License as published by the Free Software Foundation; either
    1.10 + *  version 2 of the License, or (at your option) any later version.
    1.11 + *
    1.12 + * This library is distributed in the hope that it will be useful,
    1.13 + *  but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.14 + *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    1.15 + *  Library General Public License for more details.
    1.16 + *
    1.17 + * You should have received a copy of the GNU Library General Public
    1.18 + *  License along with this library; if not, write to the
    1.19 + *  Free Software Foundation, Inc., 59 Temple Place - Suite 330,
    1.20 + *  Boston, MA  02111-1307, USA.
    1.21 + * Or go to http://www.gnu.org/copyleft/lgpl.html
    1.22 + */
    1.23 +
    1.24 +#include "config.h"
    1.25 +
    1.26 +#include <stdlib.h>
    1.27 +
    1.28 +#include "alMain.h"
    1.29 +#include "alThunk.h"
    1.30 +
    1.31 +
    1.32 +#ifdef _WIN32
    1.33 +
    1.34 +typedef struct {
    1.35 +    ALuint (*func)(ALvoid*);
    1.36 +    ALvoid *ptr;
    1.37 +    HANDLE thread;
    1.38 +} ThreadInfo;
    1.39 +
    1.40 +static DWORD CALLBACK StarterFunc(void *ptr)
    1.41 +{
    1.42 +    ThreadInfo *inf = (ThreadInfo*)ptr;
    1.43 +    ALint ret;
    1.44 +
    1.45 +    ret = inf->func(inf->ptr);
    1.46 +    ExitThread((DWORD)ret);
    1.47 +
    1.48 +    return (DWORD)ret;
    1.49 +}
    1.50 +
    1.51 +ALvoid *StartThread(ALuint (*func)(ALvoid*), ALvoid *ptr)
    1.52 +{
    1.53 +    DWORD dummy;
    1.54 +    ThreadInfo *inf = malloc(sizeof(ThreadInfo));
    1.55 +    if(!inf) return 0;
    1.56 +
    1.57 +    inf->func = func;
    1.58 +    inf->ptr = ptr;
    1.59 +
    1.60 +    inf->thread = CreateThread(NULL, 0, StarterFunc, inf, 0, &dummy);
    1.61 +    if(!inf->thread)
    1.62 +    {
    1.63 +        free(inf);
    1.64 +        return NULL;
    1.65 +    }
    1.66 +
    1.67 +    return inf;
    1.68 +}
    1.69 +
    1.70 +ALuint StopThread(ALvoid *thread)
    1.71 +{
    1.72 +    ThreadInfo *inf = thread;
    1.73 +    DWORD ret = 0;
    1.74 +
    1.75 +    WaitForSingleObject(inf->thread, INFINITE);
    1.76 +    GetExitCodeThread(inf->thread, &ret);
    1.77 +    CloseHandle(inf->thread);
    1.78 +
    1.79 +    free(inf);
    1.80 +
    1.81 +    return (ALuint)ret;
    1.82 +}
    1.83 +
    1.84 +#else
    1.85 +
    1.86 +#include <pthread.h>
    1.87 +
    1.88 +typedef struct {
    1.89 +    ALuint (*func)(ALvoid*);
    1.90 +    ALvoid *ptr;
    1.91 +    ALuint ret;
    1.92 +    pthread_t thread;
    1.93 +} ThreadInfo;
    1.94 +
    1.95 +static void *StarterFunc(void *ptr)
    1.96 +{
    1.97 +    ThreadInfo *inf = (ThreadInfo*)ptr;
    1.98 +    inf->ret = inf->func(inf->ptr);
    1.99 +    return NULL;
   1.100 +}
   1.101 +
   1.102 +ALvoid *StartThread(ALuint (*func)(ALvoid*), ALvoid *ptr)
   1.103 +{
   1.104 +    ThreadInfo *inf = malloc(sizeof(ThreadInfo));
   1.105 +    if(!inf) return NULL;
   1.106 +
   1.107 +    inf->func = func;
   1.108 +    inf->ptr = ptr;
   1.109 +    if(pthread_create(&inf->thread, NULL, StarterFunc, inf) != 0)
   1.110 +    {
   1.111 +        free(inf);
   1.112 +        return NULL;
   1.113 +    }
   1.114 +
   1.115 +    return inf;
   1.116 +}
   1.117 +
   1.118 +ALuint StopThread(ALvoid *thread)
   1.119 +{
   1.120 +    ThreadInfo *inf = thread;
   1.121 +    ALuint ret;
   1.122 +
   1.123 +    pthread_join(inf->thread, NULL);
   1.124 +    ret = inf->ret;
   1.125 +
   1.126 +    free(inf);
   1.127 +
   1.128 +    return ret;
   1.129 +}
   1.130 +
   1.131 +#endif