From 6cd2711a88cdadcc11a4536b9262c55b836decd0 Mon Sep 17 00:00:00 2001
From: pans <pans@454eff88-639b-444f-9e54-f578c98de674>
Date: 星期五, 13 一月 2017 16:29:51 +0800
Subject: [PATCH] 

---
 RtspFace/PipeLinePool.cpp |   50 ++++++++++++++++++++++++++++++++------------------
 1 files changed, 32 insertions(+), 18 deletions(-)

diff --git a/RtspFace/PipeLinePool.cpp b/RtspFace/PipeLinePool.cpp
index 3b01327..cba5a1b 100644
--- a/RtspFace/PipeLinePool.cpp
+++ b/RtspFace/PipeLinePool.cpp
@@ -20,6 +20,19 @@
 	} \
 }
 
+struct MutexLocker
+{
+	pthread_mutex_t* mut;
+	MutexLocker(void* _mut) : mut((pthread_mutex_t*)_mut)
+	{
+		PLP_MUTEX_LOCK(mut,);
+	}
+	~MutexLocker()
+	{
+		PLP_MUTEX_UNLOCK(mut,);
+	}
+};
+
 PipeLinePool::PipeLinePool(bool _multithread_safe) : 
 	multithread_safe(_multithread_safe), tsafe_mutex(nullptr), pl_mutex(nullptr), 
 	pipelines(), pipelines_free()
@@ -32,6 +45,7 @@
 		pl_mutex = new pthread_mutex_t;
 		pthread_mutex_init((pthread_mutex_t*)pl_mutex, NULL);
 		
+		// in ctor pool is empty
 		PLP_MUTEX_LOCK(pl_mutex,);
 	}
 }
@@ -64,35 +78,26 @@
 	if (pl == nullptr)
 		return;
 
-	PLP_MUTEX_LOCK(tsafe_mutex,);
+	MutexLocker _ml(tsafe_mutex);
 	
 	if (pipelines.find(pl) != pipelines.end())
 		return;
 	
 	pipelines.insert(pl);
 	pipelines_free.insert(pl);
-	
-	PLP_MUTEX_UNLOCK(tsafe_mutex,);
 }
 
 void PipeLinePool::unmanage(PipeLine* pl)
 {
-	PLP_MUTEX_LOCK(tsafe_mutex,);
+	MutexLocker _ml(tsafe_mutex);
 
 	pipelines.erase(pl);
 	pipelines_free.erase(pl);
-	
-	PLP_MUTEX_UNLOCK(tsafe_mutex,);
 }
 
 PipeLine* PipeLinePool::get_free()
 {
-	if (pipelines_free.empty())
-	{
-		PLP_MUTEX_LOCK(pl_mutex, nullptr);
-	}
-	
-	PLP_MUTEX_LOCK(tsafe_mutex, nullptr);
+	MutexLocker _ml(tsafe_mutex);
 	
 	if (pipelines_free.empty())
 		return nullptr;
@@ -101,23 +106,32 @@
 	PipeLine* pl = *iter;
 	pipelines_free.erase(iter);
 	
-	PLP_MUTEX_UNLOCK(tsafe_mutex, nullptr);
-	
 	return pl;
 }
 
 void PipeLinePool::release(PipeLine* pl)
 {
+	MutexLocker _ml(tsafe_mutex);
+	
 	if (pipelines.find(pl) == pipelines.end())
 		return;
 	if (pipelines_free.find(pl) != pipelines.end())
 		return;
 
-	PLP_MUTEX_LOCK(tsafe_mutex,);
-	
 	pipelines_free.insert(pl);
+}
 
-	PLP_MUTEX_UNLOCK(tsafe_mutex,);
+bool PipeLinePool::wait_free()
+{
+	if (pipelines_free.empty())
+	{
+		PLP_MUTEX_LOCK(pl_mutex, false);
+	}
 	
-	PLP_MUTEX_UNLOCK(pl_mutex,);
+	return true;
+}
+
+bool PipeLinePool::notify_free()
+{
+	PLP_MUTEX_UNLOCK(pl_mutex, false);
 }

--
Gitblit v1.8.0