Updated to latest source.
[AROS-Contrib.git] / SDL / SDL_systhread.c
bloba7ef6e202c1772d9573b64df7a81f7f169d4fded
1 /*
2 SDL - Simple DirectMedia Layer
3 Copyright (C) 1997, 1998, 1999, 2000, 2001 Sam Lantinga
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with this library; if not, write to the Free
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 Sam Lantinga
20 slouken@libsdl.org
23 #ifdef SAVE_RCSID
24 static char rcsid =
25 "@(#) $Id$";
26 #endif
28 /* BeOS thread management routines for SDL */
30 #include "SDL_error.h"
31 #include "SDL_mutex.h"
32 #include "SDL_thread.h"
33 #include "SDL_thread_c.h"
34 #include "SDL_systhread.h"
35 #include "mydebug.h"
37 #ifdef __AROS__
38 #include <stdlib.h>
39 #endif
41 typedef struct {
42 int (*func)(void *);
43 void *data;
44 SDL_Thread *info;
45 struct Task *wait;
46 #ifdef SHARED_LIB
47 APTR LibBase;
48 #endif
49 } thread_args;
51 #ifndef MORPHOS
53 #if defined(__SASC) && !defined(__PPC__)
54 __saveds __asm Uint32 RunThread(register __a0 char *args )
55 #elif defined(__PPC__) || defined(__AROS__)
56 Uint32 RunThread(char *args)
57 #elif !defined(SHARED_LIB)
58 Uint32 __saveds RunThread(char *args __asm("a0") )
59 #else
60 Uint32 RunThread(char *args __asm("a0") )
61 #endif
63 struct Task *Father;
65 #ifndef SHARED_LIB
66 #ifdef WARPOS
67 thread_args *data=(thread_args *)args;
68 #else
69 thread_args *data=(thread_args *)atol(args);
70 #endif
72 #else /* SHARED_LIB */
73 thread_args *data;
74 register APTR base __asm("a6");
76 ULONG temp=0;
78 while(*args >= '0' && *args <= '9')
79 temp = (temp * 10) + (*args++ - '0');
81 data=(thread_args *)temp;
84 base=data->LibBase;
85 mygeta4();
86 D(bug("Library base: %lx\n",base));
87 #endif
89 D(bug("Received data: %lx, father:%lx\n", data, data->wait));
90 Father=data->wait;
92 SDL_RunThread(data);
93 D(bug("Thread exited, signaling father (%lx)...\n",Father));
94 Signal(Father,SIGBREAKF_CTRL_F);
95 D(bug("Thread with data %lx ended\n",data));
96 return(0);
99 #else /* Morphos code */
101 #include <emul/emulinterface.h>
103 Uint32 RunTheThread(void)
105 thread_args *data=(thread_args *)atol((char *)REG_A0);
106 struct Task *Father;
108 D(bug("Received data: %lx\n",data));
109 Father=data->wait;
111 SDL_RunThread(data);
113 Signal(Father,SIGBREAKF_CTRL_F);
114 D(bug("Thread with data %lx ended\n",data));
115 return(0);
118 struct EmulLibEntry RunThreadStruct=
120 TRAP_LIB,
122 (ULONG)RunTheThread
125 void *RunThread=&RunThreadStruct;
126 #endif
129 int SDL_SYS_CreateThread(SDL_Thread *thread, void *args)
131 /* Create the thread and go! */
132 char buffer[20];
134 #ifdef WARPOS
135 struct TagItem tags[6];
136 #endif
138 #ifdef SHARED_LIB
139 extern void *myLibPtr;
141 ((thread_args *)args)->LibBase= myLibPtr;
142 D(bug("CreateThread: librarybase %lx\n",myLibPtr));
143 #endif
145 D(bug("Sending %lx to the new thread...\n",args));
147 sprintf(buffer,"%ld",args);
150 #ifdef WARPOS
151 tags[0].ti_Tag = TASKATTR_CODE; tags[0].ti_Data = (ULONG)RunThread;
152 tags[1].ti_Tag = TASKATTR_NAME; tags[1].ti_Data = (ULONG)"SDL subtask";
153 tags[2].ti_Tag = TASKATTR_STACKSIZE; tags[2].ti_Data = 100000;
154 tags[3].ti_Tag = (args ? TASKATTR_R3 : TAG_IGNORE); tags[3].ti_Data = (ULONG)args;
155 tags[4].ti_Tag = TASKATTR_INHERITR2; tags[4].ti_Data = TRUE;
156 tags[5].ti_Tag = TAG_DONE; tags[5].ti_Data = 0;
158 thread->handle=CreateTaskPPC(tags);
159 #else
160 thread->handle=(struct Task *)CreateNewProcTags(
161 NP_Output, Output(),
162 NP_Input, Input(),
163 NP_Name, (ULONG)"SDL subtask",
164 NP_CloseOutput, FALSE,
165 NP_CloseInput, FALSE,
166 NP_StackSize, 20000,
167 NP_Entry, (ULONG)RunThread,
168 NP_Arguments, (ULONG)buffer,
169 TAG_DONE);
170 #endif
172 if(!thread->handle)
174 SDL_SetError("Not enough resources to create thread");
175 return(-1);
178 return(0);
181 void SDL_SYS_SetupThread(void)
185 Uint32 SDL_ThreadID(void)
187 return((Uint32)FindTask(NULL));
190 void SDL_SYS_WaitThread(SDL_Thread *thread)
192 SetSignal(0L,SIGBREAKF_CTRL_F|SIGBREAKF_CTRL_C);
193 Wait(SIGBREAKF_CTRL_F|SIGBREAKF_CTRL_C);
196 void SDL_SYS_KillThread(SDL_Thread *thread)
198 Signal((struct Task *)thread->handle,SIGBREAKF_CTRL_C);