forgot what changed

This commit is contained in:
malloc 2018-04-16 09:45:58 -05:00
parent bbefe08144
commit 3ce74b2312

View file

@ -34,10 +34,13 @@ public:
void Start(); void Start();
bool AddClient(T* client); bool AddClient(T* client);
int ClientCount() const; int ClientCount() const;
inline bool IsOpen() const {
return this->is_open;
}
void Stop(); void Stop();
protected: protected:
virtual void ProcessClient(T* client) = 0; virtual bool ProcessClient(T* client) = 0;
private: private:
class Stack { class Stack {
public: public:
@ -46,6 +49,9 @@ private:
void AddClient(T* client); void AddClient(T* client);
int ClientCount() const; int ClientCount() const;
inline bool IsOpen() const {
return this->is_open;
}
void Stop(); void Stop();
private: private:
@ -54,13 +60,15 @@ private:
std::thread thread; std::thread thread;
Pool<T> *pool; Pool<T> *pool;
bool is_open; bool is_open;
bool is_running;
std::list<T*> clients; std::list<T*> clients;
std::mutex clients_mtx; std::mutex clients_mtx;
}; };
poolinfo_t info; poolinfo_t info;
bool is_running; bool is_open;
std::vector<Stack> stacks; std::vector<Stack> stacks;
friend class Stack; friend class Stack;
@ -119,25 +127,64 @@ template<class T>
Pool<T>::Stack::Stack(Pool<T>* pool) { Pool<T>::Stack::Stack(Pool<T>* pool) {
this->pool = pool; this->pool = pool;
this->is_open = false; this->is_open = false;
this->is_running = false;
} }
template<class T> template<class T>
void Pool<T>::Stack::Start() { void Pool<T>::Stack::Start() {
if(this->is_open) if(this->is_open || this->is_running)
return; return;
this->thread = std::thread(this->StackThread, this);
this->is_open = true; this->is_open = true;
this->is_running = true;
this->thread = std::thread(this->StackThread, this);
} }
template<class T> template<class T>
void Pool<T>::Stack::AddClient(T* client) { void Pool<T>::Stack::AddClient(T* client) {
if(!this->is_open || !this->is_running)
return;
this->clients_mtx.lock();
this->clients.push_back(client);
this->clients_mtx.unlock();
} }
template<class T> template<class T>
int Pool<T>::Stack::ClientCount() const { int Pool<T>::Stack::ClientCount() const {
if(!this->is_open || !this->is_running)
return 0;
this->clients_mtx.lock();
int count = this->clients.size();
this->clients_mtx.unlock();
return count;
}
template<class T>
void Pool<T>::Stack::StackThread() {
while(this->is_running) {
for(auto i = this->clients.begin(); i != this->clients.end(); ++i) {
if(!this->is_running)
break;
this->clients_mtx.lock();
if(!this->pool->ProcessClient(*i))
this->clients.erase(i);
this->clients_mtx.unlock();
}
}
}
template<class T>
void Pool<T>::Stack::Stop() {
if(!this->is_open || !this->is_running)
return;
this->is_running = false;
this->thread.join();
this->is_open = false;
} }
} }