Changeset: 67dbd1ca0e08 for MonetDB
URL: http://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=67dbd1ca0e08
Modified Files:
gdk/gdk_system.c
gdk/gdk_system_private.h
gdk/gdk_utils.c
monetdb5/modules/mal/mal_mapi.c
Branch: Jun2016
Log Message:
Start thread serving client as detached thread.
When started as joinable and registered thread, we only join at server
exit, causing a limit in the number of times we can fork off a thread
(and also a massive use of memory).
diffs (94 lines):
diff --git a/gdk/gdk_system.c b/gdk/gdk_system.c
--- a/gdk/gdk_system.c
+++ b/gdk/gdk_system.c
@@ -497,6 +497,30 @@ join_threads(void)
pthread_mutex_unlock(&posthread_lock);
}
+void
+join_detached_threads(void)
+{
+ struct posthread *p;
+ int waited;
+ pthread_t tid;
+
+ pthread_mutex_lock(&posthread_lock);
+ do {
+ waited = 0;
+ for (p = posthreads; p; p = p->next) {
+ tid = p->tid;
+ rm_posthread_locked(p);
+ free(p);
+ pthread_mutex_unlock(&posthread_lock);
+ pthread_join(tid, NULL);
+ pthread_mutex_lock(&posthread_lock);
+ waited = 1;
+ break;
+ }
+ } while (waited);
+ pthread_mutex_unlock(&posthread_lock);
+}
+
int
MT_create_thread(MT_Id *t, void (*f) (void *), void *arg, enum MT_thr_detach d)
{
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
@@ -12,6 +12,8 @@
#error this file should not be included outside its source directory
#endif
+__hidden void join_detached_threads(void)
+ __attribute__((__visibility__("hidden")));
__declspec(noreturn) void MT_exit_thread(int status)
__attribute__((__noreturn__));
__hidden void MT_global_exit(int status)
diff --git a/gdk/gdk_utils.c b/gdk/gdk_utils.c
--- a/gdk/gdk_utils.c
+++ b/gdk/gdk_utils.c
@@ -749,6 +749,7 @@ GDKreset(int status)
GDKfree(st);
}
MT_lock_unset(&GDKthreadLock);
+ join_detached_threads();
if (status == 0) {
/* they had their chance, now kill them */
diff --git a/monetdb5/modules/mal/mal_mapi.c b/monetdb5/modules/mal/mal_mapi.c
--- a/monetdb5/modules/mal/mal_mapi.c
+++ b/monetdb5/modules/mal/mal_mapi.c
@@ -392,7 +392,7 @@ SERVERlistenThread(SOCKET *Sock)
"cannot allocate stream");
continue;
}
- if (MT_create_thread(&tid, doChallenge, data, MT_THR_JOINABLE))
{
+ if (MT_create_thread(&tid, doChallenge, data, MT_THR_DETACHED))
{
mnstr_destroy(data->in);
mnstr_destroy(data->out);
GDKfree(data);
@@ -401,7 +401,6 @@ SERVERlistenThread(SOCKET *Sock)
"cannot fork new client
thread");
continue;
}
- GDKregister(tid);
} while (!ATOMIC_GET(serverexiting, atomicLock) &&
!GDKexiting());
(void) ATOMIC_DEC(nlistener, atomicLock);
@@ -789,7 +788,7 @@ SERVERclient(void *res, const Stream *In
throw(MAL,"serverClient",MAL_MALLOC_FAIL);
data->in = *In;
data->out = *Out;
- if (MT_create_thread(&tid, doChallenge, data, MT_THR_JOINABLE)) {
+ if (MT_create_thread(&tid, doChallenge, data, MT_THR_DETACHED)) {
mnstr_printf(data->out, "!internal server error (cannot fork
new "
"client thread), please try again
later\n");
mnstr_flush(data->out);
@@ -797,7 +796,6 @@ SERVERclient(void *res, const Stream *In
"cannot fork new client thread");
free(data);
}
- GDKregister(tid);
return MAL_SUCCEED;
}
_______________________________________________
checkin-list mailing list
[email protected]
https://www.monetdb.org/mailman/listinfo/checkin-list