Home | History | Annotate | Download | only in epoc
      1 /*
      2     SDL - Simple DirectMedia Layer
      3     Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002  Sam Lantinga
      4 
      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.
      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     Library General Public License for more details.
     14 
     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
     18 
     19     Sam Lantinga
     20     slouken (at) libsdl.org
     21 */
     22 #include "SDL_config.h"
     23 
     24 /*
     25     SDL_systhread.cpp
     26     Epoc thread management routines for SDL
     27 
     28     Epoc version by Markus Mertama  (w (at) iki.fi)
     29 */
     30 
     31 
     32 extern "C" {
     33 #undef NULL
     34 #include "SDL_error.h"
     35 #include "SDL_thread.h"
     36 #include "../SDL_systhread.h"
     37     };
     38 
     39 #include <e32std.h>
     40 
     41 
     42 static int object_count;
     43 
     44 int RunThread(TAny* data)
     45 {
     46 	SDL_RunThread(data);
     47 	return(0);
     48 }
     49 
     50 
     51 TInt NewThread(const TDesC& aName, TAny* aPtr1, TAny* aPtr2)
     52     {
     53     return ((RThread*)(aPtr1))->Create(aName,
     54             RunThread,
     55             KDefaultStackSize,
     56             NULL,
     57             aPtr2);
     58     }
     59 
     60 int CreateUnique(TInt (*aFunc)(const TDesC& aName, TAny*, TAny*), TAny* aPtr1, TAny* aPtr2)
     61     {
     62     TBuf<16> name;
     63     TInt status = KErrNone;
     64     do
     65         {
     66         object_count++;
     67         name.Format(_L("SDL_%x"), object_count);
     68         status = aFunc(name, aPtr1, aPtr2);
     69         }
     70         while(status == KErrAlreadyExists);
     71     return status;
     72     }
     73 
     74 
     75 int SDL_SYS_CreateThread(SDL_Thread *thread, void *args)
     76 {
     77     RThread rthread;
     78 
     79     TInt status = CreateUnique(NewThread, &rthread, args);
     80     if (status != KErrNone)
     81     {
     82         delete(((RThread*)(thread->handle)));
     83         thread->handle = NULL;
     84 		SDL_SetError("Not enough resources to create thread");
     85 		return(-1);
     86 	}
     87 	rthread.Resume();
     88     thread->handle = rthread.Handle();
     89 	return(0);
     90 }
     91 
     92 void SDL_SYS_SetupThread(void)
     93 {
     94 	return;
     95 }
     96 
     97 Uint32 SDL_ThreadID(void)
     98 {
     99     RThread current;
    100     TThreadId id = current.Id();
    101 	return id;
    102 }
    103 
    104 void SDL_SYS_WaitThread(SDL_Thread *thread)
    105 {
    106     RUndertaker taker;
    107     taker.Create();
    108     TRequestStatus status;
    109     taker.Logon(status, thread->handle);
    110     User::WaitForRequest(status);
    111     taker.Close();
    112 }
    113 
    114 /* WARNING: This function is really a last resort.
    115  * Threads should be signaled and then exit by themselves.
    116  * TerminateThread() doesn't perform stack and DLL cleanup.
    117  */
    118 void SDL_SYS_KillThread(SDL_Thread *thread)
    119 {
    120     RThread rthread;
    121     rthread.SetHandle(thread->handle);
    122 	rthread.Kill(0);
    123 	rthread.Close();
    124 }
    125