Changeset: 16b194693439 for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=16b194693439
Modified Files:
        gdk/gdk_hash.c
        gdk/gdk_imprints.c
        gdk/gdk_orderidx.c
        gdk/gdk_storage.c
        gdk/gdk_system.c
        gdk/gdk_system.h
        gdk/gdk_utils.c
        monetdb5/mal/mal_client.c
        monetdb5/mal/mal_client.h
        monetdb5/mal/mal_dataflow.c
        monetdb5/mal/mal_exception.c
        monetdb5/mal/mal_profiler.c
        monetdb5/modules/mal/mal_mapi.c
        monetdb5/modules/mal/wlc.c
        sql/backends/monet5/UDF/pyapi/pyapi.c
        sql/backends/monet5/vaults/bam/bam_loader.c
        sql/backends/monet5/wlr.c
        tools/mserver/shutdowntest.c
Branch: Apr2019
Log Message:

Give each thread a (mostly unique) name, use that when logging errors.


diffs (truncated from 486 to 300 lines):

diff --git a/gdk/gdk_hash.c b/gdk/gdk_hash.c
--- a/gdk/gdk_hash.c
+++ b/gdk/gdk_hash.c
@@ -586,7 +586,8 @@ BAThash(BAT *b)
                        MT_Id tid;
                        BBPfix(b->batCacheid);
                        if (MT_create_thread(&tid, BAThashsync, b,
-                                            MT_THR_DETACHED) < 0) {
+                                            MT_THR_DETACHED,
+                                            "BAThashsync") < 0) {
                                /* couldn't start thread: clean up */
                                BBPunfix(b->batCacheid);
                        }
diff --git a/gdk/gdk_imprints.c b/gdk/gdk_imprints.c
--- a/gdk/gdk_imprints.c
+++ b/gdk/gdk_imprints.c
@@ -528,7 +528,8 @@ BATimprints(BAT *b)
                    !b->theap.dirty) {
                        MT_Id tid;
                        BBPfix(b->batCacheid);
-                       if (MT_create_thread(&tid, BATimpsync, b, 
MT_THR_DETACHED) < 0)
+                       if (MT_create_thread(&tid, BATimpsync, b,
+                                            MT_THR_DETACHED, "BATimpsync") < 0)
                                BBPunfix(b->batCacheid);
                }
        }
