Changeset: dc2d49ae9657 for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB/rev/dc2d49ae9657
Modified Files:
        clients/Tests/exports.stable.out
        gdk/gdk.h
        gdk/gdk_system.c
        gdk/gdk_system_private.h
        gdk/gdk_utils.c
        monetdb5/mal/mal_client.c
        monetdb5/mal/mal_client.h
        monetdb5/mal/mal_dataflow.c
        monetdb5/mal/mal_interpreter.c
Branch: default
Log Message:

Move stuff from the Thread structure to the mtthread structure.


diffs (truncated from 447 to 300 lines):

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
@@ -406,7 +406,7 @@ void *THRdata[THREADDATA];
 void THRdel(Thread t);
 void *THRgetdata(int);
 int THRgettid(void);
-int THRhighwater(void);
+bool THRhighwater(void);
 void THRsetdata(int, void *);
 gdk_return TMsubcommit(BAT *bl) __attribute__((__warn_unused_result__));
 gdk_return TMsubcommit_list(bat *restrict subcommit, BUN *restrict sizes, int 
cnt, lng logno, lng transid) __attribute__((__warn_unused_result__));
diff --git a/gdk/gdk.h b/gdk/gdk.h
--- a/gdk/gdk.h
+++ b/gdk/gdk.h
@@ -1991,9 +1991,7 @@ typedef struct threadStruct {
        ATOMIC_TYPE pid;        /* thread id, 0 = unallocated */
        bat freebats;           /* linked list of free bats */
        uint32_t nfreebats;     /* number of free bats in .freebats */
-       char name[MT_NAME_LEN];
        void *data[THREADDATA];
-       uintptr_t sp;
 } *Thread;
 
 
