00001 // 00002 // thread.h 00003 // 00004 // Copyright (C) 1997 Limit Point Systems, Inc. 00005 // 00006 // Author: Edward Seidl <seidl@janed.com> 00007 // Maintainer: LPS 00008 // 00009 // This file is part of the SC Toolkit. 00010 // 00011 // The SC Toolkit is free software; you can redistribute it and/or modify 00012 // it under the terms of the GNU Library General Public License as published by 00013 // the Free Software Foundation; either version 2, or (at your option) 00014 // any later version. 00015 // 00016 // The SC Toolkit is distributed in the hope that it will be useful, 00017 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00018 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00019 // GNU Library General Public License for more details. 00020 // 00021 // You should have received a copy of the GNU Library General Public License 00022 // along with the SC Toolkit; see the file COPYING.LIB. If not, write to 00023 // the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. 00024 // 00025 // The U.S. Government is granted a limited license as per AL 91-7. 00026 // 00027 00028 #ifndef _util_group_thread_h 00029 #define _util_group_thread_h 00030 00031 #ifdef __GNUC__ 00032 #pragma interface 00033 #endif 00034 00035 #include <util/class/class.h> 00036 00037 namespace sc { 00038 00044 class ThreadLock : public RefCount { 00045 public: 00046 ThreadLock(); 00047 virtual ~ThreadLock(); 00048 00050 virtual void lock() =0; 00052 virtual void unlock() =0; 00053 }; 00054 00055 00060 class ThreadLockHolder { 00061 Ref<ThreadLock> lock_; 00062 bool locked_; 00063 public: 00065 ThreadLockHolder(const Ref<ThreadLock> &l): lock_(l) { 00066 lock_->lock(); 00067 locked_ = true; 00068 } 00070 void unlock() { if (locked_) { lock_->unlock(); locked_ = false; } } 00072 void lock() { if (!locked_) { lock_->lock(); locked_ = true; } } 00074 ~ThreadLockHolder() { unlock(); } 00075 }; 00076 00079 class Thread { 00080 public: 00081 Thread(); 00082 virtual ~Thread(); 00083 00084 static void *run_Thread_run(void*thread); 00085 00087 virtual void run() =0; 00088 }; 00089 00092 class ThreadGrp: public DescribedClass { 00093 protected: 00094 Thread** threads_; 00095 int nthread_; 00096 00097 public: 00098 ThreadGrp(); 00099 ThreadGrp(const Ref<KeyVal>&); 00100 ThreadGrp(const ThreadGrp&, int nthread = -1); 00101 virtual ~ThreadGrp(); 00102 00105 virtual void add_thread(int threadnum, Thread* thread); 00109 virtual void add_thread(int threadnum, Thread* thread, int priority); 00111 int nthread() const { return nthread_; } 00112 00113 void delete_threads(); 00114 00117 virtual int start_threads() =0; 00120 virtual int wait_threads() =0; 00122 virtual Ref<ThreadLock> new_lock() =0; 00123 00128 virtual ThreadGrp* clone(int nthread = -1); 00129 00132 static void set_default_threadgrp(const Ref<ThreadGrp>&); 00136 static ThreadGrp * get_default_threadgrp(); 00143 static ThreadGrp * initial_threadgrp(int &argc, char ** argv); 00144 }; 00145 00146 00150 class ProcThreadGrp: public ThreadGrp { 00151 public: 00152 ProcThreadGrp(); 00153 ProcThreadGrp(const Ref<KeyVal>&); 00154 ~ProcThreadGrp(); 00155 00156 int start_threads(); 00157 int wait_threads(); 00158 00159 Ref<ThreadLock> new_lock(); 00160 00161 ThreadGrp* clone(int nthread = -1); 00162 }; 00163 00164 } 00165 00166 extern "C" { 00167 // a C linkage interface to run_Thread_run 00168 void *Thread__run_Thread_run(void*thread); 00169 } 00170 00171 #endif 00172 00173 // Local Variables: 00174 // mode: c++ 00175 // c-file-style: "ETS" 00176 // End: