Explorar el Código

Add basic threading support

Daniele Bartolini hace 13 años
padre
commit
35719a3fde
Se han modificado 6 ficheros con 227 adiciones y 0 borrados
  1. 14 0
      src/CMakeLists.txt
  2. 4 0
      src/Crown.h
  3. 55 0
      src/core/threads/Mutex.cpp
  4. 49 0
      src/core/threads/Mutex.h
  5. 56 0
      src/core/threads/Thread.cpp
  6. 49 0
      src/core/threads/Thread.h

+ 14 - 0
src/CMakeLists.txt

@@ -168,6 +168,16 @@ set (COMPRESSORS_HEADERS
 	core/compressors/ZipCompressor.h
 	core/compressors/ZipCompressor.h
 )
 )
 
 
+set (THREADS_HEADERS
+	core/threads/Thread.h
+	core/threads/Mutex.h
+)
+
+set (THREADS_SRC
+	core/threads/Thread.cpp
+	core/threads/Mutex.cpp
+)
+
 set (INPUT_SRC
 set (INPUT_SRC
 	input/EventDispatcher.cpp
 	input/EventDispatcher.cpp
 	input/InputManager.cpp
 	input/InputManager.cpp
@@ -268,6 +278,10 @@ set (SOURCES
 	${MEM_HEADERS}
 	${MEM_HEADERS}
 	${COMPRESSORS_SRC}
 	${COMPRESSORS_SRC}
 	${COMPRESSORS_HEADERS}
 	${COMPRESSORS_HEADERS}
+
+	${THREADS_SRC}
+	${THREADS_HEADERS}
+
 	${INPUT_SRC}
 	${INPUT_SRC}
 	${INPUT_HEADERS}
 	${INPUT_HEADERS}
 
 

+ 4 - 0
src/Crown.h

@@ -83,6 +83,10 @@ OTHER DEALINGS IN THE SOFTWARE.
 #include "TextReader.h"
 #include "TextReader.h"
 #include "TextWriter.h"
 #include "TextWriter.h"
 
 
+// Core/Threads
+#include "Thread.h"
+#include "Mutex.h"
+
 // Engine
 // Engine
 #include "Camera.h"
 #include "Camera.h"
 #include "Device.h"
 #include "Device.h"

+ 55 - 0
src/core/threads/Mutex.cpp

@@ -0,0 +1,55 @@
+/*
+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 "Mutex.h"
+
+namespace crown
+{
+
+//-----------------------------------------------------------------------------
+Mutex::Mutex()
+{
+	os::mutex_create(m_mutex);
+}
+
+//-----------------------------------------------------------------------------
+Mutex::~Mutex()
+{
+	os::mutex_destroy(m_mutex);
+}
+
+//-----------------------------------------------------------------------------
+void Mutex::lock()
+{
+	os::mutex_lock(m_mutex);
+}
+
+//-----------------------------------------------------------------------------
+void Mutex::unlock()
+{
+	os::mutex_unlock(m_mutex);
+}
+
+} // namespace crown

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

@@ -0,0 +1,49 @@
+/*
+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 "OS.h"
+
+namespace crown
+{
+
+class Mutex
+{
+public:
+
+					Mutex();
+					~Mutex();
+
+	void			lock();
+	void			unlock();
+
+private:
+
+	os::OSMutex		m_mutex;
+};
+
+} // namespace crown

+ 56 - 0
src/core/threads/Thread.cpp

@@ -0,0 +1,56 @@
+/*
+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 "Thread.h"
+
+namespace crown
+{
+
+//-----------------------------------------------------------------------------
+Thread::Thread(os::ThreadFunction f, void* args, const char* name)
+{
+	memset(&m_thread, 0, sizeof(os::OSThread));
+
+	os::thread_create(f, args, m_thread, name);
+}
+
+//-----------------------------------------------------------------------------
+Thread::~Thread()
+{
+}
+
+//-----------------------------------------------------------------------------
+void Thread::join()
+{
+	os::thread_join(m_thread);
+}
+
+//-----------------------------------------------------------------------------
+void Thread::detach()
+{
+	os::thread_detach(m_thread);
+}
+
+} // namespace crown

+ 49 - 0
src/core/threads/Thread.h

@@ -0,0 +1,49 @@
+/*
+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 "OS.h"
+
+namespace crown
+{
+
+class Thread
+{
+public:
+
+					Thread(os::ThreadFunction f, void* args, const char* name);
+					~Thread();
+
+	void			join();
+	void			detach();
+
+private:
+
+	os::OSThread	m_thread;
+};
+
+} // namespace crown