@@ -2002,7 +2000,7 @@ gdk_export MT_Id THRcreate(void (*f) (vo
 gdk_export void THRdel(Thread t);
 gdk_export void THRsetdata(int, void *);
 gdk_export void *THRgetdata(int);
-gdk_export int THRhighwater(void);
+gdk_export bool THRhighwater(void);
 
 gdk_export void *THRdata[THREADDATA];
 
diff --git a/gdk/gdk_system.c b/gdk/gdk_system.c
--- a/gdk/gdk_system.c
+++ b/gdk/gdk_system.c
@@ -180,8 +180,6 @@ GDKlockstatistics(int what)
 
 #endif /* LOCK_STATS */
 
-static void MT_thread_setcondwait(MT_Cond *cond);
-
 static struct mtthread {
        struct mtthread *next;
        void (*func) (void *);  /* function to be called */
@@ -207,6 +205,7 @@ static struct mtthread {
        HANDLE hdl;
 #endif
        MT_Id tid;
+       uintptr_t sp;
 } *mtthreads = NULL;
 struct mtthread mainthread = {
        .threadname = "main thread",
@@ -231,6 +230,35 @@ static DWORD threadslot = TLS_OUT_OF_IND
 #endif
 static bool thread_initialized = false;
 
+#if defined(_MSC_VER) && _MSC_VER >= 1900
+#pragma warning(disable : 4172)
+#endif
+static inline uintptr_t
+THRsp(void)
+{
+#if defined(__GNUC__) || defined(__clang__)
+       return (uintptr_t) __builtin_frame_address(0);
+#else
+       int l = 0;
+       uintptr_t sp = (uintptr_t) (&l);
+
+       return sp;
+#endif
+}
+
+bool
+THRhighwater(void)
+{
+       struct mtthread *s = thread_self();
+       if (s != NULL && s->sp != 0) {
+               uintptr_t c = THRsp();
+               size_t diff = c < s->sp ? s->sp - c : c - s->sp;
+               if (diff > THREAD_STACK_SIZE - 80 * 1024)
+                       return true;
+       }
+       return false;
+}
+
 void
 dump_threads(void)
 {
@@ -268,6 +296,7 @@ rm_mtthread(struct mtthread *t)
 {
        struct mtthread **pt;
 
+       assert(t != &mainthread);
        thread_lock();
        for (pt = &mtthreads; *pt && *pt != t; pt = &(*pt)->next)
                ;
@@ -312,6 +341,8 @@ MT_thread_init(void)
        }
        InitializeCriticalSection(&winthread_cs);
 #endif
+       mainthread.next = NULL;
+       mtthreads = &mainthread;
        thread_initialized = true;
        return true;
 }
@@ -349,7 +380,7 @@ MT_thread_register(void)
                .refs = 1,
        };
        snprintf(self->threadname, sizeof(self->threadname), "foreign %zu", 
self->tid);
-       Thread t = THRnew(self->threadname, self->tid);
+       Thread t = THRnew(self->tid);
        if (t == NULL) {
                free(self);
                thread_unlock();
@@ -466,7 +497,7 @@ MT_thread_setsemawait(MT_Sema *sema)
                self->semawait = sema;
 }
 
-void
+static void
 MT_thread_setcondwait(MT_Cond *cond)
 {
        if (!thread_initialized)
@@ -586,6 +617,7 @@ thread_starter(void *arg)
        void *data = self->data;
 
        self->data = NULL;
+       self->sp = THRsp();
        thread_setself(self);
        (*self->func)(data);
        ATOMIC_SET(&self->exited, 1);
@@ -824,38 +856,51 @@ MT_join_thread(MT_Id tid)
        return -1;
 }
 
-int
-MT_kill_thread(MT_Id tid)
+static bool
+MT_kill_thread(struct mtthread *t)
 {
-       struct mtthread *t;
-
-       assert(tid != mainthread.tid);
+       assert(t != thread_self());
        join_threads();
-       t = find_mtthread(tid);
-       if (t == NULL)
-               return -1;
 #ifdef HAVE_PTHREAD_H
 #ifdef HAVE_PTHREAD_KILL
        if (pthread_kill(t->hdl, SIGHUP) == 0)
-               return 0;
+               return true;
 #endif
 #else
        if (t->hdl == NULL) {
                /* detached thread */
                HANDLE h;
-               int ret = 0;
+               bool ret = false;
                h = OpenThread(THREAD_ALL_ACCESS, 0, (DWORD) tid);
                if (h == NULL)
-                       return -1;
+                       return false;
                if (TerminateThread(h, -1))
-                       ret = -1;
+                       ret = true;
                CloseHandle(h);
                return ret;
        }
        if (TerminateThread(t->hdl, -1))
-               return 0;
+               return true;
 #endif
-       return -1;
+       return false;
+}
+
+bool
+MT_kill_threads(void)
+{
+       struct mtthread *self = thread_self();
+       bool killed = false;
+
+       assert(self == &mainthread);
+       thread_lock();
+       for (struct mtthread *t = mtthreads; t; t = t->next) {
+               if (t == self)
+                       continue;
+               TRC_INFO(GDK, "Killing thread %s\n", t->threadname);
+               killed |= MT_kill_thread(t);
+       }
+       thread_unlock();
+       return killed;
 }
 
 int
diff --git a/gdk/gdk_system_private.h b/gdk/gdk_system_private.h
--- a/gdk/gdk_system_private.h
+++ b/gdk/gdk_system_private.h
@@ -18,11 +18,11 @@ void dump_threads(void)
        __attribute__((__visibility__("hidden")));
 void join_detached_threads(void)
        __attribute__((__visibility__("hidden")));
-int MT_kill_thread(MT_Id t)
+bool MT_kill_threads(void)
        __attribute__((__visibility__("hidden")));
 bool MT_thread_override_limits(void)
        __attribute__((__visibility__("hidden")));
-Thread THRnew(const char *name, MT_Id pid)
+Thread THRnew(MT_Id pid)
        __attribute__((__visibility__("hidden")));
 #ifdef NATIVE_WIN32
 #define GDKwinerror(format, ...)                                       \
diff --git a/gdk/gdk_utils.c b/gdk/gdk_utils.c
--- a/gdk/gdk_utils.c
+++ b/gdk/gdk_utils.c
@@ -1271,8 +1271,6 @@ GDKprepareExit(void)
 void
 GDKreset(int status)
 {
-       MT_Id pid = MT_getpid();
-
        assert(GDKexiting());
 
        if (GDKembedded())
@@ -1300,22 +1298,7 @@ GDKreset(int status)
 
        if (status == 0) {
                /* they had their chance, now kill them */
-               bool killed = false;
-               for (Thread t = GDKthreads; t < GDKthreads + THREADS; t++) {
-                       MT_Id victim;
-                       if ((victim = (MT_Id) ATOMIC_GET(&t->pid)) != 0) {
-                               if (pid && victim != pid) {
-                                       int e;
-
-                                       killed = true;
-                                       e = MT_kill_thread(victim);
-                                       TRC_INFO(GDK, "Killing thread: %d\n", 
e);
-                                       ATOMIC_DEC(&GDKnrofthreads);
-                               }
-                               ATOMIC_SET(&t->pid, 0);
-                       }
-               }
-               assert(ATOMIC_GET(&GDKnrofthreads) <= 1);
+               bool killed = MT_kill_threads();
                /* all threads ceased running, now we can clean up */
                if (!killed) {
                        /* we can't clean up after killing threads */
@@ -1620,22 +1603,6 @@ GDKms(void)
  */
 void *THRdata[THREADDATA] = { 0 };
 
-#if defined(_MSC_VER) && _MSC_VER >= 1900
-#pragma warning(disable : 4172)
-#endif
-static inline uintptr_t
-THRsp(void)
-{
-#if defined(__GNUC__) || defined(__clang__)
-       return (uintptr_t) __builtin_frame_address(0);
-#else
-       int l = 0;
-       uintptr_t sp = (uintptr_t) (&l);
-
-       return sp;
-#endif
-}
-
 static inline Thread
 GDK_find_self(void)
 {
@@ -1643,7 +1610,7 @@ GDK_find_self(void)
 }
 
 Thread
-THRnew(const char *name, MT_Id pid)
+THRnew(MT_Id pid)
 {
        for (Thread s = GDKthreads; s < GDKthreads + THREADS; s++) {
                ATOMIC_BASE_TYPE npid = 0;
@@ -1651,14 +1618,11 @@ THRnew(const char *name, MT_Id pid)
                        /* successfully allocated, fill in rest */
                        s->data[0] = THRdata[0];
                        s->data[1] = THRdata[1];
-                       s->sp = THRsp();
                        s->freebats = 0;
                        s->nfreebats = 0;
-                       strcpy_len(s->name, name, sizeof(s->name));
-                       TRC_DEBUG(PAR, "%x %zu sp = %zu\n",
-                                 (unsigned) s->tid,
-                                 (size_t) ATOMIC_GET(&s->pid),
-                                 (size_t) s->sp);
+                       TRC_DEBUG(PAR, "%d %zu\n",
+                                 s->tid,
+                                 (size_t) ATOMIC_GET(&s->pid));
                        TRC_DEBUG(PAR, "Number of threads: %d\n",
_______________________________________________
checkin-list mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to