1 /* 2 SDL - Simple DirectMedia Layer 3 Copyright (C) 1997-2006 Sam Lantinga 4 5 This library is free software; you can redistribute it and/or 6 modify it under the terms of the GNU Lesser General Public 7 License as published by the Free Software Foundation; either 8 version 2.1 of the License, or (at your option) any later version. 9 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 Lesser General Public License for more details. 14 15 You should have received a copy of the GNU Lesser General Public 16 License along with this library; if not, write to the Free Software 17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 18 19 Sam Lantinga 20 slouken (at) libsdl.org 21 */ 22 #include "SDL_config.h" 23 24 /* BeOS thread management routines for SDL */ 25 26 #include "SDL_mutex.h" 27 #include "SDL_thread.h" 28 #include "../SDL_thread_c.h" 29 #include "../SDL_systhread.h" 30 #include "mydebug.h" 31 32 typedef struct { 33 int (*func)(void *); 34 void *data; 35 SDL_Thread *info; 36 struct Task *wait; 37 } thread_args; 38 39 #ifndef MORPHOS 40 41 #if defined(__SASC) && !defined(__PPC__) 42 __saveds __asm Uint32 RunThread(register __a0 char *args ) 43 #elif defined(__PPC__) 44 Uint32 RunThread(char *args) 45 #else 46 Uint32 __saveds RunThread(char *args __asm("a0") ) 47 #endif 48 { 49 #ifdef STORMC4_WOS 50 thread_args *data=(thread_args *)args; 51 #else 52 thread_args *data=(thread_args *)atol(args); 53 #endif 54 55 struct Task *Father; 56 57 D(bug("Received data: %lx\n",data)); 58 Father=data->wait; 59 60 SDL_RunThread(data); 61 62 Signal(Father,SIGBREAKF_CTRL_F); 63 D(bug("Thread with data %lx ended\n",data)); 64 return(0); 65 } 66 67 #else 68 69 #include <emul/emulinterface.h> 70 71 Uint32 RunTheThread(void) 72 { 73 thread_args *data=(thread_args *)atol((char *)REG_A0); 74 struct Task *Father; 75 76 D(bug("Received data: %lx\n",data)); 77 Father=data->wait; 78 79 SDL_RunThread(data); 80 81 Signal(Father,SIGBREAKF_CTRL_F); 82 D(bug("Thread with data %lx ended\n",data)); 83 return(0); 84 } 85 86 struct EmulLibEntry RunThreadStruct= 87 { 88 TRAP_LIB, 89 0, 90 (ULONG)RunTheThread 91 }; 92 93 void *RunThread=&RunThreadStruct; 94 #endif 95 96 97 int SDL_SYS_CreateThread(SDL_Thread *thread, void *args) 98 { 99 /* Create the thread and go! */ 100 char buffer[20]; 101 102 D(bug("Sending %lx to the new thread...\n",args)); 103 104 if(args) 105 SDL_snprintf(buffer, SDL_arraysize(buffer),"%ld",args); 106 107 #ifdef STORMC4_WOS 108 thread->handle=CreateTaskPPCTags(TASKATTR_CODE, RunThread, 109 TASKATTR_NAME, "SDL subtask", 110 TASKATTR_STACKSIZE, 100000, 111 (args ? TASKATTR_R3 : TAG_IGNORE), args, 112 TASKATTR_INHERITR2, TRUE, 113 TAG_DONE); 114 #else 115 thread->handle=(struct Task *)CreateNewProcTags(NP_Output,Output(), 116 NP_Name,(ULONG)"SDL subtask", 117 NP_CloseOutput, FALSE, 118 NP_StackSize,20000, 119 NP_Entry,(ULONG)RunThread, 120 args ? NP_Arguments : TAG_IGNORE,(ULONG)buffer, 121 TAG_DONE); 122 #endif 123 124 if(!thread->handle) 125 { 126 SDL_SetError("Not enough resources to create thread"); 127 return(-1); 128 } 129 130 return(0); 131 } 132 133 void SDL_SYS_SetupThread(void) 134 { 135 } 136 137 Uint32 SDL_ThreadID(void) 138 { 139 return((Uint32)FindTask(NULL)); 140 } 141 142 void SDL_SYS_WaitThread(SDL_Thread *thread) 143 { 144 SetSignal(0L,SIGBREAKF_CTRL_F|SIGBREAKF_CTRL_C); 145 Wait(SIGBREAKF_CTRL_F|SIGBREAKF_CTRL_C); 146 } 147 148 void SDL_SYS_KillThread(SDL_Thread *thread) 149 { 150 Signal((struct Task *)thread->handle,SIGBREAKF_CTRL_C); 151 } 152