فهرست منبع

Wrappers to posix threads

Daniele Bartolini 13 سال پیش
والد
کامیت
7055a98b93
2فایلهای تغییر یافته به همراه103 افزوده شده و 3 حذف شده
  1. 36 2
      src/os/OS.h
  2. 67 1
      src/os/linux/LinuxOS.cpp

+ 36 - 2
src/os/OS.h

@@ -29,6 +29,10 @@ OTHER DEALINGS IN THE SOFTWARE.
 #include "List.h"
 #include "Types.h"
 
+#ifdef LINUX
+	#include <pthread.h>
+#endif
+
 namespace crown
 {
 
@@ -44,6 +48,20 @@ const size_t	MAX_PATH_LENGTH = 1024;
 const char		PATH_SEPARATOR = '/';
 
 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;
+};
 #endif
 
 #ifdef WINDOWS
@@ -51,6 +69,9 @@ const size_t	MAX_PATH_LENGTH = 1024;
 const char		PATH_SEPARATOR = '\\';
 
 const size_t	MAX_EVENTS = 512;
+
+const size_t	MAX_THREADS = 16;
+const size_t	MAX_MUTEXES = 16;
 #endif
 
 //-----------------------------------------------------------------------------
@@ -122,12 +143,25 @@ struct OSEvent
 };
 
 //! Pushes @a event into @a event_queue
-void				push_event(OSEventType type, int32_t data_a, int32_t data_b, int32_t data_c, int32_t data_d);
+void			push_event(OSEventType type, int32_t data_a, int32_t data_b, int32_t data_c, int32_t data_d);
 
 //! Returns the event on top of the event_queue	
-OSEvent&			pop_event();
+OSEvent&		pop_event();
+
+//-----------------------------------------------------------------------------
+// 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);
 
 //-----------------------------------------------------------------------------
 //		Networking

+ 67 - 1
src/os/linux/LinuxOS.cpp

@@ -34,6 +34,7 @@ OTHER DEALINGS IN THE SOFTWARE.
 #include <cstdlib>
 #include <sys/time.h>
 #include <time.h>
+#include <pthread.h>
 
 namespace crown
 {
@@ -229,6 +230,71 @@ uint64_t microseconds()
 	return (tmp.tv_sec - base_time.tv_sec) * 1000000 + (tmp.tv_nsec - base_time.tv_nsec) / 1000;
 }
 
+//-----------------------------------------------------------------------------
+void thread_create(ThreadFunction f, void* params, OSThread& thread, const char* name)
+{
+	OSThread tid;
+	tid.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(&tid.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);
+
+	thread = tid;
+}
+
+//-----------------------------------------------------------------------------
+void thread_join(OSThread thread)
+{
+	pthread_join(thread.thread, NULL);
+}
+
+//-----------------------------------------------------------------------------
+void thread_detach(OSThread thread)
+{
+	pthread_detach(thread.thread);
+}
+
+//-----------------------------------------------------------------------------
+void mutex_create(OSMutex& mutex)
+{
+	OSMutex mut;
+
+	pthread_mutex_init(&mut.mutex, NULL);
+
+	mutex = mut;
+}
+
+//-----------------------------------------------------------------------------
+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);
+}
+
 } // namespace os
 } // namespace crown
-