// $Revision: 1.11 $ //------------------------------------------------------------------------------ // // Copyright (C) 1994 Taligent, Inc. All rights reserved. // // Project: ConcurrentActors // File: LocalUtilities.h // Build/Version: 1.0.0 // // Description: Concurrent Actors has a few classes that aren't part of a // subsystem: // // TPCQueue // TBoundThreadProgram // TPeriodicThread // TStubThreadProgram // // TPCQueue is used by the Universe subsystem for queueing // actor insertions and deletions. The thread classes are // used by the Movement, Refresh, and Universe subsystems. // TPeriodicThread is the base class for the time-based // movement and refresh threads. TStubThreadProgram and // TBoundThreadProgram separate the protocol of the thread // object from the thread state. // //------------------------------------------------------------------------------ #ifndef TaligentSamples_LOCALUTILITIES #define TaligentSamples_LOCALUTILITIES class TBoundThreadProgram; class TStubBoundThreadProgram; // This is an internal implementation class. class TPeriodicThread; class TPeriodicThreadEntry; class TPrimPCQueue; // This is an internal implementation class. Use the TPCQueue class. template class TPCQueue; #ifndef Taligent_COREPRIMITIVECLASSES #include #endif #ifndef Taligent_STANDARDTEXT #include #endif #ifndef Taligent_CLOCK #include #endif #ifndef Taligent_CLASSICDATASTRUCTURES #include #endif #ifndef Taligent_COLLECTIONS #include #endif #ifndef Taligent_SYNCHRONIZATION #include // TMonitorLock #endif class TThreadHandle; // TThreadHandle is defined in Task.h. //============================================================================== // TBoundThreadProgram class TBoundThreadProgram { public: TBoundThreadProgram(); virtual ~TBoundThreadProgram(); virtual void Start(); virtual void Terminate(); protected: virtual void BoundPrepare(); virtual void BoundRun() = 0; virtual void BoundExit() = 0; friend class TStubBoundThreadProgram; private: TThreadHandle* fThread; }; //============================================================================== // TPeriodicThread class TPeriodicThread : public TBoundThreadProgram { public: TPeriodicThread(); virtual ~TPeriodicThread(); virtual void Pause(); virtual void Resume(); protected: virtual void CheckInterval(); virtual void BoundPrepare(); virtual void BoundRun(); virtual void BoundExit(); virtual void HandleIntervalPassed(const TTime& interval) = 0; virtual void GetDelayInterval(TTime& interval) const = 0; private: TSystemClock fClock; TDelay fDelay; TLocalSemaphore fLock; bool fDone; }; //============================================================================== // TPeriodicThreadEntry class TPeriodicThreadEntry { public: TPeriodicThreadEntry(TPeriodicThread* thread); virtual ~TPeriodicThreadEntry(); private: TPeriodicThread* fThread; }; //============================================================================== // TPrimPCQueue class TPrimPCQueue { public: TPrimPCQueue(unsigned long maxSize = LONG_MAX); virtual ~TPrimPCQueue(); unsigned long Count() const; void Close(); void Reopen(); bool IsOpen() const; void RemoveAll(); void DeleteAll(); protected: void PAdd(void* item); void* PRemoveNext(); private: virtual void DoDelete(void* item) const = 0; void ThrowQueueClosedException() const; long fMaxSize; TDequeOf fItems; TMonitorLock fLock; TMonitorCondition fItemInserted; TMonitorCondition fItemRemoved; bool fIsOpen; }; //============================================================================== // TPCQueue template class TPCQueue : public TPrimPCQueue { public: TPCQueue(unsigned long maxSize = LONG_MAX); virtual ~TPCQueue(); void Add(AType* item); AType* RemoveNext(); private: virtual void DoDelete(void* item) const; }; //============================================================================== // TPCQueue implementation // #ifndef HIDE_TEMPLATE_DEFINITIONS #define MAKE_TPCQueue_DEFINITIONS_VISIBLE #endif #ifdef MAKE_TPCQueue_DEFINITIONS_VISIBLE template TPCQueue::TPCQueue(unsigned long maxSize) : TPrimPCQueue(maxSize) { } template TPCQueue::~TPCQueue() { } template inline void TPCQueue::Add(AType* item) { TPrimPCQueue::PAdd(item); } template inline AType* TPCQueue::RemoveNext() { return (AType*)TPrimPCQueue::PRemoveNext(); } template void TPCQueue::DoDelete(void* item) const { delete (AType*)item; } #endif #endif