2 SDL - Simple DirectMedia Layer
3 Copyright (C) 1997-2009 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
20 slouken@devolution.com
25 Epoc thread management routines for SDL
27 Epoc version by Markus Mertama (w@iki.fi)
39 #include "SDL_error.h"
40 #include "SDL_thread.h"
41 #include "SDL_systhread.h"
42 #include "SDL_thread_c.h"
49 static int object_count;
51 int RunThread(TAny* data)
53 CTrapCleanup* cleanup = CTrapCleanup::New();
54 TRAPD(err, SDL_RunThread(data));
55 EpocSdlEnv::CleanupItems();
61 TInt NewThread(const TDesC& aName, TAny* aPtr1, TAny* aPtr2)
63 return ((RThread*)(aPtr1))->Create(aName,
70 int CreateUnique(TInt (*aFunc)(const TDesC& aName, TAny*, TAny*), TAny* aPtr1, TAny* aPtr2)
73 TInt status = KErrNone;
77 name.Format(_L("SDL_%x"), object_count);
78 status = aFunc(name, aPtr1, aPtr2);
80 while(status == KErrAlreadyExists);
85 int SDL_SYS_CreateThread(SDL_Thread *thread, void *args)
89 const TInt status = CreateUnique(NewThread, &rthread, args);
90 if (status != KErrNone)
92 delete(((RThread*)(thread->handle)));
93 thread->handle = NULL;
94 SDL_SetError("Not enough resources to create thread");
98 thread->handle = rthread.Handle();
102 void SDL_SYS_SetupThread(void)
107 Uint32 SDL_ThreadID(void)
110 const TThreadId id = current.Id();
114 void SDL_SYS_WaitThread(SDL_Thread *thread)
116 SDL_TRACE1("Close thread", thread);
118 const TInt err = t.Open(thread->threadid);
119 if(err == KErrNone && t.ExitType() == EExitPending)
121 TRequestStatus status;
123 User::WaitForRequest(status);
127 /* RUndertaker taker;
129 TRequestStatus status;
130 taker.Logon(status, thread->handle);
131 User::WaitForRequest(status);
133 SDL_TRACE1("Closed thread", thread);
136 /* WARNING: This function is really a last resort.
137 * Threads should be signaled and then exit by themselves.
138 * TerminateThread() doesn't perform stack and DLL cleanup.
140 void SDL_SYS_KillThread(SDL_Thread *thread)
143 rthread.SetHandle(thread->handle);