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

Thread.cpp

Go to the documentation of this file.
00001 
00002 
00003 #include "Thread.hpp"
00004 #include <iostream>
00005 #include <sys/time.h>
00006 #include <sys/types.h>
00007 #include <unistd.h>
00008 #include "l1394_resource.h"
00009 
00010 namespace L1394 {
00011 ThreadMutex::ThreadMutex(MutexKind mk) {
00012   mutex_kind   = mk;
00013   lock_counter = 0;
00014   current_thread = 0;
00015   pthread_mutex_init(&mutex, 0);
00016 }
00017 
00018 ThreadMutex::~ThreadMutex() {
00019   pthread_mutex_destroy(&mutex);
00020 }
00021 
00022 int ThreadMutex::tryLock() const {
00023   switch(mutex_kind) {
00024     case FAST:
00025       if (pthread_mutex_trylock(&mutex) == 0)  {
00026         current_thread = pthread_self();
00027         lock_counter = 1;
00028         return L1394_SUCCESS;
00029       }
00030       return L1394_FAILED;
00031 
00032     case RECURSIVE :
00033       if ( lock_counter > 0 ) {
00034         if ( pthread_equal(current_thread, pthread_self() ) != 0 ) {
00035           lock_counter++;
00036           return L1394_SUCCESS;
00037         } else
00038           return L1394_FAILED;
00039       } else {
00040           if (pthread_mutex_trylock(&mutex) == 0)  {
00041             current_thread = pthread_self();
00042             lock_counter = 1;
00043             return L1394_SUCCESS;
00044           }
00045       }
00046       break;
00047   }
00048   return L1394_FAILED;
00049 }
00050 
00051 void ThreadMutex::lock() const {
00052   switch (mutex_kind) {
00053     case FAST :
00054       pthread_mutex_lock(&mutex);
00055       break;
00056     case RECURSIVE :
00057       if (lock_counter > 0)     {
00058         if ( pthread_equal(current_thread, pthread_self() ) != 0) {
00059           lock_counter++;
00060         } else {
00061           pthread_mutex_lock(&mutex);
00062           current_thread = pthread_self();
00063           lock_counter = 1;
00064         }
00065       } else    {
00066         pthread_mutex_lock(&mutex);
00067         current_thread = pthread_self();
00068         lock_counter = 1;                       
00069       }
00070      break;
00071    }
00072 }               
00073 
00074 
00075 void ThreadMutex::unlock() const {
00076   switch (mutex_kind)  {
00077     case FAST:
00078       pthread_mutex_unlock(&mutex);
00079       break;
00080     case RECURSIVE:
00081       lock_counter--;
00082       if (lock_counter <= 0)    
00083         pthread_mutex_unlock(&mutex);
00084       break;
00085   }
00086 }
00087 
00088 
00089 ThreadCondition::ThreadCondition(const ThreadMutex& mutex) : mutex(mutex) {
00090   pthread_cond_init(&cond, 0);
00091 }
00092 
00093 
00094 ThreadCondition::~ThreadCondition() {
00095   pthread_cond_destroy(&cond);
00096 }
00097 
00098 int ThreadCondition::timedWait(long sec, long nsec) const {
00099   struct timespec timeout;
00100   timeout.tv_sec  = sec;
00101   timeout.tv_nsec = nsec;
00102   int result = pthread_cond_timedwait(&cond, &(mutex.mutex), &timeout);
00103   if(result != 0) {
00104     return L1394_FAILED;
00105   } else {
00106     return L1394_SUCCESS;
00107   }
00108 }
00109 
00110 void ThreadCondition::wait() const {
00111   pthread_cond_wait(&cond, &(mutex.mutex));
00112 }
00113 
00114 Thread::Thread(){
00115 }
00116 Thread::~Thread() {
00117 }
00118 void Thread::start(  ThrFunction function, void *arg, pthread_attr_t* attr )
00119   //throw (const Exception&)
00120 {
00121   //if (
00122   pthread_create(&thread, attr, function, arg);
00123   //!= 0)
00124   //  throw (Exception("not enough system resources to create a process for the new thread"));
00125 }
00126 
00127 
00128 int Thread::join() {
00129   if (pthread_join(thread, 0) == 0)
00130     return L1394_SUCCESS;
00131   return L1394_FAILED;
00132 }
00133 
00134 void Thread::kill()
00135 {
00136   pthread_kill(thread, 9);
00137 }
00138 
00139 }

Generated on Wed Aug 24 00:36:41 2005 for L1394 by doxygen 1.4.2
L1394 library (NMM) grahics.cs.uni-sb.de/~repplix/l1394_home/