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

OS::Thread Class Reference

Thread is kind of new process. More...

#include <Thread.h>

Inheritance diagram for OS::Thread:

Inheritance graph
[legend]
Collaboration diagram for OS::Thread:

Collaboration graph
[legend]
List of all members.

Public Member Functions

 Thread (Callback< void > *receiver)
virtual ~Thread (void)
virtual StringclassName (void) const
 Answer receiver class name.
virtual void arguments (Array *)
virtual void resume (void)
virtual void run (void)
virtual void finalize (void)
virtual void suspend (void)
virtual void terminate (void)
virtual void yield (void)
virtual bool isRunning (void)

Static Public Member Functions

static Threadfork (Callback< void > *receiver)
 Start new thread and execute a Callback in it.
static Threadfork (Callback< void > *receiver, Array *arguments)
 Start new thread and execute a Callback in it.
template<class TReceiver>
static Threadfork (TReceiver *receiver, void(TReceiver::*execFunc)(void))
 Start new thread and execute an execFunc in it.
template<class TReceiver>
static Threadfork (TReceiver *receiver, void(TReceiver::*execFunc)(Array *), Array *args)
 Start new thread and execute an execFunc in it.
static void finalizeThreads (void)

Protected Attributes

Callback< void > * _receiver
Array_arguments
pthread_t hThread
pthread_cond_t runCond
pthread_mutex_t mutVar
bool runningThread

Static Protected Attributes

static Set threads

Static Private Member Functions

static void * threadProc (void *)
 This is the main thread function.

Detailed Description

Thread is kind of new process.

Sometimes it's called light-weight process. Unlike Process the Threads are sharing their data.

Definition at line 48 of file Thread.h.


Constructor & Destructor Documentation

Thread::Thread Callback< void > *  receiver  ) 
 

Definition at line 38 of file Thread.cc.

References _arguments, _receiver, finalizeThreads(), mutVar, nil, runCond, and runningThread.

Referenced by fork().

00039 {
00040     finalizeThreads();
00041     // Nainicializujeme podminku behu vlakna a mutex
00042     pthread_mutex_init(&mutVar, NULL);
00043     pthread_cond_init(&runCond, NULL);
00044     // Nastavime vykonnou funkci
00045     _receiver = receiver;
00046     _arguments = nil;
00047     // Nastavime priznak beziciho vlakna
00048     runningThread = false;
00049 }

Thread::~Thread void   )  [virtual]
 

Definition at line 51 of file Thread.cc.

References _receiver, mutVar, nil, runCond, and terminate().

00052 {
00053     terminate();
00054     // Zrusime podminku a mutex
00055     pthread_cond_destroy(&runCond);
00056     pthread_mutex_destroy(&mutVar);
00057     _receiver = nil;
00058 }


Member Function Documentation

void Thread::arguments Array  )  [virtual]
 

Definition at line 93 of file Thread.cc.

References _arguments.

Referenced by fork().

00094 {
00095     _arguments = args;
00096 }

String * Thread::className void   )  const [virtual]
 

Answer receiver class name.

Because there isn't any standard way to obtain class name this method comes to place.

Every class should rewrite this method but many didn't (yet).

Reimplemented from Core::Object.

Definition at line 61 of file Thread.cc.

00062 {
00063     return new String("Thread");
00064 }

void Thread::finalize void   )  [virtual]
 

Definition at line 127 of file Thread.cc.

References hThread, Core::Set::remove(), runningThread, and threads.

Referenced by finalizeThreads(), and terminate().

00128 {
00129     pthread_join(hThread, NULL);
00130     runningThread = false;
00131     try {
00132         threads.remove(this);
00133     }
00134     catch (NotFoundError *ex) {
00135         /* It doesn't matter if it was there or not. It isn't now for sure. */
00136     }
00137 }

void Thread::finalizeThreads void   )  [static]
 

Definition at line 82 of file Thread.cc.

References Core::Set::copy(), finalize(), isRunning(), Core::Collection::iterator(), and threads.

Referenced by Thread().

00083 {
00084     Set *temp = (Set *) threads.copy();
00085     for (Iterator* i = temp->iterator(); !i->finished(); i->next()) {
00086         Thread *th = (Thread *) i->value();
00087         if (!th->isRunning())
00088             th->finalize();
00089     }
00090     delete temp;
00091 }

template<class TReceiver>
static Thread* OS::Thread::fork TReceiver *  receiver,
void(TReceiver::*)(Array *)  execFunc,
Array args
[inline, static]
 

Start new thread and execute an execFunc in it.

It can pass arguments Array to the callback. Although it looks ugly it's easy to use. Just pass an object, its class method address and arguments Array to it. Like this:

  class MyObject : public Object
  {
      public:
          void myMethod(Array*);
  };

  Thread::fork(new MyObject, &MyObject::myMethod, new Array);
  
Sure that's all.

Definition at line 123 of file Thread.h.

References fork().

00125     {
00126         Callback1<TReceiver,void,Array*> *callback;
00127         callback = new Callback1<TReceiver,void,Array*>(receiver, execFunc);
00128         return fork(callback, args);
00129     }

template<class TReceiver>
static Thread* OS::Thread::fork TReceiver *  receiver,
void(TReceiver::*)(void)  execFunc
[inline, static]
 

