Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/openal-0.0.8/src/al_threadlib.c @ 17

Last change on this file since 17 was 17, checked in by landauf, 17 years ago

added openal

File size: 3.7 KB
Line 
1/* -*- mode: C; tab-width:8; c-basic-offset:8 -*-
2 * vi:set ts=8:
3 *
4 * al_threadlib.c
5 */
6
7#include <stdlib.h>
8#include "al_threadlib.h"
9
10#if defined(USE_POSIXTHREADING)
11
12typedef int ( *ptfunc ) ( void * );
13
14static void *runThread( void *data ) AL_ATTRIBUTE_NORETURN_;
15
16static void *runThread( void *data )
17{
18        ptfunc fn = ( ptfunc ) data;
19        fn( NULL );
20        pthread_exit( NULL );
21#ifndef HAVE___ATTRIBUTE__
22        return NULL;
23#endif
24}
25
26ThreadID _alCreateThread( int ( *fn ) ( void * ) )
27{
28        pthread_attr_t type;
29        pthread_t *thread = malloc( sizeof *thread );
30        if( thread == NULL ) {
31                return NULL;
32        }
33
34        if( pthread_attr_init( &type ) != 0 ) {
35                free( thread );
36                return NULL;
37        }
38
39        pthread_attr_setdetachstate( &type, PTHREAD_CREATE_JOINABLE );
40
41        if( pthread_create( thread, &type, runThread, ( void * ) fn ) != 0 ) {
42                free( thread );
43                return NULL;
44        }
45
46        return thread;
47}
48
49int _alWaitThread( ThreadID thread )
50{
51        int retval = pthread_join( *thread, NULL );
52        free( thread );
53        return retval;
54}
55
56unsigned int _alSelfThread( void )
57{
58        return ( unsigned int ) pthread_self(  );
59}
60
61void _alExitThread( void )
62{
63        pthread_exit( NULL );
64}
65
66#elif defined(USE_WINDOWSTHREADING)
67
68/* for _alMicroSleep */
69#include "al_main.h"
70
71ThreadID _alCreateThread( int ( *fn ) ( void * ) )
72{
73        DWORD dummy;
74        return CreateThread( NULL, 0, ( LPTHREAD_START_ROUTINE ) fn, NULL, 0,
75                             &dummy );
76}
77
78int _alWaitThread( ThreadID thread )
79{
80        int tries = 20;         /* gets tries iterations before we nuke it */
81        const int interval = 40000;
82
83        do {
84                DWORD ExitCode;
85                if( GetExitCodeThread( thread, &ExitCode ) == 0 ) {
86                        break;
87                }
88
89                /* thread is still running, be nice and wait a bit */
90                _alMicroSleep( interval );
91        } while( tries-- );
92
93        return ( TerminateThread( thread, 0 ) == 0 ) ? -1 : 0;
94}
95
96void _alExitThread( void )
97{
98        ExitThread( 0 );
99}
100
101unsigned int _alSelfThread( void )
102{
103        return ( unsigned int ) GetCurrentThreadId(  );
104}
105
106#elif defined(USE_MORPHOSTHREADING)
107
108#include <dos/dostags.h>
109#include <proto/dos.h>
110#include <proto/exec.h>
111
112#include "al_types.h"
113
114struct ThreadStartMsg {
115        struct Message tsm_Msg;
116        int tsm_Result;
117};
118
119ThreadID _alCreateThread( int ( *fn ) ( void * ) )
120{
121        struct ThreadData *thread =
122            AllocVec( sizeof( struct ThreadData ), MEMF_PUBLIC );
123        if( thread == NULL ) {
124                return NULL;
125        }
126
127        thread->td_MsgPort = CreateMsgPort(  );
128        if( !thread->td_MsgPort ) {
129                FreeVec( thread );
130                return NULL;
131        }
132
133        struct ThreadStartMsg *startup_msg =
134            AllocVec( sizeof( struct ThreadStartMsg ),
135                      MEMF_PUBLIC | MEMF_CLEAR );
136        if( startup_msg == NULL ) {
137                DeleteMsgPort( thread->td_MsgPort );
138                FreeVec( thread );
139                return NULL;
140        }
141
142        startup_msg->tsm_Msg.mn_Node.ln_Type = NT_MESSAGE;
143        startup_msg->tsm_Msg.mn_ReplyPort = thread->td_MsgPort;
144        startup_msg->tsm_Msg.mn_Length = sizeof( *startup_msg );
145
146        thread->td_Thread =
147            CreateNewProcTags( NP_Entry, ( ULONG ) fn, NP_Name, ( ULONG )
148                               "OpenAL Thread",
149                               NP_CodeType,
150                               CODETYPE_PPC,
151                               NP_StartupMsg,
152                               ( ULONG ) startup_msg,
153                               NP_PPC_Arg1, 0, TAG_DONE );
154        if( !thread->td_Thread ) {
155                FreeVec( startup_msg );
156                DeleteMsgPort( thread->td_MsgPort );
157                FreeVec( thread );
158                return NULL;
159        }
160
161        return thread;
162}
163
164int _alWaitThread( ThreadID thread )
165{
166        struct ThreadStartMsg *tsm;
167        int retval;
168        WaitPort( thread->td_MsgPort );
169        tsm = ( struct ThreadStartMsg * ) GetMsg( thread->td_MsgPort );
170        DeleteMsgPort( thread->td_MsgPort );
171        retval = tsm->tsm_Result;
172        FreeVec( tsm );
173        FreeVec( thread );
174        return retval;
175}
176
177unsigned int _alSelfThread( void )
178{
179        return ( unsigned int ) FindTask( NULL );
180}
181
182void _alExitThread( void )
183{
184        struct ThreadStartMsg *msg;
185        if( NewGetTaskAttrs( NULL, &msg, sizeof( msg ), TASKINFOTYPE_STARTUPMSG,
186                             TAG_DONE ) && msg )
187                msg->tsm_Result = 0;
188        /*RemTask(NULL); */
189}
190
191#endif
Note: See TracBrowser for help on using the repository browser.