Changeset: 6d90febdc9bc for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB/rev/6d90febdc9bc
Modified Files:
        gdk/gdk_system.c
        gdk/gdk_system.h
Branch: default
Log Message:

Add condition variables to gdk_system


diffs (172 lines):

diff --git a/gdk/gdk_system.c b/gdk/gdk_system.c
--- a/gdk/gdk_system.c
+++ b/gdk/gdk_system.c
@@ -177,6 +177,8 @@ GDKlockstatistics(int what)
 
 #endif /* LOCK_STATS */
 
+static void MT_thread_setcondwait(MT_Cond *cond);
+
 #if !defined(HAVE_PTHREAD_H) && defined(WIN32)
 static struct winthread {
        struct winthread *next;
@@ -186,6 +188,7 @@ static struct winthread {
        void *data;
        MT_Lock *lockwait;      /* lock we're waiting for */
        MT_Sema *semawait;      /* semaphore we're waiting for */
+       MT_Cond *condwait;      /* condition variable we're waiting for */
        struct winthread *joinwait; /* process we are joining with */
        const char *working;    /* what we're currently doing */
        char algorithm[512];    /* the algorithm used in the last operation */
@@ -213,6 +216,7 @@ dump_threads(void)
                                        w->threadname,
                                        w->lockwait ? w->lockwait->name :
                                        w->semawait ? w->semawait->name :
+                                       w->condwait ? w->condwait->name :
                                        w->joinwait ? w->joinwait->threadname :
                                        "nothing",
                                        ATOMIC_GET(&w->exited) ? "exiting" :
@@ -328,6 +332,17 @@ MT_thread_setsemawait(MT_Sema *sema)
                w->semawait = sema;
 }
 
+static void
+MT_thread_setcondwait(MT_Cond *cond)
+{
+       if (threadslot == TLS_OUT_OF_INDEXES)
+               return;
+       struct winthread *w = TlsGetValue(threadslot);
+
+       if (w)
+               w->condwait = cond;
+}
+
 void
 MT_thread_setworking(const char *work)
 {
@@ -591,6 +606,7 @@ static struct posthread {
        void *data;
        MT_Lock *lockwait;      /* lock we're waiting for */
        MT_Sema *semawait;      /* semaphore we're waiting for */
+       MT_Cond *condwait;      /* condition variable we're waiting for */
        struct posthread *joinwait; /* process we are joining with */
        const char *working;    /* what we're currently doing */
        char algorithm[512];    /* the algorithm used in the last operation */
@@ -623,6 +639,7 @@ dump_threads(void)
                                        p->threadname,
                                        p->lockwait ? p->lockwait->name :
                                        p->semawait ? p->semawait->name :
+                                       p->condwait ? p->condwait->name :
                                        p->joinwait ? p->joinwait->threadname :
                                        "nothing",
                                        ATOMIC_GET(&p->exited) ? "exiting" :
@@ -738,6 +755,17 @@ MT_thread_setsemawait(MT_Sema *sema)
 }
 
 void
+MT_thread_setcondwait(MT_Cond *cond)
+{
+       if (!thread_initialized)
+               return;
+       struct posthread *p = pthread_getspecific(threadkey);
+
+       if (p)
+               p->condwait = cond;
+}
+
+void
 MT_thread_setworking(const char *work)
 {
        if (!thread_initialized)
@@ -1108,3 +1136,57 @@ MT_check_nr_cores(void)
 
        return ncpus;
 }
+
+
+void
+MT_cond_init(MT_Cond *cond)
+{
+#if !defined(HAVE_PTHREAD_H) && defined(WIN32)
+       InitializeConditionVariable(&cond->cv);
+#else
+       pthread_cond_init(&cond->cv, NULL);
+#endif
+}
+
+
+void
+MT_cond_destroy(MT_Cond *cond)
+{
+#if !defined(HAVE_PTHREAD_H) && defined(WIN32)
+       /* no need */
+#else
+       pthread_cond_destroy(&cond->cv);
+#endif
+}
+
+void
+MT_cond_wait(MT_Cond *cond, MT_Lock *lock)
+{
+       MT_thread_setcondwait(cond);
+#if !defined(HAVE_PTHREAD_H) && defined(WIN32)
+       SleepConditionVariableCS(&cond->cv, &lock->lock);
+#else
+       pthread_cond_wait(&cond->cv, &lock->lock);
+#endif
+       MT_thread_setcondwait(NULL);
+}
+
+void
+MT_cond_signal(MT_Cond *cond)
+{
+#if !defined(HAVE_PTHREAD_H) && defined(WIN32)
+       WakeConditionVariable(&cond->cv);
+#else
+       pthread_cond_signal(&cond->cv);
+#endif
+}
+
+void
+MT_cond_broadcast(MT_Cond *cond)
+{
+#if !defined(HAVE_PTHREAD_H) && defined(WIN32)
+       WakeAllConditionVariable(&cond->cv);
+#else
+       pthread_cond_broadcast(&cond->cv);
+#endif
+}
diff --git a/gdk/gdk_system.h b/gdk/gdk_system.h
--- a/gdk/gdk_system.h
+++ b/gdk/gdk_system.h
@@ -710,4 +710,30 @@ gdk_export const char *MT_thread_getalgo
 
 gdk_export int MT_check_nr_cores(void);
 
+/*
+ * @ Condition Variable API
+ */
+
+typedef struct MT_Cond {
+#if !defined(HAVE_PTHREAD_H) && defined(WIN32)
+       CONDITION_VARIABLE cv;
+#else
+       pthread_cond_t cv;
+#endif
+       char name[MT_NAME_LEN];
+} MT_Cond;
+
+#if !defined(HAVE_PTHREAD_H) && defined(WIN32)
+#  define MT_COND_INITIALIZER(N) { .cv = CONDITION_VARIABLE_INIT, .name = #N }
+#else
+#  define MT_COND_INITIALIZER(N) { .cv = PTHREAD_COND_INITIALIZER, .name = #N }
+#endif
+
+gdk_export void MT_cond_init(MT_Cond *cond);
+gdk_export void MT_cond_destroy(MT_Cond *cond);
+gdk_export void MT_cond_wait(MT_Cond *cond, MT_Lock *lock);
+gdk_export void MT_cond_signal(MT_Cond *cond);
+gdk_export void MT_cond_broadcast(MT_Cond *cond);
+
+
 #endif /*_GDK_SYSTEM_H_*/
_______________________________________________
checkin-list mailing list -- checkin-list@monetdb.org
To unsubscribe send an email to checkin-list-le...@monetdb.org

Reply via email to