Start new thread and execute an execFunc in it.

Although it looks ugly it's easy to use. Just pass an object and its class method address to it. Like this:

  class MyObject : public Object
  {
      public:
          void myMethod(void);
  };

  Thread::fork(new MyObject, &MyObject::myMethod);
  
Sure that's all.

Definition at line 100 of file Thread.h.

References fork().

00101     {
00102         Callback0<TReceiver,void> *callback;
00103         callback = new Callback0<TReceiver,void>(receiver, execFunc);
00104         return fork(callback);
00105     }

Thread * Thread::fork Callback< void > *  receiver,
Array arguments
[static]
 

Start new thread and execute a Callback in it.

It can pass arguments Array to the callback. The callbacked method must accept one Array* argument.

Definition at line 73 of file Thread.cc.

References arguments(), run(), and Thread().

00074 {
00075     Thread *thread = new Thread(receiver);
00076     thread->arguments(args);
00077     thread->run();
00078     return thread;
00079 }

Thread * Thread::fork Callback< void > *  receiver  )  [static]
 

Start new thread and execute a Callback in it.

Definition at line 66 of file Thread.cc.

References run(), and Thread().

Referenced by fork().

00067 {
00068     Thread *thread = new Thread(receiver);
00069     thread->run();
00070     return thread;
00071 }

bool Thread::isRunning void   )  [virtual]
 

Definition at line 155 of file Thread.cc.

References runningThread.

Referenced by finalizeThreads().

00156 {
00157     return runningThread;
00158 }

void Thread::resume void   )  [virtual]
 

Definition at line 99 of file Thread.cc.

References runCond, and runningThread.

00100 {
00101     if (runningThread == false) {  // vlakno dosud nebylo vytvoreno
00102         // zadna akce
00103         return;
00104     }
00105     pthread_cond_signal(&runCond);
00106 }

void Thread::run void   )  [virtual]
 

Definition at line 120 of file Thread.cc.

References Core::Set::add(), hThread, runningThread, threadProc(), and threads.

Referenced by fork(), and OS::Promise::run().

00121 {
00122     runningThread = true;
00123     pthread_create(&hThread, NULL, threadProc, this);
00124     threads.add(this);
00125 }

void Thread::suspend void   )  [virtual]
 

Definition at line 109 of file Thread.cc.

References mutVar, runCond, and runningThread.

00110 {
00111     if (runningThread == false) {  // vlakno dosud nebylo vytvoreno
00112         // zadna akce
00113         return;
00114     }
00115     pthread_cond_wait(&runCond, &mutVar);
00116 }

void Thread::terminate void   )  [virtual]
 

Definition at line 139 of file Thread.cc.

References finalize(), hThread, and runningThread.

Referenced by ~Thread().

00140 {
00141     // zruseni vlakna
00142     if (runningThread != false) {
00143         pthread_cancel(hThread);
00144         // Pockame az vlakno dobehne a odchytime navratovou hodnotu
00145         finalize();
00146     }
00147 }

void * Thread::threadProc void *   )  [static, private]
 

This is the main thread function.

It must be declared static due to C calling convention (It's called from pure C library).

Definition at line 161 of file Thread.cc.

References _arguments, _receiver, Core::Callback< void >::executeWith(), and runningThread.

Referenced by run().

00162 {
00163     // pomoci myThread lze pristupovat i k nestatickym clenum tridy
00164     Thread *myThread = (Thread *) x;
00165 
00166     pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
00167     pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
00168     myThread->runningThread = true;
00169 
00170     myThread->_receiver->executeWith(myThread->_arguments);
00171 
00172     myThread->runningThread = false;
00173 
00174     return NULL;                 // opustenim ThreadProc zanika
00175                                  // pracovni vlakno objektu Thread
00176 }

void Thread::yield void   )  [virtual]
 

Definition at line 149 of file Thread.cc.

00150 {
00151     sleep(1);
00152 }


Member Data Documentation

Array* OS::Thread::_arguments [protected]
 

Definition at line 52 of file Thread.h.

Referenced by arguments(), Thread(), and threadProc().

Callback<void>* OS::Thread::_receiver [protected]
 

Definition at line 51 of file Thread.h.

Referenced by Thread(), threadProc(), and ~Thread().

pthread_t OS::Thread::hThread [protected]
 

Definition at line 54 of file Thread.h.

Referenced by finalize(), run(), and terminate().

pthread_mutex_t OS::Thread::mutVar [protected]
 

Definition at line 56 of file Thread.h.

Referenced by suspend(), Thread(), and ~Thread().

pthread_cond_t OS::Thread::runCond [protected]
 

Definition at line 55 of file Thread.h.

Referenced by resume(), suspend(), Thread(), and ~Thread().

bool OS::Thread::runningThread [protected]
 

Definition at line 57 of file Thread.h.

Referenced by finalize(), isRunning(), resume(), run(), suspend(), terminate(), Thread(), and threadProc().

Set Thread::threads [static, protected]
 

Referenced by finalize(), finalizeThreads(), and run().


The documentation for this class was generated from the following files:
Generated on Mon Nov 27 09:52:24 2006 for Smalltalk like C++ Class Library by  doxygen 1.4.2