فهرست منبع

implement semaphore

mikymod 12 سال پیش
والد
کامیت
61145d5ff9
3فایلهای تغییر یافته به همراه32 افزوده شده و 68 حذف شده
  1. 0 55
      engine/os/win/Cond.cpp
  2. 0 0
      engine/os/win/OsThread.cpp
  3. 32 13
      engine/os/win/Semaphore.h

+ 0 - 55
engine/os/win/Cond.cpp

@@ -1,55 +0,0 @@
-/*
-Copyright (c) 2013 Daniele Bartolini, Michele Rossi
-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.
-*/
-
-#include "Cond.h"
-
-namespace crown
-{
-
-//-----------------------------------------------------------------------------
-Cond::Cond()
-{
-	InitializeConditionVariable(&m_cond);
-}
-
-//-----------------------------------------------------------------------------
-Cond::~Cond()
-{
-}
-
-//-----------------------------------------------------------------------------
-void Cond::signal()
-{
-	WakeConditionVariable(&m_cond);
-}
-
-//-----------------------------------------------------------------------------
-void Cond::wait(Mutex& mutex)
-{
-	SleepConditionVariableCS(&m_cond, &mutex.m_cs, INFINITE);
-}
-
-} // namespace crown

+ 0 - 0
engine/os/win/Thread.cpp → engine/os/win/OsThread.cpp


+ 32 - 13
engine/os/win/Mutex.cpp → engine/os/win/Semaphore.h

@@ -24,34 +24,53 @@ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 OTHER DEALINGS IN THE SOFTWARE.
 */
 
-#include "Mutex.h"
+#pragma once
+
+#include <windows.h>
+#include <limits.h>
+
 #include "Assert.h"
 
-namespace crown
+class Semaphore
 {
+public:
+
+	Semaphore();
+	~Semaphore();
+
+	void post(uint32_t _count = 1) const;
+	bool wait(int32_t _msecs = -1) const;
+
+private:
+
+	Semaphore(const Semaphore& s); // no copy constructor
+	Semaphore& operator=(const Semaphore& s); // no assignment operator
+
+	HANDLE m_handle;
+};
 
 //-----------------------------------------------------------------------------
-Mutex::Mutex()
+inline Semaphore::Semaphore()
 {
-	InitializeCriticalSection(&m_cs);
+	m_handle = CreateSemaphore(NULL, 0, LONG_MAX, NULL);
+	CE_ASSERT(m_handle != NULL, "Unable to create semaphore!");
 }
 
 //-----------------------------------------------------------------------------
-Mutex::~Mutex()
+inline Semaphore::~Semaphore()
 {
-	DeleteCriticalSection(&m_cs);
+	CloseHandle(m_handle);
 }
 
 //-----------------------------------------------------------------------------
-void Mutex::lock()
+inline void Semaphore::post(uint32_t count = 1) const
 {
-    EnterCriticalSection(&m_cs); 
+	ReleaseSemaphore(m_handle, count, NULL);
 }
 
 //-----------------------------------------------------------------------------
-void Mutex::unlock()
+inline bool Semaphore::wait(int32_t msecs = -1) const
 {
-    LeaveCriticalSection(&m_cs);
-}
-
-} // namespace crown
+	DWORD milliseconds = (0 > msecs) ? INFINITE : msecs;
+	return WAIT_OBJECT_0 == WaitForSingleObject(m_handle, milliseconds);
+}