Author: cazfi
Date: Wed Dec 17 20:37:43 2014
New Revision: 27333

URL: http://svn.gna.org/viewcvs/freeciv?rev=27333&view=rev
Log:
Added currently-disabled fc_thread implementation based on C11 threads.

See patch #5560

Modified:
    trunk/utility/fcthread.c
    trunk/utility/fcthread.h

Modified: trunk/utility/fcthread.c
URL: 
http://svn.gna.org/viewcvs/freeciv/trunk/utility/fcthread.c?rev=27333&r1=27332&r2=27333&view=diff
==============================================================================
--- trunk/utility/fcthread.c    (original)
+++ trunk/utility/fcthread.c    Wed Dec 17 20:37:43 2014
@@ -22,7 +22,122 @@
 
 #include "fcthread.h"
 
-#ifdef FREECIV_HAVE_PTHREAD
+#ifdef FREECIV_HAVE_C11_THREADS
+
+struct fc_thread_wrap_data {
+  void *arg;
+  void (*func)(void *arg);
+};
+
+/**********************************************************************
+  Wrapper which fingerprint matches one required by pthread_create().
+  Calls function which matches fingerprint required by fc_thread_start()
+***********************************************************************/
+static int fc_thread_wrapper(void *arg)
+{
+  struct fc_thread_wrap_data *data = (struct fc_thread_wrap_data *) arg;
+
+  data->func(data->arg);
+
+  free(data);
+
+  return EXIT_SUCCESS;
+}
+
+/**********************************************************************
+  Create new thread
+***********************************************************************/
+int fc_thread_start(fc_thread *thread, void (*function) (void *arg),
+                    void *arg)
+{
+  int ret;
+
+  /* Freed by child thread once it's finished with data */
+  struct fc_thread_wrap_data *data = fc_malloc(sizeof(*data));
+
+  data->arg = arg;
+  data->func = function;
+
+  ret = thrd_create(thread, &fc_thread_wrapper, data);
+
+  return ret != thrd_success;
+}
+
+/**********************************************************************
+  Wait for thread to finish
+***********************************************************************/
+void fc_thread_wait(fc_thread *thread)
+{
+  int *return_value = NULL;
+
+  thrd_join(*thread, return_value);
+}
+
+/**********************************************************************
+  Initialize mutex
+***********************************************************************/
+void fc_init_mutex(fc_mutex *mutex)
+{
+  mtx_init(mutex, mtx_plain|mtx_recursive);
+}
+
+/**********************************************************************
+  Destroy mutex
+***********************************************************************/
+void fc_destroy_mutex(fc_mutex *mutex)
+{
+  mtx_destroy(mutex);
+}
+
+/**********************************************************************
+  Lock mutex
+***********************************************************************/
+void fc_allocate_mutex(fc_mutex *mutex)
+{
+  mtx_lock(mutex);
+}
+
+/**********************************************************************
+  Release mutex
+***********************************************************************/
+void fc_release_mutex(fc_mutex *mutex)
+{
+  mtx_unlock(mutex);
+}
+
+/**********************************************************************
+  Initialize condition
+***********************************************************************/
+void fc_thread_cond_init(fc_thread_cond *cond)
+{
+  cnd_init(cond);
+}
+
+/**********************************************************************
+  Destroy condition
+***********************************************************************/
+void fc_thread_cond_destroy(fc_thread_cond *cond)
+{
+  cnd_destroy(cond);
+}
+
+/**********************************************************************
+  Wait for condition to be fulfilled
+***********************************************************************/
+void fc_thread_cond_wait(fc_thread_cond *cond, fc_mutex *mutex)
+{
+  cnd_wait(cond, mutex);
+}
+
+/**********************************************************************
+  Signal other thread to continue on fulfilled condition
+***********************************************************************/
+void fc_thread_cond_signal(fc_thread_cond *cond)
+{
+  cnd_signal(cond);
+}
+
+#elif defined(FREECIV_HAVE_PTHREAD)
 
 struct fc_thread_wrap_data {
   void *arg;

Modified: trunk/utility/fcthread.h
URL: 
http://svn.gna.org/viewcvs/freeciv/trunk/utility/fcthread.h?rev=27333&r1=27332&r2=27333&view=diff
==============================================================================
--- trunk/utility/fcthread.h    (original)
+++ trunk/utility/fcthread.h    Wed Dec 17 20:37:43 2014
@@ -24,7 +24,17 @@
 /* utility */
 #include "support.h" /* bool */
 
-#ifdef FREECIV_HAVE_PTHREAD
+
+#ifdef FREECIV_HAVE_C11_THREADS
+
+#include <threads.h>
+
+#define fc_thread      thrd_t
+#define fc_mutex       mtx_t
+#define fc_thread_cond cnd_t
+
+#elif defined(FREECIV_HAVE_PTHREAD)
+
 #include <pthread.h>
 
 #define fc_thread      pthread_t


_______________________________________________
Freeciv-commits mailing list
Freeciv-commits@gna.org
https://mail.gna.org/listinfo/freeciv-commits

Reply via email to