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 public: 00063 ThreadLockHolder(const Ref<ThreadLock> &l): lock_(l) { lock_->lock(); } 00064 ~ThreadLockHolder() { lock_->unlock(); } 00065 }; 00066 00069 class Thread { 00070 public: 00071 Thread(); 00072 virtual ~Thread(); 00073 00074 static void *run_Thread_run(void*thread); 00075 00077 virtual void run() =0; 00078 }; 00079 00082 class ThreadGrp: public DescribedClass { 00083 protected: 00084 Thread** threads_; 00085 int nthread_; 00086 00087 public: 00088 ThreadGrp(); 00089 ThreadGrp(const Ref<KeyVal>&); 00090 ThreadGrp(const ThreadGrp&, int nthread = -1); 00091 virtual ~ThreadGrp(); 00092 00095 virtual void add_thread(int threadnum, Thread* thread); 00099 virtual void add_thread(int threadnum, Thread* thread, int priority); 00101 int nthread() const { return nthread_; } 00102 00103 void delete_threads(); 00104 00107 virtual int start_threads() =0; 00110 virtual int wait_threads() =0; 00112 virtual Ref<ThreadLock> new_lock() =0; 00113 00118 virtual ThreadGrp* clone(int nthread = -1); 00119 00120 static void set_default_threadgrp(const Ref<ThreadGrp>&); 00121 static ThreadGrp * get_default_threadgrp(); 00122 static ThreadGrp * initial_threadgrp(int &argc, char ** argv); 00123 }; 00124 00125 00129 class ProcThreadGrp: public ThreadGrp { 00130 public: 00131 ProcThreadGrp(); 00132 ProcThreadGrp(const Ref<KeyVal>&); 00133 ~ProcThreadGrp(); 00134 00135 int start_threads(); 00136 int wait_threads(); 00137 00138 Ref<ThreadLock> new_lock(); 00139 00140 ThreadGrp* clone(int nthread = -1); 00141 }; 00142 00143 } 00144 00145 extern "C" { 00146 // a C linkage interface to run_Thread_run 00147 void *Thread__run_Thread_run(void*thread); 00148 } 00149 00150 #endif 00151 00152 // Local Variables: 00153 // mode: c++ 00154 // c-file-style: "ETS" 00155 // End: