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

Removed the last compiled-in limit to the number of threads.


diffs (201 lines):

diff --git a/gdk/ChangeLog b/gdk/ChangeLog
--- a/gdk/ChangeLog
+++ b/gdk/ChangeLog
@@ -1,6 +1,11 @@
 # ChangeLog file for GDK
 # This file is updated with Maddlog
 
+* Thu Nov  2 2023 Sjoerd Mullender <sjo...@acm.org>
+- Removed the compiled-in limit on the number of threads that can be used.
+  The number of threads are still limited, but the limit is dictated
+  solely by the operating system and the availability of enough memory.
+
 * Thu Sep 28 2023 Sjoerd Mullender <sjo...@acm.org>
 - We now prevent accidental upgrades from a database without 128 bit
   integers to one with 128 bit integers (also known as HUGEINT) from
diff --git a/gdk/gdk.h b/gdk/gdk.h
--- a/gdk/gdk.h
+++ b/gdk/gdk.h
@@ -1971,18 +1971,7 @@ VALptr(const ValRecord *v)
        }
 }
 
-/*
- * The kernel maintains a central table of all active threads.  They
- * are indexed by their tid. The structure contains information on the
- * input/output file descriptors, which should be set before a
- * database operation is started. It ensures that output is delivered
- * to the proper client.
- *
- * The Thread structure should be ideally made directly accessible to
- * each thread. This speeds up access to tid and file descriptors.
- */
-#define THREADS        1024
-#define THREADDATA     3
+#define THREADS                1024    /* maximum value for gdk_nr_threads */
 
 typedef struct threadStruct *Thread;
 
diff --git a/gdk/gdk_system.c b/gdk/gdk_system.c
--- a/gdk/gdk_system.c
+++ b/gdk/gdk_system.c
@@ -262,36 +262,6 @@ THRhighwater(void)
        return false;
 }
 
-static uint32_t allocated[THREADS / 32];
-static MT_Lock alloclock = MT_LOCK_INITIALIZER(alloclock);
-
-static MT_Id
-alloc_thread(void)
-{
-       MT_Id mtid = 0;
-       MT_lock_set(&alloclock);
-       for (int i = 0; i < THREADS / 32; i++) {
-               if (allocated[i] != ~UINT32_C(0)) {
-                       int x = candmask_lobit(~allocated[i]);
-                       allocated[i] |= UINT32_C(1) << x;
-                       mtid = (MT_Id) (i * 32 + x + 1);
-                       break;
-               }
-       }
-       MT_lock_unset(&alloclock);
-       return mtid;
-}
-
-static void
-dealloc_thread(MT_Id mtid)
-{
-       assert(mtid > 0 && mtid <= THREADS);
-       mtid--;
-       MT_lock_set(&alloclock);
-       allocated[mtid / 32] &= ~(UINT32_C(1) << (mtid % 32));
-       MT_lock_unset(&alloclock);
-}
-
 void
 dump_threads(void)
 {
@@ -329,7 +299,6 @@ static void
 rm_mtthread(struct mtthread *t)
 {
        struct mtthread **pt;
-       MT_Id mtid = t->tid;
 
        assert(t != &mainthread);
        thread_lock();
@@ -340,7 +309,6 @@ rm_mtthread(struct mtthread *t)
        ATOMIC_DESTROY(&t->exited);
        free(t);
        thread_unlock();
-       dealloc_thread(mtid);
 }
 
 bool
@@ -375,8 +343,7 @@ MT_thread_init(void)
        }
        InitializeCriticalSection(&winthread_cs);
 #endif
-       allocated[0] = 1;
-       mainthread.tid = 1;
+       mainthread.tid = (MT_Id) &mainthread;
        mainthread.next = NULL;
        mtthreads = &mainthread;
        thread_initialized = true;
@@ -406,11 +373,7 @@ MT_thread_register(void)
        if (self == NULL)
                return false;
 
-       if ((mtid = alloc_thread()) == 0) {
-               TRC_DEBUG(IO_, "Too many threads\n");
-               GDKerror("too many threads\n");
-               return false;
-       }
+       mtid = (MT_Id) self;
        *self = (struct mtthread) {
                .detached = false,
 #ifdef HAVE_PTHREAD_H
@@ -824,24 +787,17 @@ MT_create_thread(MT_Id *t, void (*f) (vo
                TRC_CRITICAL(GDK, "Thread's name is too large\n");
                return -1;
        }
-       if ((mtid = alloc_thread()) == 0) {
-               TRC_DEBUG(IO_, "Too many threads\n");
-               GDKerror("too many threads\n");
-               return -1;
-       }
 
 #ifdef HAVE_PTHREAD_H
        pthread_attr_t attr;
        int ret;
        if ((ret = pthread_attr_init(&attr)) != 0) {
                GDKsyserr(ret, "Cannot init pthread attr");
-               dealloc_thread(mtid);
                return -1;
        }
        if ((ret = pthread_attr_setstacksize(&attr, THREAD_STACK_SIZE)) != 0) {
                GDKsyserr(ret, "Cannot set stack size");
                pthread_attr_destroy(&attr);
-               dealloc_thread(mtid);
                return -1;
        }
 #endif
@@ -851,9 +807,9 @@ MT_create_thread(MT_Id *t, void (*f) (vo
 #ifdef HAVE_PTHREAD_H
                pthread_attr_destroy(&attr);
 #endif
-               dealloc_thread(mtid);
                return -1;
        }
+       mtid = (MT_Id) self;
 
        *self = (struct mtthread) {
                .func = f,
@@ -867,9 +823,10 @@ MT_create_thread(MT_Id *t, void (*f) (vo
        strcpy_len(self->threadname, threadname, sizeof(self->threadname));
        char *p;
        if ((p = strstr(self->threadname, "XXXX")) != NULL) {
-               /* overwrite XXXX with thread ID */
+               /* overwrite XXXX with thread ID; bottom three bits are
+                * likely 0, so skip those */
                char buf[5];
-               snprintf(buf, 5, "%04zu", mtid % 9999);
+               snprintf(buf, 5, "%04zu", (mtid >> 3) % 9999);
                memcpy(p, buf, 4);
        }
        TRC_DEBUG(THRD, "Create thread \"%s\"\n", self->threadname);
@@ -889,7 +846,6 @@ MT_create_thread(MT_Id *t, void (*f) (vo
        if (ret != 0) {
                GDKsyserr(ret, "Cannot start thread");
                free(self);
-               dealloc_thread(mtid);
                return -1;
        }
 #else
@@ -898,7 +854,6 @@ MT_create_thread(MT_Id *t, void (*f) (vo
        if (self->hdl == NULL) {
                GDKwinerror("Failed to create thread");
                free(self);
-               dealloc_thread(mtid);
                return -1;
        }
 #endif
@@ -914,13 +869,13 @@ MT_create_thread(MT_Id *t, void (*f) (vo
 MT_Id
 MT_getpid(void)
 {
-       if (!thread_initialized)
-               return 0;
-
        struct mtthread *self;
 
-       self = thread_self();
-       return self ? self->tid : 0;
+       if (!thread_initialized)
+               self = &mainthread;
+       else
+               self = thread_self();
+       return self->tid;
 }
 
 void
_______________________________________________
checkin-list mailing list -- checkin-list@monetdb.org
To unsubscribe send an email to checkin-list-le...@monetdb.org

Reply via email to