Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

Thread.h

Go to the documentation of this file.
00001 /*
00002  * Thread.h
00003  *
00004  * Smalltalk like class library for C++
00005  * Thread wrapper. Header.
00006  *
00007  * Copyright (c) 2003-2006 Milan Cermak
00008  */
00009 /*
00010  * This library is free software; you can redistribute it and/or
00011  * modify it under the terms of the GNU Lesser General Public
00012  * License as published by the Free Software Foundation; either
00013  * version 2.1 of the License, or (at your option) any later version.
00014  *
00015  * This library is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018  * Lesser General Public License for more details.
00019  *
00020  * You should have received a copy of the GNU Lesser General Public
00021  * License along with this library; if not, write to the Free Software
00022  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00023  */
00024 #ifndef __THREAD_H
00025 #define __THREAD_H
00026 
00027 #if !defined(_REENTRANT)
00028 #  define _REENTRANT
00029 #endif
00030 #include <pthread.h>
00031 
00032 #include <stlib/Object.h>
00033 #include <stlib/Array.h>
00034 #include <stlib/Set.h>
00035 #include <stlib/String.h>
00036 
00037 #include <stlib/Callback0.h>
00038 #include <stlib/Callback1.h>
00039 
00043 namespace OS {
00044 
00048 class Thread : public Core::Object
00049 {
00050   protected:
00051     Callback<void> *_receiver;   // what to do in the thread
00052     Array *_arguments;
00053 
00054     pthread_t hThread;           // odkaz na vlakno
00055     pthread_cond_t runCond;      // podminka behu vlakna
00056     pthread_mutex_t mutVar;
00057     bool runningThread;          // Priznak behu vlakna
00058 
00059     static Set threads;
00060 
00061   public:
00062     Thread(Callback<void> *receiver);
00063     virtual ~Thread(void);
00064 
00065   private:
00070     static void *threadProc(void *);
00071 
00072   public:
00073     /* Class-accessing protocol */
00074     virtual String *className(void) const;
00075 
00076     /* Instance creation protocol */
00078     static Thread *fork(Callback<void> *receiver);
00083     static Thread *fork(Callback<void> *receiver, Array *arguments);
00084 
00099     template <class TReceiver>
00100     static Thread *fork(TReceiver *receiver, void (TReceiver::*execFunc)(void))
00101     {
00102         Callback0<TReceiver,void> *callback;
00103         callback = new Callback0<TReceiver,void>(receiver, execFunc);
00104         return fork(callback);
00105     }
00106 
00122     template <class TReceiver>
00123     static Thread *fork(TReceiver *receiver, void (TReceiver::*execFunc)(Array *),
00124                         Array *args)
00125     {
00126         Callback1<TReceiver,void,Array*> *callback;
00127         callback = new Callback1<TReceiver,void,Array*>(receiver, execFunc);
00128         return fork(callback, args);
00129     }
00130 
00131     /* Initialize-release protocol */
00132     static void finalizeThreads(void);
00133 
00134     /* Accessing protocol */
00135     virtual void arguments(Array *);
00136 
00137     virtual void resume(void);
00138     virtual void run(void);
00139     virtual void finalize(void);
00140     virtual void suspend(void);
00141     virtual void terminate(void);
00142     virtual void yield(void);
00143 
00144     /* Testing protocol */
00145     virtual bool isRunning(void);
00146 };
00147 
00148 };
00149 
00150 #endif /* __THREAD_H */

Generated on Mon Nov 27 09:47:55 2006 for Smalltalk like C++ Class Library by  doxygen 1.4.2