MPQC  3.0.0-alpha
thread.h
1 //
2 // thread.h
3 //
4 // Copyright (C) 1997 Limit Point Systems, Inc.
5 //
6 // Author: Edward Seidl <seidl@janed.com>
7 // Maintainer: LPS
8 //
9 // This file is part of the SC Toolkit.
10 //
11 // The SC Toolkit is free software; you can redistribute it and/or modify
12 // it under the terms of the GNU Library General Public License as published by
13 // the Free Software Foundation; either version 2, or (at your option)
14 // any later version.
15 //
16 // The SC Toolkit is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 // GNU Library General Public License for more details.
20 //
21 // You should have received a copy of the GNU Library General Public License
22 // along with the SC Toolkit; see the file COPYING.LIB. If not, write to
23 // the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
24 //
25 // The U.S. Government is granted a limited license as per AL 91-7.
26 //
27 
28 #ifndef _util_group_thread_h
29 #define _util_group_thread_h
30 
31 #include <util/class/class.h>
32 
33 namespace sc {
34 
40 class ThreadLock : public RefCount {
41  public:
42  ThreadLock();
43  virtual ~ThreadLock();
44 
46  virtual void lock() =0;
48  virtual void unlock() =0;
49 };
50 
51 
57  Ref<ThreadLock> lock_;
58  bool locked_;
59  public:
61  ThreadLockHolder(const Ref<ThreadLock> &l): lock_(l) {
62  lock_->lock();
63  locked_ = true;
64  }
66  void unlock() { if (locked_) { lock_->unlock(); locked_ = false; } }
68  void lock() { if (!locked_) { lock_->lock(); locked_ = true; } }
71 };
72 
75 class Thread {
76  public:
77  Thread();
78  virtual ~Thread();
79 
80  static void *run_Thread_run(void*thread);
81 
83  virtual void run() =0;
84 };
85 
88 class ThreadGrp: public DescribedClass {
89  protected:
90  Thread** threads_;
91  int nthread_;
92 
93  public:
94  ThreadGrp();
95  ThreadGrp(const Ref<KeyVal>&);
96  ThreadGrp(const ThreadGrp&, int nthread = -1);
97  virtual ~ThreadGrp();
98 
101  virtual void add_thread(int threadnum, Thread* thread);
105  virtual void add_thread(int threadnum, Thread* thread, int priority);
107  int nthread() const { return nthread_; }
108 
109  void delete_threads();
110 
113  virtual int start_threads() =0;
116  virtual int wait_threads() =0;
118  virtual Ref<ThreadLock> new_lock() =0;
119 
124  virtual ThreadGrp* clone(int nthread = -1);
125 
128  static void set_default_threadgrp(const Ref<ThreadGrp>&);
132  static ThreadGrp * get_default_threadgrp();
139  static ThreadGrp * initial_threadgrp(int &argc, char ** argv);
140 };
141 
142 
146 class ProcThreadGrp: public ThreadGrp {
147  public:
148  ProcThreadGrp();
149  ProcThreadGrp(const Ref<KeyVal>&);
150  ~ProcThreadGrp();
151 
152  int start_threads();
153  int wait_threads();
154 
156 
157  ThreadGrp* clone(int nthread = -1);
158 };
159 
160 }
161 
162 extern "C" {
163  // a C linkage interface to run_Thread_run
164  void *Thread__run_Thread_run(void*thread);
165 }
166 
167 #endif
168 
169 // Local Variables:
170 // mode: c++
171 // c-file-style: "ETS"
172 // End:
sc::ThreadLockHolder::~ThreadLockHolder
~ThreadLockHolder()
Releases the lock if it is still held.
Definition: thread.h:70
sc::ProcThreadGrp
The ProcThreadGrp class privides a concrete thread group appropriate for an environment where there i...
Definition: thread.h:146
sc::Ref
A template class that maintains references counts.
Definition: ref.h:361
sc::ThreadLock::unlock
virtual void unlock()=0
Release the lock.
sc::Thread
The Thread abstract class defines an interface which must be implemented by classes wishing to be run...
Definition: thread.h:75
sc::ThreadLockHolder::lock
void lock()
Acquire the lock once more.
Definition: thread.h:68
sc::ThreadGrp::set_default_threadgrp
static void set_default_threadgrp(const Ref< ThreadGrp > &)
Sets the default ThreadGrp.
sc::ThreadGrp::add_thread
virtual void add_thread(int threadnum, Thread *thread)
Assigns a Thread object to each thread.
sc::ProcThreadGrp::clone
ThreadGrp * clone(int nthread=-1)
Create a ThreadGrp like the current one.
sc::ThreadLock::lock
virtual void lock()=0
Obtain the lock.
sc::ThreadGrp::initial_threadgrp
static ThreadGrp * initial_threadgrp(int &argc, char **argv)
Create a ThreadGrp.
sc::ThreadGrp::nthread
int nthread() const
The number of threads that will be run by start_thread.
Definition: thread.h:107
sc::ProcThreadGrp::new_lock
Ref< ThreadLock > new_lock()
Return a local object.
sc::ThreadGrp::wait_threads
virtual int wait_threads()=0
Wait for all the threads to complete.
sc::ThreadGrp::new_lock
virtual Ref< ThreadLock > new_lock()=0
Return a local object.
sc::ThreadLockHolder::unlock
void unlock()
Release the lock before the DTOR is called, if it is still held.
Definition: thread.h:66
sc::ThreadGrp
The ThreadGrp abstract class provides a means to manage separate threads of control.
Definition: thread.h:88
sc::ProcThreadGrp::start_threads
int start_threads()
Starts the threads running.
sc::ThreadGrp::clone
virtual ThreadGrp * clone(int nthread=-1)
Create a ThreadGrp like the current one.
sc::ThreadGrp::start_threads
virtual int start_threads()=0
Starts the threads running.
sc::Thread::run
virtual void run()=0
This is called with the Thread is run from a ThreadGrp.
sc::DescribedClass
Classes which need runtime information about themselves and their relationship to other classes can v...
Definition: class.h:233
sc::ProcThreadGrp::wait_threads
int wait_threads()
Wait for all the threads to complete.
sc::ThreadLock
The ThreadLock abstract class provides mutex locks to be used in conjunction with ThreadGrp's.
Definition: thread.h:40
sc::ThreadLockHolder::ThreadLockHolder
ThreadLockHolder(const Ref< ThreadLock > &l)
Acquires the lock.
Definition: thread.h:61
sc::ThreadLockHolder
Acquire a lock on creation and release it on destruction.
Definition: thread.h:56
sc::RefCount
The base class for all reference counted objects.
Definition: ref.h:192
sc::ThreadGrp::get_default_threadgrp
static ThreadGrp * get_default_threadgrp()
Returns the default ThreadGrp.
sc
Contains all MPQC code up to version 3.
Definition: mpqcin.h:14

Generated at Sun Jan 26 2020 23:24:01 for MPQC 3.0.0-alpha using the documentation package Doxygen 1.8.16.