Changeset: 1338a980f264 for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=1338a980f264
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_sample.c
        gdk/gdk_system.h
        gdk/gdk_utils.c
        monetdb5/extras/rapi/rapi.c
        monetdb5/mal/mal.c
        monetdb5/mal/mal_dataflow.c
        monetdb5/mal/mal_resource.c
        monetdb5/mal/mal_scenario.c
        monetdb5/modules/kernel/mmath.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
        monetdb5/optimizer/optimizer.c
        monetdb5/optimizer/optimizer_private.h
        sql/backends/monet5/UDF/pyapi/pyapi.c
        sql/backends/monet5/sql_scenario.c
        sql/backends/monet5/vaults/lidar/lidar.c
        sql/backends/monet5/vaults/lidar/lidar.h
        sql/backends/monet5/vaults/lidar/lidar.mal
        sql/storage/store.c
Branch: Apr2019
Log Message:

On Windows, dynamically, but safely, initialize mutexes.
On Windows, you need to call CreateMutex() to allocate and initialize
a mutex.  This cannot be done statically, so we need to do it at run
time.  By using the _InterlockedCompareExchangePointer we can do this
safely at run time, even if multiple threads were to attempt the
initialization at the same time (the first one wins).


diffs (truncated from 904 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
@@ -11187,7 +11187,6 @@ Ready.
 [ "lidar",     "attach",       "pattern lidar.attach(fname:str):void ",        
"LIDARattach;", "Open a LIDAR file and return catalog of the table HDUs"        
]
 [ "lidar",     "export",       "pattern lidar.export(tablename:str, fname:str, 
type:str):void ",       "LIDARexportTable;",    "Exporta LIDAR table to a 
LAS/LAZ file" ]
 [ "lidar",     "load", "pattern lidar.load(tablename:str):void ",      
"LIDARloadTable;",      "Load a LIDAR table from an attached file"      ]
-[ "lidar",     "prelude",      "command lidar.prelude():void ",        
"LIDARprelude;",        ""      ]
 [ "mal",       "manifold",     "pattern mal.manifold(mod:str, fcn:str, 
a:any...):bat[:any] ",  "MANIFOLDevaluate;",    ""      ]
 [ "mal",       "multiplex",    "pattern mal.multiplex(mod:str, fcn:str, 
a:any...):any... ",    "MANIFOLDremapMultiplex;",      ""      ]
 [ "manual",    "functions",    "pattern manual.functions() (mod:bat[:str], 
fcn:bat[:str], sig:bat[:str], adr:bat[:str], com:bat[:str]) ",      
"MANUALcreateOverview;",        "Produces a table with all MAL functions known" 
]
@@ -12118,7 +12117,6 @@ 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
@@ -15597,7 +15597,6 @@ Ready.
 [ "lidar",     "attach",       "pattern lidar.attach(fname:str):void ",        
"LIDARattach;", "Open a LIDAR file and return catalog of the table HDUs"        
]
 [ "lidar",     "export",       "pattern lidar.export(tablename:str, fname:str, 
type:str):void ",       "LIDARexportTable;",    "Exporta LIDAR table to a 
LAS/LAZ file" ]
 [ "lidar",     "load", "pattern lidar.load(tablename:str):void ",      
"LIDARloadTable;",      "Load a LIDAR table from an attached file"      ]
-[ "lidar",     "prelude",      "command lidar.prelude():void ",        
"LIDARprelude;",        ""      ]
 [ "mal",       "manifold",     "pattern mal.manifold(mod:str, fcn:str, 
a:any...):bat[:any] ",  "MANIFOLDevaluate;",    ""      ]
 [ "mal",       "multiplex",    "pattern mal.multiplex(mod:str, fcn:str, 
a:any...):any... ",    "MANIFOLDremapMultiplex;",      ""      ]
 [ "manual",    "functions",    "pattern manual.functions() (mod:bat[:str], 
fcn:bat[:str], sig:bat[:str], adr:bat[:str], com:bat[:str]) ",      
"MANUALcreateOverview;",        "Produces a table with all MAL functions known" 
]
@@ -16546,7 +16545,6 @@ 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
@@ -1975,7 +1975,6 @@ 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);
@@ -2283,7 +2282,6 @@ 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
@@ -232,7 +232,7 @@ static volatile MT_Id locked_by = 0;
        } while (0)
 
 static int BBPunloadCnt = 0;
-static MT_Lock GDKunloadLock MT_LOCK_INITIALIZER("GDKunloadLock");
+static MT_Lock GDKunloadLock = MT_LOCK_INITIALIZER("GDKunloadLock");
 
 void
 BBPlock(void)
@@ -1022,16 +1022,6 @@ BBPinit(void)
        if(!(backupbbpdirstr = GDKfilepath(0, BAKDIR, "BBP", "dir")))
                GDKfatal("BBPinit: GDKmalloc failed\n");
 
