thread.h
//
// thread.h
//
// Copyright (C) 1997 Limit Point Systems, Inc.
//
// Author: Edward Seidl <seidl@janed.com>
// Maintainer: LPS
//
// This file is part of the SC Toolkit.
//
// The SC Toolkit is free software; you can redistribute it and/or modify
// it under the terms of the GNU Library General Public License as published by
// the Free Software Foundation; either version 2, or (at your option)
// any later version.
//
// The SC Toolkit is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Library General Public License for more details.
//
// You should have received a copy of the GNU Library General Public License
// along with the SC Toolkit; see the file COPYING.LIB. If not, write to
// the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
//
// The U.S. Government is granted a limited license as per AL 91-7.
//
#ifndef _util_group_thread_h
#define _util_group_thread_h
#ifdef __GNUC__
#pragma interface
#endif
#include <util/class/class.h>
namespace sc {
class ThreadLock : public RefCount {
public:
ThreadLock();
virtual ~ThreadLock();
virtual void lock() =0;
virtual void unlock() =0;
};
class ThreadLockHolder {
Ref<ThreadLock> lock_;
bool locked_;
public:
ThreadLockHolder(const Ref<ThreadLock> &l): lock_(l) {
lock_->lock();
locked_ = true;
}
void unlock() { if (locked_) { lock_->unlock(); locked_ = false; } }
void lock() { if (!locked_) { lock_->lock(); locked_ = true; } }
~ThreadLockHolder() { unlock(); }
};
class Thread {
public:
Thread();
virtual ~Thread();
static void *run_Thread_run(void*thread);
virtual void run() =0;
};
class ThreadGrp: public DescribedClass {
protected:
Thread** threads_;
int nthread_;
public:
ThreadGrp();
ThreadGrp(const Ref<KeyVal>&);
ThreadGrp(const ThreadGrp&, int nthread = -1);
virtual ~ThreadGrp();
virtual void add_thread(int threadnum, Thread* thread);
virtual void add_thread(int threadnum, Thread* thread, int priority);
int nthread() const { return nthread_; }
void delete_threads();
virtual int start_threads() =0;
virtual int wait_threads() =0;
virtual Ref<ThreadLock> new_lock() =0;
virtual ThreadGrp* clone(int nthread = -1);
static void set_default_threadgrp(const Ref<ThreadGrp>&);
static ThreadGrp * get_default_threadgrp();
static ThreadGrp * initial_threadgrp(int &argc, char ** argv);
};
class ProcThreadGrp: public ThreadGrp {
public:
ProcThreadGrp();
ProcThreadGrp(const Ref<KeyVal>&);
~ProcThreadGrp();
int start_threads();
int wait_threads();
Ref<ThreadLock> new_lock();
ThreadGrp* clone(int nthread = -1);
};
}
extern "C" {
// a C linkage interface to run_Thread_run
void *Thread__run_Thread_run(void*thread);
}
#endif
// Local Variables:
// mode: c++
// c-file-style: "ETS"
// End: