瀏覽代碼

Implement POSIX threads

Daniele Bartolini 12 年之前
父節點
當前提交
700888ac61
共有 9 個文件被更改,包括 230 次插入210 次删除
  1. 0 39
      src/os/OS.h
  2. 6 0
      src/os/linux/CMakeLists.txt
  3. 0 83
      src/os/linux/LinuxOS.cpp
  4. 60 0
      src/os/posix/Cond.cpp
  5. 6 27
      src/os/posix/Cond.h
  6. 61 0
      src/os/posix/Mutex.cpp
  7. 11 34
      src/os/posix/Mutex.h
  8. 76 0
      src/os/posix/Thread.cpp
  9. 10 27
      src/os/posix/Thread.h

+ 0 - 39
src/os/OS.h

@@ -30,10 +30,6 @@ OTHER DEALINGS IN THE SOFTWARE.
 #include <cstdarg>
 #include "Types.h"
 
-#ifdef LINUX
-	#include <pthread.h>
-#endif
-
 namespace crown
 {
 
@@ -50,23 +46,6 @@ const size_t	MAX_EVENTS = 512;
 
 const size_t	MAX_THREADS = 16;
 const size_t	MAX_MUTEXES = 16;
-
-struct OSThread
-{
-	pthread_t	thread;
-	const char*	name;
-};
-
-struct OSMutex
-{
-	pthread_mutex_t mutex;
-};
-
-struct OSCond
-{
-	pthread_cond_t cond;
-};
-
 #endif
 
 #ifdef WINDOWS
@@ -191,24 +170,6 @@ void*			open_library(const char* path);
 void			close_library(void* library);
 void*			lookup_symbol(void* library, const char* name);
 
-//-----------------------------------------------------------------------------
-// Threads
-//-----------------------------------------------------------------------------
-typedef			void* (*ThreadFunction)(void*);
-
-void			thread_create(ThreadFunction f, void* params, OSThread* thread, const char* name);
-void			thread_join(OSThread* thread);
-void			thread_detach(OSThread* thread);
-
-void			mutex_create(OSMutex* mutex);
-void			mutex_destroy(OSMutex* mutex);
-void			mutex_lock(OSMutex* mutex);
-void			mutex_unlock(OSMutex* mutex);
-void			cond_create(OSCond* cond);
-void			cond_destroy(OSCond* cond);
-void			cond_signal(OSCond* cond);
-void			cond_wait(OSCond* cond, OSMutex* mutex);
-
 } // namespace os
 } // namespace crown
 

+ 6 - 0
src/os/linux/CMakeLists.txt

@@ -9,12 +9,18 @@ set (LINUX_SRC
 	../posix/TCPSocket.cpp
 	../posix/UDPSocket.cpp	
 	../posix/File.cpp
+	../posix/Thread.cpp
+	../posix/Mutex.cpp
+	../posix/Cond.cpp
 )
 
 set (LINUX_HEADERS
 	../posix/TCPSocket.h
 	../posix/UDPSocket.h
 	../posix/File.h
+	../posix/Thread.h
+	../posix/Mutex.h
+	../posix/Cond.h
 )
 
 link_libraries(X11 Xrandr pthread)

+ 0 - 83
src/os/linux/LinuxOS.cpp

@@ -301,88 +301,5 @@ void* lookup_symbol(void* library, const char* name)
 	return symbol;
 }
 
