Changeset: 7b9c41026683 for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=7b9c41026683
Modified Files:
        clients/Tests/MAL-signatures.stable.out
        clients/Tests/MAL-signatures.stable.out.int128
        clients/Tests/exports.stable.out
        gdk/gdk_bbp.c
        gdk/gdk_posix.h
        gdk/gdk_private.h
        gdk/gdk_system.c
        gdk/gdk_system.h
        gdk/gdk_utils.c
        gdk/gdk_utils.h
        monetdb5/extras/rapi/rapi.c
        monetdb5/mal/mal.c
        monetdb5/mal/mal_client.c
        monetdb5/mal/mal_client.h
        monetdb5/mal/mal_dataflow.c
        monetdb5/mal/mal_profiler.c
        monetdb5/mal/mal_readline.c
        monetdb5/mal/mal_resource.c
        monetdb5/mal/mal_scenario.c
        monetdb5/modules/kernel/mmath.c
        monetdb5/modules/mal/mal_mapi.c
        monetdb5/modules/mal/tablet.c
        monetdb5/modules/mal/tablet.h
        monetdb5/modules/mal/wlc.c
        monetdb5/modules/mal/wlc.mal
        monetdb5/optimizer/opt_pipes.c
        sql/backends/monet5/UDF/pyapi/pyapi.c
        sql/backends/monet5/sql_scenario.c
        sql/backends/monet5/vaults/lidar/lidar.c
        sql/common/sql_list.c
        sql/storage/store.c
        tools/mserver/mserver5.c
Branch: Apr2019
Log Message:

Cleanup of locks and semphores.
- Make sure all locks are initialized exactly once.
- Make having lock statistics possible even in non-debug mode.
- Also maintain lock statistics (if enabled) when using "native" locking.
- Get rid of fake pthread functions.
- Give locks and semaphores more unique names.


diffs (truncated from 1630 to 300 lines):

