19682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*
29682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL - Simple DirectMedia Layer
39682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Copyright (C) 1997-2012 Sam Lantinga
49682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
59682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    This library is free software; you can redistribute it and/or
69682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    modify it under the terms of the GNU Library General Public
79682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    License as published by the Free Software Foundation; either
89682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    version 2 of the License, or (at your option) any later version.
99682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    This library is distributed in the hope that it will be useful,
119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    but WITHOUT ANY WARRANTY; without even the implied warranty of
129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Library General Public License for more details.
149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    You should have received a copy of the GNU Library General Public
169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    License along with this library; if not, write to the Free
179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Sam Lantinga
209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    slouken@devolution.com
219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall*/
229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*
249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_systhread.cpp
259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Epoc thread management routines for SDL
269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Epoc version by Markus Mertama  (w@iki.fi)
289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall*/
299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "epoc_sdl.h"
319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall//#include <stdlib.h>
339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall//#include <stdio.h>
349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallextern "C" {
389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#undef NULL
399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_error.h"
409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_thread.h"
419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_systhread.h"
429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_thread_c.h"
439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <e32std.h>
469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "epoc_sdl.h"
479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int object_count;
509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint RunThread(TAny* data)
529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	CTrapCleanup* cleanup = CTrapCleanup::New();
549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	TRAPD(err, SDL_RunThread(data));
559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	EpocSdlEnv::CleanupItems();
569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	delete cleanup;
579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(err);
589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallTInt NewThread(const TDesC& aName, TAny* aPtr1, TAny* aPtr2)
629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return ((RThread*)(aPtr1))->Create(aName,
649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            RunThread,
659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            KDefaultStackSize,
669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            NULL,
679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            aPtr2);
689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint CreateUnique(TInt (*aFunc)(const TDesC& aName, TAny*, TAny*), TAny* aPtr1, TAny* aPtr2)
719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    TBuf<16> name;
739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    TInt status = KErrNone;
749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    do
759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        {
769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        object_count++;
779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        name.Format(_L("SDL_%x"), object_count);
789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        status = aFunc(name, aPtr1, aPtr2);
799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        while(status == KErrAlreadyExists);
819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return status;
829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint SDL_SYS_CreateThread(SDL_Thread *thread, void *args)
869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    RThread rthread;
889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    const TInt status = CreateUnique(NewThread, &rthread, args);
909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (status != KErrNone)
919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        delete(((RThread*)(thread->handle)));
939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        thread->handle = NULL;
949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SetError("Not enough resources to create thread");
959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(-1);
969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	rthread.Resume();
989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    thread->handle = rthread.Handle();
999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(0);
1009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid SDL_SYS_SetupThread(void)
1039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return;
1059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallUint32 SDL_ThreadID(void)
1089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    RThread current;
1109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    const TThreadId id = current.Id();
1119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return id;
1129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid SDL_SYS_WaitThread(SDL_Thread *thread)
1159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_TRACE1("Close thread", thread);
1179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    RThread t;
1189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    const TInt err = t.Open(thread->threadid);
1199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if(err == KErrNone && t.ExitType() == EExitPending)
1209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        {
1219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        TRequestStatus status;
1229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        t.Logon(status);
1239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        User::WaitForRequest(status);
1249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
1259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    t.Close();
1269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  /*  RUndertaker taker;
1289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    taker.Create();
1299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    TRequestStatus status;
1309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    taker.Logon(status, thread->handle);
1319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    User::WaitForRequest(status);
1329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    taker.Close();*/
1339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_TRACE1("Closed thread", thread);
1349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* WARNING: This function is really a last resort.
1379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * Threads should be signaled and then exit by themselves.
1389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * TerminateThread() doesn't perform stack and DLL cleanup.
1399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall */
1409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid SDL_SYS_KillThread(SDL_Thread *thread)
1419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    RThread rthread;
1439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    rthread.SetHandle(thread->handle);
1449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	rthread.Kill(0);
1459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	rthread.Close();
1469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
147