-//-----------------------------------------------------------------------------
-void thread_create(ThreadFunction f, void* params, OSThread* thread, const char* name)
-{
-	thread->name = name;
-
-	// Make thread joinable
-	pthread_attr_t attr;
-	pthread_attr_init(&attr);
-	pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
-
-	// Create thread
-	int rc = pthread_create(&(thread->thread), &attr, f, (void*)params);
-
-	if (rc != 0)
-	{
-		os::printf("OS: ERROR: Unable to create the thread '%s' Error code: %d\n", name, rc);
-		exit(-1);
-	}
-
-	// Free memory
-	pthread_attr_destroy(&attr);
-}
-
-//-----------------------------------------------------------------------------
-void thread_join(OSThread* thread)
-{
-	pthread_join(thread->thread, NULL);
-}
-
-//-----------------------------------------------------------------------------
-void thread_detach(OSThread* thread)
-{
-	pthread_detach(thread->thread);
-}
-
-//-----------------------------------------------------------------------------
-void mutex_create(OSMutex* mutex)
-{
-	pthread_mutex_init(&mutex->mutex, NULL);
-}
-
-//-----------------------------------------------------------------------------
-void mutex_destroy(OSMutex* mutex)
-{
-	pthread_mutex_destroy(&mutex->mutex);
-}
-
-//-----------------------------------------------------------------------------
-void mutex_lock(OSMutex* mutex)
-{
-	pthread_mutex_lock(&mutex->mutex);
-}
-
-//-----------------------------------------------------------------------------
-void mutex_unlock(OSMutex* mutex)
-{
-	pthread_mutex_unlock(&mutex->mutex);
-}
-
-//-----------------------------------------------------------------------------
-void cond_create(OSCond* cond)
-{
-	pthread_cond_init(&cond->cond, NULL);
-}
-
-//-----------------------------------------------------------------------------
-void cond_destroy(OSCond* cond)
-{
-	pthread_cond_destroy(&cond->cond);
-}
-
-//-----------------------------------------------------------------------------
-void cond_signal(OSCond* cond)
-{
-	pthread_cond_signal(&cond->cond);
-}
-
-//-----------------------------------------------------------------------------
-void cond_wait(OSCond* cond, OSMutex* mutex)
-{
-	pthread_cond_wait(&cond->cond, &mutex->mutex);
-}
-
 } // namespace os
 } // namespace crown

+ 60 - 0
src/os/posix/Cond.cpp