-#ifdef NEED_MT_LOCK_INIT
-       static initialized = false;
-       if (!initialized) {
-               /* only do this once */
-               MT_lock_init(&GDKunloadLock, "GDKunloadLock");
-               BATsample(NULL, 0);     /* initializes the lock */
-               initialized = true;
-       }
-#endif
-
        if (BBPfarms[0].dirname == NULL) {
                BBPaddfarm(".", 1 << PERSISTENT);
                BBPaddfarm(".", 1 << TRANSIENT);
diff --git a/gdk/gdk_sample.c b/gdk/gdk_sample.c
--- a/gdk/gdk_sample.c
+++ b/gdk/gdk_sample.c
@@ -176,17 +176,8 @@ BAT *
 BATsample(BAT *b, BUN n)
 {
        static random_state_engine rse;
-       static MT_Lock rse_lock MT_LOCK_INITIALIZER("rse_lock");
+       static MT_Lock rse_lock = MT_LOCK_INITIALIZER("rse_lock");
 
-#ifdef NEED_MT_LOCK_INIT
-       static bool rse_lock_initialized = false;
-       if (!rse_lock_initialized) {
-               MT_lock_init(&rse_lock, "rse_lock");
-               rse_lock_initialized = true;
-               if (b == NULL && n == 0)
-                       return NULL;
-       }
-#endif
        MT_lock_set(&rse_lock);
        if (rse[0] == 0 && rse[1] == 0 && rse[2] == 0 && rse[3] == 0)
                init_random_state_engine(rse, (uint64_t) GDKusec());
diff --git a/gdk/gdk_system.h b/gdk/gdk_system.h
--- a/gdk/gdk_system.h
+++ b/gdk/gdk_system.h
@@ -176,11 +176,13 @@ gdk_export int MT_join_thread(MT_Id t);
                TEMDEBUG fprintf(stderr, "#%s: %s: locking %s...\n",    \
                                 MT_thread_getname(), __func__, (l)->name); \
        } while (0)
+
 #define _DBG_LOCK_LOCKER(l)                            \
        do {                                            \
                (l)->locker = __func__;                 \
                (l)->thread = MT_thread_getname();      \
        } while (0)
+
 #define _DBG_LOCK_UNLOCKER(l)                                  \
        do {                                                    \
                (l)->locker = __func__;                         \
@@ -188,6 +190,7 @@ gdk_export int MT_join_thread(MT_Id t);
                TEMDEBUG fprintf(stderr, "#%s: %s: unlocking %s\n",     \
                                 MT_thread_getname(), __func__, (l)->name); \
        } while (0)
+
 #define _DBG_LOCK_CONTENTION(l)                                                
\
        do {                                                            \
                TEMDEBUG fprintf(stderr, "#%s: %s: lock %s contention\n", \
@@ -195,12 +198,14 @@ gdk_export int MT_join_thread(MT_Id t);
                (void) ATOMIC_INC(&GDKlockcontentioncnt);               \
                (void) ATOMIC_INC(&(l)->contention);                    \
        } while (0)
+
 #define _DBG_LOCK_SLEEP(l)                                             \
        do {                                                            \
                if (_spincnt == 1024)                                   \
                        (void) ATOMIC_INC(&GDKlocksleepcnt);            \
                (void) ATOMIC_INC(&(l)->sleep);                         \
        } while (0)
+
 #define _DBG_LOCK_COUNT_2(l)                                           \
        do {                                                            \
                (l)->count++;                                           \
@@ -214,6 +219,7 @@ gdk_export int MT_join_thread(MT_Id t);
                TEMDEBUG fprintf(stderr, "#%s: %s: locking %s complete\n", \
                                 MT_thread_getname(), __func__, (l)->name); \
        } while (0)
+
 #define _DBG_LOCK_INIT(l)                                              \
        do {                                                            \
                (l)->count = 0;                                         \
@@ -238,6 +244,7 @@ gdk_export int MT_join_thread(MT_Id t);
                        (l)->next = NULL;                               \
                }                                                       \
        } while (0)
+
 #define _DBG_LOCK_DESTROY(l)                                           \
        do {                                                            \
                /* if name starts with "sa_" don't link in GDKlocklist */ \
@@ -286,6 +293,14 @@ typedef struct MT_Lock {
 #endif
 } MT_Lock;
 
+#ifdef LOCK_STATS
+#define MT_LOCK_INITIALIZER(n) { .lock = NULL, .name = n, .next = (struct 
MT_Lock *) -1, }
+#else
+#define MT_LOCK_INITIALIZER(n) { .lock = NULL, .name = n, }
+#endif
+
+#pragma intrinsic(_InterlockedCompareExchangePointer)
+
 #define MT_lock_init(l, n)                                     \
        do {                                                    \
                assert((l)->lock == NULL);                      \
@@ -294,27 +309,48 @@ typedef struct MT_Lock {
                (l)->name[sizeof((l)->name) - 1] = 0;           \
                _DBG_LOCK_INIT(l);                              \
        } while (0)
+
 #define MT_lock_set(l)                                                 \
        do {                                                            \
+               if ((l)->lock == NULL) {                                \
+                       HANDLE _p = CreateMutex(NULL, 0, NULL);         \
+                       if (_InterlockedCompareExchangePointer(         \
+                                   &(l)->lock, _p, NULL) != NULL)      \
+                               CloseHandle(_p);                        \
+               }                                                       \
                assert((l)->lock);                                      \
                _DBG_LOCK_COUNT_0(l);                                   \
                if (WaitForSingleObject((l)->lock, 0) != WAIT_OBJECT_0) { \
                        _DBG_LOCK_CONTENTION(l);                        \
                        MT_thread_setlockwait(l);                       \
-                       while (WaitForSingleObject((l)->lock, INFINITE) != 
WAIT_OBJECT_0) \
+                       while (WaitForSingleObject(                     \
+                                      (l)->lock, INFINITE) != WAIT_OBJECT_0) \
                                ;                                       \
                        MT_thread_setlockwait(NULL);                    \
                }                                                       \
                _DBG_LOCK_LOCKER(l);                                    \
                _DBG_LOCK_COUNT_2(l);                                   \
        } while (0)
+
 #define MT_lock_unset(l)                       \
        do {                                    \
-               assert((l)->lock);                                      \
+               assert((l)->lock);              \
                _DBG_LOCK_UNLOCKER(l);          \
                ReleaseMutex((l)->lock);        \
        } while (0)
-#define MT_lock_try(l) (WaitForSingleObject((l)->lock, 0) == WAIT_OBJECT_0)
+
+static bool inline
+MT_lock_try(MT_Lock *l)
+{
+       if (l->lock == NULL) {
+               HANDLE p = CreateMutex(NULL, 0, NULL);
+               if (_InterlockedCompareExchangePointer(
+                           &l->lock, p, NULL) != NULL)
+                       CloseHandle(p);
+       }
+       return WaitForSingleObject(l->lock, 0) == WAIT_OBJECT_0;
+}
+
 #define MT_lock_destroy(l)                     \
        do {                                    \
                assert((l)->lock);              \
@@ -324,6 +360,7 @@ typedef struct MT_Lock {
        } while (0)
 
 #else
+
 typedef struct MT_Lock {
        pthread_mutex_t lock;
        char name[16];
@@ -336,6 +373,13 @@ typedef struct MT_Lock {
        const char *thread;
 #endif
 } MT_Lock;
+
+#ifdef LOCK_STATS
+#define MT_LOCK_INITIALIZER(n) { .lock = PTHREAD_MUTEX_INITIALIZER, .name = n, 
.next = (struct MT_Lock *) -1, }
+#else
+#define MT_LOCK_INITIALIZER(n) { .lock = PTHREAD_MUTEX_INITIALIZER, .name = n, 
}
+#endif
+
 #define MT_lock_init(l, n)                                     \
        do {                                                    \
                pthread_mutex_init(&(l)->lock, 0);              \
@@ -343,6 +387,7 @@ typedef struct MT_Lock {
                (l)->name[sizeof((l)->name) - 1] = 0;           \
                _DBG_LOCK_INIT(l);                              \
        } while (0)
+
 #define MT_lock_set(l)                                                 \
        do {                                                            \
                _DBG_LOCK_COUNT_0(l);                                   \
@@ -356,25 +401,21 @@ typedef struct MT_Lock {
                _DBG_LOCK_LOCKER(l);                                    \
                _DBG_LOCK_COUNT_2(l);                                   \
        } while (0)
+
 #define MT_lock_unset(l)                                               \
        do {                                                            \
                _DBG_LOCK_UNLOCKER(l);          \
                pthread_mutex_unlock(&(l)->lock);                       \
        } while (0)
+
 #define MT_lock_try(l)         (pthread_mutex_trylock(&(l)->lock) == 0)
+
 #define MT_lock_destroy(l)                             \
        do {                                            \
                _DBG_LOCK_DESTROY(l);                   \
                pthread_mutex_destroy(&(l)->lock);      \
        } while (0)
 
-#ifdef PTHREAD_MUTEX_INITIALIZER
-#ifdef LOCK_STATS
-#define MT_LOCK_INITIALIZER(n) = { .lock = PTHREAD_MUTEX_INITIALIZER, .next = 
(struct MT_Lock *) -1, .name = n, }
-#else
-#define MT_LOCK_INITIALIZER(n) = { .lock = PTHREAD_MUTEX_INITIALIZER, .name = 
n, }
-#endif
-#endif
 #endif
 
 #else
@@ -394,6 +435,12 @@ typedef struct MT_Lock {
 #endif
 } MT_Lock;
 
+#ifdef LOCK_STATS
+#define MT_LOCK_INITIALIZER(n) { .next = (struct MT_Lock *) -1, .name = n, }
+#else
+#define MT_LOCK_INITIALIZER(n) { .name = n, }
+#endif
+
_______________________________________________
checkin-list mailing list
[email protected]
https://www.monetdb.org/mailman/listinfo/checkin-list

Reply via email to