Ver código fonte

Implement condition variables

Daniele Bartolini 13 anos atrás
pai
commit
c6d3ee1315
4 arquivos alterados com 80 adições e 2 exclusões
  1. 1 2
      src/CMakeLists.txt
  2. 1 0
      src/Crown.h
  3. 74 0
      src/core/threads/Cond.h
  4. 4 0
      src/core/threads/Mutex.h

+ 1 - 2
src/CMakeLists.txt

@@ -169,11 +169,10 @@ set (COMPRESSORS_HEADERS
 set (THREADS_HEADERS
 set (THREADS_HEADERS
 	core/threads/Thread.h
 	core/threads/Thread.h
 	core/threads/Mutex.h
 	core/threads/Mutex.h
+	core/threads/Cond.h
 )
 )
 
 
 set (THREADS_SRC
 set (THREADS_SRC
-	core/threads/Thread.cpp
-	core/threads/Mutex.cpp
 )
 )
 
 
 set (INPUT_SRC
 set (INPUT_SRC

+ 1 - 0
src/Crown.h

@@ -86,6 +86,7 @@ OTHER DEALINGS IN THE SOFTWARE.
 // Core/Threads
 // Core/Threads
 #include "Thread.h"
 #include "Thread.h"
 #include "Mutex.h"
 #include "Mutex.h"
+#include "Cond.h"
 
 
 // Engine
 // Engine
 #include "Camera.h"
 #include "Camera.h"

+ 74 - 0
src/core/threads/Cond.h

@@ -0,0 +1,74 @@
+/*
+Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
+
+Permission is hereby granted, free of charge, to any person
+obtaining a copy of this software and associated documentation
+files (the "Software"), to deal in the Software without
+restriction, including without limitation the rights to use,
+copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the
+Software is furnished to do so, subject to the following
+conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+OTHER DEALINGS IN THE SOFTWARE.
+*/
+
+#pragma once
+
+#include "Types.h"
+#include "Mutex.h"
+#include "OS.h"
+
+namespace crown
+{
+
+class Cond
+{
+public:
+
+					Cond();
+					~Cond();
+
+	void			signal();
+	void			wait(Mutex& mutex);
+
+private:
+
+	os::OSCond		m_cond;
+};
+
+//-----------------------------------------------------------------------------
+inline Cond::Cond()
+{
+	os::cond_create(&m_cond);
+}
+
+//-----------------------------------------------------------------------------
+inline Cond::~Cond()
+{
+	os::cond_destroy(&m_cond);
+}
+
+//-----------------------------------------------------------------------------
+inline void Cond::signal()
+{
+	os::cond_signal(&m_cond);
+}
+
+//-----------------------------------------------------------------------------
+inline void Cond::wait(Mutex& mutex)
+{
+	os::cond_wait(&m_cond, &mutex.m_mutex);
+}
+
+} // namespace crown

+ 4 - 0
src/core/threads/Mutex.h

@@ -44,6 +44,10 @@ public:
 private:
 private:
 
 
 	os::OSMutex		m_mutex;
 	os::OSMutex		m_mutex;
+
+private:
+
+	friend class	Cond;
 };
 };
 
 
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------