diff --git a/gdk/gdk_orderidx.c b/gdk/gdk_orderidx.c
--- a/gdk/gdk_orderidx.c
+++ b/gdk/gdk_orderidx.c
@@ -161,7 +161,8 @@ persistOIDX(BAT *b)
            !b->theap.dirty) {
                MT_Id tid;
                BBPfix(b->batCacheid);
-               if (MT_create_thread(&tid, BATidxsync, b, MT_THR_DETACHED) < 0)
+               if (MT_create_thread(&tid, BATidxsync, b,
+                                    MT_THR_DETACHED, "BATidxsync") < 0)
                        BBPunfix(b->batCacheid);
        } else
                ALGODEBUG fprintf(stderr, "#persistOIDX(" ALGOBATFMT "): NOT 
persisting order index\n", ALGOBATPAR(b));
@@ -468,7 +469,8 @@ GDKmergeidx(BAT *b, BAT**a, int n_ar)
            b->batInserted == b->batCount) {
                MT_Id tid;
                BBPfix(b->batCacheid);
-               if (MT_create_thread(&tid, BATidxsync, b, MT_THR_DETACHED) < 0)
+               if (MT_create_thread(&tid, BATidxsync, b,
+                                    MT_THR_DETACHED, "BATidxsync") < 0)
                        BBPunfix(b->batCacheid);
        } else
                ALGODEBUG fprintf(stderr, "#GDKmergeidx(%s): NOT persisting 
index\n", BATgetId(b));
diff --git a/gdk/gdk_storage.c b/gdk/gdk_storage.c
--- a/gdk/gdk_storage.c
+++ b/gdk/gdk_storage.c
@@ -701,7 +701,8 @@ BATmsync(BAT *b)
                        arg->h = &b->theap;
                        BBPfix(b->batCacheid);
 #ifdef MSYNC_BACKGROUND
-                       if (MT_create_thread(&tid, BATmsyncImplementation, arg, 
MT_THR_DETACHED) < 0) {
+                       if (MT_create_thread(&tid, BATmsyncImplementation, arg,
+                                            MT_THR_DETACHED, "BATmsync") < 0) {
                                /* don't bother if we can't create a thread */
                                BBPunfix(b->batCacheid);
                                GDKfree(arg);
@@ -717,7 +718,8 @@ BATmsync(BAT *b)
                        arg->h = b->tvheap;
                        BBPfix(b->batCacheid);
 #ifdef MSYNC_BACKGROUND
-                       if (MT_create_thread(&tid, BATmsyncImplementation, arg, 
MT_THR_DETACHED) < 0) {
+                       if (MT_create_thread(&tid, BATmsyncImplementation, arg,
+                                            MT_THR_DETACHED, "BATmsync") < 0) {
                                /* don't bother if we can't create a thread */
                                BBPunfix(b->batCacheid);
                                GDKfree(arg);
diff --git a/gdk/gdk_system.c b/gdk/gdk_system.c
--- a/gdk/gdk_system.c
+++ b/gdk/gdk_system.c
@@ -160,17 +160,51 @@ static struct winthread {
        void (*func) (void *);
        void *arg;
        int flags;
+       const char *threadname;
 } *winthreads = NULL;
 #define EXITED         1
 #define DETACHED       2
 #define WAITING                4
 static CRITICAL_SECTION winthread_cs;
-static bool winthread_cs_init = false;
+static DWORD threadnameslot = TLS_OUT_OF_INDEXES;
+
+bool
+MT_thread_init(void)
+{
+       if (threadnameslot == TLS_OUT_OF_INDEXES) {
+               threadnameslot = TlsAlloc();
+               if (threadnameslot == TLS_OUT_OF_INDEXES)
+                       return false;
+               if (TlsSetValue(threadnameslot, "main thread") == 0) {
+                       TlsFree(threadnameslot);
+                       threadnameslot = TLS_OUT_OF_INDEXES;
+                       return false;
+               }
+               InitializeCriticalSection(&winthread_cs);
+       }
+       return true;
+}
+
+const char *
+MT_thread_name(void)
+{
+       const char *name = TlsGetValue(threadnameslot);
+       return name ? name : "unknown thread";
+}
+
+void
+MT_thread_setname(const char *name)
+{
+       TlsSetValue(threadnameslot, (LPVOID) name);
+}
 
 void
 gdk_system_reset(void)
 {
-       winthread_cs_init = false;
+       assert(threadnameslot != TLS_OUT_OF_INDEXES);
+       TlsFree(threadnameslot);
+       threadnameslot = TLS_OUT_OF_INDEXES;
+       DeleteCriticalSection(&winthread_cs);
 }
 
 static struct winthread *
@@ -191,7 +225,6 @@ rm_winthread(struct winthread *w)
 {
        struct winthread **wp;
 
-       assert(winthread_cs_init);
        EnterCriticalSection(&winthread_cs);
        for (wp = &winthreads; *wp && *wp != w; wp = &(*wp)->next)
                ;
@@ -204,6 +237,8 @@ rm_winthread(struct winthread *w)
 static DWORD WINAPI
 thread_starter(LPVOID arg)
 {
+       TlsSetValue(threadnameslot,
+                   (LPVOID) ((struct winthread *) arg)->threadname);
        (*((struct winthread *) arg)->func)(((struct winthread *) arg)->arg);
        ((struct winthread *) arg)->flags |= EXITED;
        ExitThread(0);
@@ -261,20 +296,13 @@ join_detached_threads(void)
 }
 
 int
-MT_create_thread(MT_Id *t, void (*f) (void *), void *arg, enum MT_thr_detach d)
+MT_create_thread(MT_Id *t, void (*f) (void *), void *arg, enum MT_thr_detach 
d, const char *threadname)
 {
        struct winthread *w = malloc(sizeof(*w));
 
        if (w == NULL)
                return -1;
 
-       if (!winthread_cs_init) {
-               /* we only get here before any threads are created,
-                * and this is the only time that winthread_cs_init is
-                * ever changed */
-               InitializeCriticalSection(&winthread_cs);
-               winthread_cs_init = true;
-       }
        join_threads();
        w->func = f;
        w->arg = arg;
@@ -285,6 +313,7 @@ MT_create_thread(MT_Id *t, void (*f) (vo
        w->next = winthreads;
        winthreads = w;
        LeaveCriticalSection(&winthread_cs);
+       w->threadname = threadname;
        w->hdl = CreateThread(NULL, THREAD_STACK_SIZE, thread_starter, w, 0, 
&w->tid);
        if (w->hdl == NULL) {
                rm_winthread(w);
@@ -307,10 +336,13 @@ MT_exiting_thread(void)
 void
 MT_exit_thread(int s)
 {
-       if (winthread_cs_init) {
+       EnterCriticalSection(&winthread_cs);
+       if (winthreads) {
+               LeaveCriticalSection(&winthread_cs);
                MT_exiting_thread();
                ExitThread(s);
        } else {
+               LeaveCriticalSection(&winthread_cs);
                /* no threads started yet, so this is a global exit */
                MT_global_exit(s);
        }
@@ -321,7 +353,6 @@ MT_join_thread(MT_Id t)
 {
        struct winthread *w;
 
-       assert(winthread_cs_init);
        join_threads();
        w = find_winthread((DWORD) t);
        if (w == NULL || w->hdl == NULL)
@@ -339,7 +370,6 @@ MT_kill_thread(MT_Id t)
 {
        struct winthread *w;
 
-       assert(winthread_cs_init);
        join_threads();
        w = find_winthread((DWORD) t);
        if (w == NULL)
@@ -429,9 +459,34 @@ static struct posthread {
        void (*func)(void *);
        void *arg;
        int exited;
+       const char *name;
 } *posthreads = NULL;
 static pthread_mutex_t posthread_lock = PTHREAD_MUTEX_INITIALIZER;
 
+static pthread_key_t threadnamekey;
+
+bool
+MT_thread_init(void)
+{
+       if (pthread_key_create(&threadnamekey, NULL) != 0)
+               return false;
+       pthread_setspecific(threadnamekey, "main thread");
+       return true;
+}
+
+const char *
+MT_thread_name(void)
+{
+       const char *name = pthread_getspecific(threadnamekey);
+       return name ? name : "unknown thread";
+}
+
+void
+MT_thread_setname(const char *name)
+{
+       pthread_setspecific(threadnamekey, name);
+}
+
 static struct posthread *
 find_posthread_locked(pthread_t tid)
 {
@@ -489,6 +544,7 @@ thread_starter(void *arg)
 {
        struct posthread *p = (struct posthread *) arg;
 
+       pthread_setspecific(threadnamekey, p->name);
        (*p->func)(p->arg);
        pthread_mutex_lock(&posthread_lock);
        /* *p may have been freed by join_threads, so try to find it
@@ -506,6 +562,7 @@ thread_starter_simple(void *arg)
        void (*pfunc)(void *) = p->func;
        void *parg = p->arg;
 
+       pthread_setspecific(threadnamekey, p->name);
        free(p);
        (*pfunc)(parg);
        return NULL;
@@ -557,7 +614,7 @@ join_detached_threads(void)
 }
 
 int
-MT_create_thread(MT_Id *t, void (*f) (void *), void *arg, enum MT_thr_detach d)
+MT_create_thread(MT_Id *t, void (*f) (void *), void *arg, enum MT_thr_detach 
d, const char *threadname)
 {
 #ifdef HAVE_PTHREAD_SIGMASK
        sigset_t new_mask, orig_mask;
@@ -595,6 +652,7 @@ MT_create_thread(MT_Id *t, void (*f) (vo
        p->func = f;
        p->arg = arg;
        p->exited = 0;
+       p->name = threadname;
        if (d == MT_THR_DETACHED) {
                pf = thread_starter;
                newtp = &p->tid;
diff --git a/gdk/gdk_system.h b/gdk/gdk_system.h
--- a/gdk/gdk_system.h
+++ b/gdk/gdk_system.h
@@ -112,8 +112,12 @@ typedef size_t MT_Id;              /* thread number.
 
 enum MT_thr_detach { MT_THR_JOINABLE, MT_THR_DETACHED };
 
+gdk_export bool MT_thread_init(void);
 gdk_export int MT_create_thread(MT_Id *t, void (*function) (void *),
-                               void *arg, enum MT_thr_detach d);
+                               void *arg, enum MT_thr_detach d,
+                               const char *threadname);
+gdk_export const char *MT_thread_name(void);
+gdk_export void MT_thread_setname(const char *name);
 gdk_export void MT_exiting_thread(void);
 gdk_export MT_Id MT_getpid(void);
 gdk_export int MT_join_thread(MT_Id t);
diff --git a/gdk/gdk_utils.c b/gdk/gdk_utils.c
--- a/gdk/gdk_utils.c
+++ b/gdk/gdk_utils.c
@@ -464,6 +464,9 @@ GDKinit(opt *set, int setlen)
        static_assert(sizeof(size_t) == SIZEOF_SIZE_T, "error in configure: bad 
value for SIZEOF_SIZE_T");
        static_assert(SIZEOF_OID == SIZEOF_INT || SIZEOF_OID == SIZEOF_LNG, 
"SIZEOF_OID should be equal to SIZEOF_INT or SIZEOF_LNG");
 
+       if (!MT_thread_init())
_______________________________________________
checkin-list mailing list
[email protected]
https://www.monetdb.org/mailman/listinfo/checkin-list

Reply via email to