diff --git a/clients/Tests/MAL-signatures.stable.out 
b/clients/Tests/MAL-signatures.stable.out
--- a/clients/Tests/MAL-signatures.stable.out
+++ b/clients/Tests/MAL-signatures.stable.out
@@ -12120,6 +12120,7 @@ Ready.
 [ "wlc",       "init", "pattern wlc.init():void ",     "WLCinitCmd;",  "Test 
for running as master"    ]
 [ "wlc",       "master",       "pattern wlc.master():void ",   "WLCmaster;",   
"Activate the workload-capture-replay process"  ]
 [ "wlc",       "master",       "pattern wlc.master(path:str):void ",   
"WLCmaster;",   "Activate the workload-capture-replay process. Use a different 
location for the logs."  ]
+[ "wlc",       "prelude",      "command wlc.prelude():void ",  "WLCprelude;",  
"Initialization of wlc module"  ]
 [ "wlc",       "query",        "pattern wlc.query(q:str):void ",       
"WLCquery;",    "Keep the queries for replay."  ]
 [ "wlc",       "rename_column",        "pattern wlc.rename_column(sname:str, 
tname:str, cname:str, newnme:str):void ", "WLCgeneric;",  "Catalog operation 
rename_column"       ]
 [ "wlc",       "rename_schema",        "pattern wlc.rename_schema(sname:str, 
newnme:str):void ",       "WLCgeneric;",  "Catalog operation rename_schema"     
  ]
diff --git a/clients/Tests/MAL-signatures.stable.out.int128 
b/clients/Tests/MAL-signatures.stable.out.int128
--- a/clients/Tests/MAL-signatures.stable.out.int128
+++ b/clients/Tests/MAL-signatures.stable.out.int128
@@ -16548,6 +16548,7 @@ Ready.
 [ "wlc",       "init", "pattern wlc.init():void ",     "WLCinitCmd;",  "Test 
for running as master"    ]
 [ "wlc",       "master",       "pattern wlc.master():void ",   "WLCmaster;",   
"Activate the workload-capture-replay process"  ]
 [ "wlc",       "master",       "pattern wlc.master(path:str):void ",   
"WLCmaster;",   "Activate the workload-capture-replay process. Use a different 
location for the logs."  ]
+[ "wlc",       "prelude",      "command wlc.prelude():void ",  "WLCprelude;",  
"Initialization of wlc module"  ]
 [ "wlc",       "query",        "pattern wlc.query(q:str):void ",       
"WLCquery;",    "Keep the queries for replay."  ]
 [ "wlc",       "rename_column",        "pattern wlc.rename_column(sname:str, 
tname:str, cname:str, newnme:str):void ", "WLCgeneric;",  "Catalog operation 
rename_column"       ]
 [ "wlc",       "rename_schema",        "pattern wlc.rename_schema(sname:str, 
newnme:str):void ",       "WLCgeneric;",  "Catalog operation rename_schema"     
  ]
diff --git a/clients/Tests/exports.stable.out b/clients/Tests/exports.stable.out
--- a/clients/Tests/exports.stable.out
+++ b/clients/Tests/exports.stable.out
@@ -460,19 +460,6 @@ int mo_system_config(opt **Set, int setl
 const oid oid_nil;
 DIR *opendir(const char *dirname);
 void print_trace(void);
-void pthread_mutex_destroy(pthread_mutex_t *);
-void pthread_mutex_init(pthread_mutex_t *, const pthread_mutexattr_t *);
-int pthread_mutex_lock(pthread_mutex_t *);
-int pthread_mutex_trylock(pthread_mutex_t *);
-int pthread_mutex_unlock(pthread_mutex_t *);
-void pthread_sema_destroy(pthread_sema_t *s);
-void pthread_sema_destroy(pthread_sema_t *s);
-void pthread_sema_down(pthread_sema_t *s);
-void pthread_sema_down(pthread_sema_t *s);
-void pthread_sema_init(pthread_sema_t *s, int flag, int nresources);
-void pthread_sema_init(pthread_sema_t *s, int flag, int nresources);
-void pthread_sema_up(pthread_sema_t *s);
-void pthread_sema_up(pthread_sema_t *s);
 ssize_t ptrFromStr(const char *src, size_t *len, ptr **dst, bool external);
 ssize_t ptrToStr(str *dst, size_t *len, const ptr *src, bool external);
 const ptr ptr_nil;
@@ -1993,6 +1980,7 @@ str WLCgetmastertick(Client cntxt, MalBl
 str WLCinit(void);
 str WLCinitCmd(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci);
 str WLCmaster(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci);
+str WLCprelude(void *ret);
 str WLCquery(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci);
 void WLCreadConfig(FILE *fd);
 void WLCreset(void);
@@ -2300,6 +2288,7 @@ void initNamespace(void);
 void initParser(void);
 void initProfiler(void);
 str initQlog(void);
+void initTablet(void);
 int initTrace(void);
 int inlineMALblock(MalBlkPtr mb, int pc, MalBlkPtr mc);
 str inplaceRef;
diff --git a/gdk/gdk_bbp.c b/gdk/gdk_bbp.c
--- a/gdk/gdk_bbp.c
+++ b/gdk/gdk_bbp.c
@@ -1026,9 +1026,14 @@ BBPinit(void)
                GDKfatal("BBPinit: GDKmalloc failed\n");
 
 #ifdef NEED_MT_LOCK_INIT
-       MT_lock_init(&GDKunloadLock, "GDKunloadLock");
-       ATOMIC_INIT(BBPsizeLock);
-       BATsample(NULL, 0);     /* initializes the lock */
+       static initialized = false;
+       if (!initialized) {
+               /* only do this once */
+               MT_lock_init(&GDKunloadLock, "GDKunloadLock");
+               ATOMIC_INIT(BBPsizeLock);
+               BATsample(NULL, 0);     /* initializes the lock */
+               initialized = true;
+       }
 #endif
 
        if (BBPfarms[0].dirname == NULL) {
diff --git a/gdk/gdk_posix.h b/gdk/gdk_posix.h
--- a/gdk/gdk_posix.h
+++ b/gdk/gdk_posix.h
@@ -32,12 +32,6 @@
 #endif
 
 /*
- * @- locking, sleep
- */
-
-gdk_export void MT_sleep_ms(unsigned int ms);
-
-/*
  * @- virtual memory
  */
 #define MT_VMUNITLOG   16
diff --git a/gdk/gdk_private.h b/gdk/gdk_private.h
--- a/gdk/gdk_private.h
+++ b/gdk/gdk_private.h
@@ -315,7 +315,7 @@ typedef struct {
        MT_Lock swap;
        MT_Lock hash;
        MT_Lock imprints;
-#ifndef NDEBUG
+#ifdef LOCK_STATS
        char swapname[16];
        char hashname[16];
        char impsname[16];
@@ -326,7 +326,7 @@ typedef struct {
        MT_Lock cache;
        MT_Lock trim;
        bat free;
-#ifndef NDEBUG
+#ifdef LOCK_STATS
        char cachename[16];
        char trimname[16];
 #endif
@@ -350,7 +350,6 @@ extern size_t GDK_mmap_pagesize; /* mmap
 extern MT_Lock GDKnameLock;
 extern MT_Lock GDKthreadLock;
 extern MT_Lock GDKtmLock;
-extern MT_Lock MT_system_lock;
 
 #define BATcheck(tst, msg, err)                                                
\
        do {                                                            \
diff --git a/gdk/gdk_system.c b/gdk/gdk_system.c
--- a/gdk/gdk_system.c
+++ b/gdk/gdk_system.c
@@ -45,9 +45,13 @@
 #include <string.h>            /* for strerror */
 #include <unistd.h>            /* for sysconf symbols */
 
-MT_Lock MT_system_lock MT_LOCK_INITIALIZER("MT_system_lock");
+#ifndef TEMDEBUG
+/* lock and semaphore macros may use this */
+#define TEMDEBUG if (/* DISABLES CODE */ (0))
+#endif
 
-#if !defined(USE_PTHREAD_LOCKS) && !defined(NDEBUG)
+#ifdef LOCK_STATS
+
 ATOMIC_TYPE volatile GDKlockcnt;
 ATOMIC_TYPE volatile GDKlockcontentioncnt;
 ATOMIC_TYPE volatile GDKlocksleepcnt;
@@ -111,6 +115,16 @@ sortlocklist(MT_Lock *l)
        return t;
 }
 
+static inline bool
+lock_isset(MT_Lock *l)
+{
+       if (MT_lock_try(l)) {
+               MT_lock_unset(l);
+               return false;
+       }
+       return true;
+}
+
 void
 GDKlockstatistics(int what)
 {
@@ -137,11 +151,11 @@ GDKlockstatistics(int what)
                if (what == 0 ||
                    (what == 1 && l->count) ||
                    (what == 2 && l->contention) ||
-                   (what == 3 && l->lock))
+                   (what == 3 && lock_isset(l)))
                        fprintf(stderr, "# %-18s\t%zu\t%zu\t%zu\t%s\t%s\t%s\n",
                                l->name ? l->name : "unknown",
                                l->count, l->contention, l->sleep,
-                               l->lock ? "locked" : "",
+                               lock_isset(l) ? "locked" : "",
                                l->locker ? l->locker : "",
                                l->thread ? l->thread : "");
        }
@@ -151,7 +165,8 @@ GDKlockstatistics(int what)
        fprintf(stderr, "#lock sleep count %zu\n", (size_t) GDKlocksleepcnt);
        ATOMIC_CLEAR(GDKlocklistlock, dummy);
 }
-#endif
+
+#endif /* LOCK_STATS */
 
 #if !defined(HAVE_PTHREAD_H) && defined(WIN32)
 static struct winthread {
@@ -160,7 +175,8 @@ static struct winthread {
        DWORD tid;
        void (*func) (void *);
        void *data;
-       bool exited:1, detached:1, waiting:1;
+       volatile ATOMIC_TYPE exited;
+       bool detached:1, waiting:1;
        const char *threadname;
 } *winthreads = NULL;
 static struct winthread mainthread = {
@@ -168,6 +184,9 @@ static struct winthread mainthread = {
 };
 
 static CRITICAL_SECTION winthread_cs;
+#ifdef ATOMIC_LOCK
+static MT_Lock exit_lock MT_LOCK_INITIALIZER("exit_lock");
+#endif
 static DWORD threadslot = TLS_OUT_OF_INDEXES;
 
 bool
@@ -184,26 +203,21 @@ MT_thread_init(void)
                        return false;
                }
                InitializeCriticalSection(&winthread_cs);
+#ifdef NEED_MT_LOCK_INIT
+               ATOMIC_INIT(exit_lock);
+#endif
        }
        return true;
 }
 
-static inline struct winthread *
-find_winthread_locked(DWORD tid)
-{
-       for (struct winthread *w = winthreads; w; w = w->next)
-               if (w->tid == tid)
-                       return w;
-       return NULL;
-}
-
 static struct winthread *
 find_winthread(DWORD tid)
 {
        struct winthread *w;
 
        EnterCriticalSection(&winthread_cs);
-       w = find_winthread_locked(tid);
+       for (w = winthreads; w && w->tid != tid; w = w->next)
+               ;
        LeaveCriticalSection(&winthread_cs);
        return w;
 }
@@ -276,9 +290,7 @@ thread_starter(LPVOID arg)
        w->data = NULL;
        TlsSetValue(threadslot, w);
        (*w->func)(data);
-       EnterCriticalSection(&winthread_cs);
-       w->exited = true;
-       LeaveCriticalSection(&winthread_cs);
+       ATOMIC_SET(w->exited, 1, exit_lock);
        ExitThread(0);
        return TRUE;
 }
@@ -292,7 +304,7 @@ join_threads(void)
        do {
                waited = false;
                for (struct winthread *w = winthreads; w; w = w->next) {
-                       if (w->exited && w->detached && !w->waiting) {
+                       if (w->detached && !w->waiting && ATOMIC_GET(w->exited, 
exit_lock)) {
                                w->waiting = true;
                                LeaveCriticalSection(&winthread_cs);
                                WaitForSingleObject(w->hdl, INFINITE);
@@ -340,14 +352,13 @@ MT_create_thread(MT_Id *t, void (*f) (vo
                return -1;
 
        join_threads();
-       w->func = f;
-       w->hdl = NULL;
-       w->tid = 0;
-       w->data = arg;
-       w->exited = false;
-       w->waiting = false;
-       w->detached = (d == MT_THR_DETACHED);
-       w->threadname = threadname;
+       *w = (struct winthread) {
+               .func = f,
+               .data = arg,
+               .waiting = false,
+               .detached = (d == MT_THR_DETACHED),
+               .threadname = threadname,
+       };
        EnterCriticalSection(&winthread_cs);
        w->next = winthreads;
        winthreads = w;
@@ -375,9 +386,7 @@ MT_exiting_thread(void)
        struct winthread *w = TlsGetValue(threadslot);
 
        if (w) {
-               EnterCriticalSection(&winthread_cs);
-               w->exited = true;
-               LeaveCriticalSection(&winthread_cs);
+               ATOMIC_SET(w->exited, 1, exit_lock);
        }
 }
 
@@ -426,66 +435,6 @@ MT_kill_thread(MT_Id t)
        return -1;
 }
 
-#ifdef USE_PTHREAD_LOCKS
-
-void
-pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t 
*mutexattr)
_______________________________________________
checkin-list mailing list
checkin-list@monetdb.org
https://www.monetdb.org/mailman/listinfo/checkin-list

Reply via email to