From: Christian P. <cp...@us...> - 2005-01-28 11:36:48
|
Update of /cvsroot/pclasses/pclasses2/src/System In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv16395/src/System Modified Files: Thread.posix.cpp Makefile.am Added Files: Thread.common.cpp Log Message: Added Thread::spawn() for spawning callback's in own threads. Added inline docs. Removed void* arg from Thread::main() and Thread::start(). Index: Makefile.am =================================================================== RCS file: /cvsroot/pclasses/pclasses2/src/System/Makefile.am,v retrieving revision 1.9 retrieving revision 1.10 diff -u -d -r1.9 -r1.10 --- Makefile.am 17 Jan 2005 22:50:39 -0000 1.9 +++ Makefile.am 28 Jan 2005 11:36:38 -0000 1.10 @@ -83,7 +83,7 @@ CPPFLAGS = -DPSYSTEM_BUILD libpclasses_system_la_SOURCES = timeout.cpp SystemError.cpp \ - CriticalSection.cpp Mutex.cpp $(Thread_Sources) \ + CriticalSection.cpp Mutex.cpp $(Thread_Sources) Thread.common.cpp \ $(Semaphore_Sources) $(SharedMem_Sources) $(SharedLib_Sources) \ SharedLib.common.cpp FileInfo.common.cpp ProcessIO.cpp Process.common.cpp \ $(IO_Sources) $(Time_Sources) PathFinder.cpp --- NEW FILE: Thread.common.cpp --- /*************************************************************************** * Copyright (C) 2005 by Christian Prochnow * * cp...@se... * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU Library General Public License as * * published by the Free Software Foundation; either version 2 of the * * License, or (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU Library General Public * * License along with this program; if not, write to the * * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ #include "pclasses/System/Thread.h" #include "pclasses/System/Semaphore.h" namespace P { namespace System { class SpawnedThread: public Thread { public: SpawnedThread(const Callback0<void>& cb, Semaphore* sem) : Thread(true), _callback(cb.clone()), _sem(sem) { } protected: int main() { if(_sem) _sem->post(); _callback->exec(); if(_sem) _sem->post(); delete this; return 0; } private: Callback0<void>* _callback; Semaphore* _sem; }; void Thread::spawn(const Callback0<void>& cb, Semaphore* sem) throw(SystemError) { SpawnedThread* thr = new SpawnedThread(cb, sem); thr->start(); // we don't leak the object ... it is self-deleted } } // !namespace System } // !namespace P Index: Thread.posix.cpp =================================================================== RCS file: /cvsroot/pclasses/pclasses2/src/System/Thread.posix.cpp,v retrieving revision 1.2 retrieving revision 1.3 diff -u -d -r1.2 -r1.3 --- Thread.posix.cpp 24 Dec 2004 17:07:06 -0000 1.2 +++ Thread.posix.cpp 28 Jan 2005 11:36:38 -0000 1.3 @@ -22,6 +22,7 @@ #include "pclasses/Alloc.h" #include "pclasses/ScopedPtr.h" #include "pclasses/System/Thread.h" +#include "pclasses/System/Semaphore.h" #include "timeout.h" #include <pthread.h> @@ -33,20 +34,22 @@ namespace System { struct Thread::Handle { - Thread* obj; - void* arg; - bool detached; - pthread_t thread; - + Thread* obj; + bool detached; + pthread_t thread; + static void* entry(void* arg) { Handle* h = (Handle*)arg; - void* ret = (void*)h->obj->main(h->arg); - + + // do not access the Thread object after running main() + // it may be alreaedy deleted... (we want self-deleting threads) + void* ret = (void*)h->obj->main(); + // detached Thread's self-delete their handle if(h->detached) delete h; - + return ret; } }; @@ -60,14 +63,14 @@ { } -void Thread::start(void* arg) throw(LogicError, SystemError) +void Thread::start() throw(LogicError, SystemError) { if(_handle) throw LogicError("Thread is already running", P_SOURCEINFO); ScopedPtr<Handle> handle(new Handle); - handle->obj = this; - handle->arg = arg; + + handle->obj = this; handle->detached = _detached; pthread_attr_t attrs; @@ -123,6 +126,7 @@ return _detached; } + } // !namespace System } // !namespace P |