123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485 |
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- #ifndef MNR_threading
- #define MNR_threading
-
- #include <set>
- #include <vector>
- #include <string>
- #include <queue>
- #include "faults.hpp"
-
- using namespace std;
-
- class ThreadManager;
- extern ThreadManager Threads;
-
-
-
-
-
-
-
-
-
- class ThreadState {
- public:
- const string Name;
- ThreadState(string N) : Name(N) {}
- };
-
-
-
-
- class ThreadType {
- public:
- const string Name;
- ThreadType(string N) : Name(N) {}
- };
-
- class Thread;
-
- class ThreadStatusRecord {
- private:
- Thread* Pointer;
- ThreadType* Type;
- ThreadState* State;
- string Name;
- bool isRunning;
- bool isBad;
- string Fault;
-
- public:
- ThreadStatusRecord(
- Thread* P,
- ThreadType& T,
- ThreadState& S,
- bool R,
- bool B,
- string F,
- string N
- ) :
- Pointer(P),
- Type(&T),
- State(&S),
- Name(N),
- isRunning(R),
- isBad(B),
- Fault(F)
- {}
-
- ThreadStatusRecord& operator=(const ThreadStatusRecord& Right) {
- Pointer = Right.Pointer;
- Type = Right.Type;
- State = Right.State;
- isRunning = Right.isRunning;
- isBad = Right.isBad;
- Fault = Right.Fault;
- Name = Right.Name;
- return *this;
- }
-
- bool operator<(const ThreadStatusRecord& Right) {
- return (Pointer < Right.Pointer);
- }
-
-
-
- const Thread* getPointer() { return Pointer; }
- const ThreadType& getType() { return *Type; }
- const ThreadState& getState() { return *State; }
- bool getRunning() { return isRunning; }
- bool getBad() { return isBad; }
- string getFault() { return Fault; }
- string getName() { return Name; }
- };
-
- typedef vector<ThreadStatusRecord> ThreadStatusReport;
-
-
-
-
-
-
-
- #ifdef WIN32
-
-
-
-
- #include <windows.h>
- #include <process.h>
-
- typedef HANDLE thread_primative;
-
-
- typedef HANDLE mutex_primative;
-
-
- inline void threading_yield() {
- SwitchToThread();
- }
-
- #else
-
-
-
-
- #include <pthread.h>
- #include <sched.h>
-
- typedef pthread_t thread_primative;
-
-
- typedef pthread_mutex_t mutex_primative;
-
-
- inline void threading_yield() {
- sched_yield();
- }
-
- #endif
-
-
-
-
-
-
-
-
-
-
-
-
- class Thread {
-
- private:
-
- ThreadState* MyThreadState;
-
- protected:
-
- const ThreadType& MyThreadType;
- const string MyThreadName;
-
- thread_primative MyThread;
- bool RunningFlag;
- bool BadFlag;
- string BadWhat;
- void CurrentThreadState(const ThreadState& TS);
-
- public:
-
- Thread();
- Thread(const ThreadType& T, string N);
- virtual ~Thread();
-
- void run();
- void join();
- void launchTask();
-
- virtual void myTask() = 0;
-
- thread_primative getMyThread();
-
- bool isRunning();
- bool isBad();
- const string MyFault();
-
- const string MyName();
- const ThreadType& MyType();
- const ThreadState& MyState();
- const ThreadState& CurrentThreadState();
-
- ThreadStatusRecord StatusReport();
-
-
-
- const static ThreadType Type;
-
- const static ThreadState ThreadInitialized;
- const static ThreadState ThreadStarted;
- const static ThreadState ThreadFailed;
- const static ThreadState ThreadStopped;
- const static ThreadState ThreadDestroyed;
-
- };
-
-
-
-
-
-
-
-
-
-
-
-
- class Mutex {
-
- private:
-
- mutex_primative MyMutex;
- volatile bool IAmLocked;
-
- public:
-
- Mutex();
- ~Mutex();
-
- void lock();
- void unlock();
- bool tryLock();
- bool isLocked();
-
- };
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- class ScopeMutex {
-
- private:
-
- Mutex& MyMutex;
-
- public:
-
- ScopeMutex(Mutex& M);
- ~ScopeMutex();
-
- };
-
-
-
-
-
-
-
-
-
-
-
-
- #ifdef WIN32
-
-
-
- class ProductionGateway {
-
- private:
-
- HANDLE MySemaphore;
-
- public:
-
- ProductionGateway();
- ~ProductionGateway();
-
- void produce();
- void consume();
-
- };
-
- #else
-
-
-
- class ProductionGateway {
-
- private:
-
- mutex_primative MyMutex;
- pthread_cond_t MyConditionVariable;
-
- int Product;
- int Waiting;
- int Signaled;
-
- public:
-
- ProductionGateway();
- ~ProductionGateway();
-
- void produce();
- void consume();
-
- };
-
- #endif
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- class ThreadManager {
- friend class Thread;
- private:
-
- Mutex MyMutex;
- set<Thread*> KnownThreads;
-
- void rememberThread(Thread* T);
- void forgetThread(Thread* T);
-
- Thread* LockedThread;
-
- public:
-
- ThreadManager():LockedThread(0){}
-
- ThreadStatusReport StatusReport();
- bool lockExistingThread(Thread* T);
- void unlockExistingThread(Thread* T);
-
- };
-
- class ScopeThreadLock {
- private:
- Thread* MyLockedThread;
-
- public:
- ScopeThreadLock(Thread* T);
- ~ScopeThreadLock();
- bool isGood();
- bool isBad();
- };
-
-
-
-
-
-
-
-
-
-
-
-
- template<typename T>
- class ProductionQueue {
- private:
- Mutex myMutex;
- volatile unsigned int LatestSize;
- ProductionGateway myGateway;
- queue<T> myQueue;
-
- public:
- ProductionQueue() : LatestSize(0) {}
-
- T take() {
- myGateway.consume();
- ScopeMutex OneAtATimePlease(myMutex);
- T O = myQueue.front();
- myQueue.pop();
- LatestSize = myQueue.size();
- return O;
- }
-
- void give(T O) {
- ScopeMutex OneAtATimePlease(myMutex);
- myQueue.push(O);
- LatestSize = myQueue.size();
- myGateway.produce();
- }
-
- unsigned int size() {
- return LatestSize;
- }
- };
-
-
-
-
- #endif
-
|