@@ -0,0 +1,60 @@
+/*
+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
+{
+namespace os
+{
+
+//-----------------------------------------------------------------------------
+Cond::Cond()
+{
+	memset(&m_cond, 0, sizeof(pthread_cond_t));
+
+	pthread_cond_init(&m_cond, NULL);
+}
+
+//-----------------------------------------------------------------------------
+Cond::~Cond()
+{
+	pthread_cond_destroy(&m_cond);
+}
+
+//-----------------------------------------------------------------------------
+void Cond::signal()
+{
+	pthread_cond_signal(&m_cond);
+}
+
+//-----------------------------------------------------------------------------
+void Cond::wait(Mutex& mutex)
+{
+	pthread_cond_wait(&m_cond, &(mutex.m_mutex));
+}
+
+} // namespace os
+} // namespace crown

+ 6 - 27
src/os/posix/Cond.h

@@ -25,12 +25,16 @@ OTHER DEALINGS IN THE SOFTWARE.
 
 #pragma once
 
+#include <pthread.h>
+
 #include "Types.h"
 #include "Mutex.h"
 #include "OS.h"
 
 namespace crown
 {
+namespace os
+{
 
 class Cond
 {
@@ -44,33 +48,8 @@ public:
 
 private:
 
-	os::OSCond		m_cond;
+	pthread_cond_t	m_cond;
 };
 
-//-----------------------------------------------------------------------------
-inline Cond::Cond()
-{
-	memset(&m_cond, 0, sizeof(os::OSCond));
-
-	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 os
 } // namespace crown

+ 61 - 0
src/os/posix/Mutex.cpp

@@ -0,0 +1,61 @@
+/*
+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
+{
+namespace os
+{
+
+//-----------------------------------------------------------------------------
+Mutex::Mutex()
+{
+	memset(&m_mutex, 0, sizeof(pthread_mutex_t));
+
+	pthread_mutex_init(&m_mutex, NULL);
+}
+
+//-----------------------------------------------------------------------------
+Mutex::~Mutex()
+{
+	pthread_mutex_destroy(&m_mutex);
+}
+
+//-----------------------------------------------------------------------------
+void Mutex::lock()
+{
+	pthread_mutex_lock(&m_mutex);
+}
+
+//-----------------------------------------------------------------------------
+void Mutex::unlock()
+{
+	pthread_mutex_unlock(&m_mutex);
+}
+
+} // namespace os
+} // namespace crown
+

+ 11 - 34
src/os/posix/Mutex.h

@@ -25,55 +25,32 @@ OTHER DEALINGS IN THE SOFTWARE.
 
 #pragma once
 
+#include <pthread.h>
+
 #include "Types.h"
 #include "OS.h"
 
 namespace crown
 {
+namespace os
+{
 
 class Mutex
 {
 public:
 
-					Mutex();
-					~Mutex();
+						Mutex();
+						~Mutex();
 
-	void			lock();
-	void			unlock();
+	void				lock();
+	void				unlock();
 
 private:
 
-	os::OSMutex		m_mutex;
-
-private:
+	pthread_mutex_t		m_mutex;
 
-	friend class	Cond;
+	friend class		Cond;
 };
 
-//-----------------------------------------------------------------------------
-inline Mutex::Mutex()
-{
-	memset(&m_mutex, 0, sizeof(os::OSMutex));
-
-	os::mutex_create(&m_mutex);
-}
-
-//-----------------------------------------------------------------------------
-inline Mutex::~Mutex()
-{
-	os::mutex_destroy(&m_mutex);
-}
-
-//-----------------------------------------------------------------------------
-inline void Mutex::lock()
-{
-	os::mutex_lock(&m_mutex);
-}
-
-//-----------------------------------------------------------------------------
-inline void Mutex::unlock()
-{
-	os::mutex_unlock(&m_mutex);
-}
-
+} // namespace os
 } // namespace crown

+ 76 - 0
src/os/posix/Thread.cpp

@@ -0,0 +1,76 @@
+/*
+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"
+#include <stdlib.h>
+
+namespace crown
+{
+namespace os
+{
+
+//-----------------------------------------------------------------------------
+Thread::Thread(os::ThreadFunction f, void* params, const char* name) :
+	m_name(name)
+{
+	memset(&m_thread, 0, sizeof(pthread_t));
+
+	// Make thread joinable
+	pthread_attr_t attr;
+	pthread_attr_init(&attr);
+	pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
+
+	// Create thread
+	int rc = pthread_create(&m_thread, &attr, f, (void*)params);
+
+	if (rc != 0)
+	{
+		os::printf("Unable to create the thread '%s' Error code: %d\n", name, rc);
+		exit(-1);
+	}
+
+	// Free attr memory
+	pthread_attr_destroy(&attr);
+}
+
+//-----------------------------------------------------------------------------
+Thread::~Thread()
+{
+}
+
+//-----------------------------------------------------------------------------
+void Thread::join()
+{
+	pthread_join(m_thread, NULL);
+}
+
+//-----------------------------------------------------------------------------
+void Thread::detach()
+{
+	pthread_detach(m_thread);
+}
+
+} // namespace os
+} // namespace crown

+ 10 - 27
src/os/posix/Thread.h

@@ -25,17 +25,23 @@ OTHER DEALINGS IN THE SOFTWARE.
 
 #pragma once
 
+#include <pthread.h>
+
 #include "Types.h"
 #include "OS.h"
 
 namespace crown
 {
+namespace os
+{
+
+typedef void* (*ThreadFunction)(void*);
 
 class Thread
 {
 public:
 
-					Thread(os::ThreadFunction f, void* args, const char* name);
+					Thread(os::ThreadFunction f, void* params, const char* name);
 					~Thread();
 
 	void			join();
@@ -43,32 +49,9 @@ public:
 
 private:
 
-	os::OSThread	m_thread;
+	pthread_t		m_thread;
+	const char*		m_name;
 };
 
-//-----------------------------------------------------------------------------
-inline 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);
-}
-
-//-----------------------------------------------------------------------------
-inline Thread::~Thread()
-{
-}
-
-//-----------------------------------------------------------------------------
-inline void Thread::join()
-{
-	os::thread_join(&m_thread);
-}
-
-//-----------------------------------------------------------------------------
-inline void Thread::detach()
-{
-	os::thread_detach(&m_thread);
-}
-
+} // namespace os
 } // namespace crown