Bläddra i källkod

Implement condition variables

Daniele Bartolini 13 år sedan
förälder
incheckning
7340344016
2 ändrade filer med 54 tillägg och 27 borttagningar
  1. 18 8
      src/os/OS.h
  2. 36 19
      src/os/linux/LinuxOS.cpp

+ 18 - 8
src/os/OS.h

@@ -62,6 +62,12 @@ struct OSMutex
 {
 {
 	pthread_mutex_t mutex;
 	pthread_mutex_t mutex;
 };
 };
+
+struct OSCond
+{
+	pthread_cond_t cond;
+};
+
 #endif
 #endif
 
 
 #ifdef WINDOWS
 #ifdef WINDOWS
@@ -154,14 +160,18 @@ OSEvent&		pop_event();
 
 
 typedef			void* (*ThreadFunction)(void*);
 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			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);
 
 
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
 //		Networking
 //		Networking

+ 36 - 19
src/os/linux/LinuxOS.cpp

@@ -231,10 +231,9 @@ uint64_t microseconds()
 }
 }
 
 
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
-void thread_create(ThreadFunction f, void* params, OSThread& thread, const char* name)
+void thread_create(ThreadFunction f, void* params, OSThread* thread, const char* name)
 {
 {
-	OSThread tid;
-	tid.name = name;
+	thread->name = name;
 
 
 	// Make thread joinable
 	// Make thread joinable
 	pthread_attr_t attr;
 	pthread_attr_t attr;
@@ -242,7 +241,7 @@ void thread_create(ThreadFunction f, void* params, OSThread& thread, const char*
 	pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
 	pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
 
 
 	// Create thread
 	// Create thread
-	int rc = pthread_create(&tid.thread, &attr, f, (void*)params);
+	int rc = pthread_create(&(thread->thread), &attr, f, (void*)params);
 
 
 	if (rc != 0)
 	if (rc != 0)
 	{
 	{
@@ -252,48 +251,66 @@ void thread_create(ThreadFunction f, void* params, OSThread& thread, const char*
 
 
 	// Free memory
 	// Free memory
 	pthread_attr_destroy(&attr);
 	pthread_attr_destroy(&attr);
+}
 
 
-	thread = tid;
+//-----------------------------------------------------------------------------
+void thread_join(OSThread* thread)
+{
+	pthread_join(thread->thread, NULL);
 }
 }
 
 
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
-void thread_join(OSThread thread)
+void thread_detach(OSThread* thread)
 {
 {
-	pthread_join(thread.thread, NULL);
+	pthread_detach(thread->thread);
 }
 }
 
 
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
-void thread_detach(OSThread thread)
+void mutex_create(OSMutex* mutex)
 {
 {
-	pthread_detach(thread.thread);
+	pthread_mutex_init(&mutex->mutex, NULL);
 }
 }
 
 
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
-void mutex_create(OSMutex& mutex)
+void mutex_destroy(OSMutex* mutex)
 {
 {
-	OSMutex mut;
+	pthread_mutex_destroy(&mutex->mutex);
+}
 
 
-	pthread_mutex_init(&mut.mutex, NULL);
+//-----------------------------------------------------------------------------
+void mutex_lock(OSMutex* mutex)
+{
+	pthread_mutex_lock(&mutex->mutex);
+}
 
 
-	mutex = mut;
+//-----------------------------------------------------------------------------
+void mutex_unlock(OSMutex* mutex)
+{
+	pthread_mutex_unlock(&mutex->mutex);
+}
+
+//-----------------------------------------------------------------------------
+void cond_create(OSCond* cond)
+{
+	pthread_cond_init(&cond->cond, NULL);
 }
 }
 
 
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
-void mutex_destroy(OSMutex mutex)
+void cond_destroy(OSCond* cond)
 {
 {
-	pthread_mutex_destroy(&mutex.mutex);
+	pthread_cond_destroy(&cond->cond);
 }
 }
 
 
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
-void mutex_lock(OSMutex mutex)
+void cond_signal(OSCond* cond)
 {
 {
-	pthread_mutex_lock(&mutex.mutex);
+	pthread_cond_signal(&cond->cond);
 }
 }
 
 
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
-void mutex_unlock(OSMutex mutex)
+void cond_wait(OSCond* cond, OSMutex* mutex)
 {
 {
-	pthread_mutex_unlock(&mutex.mutex);
+	pthread_cond_wait(&cond->cond, &mutex->mutex);
 }
 }
 
 
 } // namespace os
 } // namespace os