Kaynağa Gözat

Filled in ScannerPool (Completed?)

git-svn-id: https://svn.microneil.com/svn/SNF4CGP/trunk@19 59e8e3e7-56fa-483b-b4b4-fa6ab0af3dfc
master
madscientist 15 yıl önce
ebeveyn
işleme
2005147208
2 değiştirilmiş dosya ile 50 ekleme ve 8 silme
  1. 46
    7
      SNF4CGP/ScannerPool.cpp
  2. 4
    1
      SNF4CGP/ScannerPool.hpp

+ 46
- 7
SNF4CGP/ScannerPool.cpp Dosyayı Görüntüle

@@ -22,6 +22,7 @@ Scanner::~Scanner() {
try { // Destructors don't throw.
if(Engine_) { // Safely destroy the engine.
Engine_->close();
delete Engine_;
Engine_=0;
}
}
@@ -42,31 +43,69 @@ snf_EngineHandler& Scanner::Engine() {
//// ScannerPool ///////////////////////////////////////////////////////////////
ScannerPool::ScannerPool() : // Constructed simply.
Rulebase_(0),
ScannerCount(0) {
Rulebase_(new snf_RulebaseHandler()),
ScannerCount(0),
Started(false) {
}
ScannerPool::~ScannerPool() { // Cleanup on the way out.
try { stop(); }
try {
stop();
delete Rulebase_;
Rulebase_ = 0;
}
catch(...) {}
}
void ScannerPool::init(string Configuration) { // Get ready to provide scanners.
RuntimeCheck CheckRulebaseExists("ScannerPool::Rulebase() Check(0 != Rulebase_)");
snf_RulebaseHandler& ScannerPool::Rulebase() { // Safe access to the rulebase.
CheckRulebaseExists(0 != Rulebase_);
return (*Rulebase_);
}
void ScannerPool::init(const string Configuration) { // Get ready to provide scanners.
Rulebase().open(Configuration.c_str(), "", ""); // Light up the rulebase manager.
Scanner* FirstScanner = new Scanner(Rulebase()); // Create our first scanner.
PooledScanners.give(FirstScanner); // Drop it into the pool.
Started = true;
}
void ScannerPool::killScannerFromPool() { // Destroy and count a pooled scanner.
Scanner* ScannerToKill = 0;
ScannerToKill = PooledScanners.take();
delete ScannerToKill;
--ScannerCount;
}
void ScannerPool::stop() { // Shutdown and clean up.
ScopeMutex Busy(AllocationControl);
while(0 < ScannerCount) killScannerFromPool();
Rulebase().close();
Started = false;
}
Scanner* ScannerPool::makeScanner() { // Create and count a scanner.
Scanner* NewScanner = new Scanner(Rulebase());
++ScannerCount;
return NewScanner;
}
Scanner& ScannerPool::grab() { // Provides a Scanner from the pool.
}
LogicCheck CheckGrabScannersOnlyWhenStarted("ScannerPool::grab() Check(Started)");
RuntimeCheck CheckForValidGrabbedScanner("ScannerPool::grab() Check(0 != GrabbedScanner)");
void ScannerPool::killScannerFromPool() { // Destroy and count a pooled scanner.
Scanner& ScannerPool::grab() { // Provides a Scanner from the pool.
ScopeMutex Busy(AllocationControl);
CheckGrabScannersOnlyWhenStarted(Started);
Scanner* GrabbedScanner = 0;
if(0 < PooledScanners.size()) GrabbedScanner = PooledScanners.take(); // Prefer scanners from the pool.
else GrabbedScanner = makeScanner(); // Make new ones if needed.
CheckForValidGrabbedScanner(0 != GrabbedScanner);
return (*GrabbedScanner);
}
void ScannerPool::drop(Scanner& S) { // Returns a Scanner to the pool.
PooledScanners.give(&S);
}
//// ScopeScanner //////////////////////////////////////////////////////////////

+ 4
- 1
SNF4CGP/ScannerPool.hpp Dosyayı Görüntüle

@@ -39,13 +39,16 @@ class ScannerPool {
unsigned int ScannerCount; // Allocates and counts scanners.
ProductionQueue<Scanner*> PooledScanners; // Pool of ready-to-go scanners.
Mutex AllocationControl; // Allocation syncrhonizer.
bool Started; // True when we're ready to operate.
Scanner* makeScanner(); // Make and count a scanner.
void killScannerFromPool(); // Kill and count a pooled scanner.
public:
ScannerPool(); // Constructed simply.
~ScannerPool(); // Cleans up when destroyed.
void init(string Configuration); // Get ready to provide scanners.
void init(const string Configuration); // Get ready to provide scanners.
void stop(); // Shutdown and clean up.
Scanner& grab(); // Provides a Scanner from the pool.
void drop(Scanner& S); // Returns a Scanner to the pool.

Loading…
İptal
Kaydet