소스 검색

Thread, Mutex and Cond implemented in win, they needs tests

mikymod 12 년 전
부모
커밋
1f04ce0580
5개의 변경된 파일238개의 추가작업 그리고 1개의 파일을 삭제
  1. 57 0
      src/os/win/Cond.cpp
  2. 55 0
      src/os/win/Cond.h
  3. 67 0
      src/os/win/Mutex.cpp
  4. 56 0
      src/os/win/Mutex.h
  5. 3 1
      src/os/win/Thread.cpp

+ 57 - 0
src/os/win/Cond.cpp

@@ -0,0 +1,57 @@
+/*
+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.
+*/
+
+#pragma once
+
+#include "Cond.h"
+
+namespace crown
+{
+namespace os
+{
+
+Cond::Cond()
+{
+	InitializeConditionVariable(&m_cond);
+}
+
+Cond::~Cond()
+{
+	
+}
+
+void Cond::signal()
+{
+	WakeConditionVariable(&h);
+}
+
+void Cond::wait(Mutex& mutex)
+{
+	SleepConditionVariableSRW(&m_cond, mutex.handle(), INFINITE, 0);
+}
+
+} // namespace os
+} // namespace crown

+ 55 - 0
src/os/win/Cond.h

@@ -0,0 +1,55 @@
+/*
+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.
+*/
+
+#pragma once
+
+#include <windows.h>
+#include "Types.h"
+#include "Mutex.h"
+#include "OS.h"
+
+namespace crown
+{
+namespace os
+{
+
+class Cond
+{
+public:
+
+							Cond();
+							~Cond();
+
+	void					signal();
+	void					wait(Mutex& mutex);
+
+private:
+
+	CONDITION_VARIABLE		m_cond;
+};
+
+} // namespace os
+} // namespace crown

+ 67 - 0
src/os/win/Mutex.cpp

@@ -0,0 +1,67 @@
+/*
+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.
+*/
+
+#pragma once
+
+#include "Mutex.h"
+#include "Assert.h"
+
+namespace crown
+{
+namespace os
+{
+
+//-----------------------------------------------------------------------------
+Mutex::Mutex()
+{
+	m_mutex = CreateMutex(NULL, false, NULL);
+
+	CE_ASSERT(m_mutex != NULL, "Unable to create mutex");
+}
+
+//-----------------------------------------------------------------------------
+Mutex::~Mutex()
+{
+}
+
+//-----------------------------------------------------------------------------
+void Mutex::lock()
+{
+	m_mutex = OpenMutex(NULL, false, NULL);
+
+	CE_ASSERT(m_mutex != NULL, "Unable to lock mutex");
+}
+
+//-----------------------------------------------------------------------------
+void Mutex::unlock()
+{
+	bool released = ReleaseMutex(m_mutex);
+
+	CE_ASSERT(released, "Unable to unlock mutex");
+}
+
+} // namespace os
+} // namespace crown

+ 56 - 0
src/os/win/Mutex.h

@@ -0,0 +1,56 @@
+/*
+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.
+*/
+
+#pragma once
+
+#include <windows.h>
+#include "Types.h"
+#include "OS.h"
+
+namespace crown
+{
+namespace os
+{
+
+class Mutex
+{
+public:
+
+						Mutex();
+						~Mutex();
+
+	void				lock();
+	void				unlock();
+
+private:
+
+	HANDLE				m_mutex;
+
+	friend class		Cond;
+};
+
+} // namespace os
+} // namespace crown

+ 3 - 1
src/os/win/Thread.cpp

@@ -28,6 +28,7 @@ OTHER DEALINGS IN THE SOFTWARE.
 
 #include "Thread.h"
 #include "Assert.h"
+#include "StringUtils.h"
 
 namespace crown
 {
@@ -40,12 +41,13 @@ Thread::Thread(os::ThreadFunction f, void* params, const char* name)
 	m_thread = CreateThread(NULL, 0, f, params, 0, NULL);
 
 	CE_ASSERT(m_thread != NULL, "Unable to create thread");
+
+	string::strcpy(m_name, name);
 }
 
 //-----------------------------------------------------------------------------
 Thread::~Thread()
 {
-	detach();
 }
 
 //-----------------------------------------------------------------------------