Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

Thread.C

00001 /* $Id: Thread.C,v 1.2 2000/11/03 22:03:28 rpav Exp $
00002  *
00003  * SFW - Studio FrameWork
00004  * Copyright (C) 2000  Ryan Pavlik
00005  *
00006  * This program is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU General Public License
00008  * as published by the Free Software Foundation; either version 2
00009  * of the License, or (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  * 
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00019  *
00020  */
00021 
00022 /*
00023  * Thread and related classes
00024  *
00025  */ 
00026 
00027 #include <iostream>
00028 #include <cstdlib>
00029 #include <errno.h>
00030 
00031 #ifndef __USE_UNIX98
00032 #    define __USE_UNIX98
00033 #endif
00034 #include <pthread.h>
00035 #include <SFW/Thread.H>
00036 #include <SFW/Log.H>
00037 
00038 using namespace SFW;
00039 
00040 /* Thread is now a subclass of Object, let's see
00041  *  how much *that* breaks. :)
00042  */
00043 TYPE_INIT(SFW::Thread);
00044 TYPE_INIT(SFW::Mutex);
00045 TYPE_INIT(SFW::Condition);
00046 
00047 /* File-local functions */
00048 static void * createThread(void*);
00049 
00050 /*
00051  * class Thread
00052  *
00053  */
00054 Thread::Thread(void) {
00055     isDetached = false;
00056 }
00057 
00058 void Thread::run(void) {
00059     pthread_create(&pt, NULL, createThread, (void*)this);
00060 }
00061 
00062 void Thread::join(void) {
00063     if(!isDetached)
00064     pthread_join(pt, NULL);
00065 }
00066 
00067 void Thread::detach(void) {
00068     if(isDetached)
00069     return;
00070     
00071     pthread_detach(pt);
00072     isDetached = true;
00073 }
00074 
00075 void Thread::cancel(void) {
00076     pthread_cancel(pt);
00077 }
00078 
00079 /* Private methods */
00080 
00081 void Thread::enableCancel() {
00082     pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, (int*)0);
00083 }
00084 
00085 void Thread::disableCancel() {
00086     pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, (int*)0);
00087 }
00088 
00089 void Thread::testCancel() {
00090     pthread_testcancel();
00091 }
00092 
00093 void Thread::exit() {
00094     pthread_exit(NULL);
00095 }
00096     
00097 /*
00098  * class Mutex
00099  *
00100  */
00101 Mutex::Mutex(MutexType t) {
00102     int kind;
00103 
00104     switch(t) {
00105     case QUICK:          kind = PTHREAD_MUTEX_FAST_NP;       break;
00106     case RECURSIVE:      kind = PTHREAD_MUTEX_RECURSIVE_NP;  break;
00107     case ERROR_CHECKING: kind = PTHREAD_MUTEX_ERRORCHECK_NP; break;
00108     }
00109     
00110     pthread_mutexattr_t attr;
00111     pthread_mutexattr_init(&attr);
00112     pthread_mutexattr_settype(&attr, kind);
00113     pthread_mutex_init(&mutex, &attr);
00114     pthread_mutexattr_destroy(&attr);
00115 }
00116 
00117 Mutex::~Mutex() {
00118     pthread_mutex_destroy(&mutex);
00119 }
00120 
00121 void Mutex::lock(void) {
00122     pthread_mutex_lock(&mutex);
00123 }
00124 
00125 void Mutex::unlock(void) {
00126     pthread_mutex_unlock(&mutex);
00127 }
00128 
00129 bool Mutex::trylock(void) {
00130     int ret = pthread_mutex_trylock(&mutex);
00131 
00132     if(ret == EBUSY)
00133     return false;
00134 
00135     return true;
00136 }
00137 
00138 /*
00139  * class Condition
00140  *
00141  */
00142 Condition::Condition(void) {
00143     pthread_cond_init(&cond, NULL);
00144 }
00145 
00146 Condition::~Condition() {
00147     pthread_cond_destroy(&cond);
00148 }
00149 
00150 void Condition::wait(ShouldLock should) {
00151     if(should == DO_LOCK)
00152         lock();
00153     
00154     pthread_cond_wait(&cond, &mutex);
00155     unlock();
00156 }
00157 
00158 void Condition::signal(ShouldLock should) {
00159     if(should == DO_LOCK)
00160         lock();
00161     
00162     pthread_cond_signal(&cond);
00163     unlock();
00164 }
00165 
00166 void Condition::broadcast(ShouldLock should) {
00167     if(should == DO_LOCK)
00168         lock();
00169     
00170     pthread_cond_broadcast(&cond);
00171     unlock();
00172 }
00173 
00174 /*
00175  * Local functions
00176  *
00177  */
00178 static void * createThread(void * arg) {
00179     Thread * t = (Thread*)arg;
00180 
00181     t->main();
00182     return (void*)0;
00183 }

Generated at Tue Jan 2 15:38:34 2001 for SFW by doxygen1.2.4 written by Dimitri van Heesch, © 1997-2000