Changeset: 108f859fcf3a for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=108f859fcf3a
Modified Files:
clients/Tests/exports.stable.out
gdk/gdk_system.c
gdk/gdk_system.h
gdk/gdk_utils.c
monetdb5/mal/mal_exception.c
sql/server/sql_mvc.c
Branch: Apr2019
Log Message:
Make more use of thread-local (specific) data + some cleanup.
diffs (truncated from 529 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
@@ -314,8 +314,11 @@ void *MT_mmap(const char *path, int mode
int MT_munmap(void *p, size_t len);
bool MT_path_absolute(const char *path);
void MT_sleep_ms(unsigned int ms);
+void *MT_thread_getdata(void);
+const char *MT_thread_getname(void);
+struct thread_info *MT_thread_info(int *nthreads);
bool MT_thread_init(void);
-const char *MT_thread_name(void);
+void MT_thread_setdata(void *data);
void MT_thread_setname(const char *name);
void OIDXdestroy(BAT *b);
ssize_t OIDfromStr(const char *src, size_t *len, oid **dst, bool external);
diff --git a/gdk/gdk_system.c b/gdk/gdk_system.c
--- a/gdk/gdk_system.c
+++ b/gdk/gdk_system.c
@@ -159,7 +159,7 @@ static struct winthread {
HANDLE hdl;
DWORD tid;
void (*func) (void *);
- void *arg;
+ void *data;
bool exited:1, detached:1, waiting:1;
const char *threadname;
} *winthreads = NULL;
@@ -209,7 +209,7 @@ find_winthread(DWORD tid)
}
const char *
-MT_thread_name(void)
+MT_thread_getname(void)
{
struct winthread *w = TlsGetValue(threadslot);
return w && w->threadname ? w->threadname : "unknown thread";
@@ -228,6 +228,23 @@ MT_thread_setname(const char *name)
}
void
+MT_thread_setdata(void *data)
+{
+ struct winthread *w = TlsGetValue(threadslot);
+
+ if (w)
+ w->data = data;
+}
+
+void *
+MT_thread_getdata(void)
+{
+ struct winthread *w = TlsGetValue(threadslot);
+
+ return w ? w->data : NULL;
+}
+
+void
gdk_system_reset(void)
{
assert(threadslot != TLS_OUT_OF_INDEXES);
@@ -254,9 +271,11 @@ static DWORD WINAPI
thread_starter(LPVOID arg)
{
struct winthread *w = (struct winthread *) arg;
+ void *data = w->data;
+ w->data = NULL;
TlsSetValue(threadslot, w);
- (*w->func)(w->arg);
+ (*w->func)(data);
EnterCriticalSection(&winthread_cs);
w->exited = true;
LeaveCriticalSection(&winthread_cs);
@@ -324,7 +343,7 @@ MT_create_thread(MT_Id *t, void (*f) (vo
w->func = f;
w->hdl = NULL;
w->tid = 0;
- w->arg = arg;
+ w->data = arg;
w->exited = false;
w->waiting = false;
w->detached = (d == MT_THR_DETACHED);
@@ -339,6 +358,7 @@ MT_create_thread(MT_Id *t, void (*f) (vo
rm_winthread(w);
return -1;
}
+ /* must not fail after this: the thread has been started */
*t = (MT_Id) w->tid;
return 0;
}
@@ -471,7 +491,7 @@ pthread_sema_down(pthread_sema_t *s)
static struct posthread {
struct posthread *next;
void (*func)(void *);
- void *arg;
+ void *data;
const char *threadname;
pthread_t tid;
MT_Id mtid;
@@ -530,7 +550,7 @@ MT_thread_setname(const char *name)
}
const char *
-MT_thread_name(void)
+MT_thread_getname(void)
{
struct posthread *p;
@@ -538,6 +558,23 @@ MT_thread_name(void)
return p && p->threadname ? p->threadname : "unknown thread";
}
+void
+MT_thread_setdata(void *data)
+{
+ struct posthread *p = pthread_getspecific(threadkey);
+
+ if (p)
+ p->data = data;
+}
+
+void *
+MT_thread_getdata(void)
+{
+ struct posthread *p = pthread_getspecific(threadkey);
+
+ return p ? p->data : NULL;
+}
+
#ifdef HAVE_PTHREAD_SIGMASK
static void
MT_thread_sigmask(sigset_t *new_mask, sigset_t *orig_mask)
@@ -574,9 +611,11 @@ static void *
thread_starter(void *arg)
{
struct posthread *p = (struct posthread *) arg;
+ void *data = p->data;
+ p->data = NULL;
pthread_setspecific(threadkey, p);
- (*p->func)(p->arg);
+ (*p->func)(data);
pthread_mutex_lock(&posthread_lock);
p->exited = true;
pthread_mutex_unlock(&posthread_lock);
@@ -660,7 +699,7 @@ MT_create_thread(MT_Id *t, void (*f) (vo
}
p->tid = 0;
p->func = f;
- p->arg = arg;
+ p->data = arg;
p->exited = false;
p->waiting = false;
p->detached = (d == MT_THR_DETACHED);
@@ -682,6 +721,8 @@ MT_create_thread(MT_Id *t, void (*f) (vo
strerror(ret));
rm_posthread(p);
ret = -1;
+ } else {
+ /* must not fail after this: the thread has been started */
}
#ifdef HAVE_PTHREAD_SIGMASK
MT_thread_sigmask(&orig_mask, NULL);
diff --git a/gdk/gdk_system.h b/gdk/gdk_system.h
--- a/gdk/gdk_system.h
+++ b/gdk/gdk_system.h
@@ -116,8 +116,10 @@ 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,
const char *threadname);
-gdk_export const char *MT_thread_name(void);
+gdk_export const char *MT_thread_getname(void);
gdk_export void MT_thread_setname(const char *name);
+gdk_export void *MT_thread_getdata(void);
+gdk_export void MT_thread_setdata(void *data);
gdk_export void MT_exiting_thread(void);
gdk_export MT_Id MT_getpid(void);
gdk_export int MT_join_thread(MT_Id t);
@@ -231,7 +233,7 @@ gdk_export ATOMIC_TYPE volatile GDKlockc
gdk_export ATOMIC_TYPE volatile GDKlockcontentioncnt;
gdk_export ATOMIC_TYPE volatile GDKlocksleepcnt;
#define _DBG_LOCK_COUNT_0(l, n) (void) ATOMIC_INC(GDKlockcnt,
dummy)
-#define _DBG_LOCK_LOCKER(l, n) ((l)->locker = (n), (l)->thread =
MT_thread_name())
+#define _DBG_LOCK_LOCKER(l, n) ((l)->locker = (n), (l)->thread =
MT_thread_getname())
#define _DBG_LOCK_CONTENTION(l, n) \
do { \
TEMDEBUG fprintf(stderr, "#lock %s contention in %s\n", \
diff --git a/gdk/gdk_utils.c b/gdk/gdk_utils.c
--- a/gdk/gdk_utils.c
+++ b/gdk/gdk_utils.c
@@ -730,22 +730,18 @@ void
GDKreset(int status, int doexit)
{
MT_Id pid = MT_getpid();
- Thread t, s;
- struct serverthread *st;
- int farmid;
- int i;
- if( GDKkey){
+ if (GDKkey) {
BBPunfix(GDKkey->batCacheid);
- GDKkey = 0;
+ GDKkey = NULL;
}
- if( GDKval){
+ if (GDKval) {
BBPunfix(GDKval->batCacheid);
- GDKval = 0;
+ GDKval = NULL;
}
MT_lock_set(&GDKthreadLock);
- for (st = serverthread; st; st = serverthread) {
+ for (struct serverthread *st = serverthread; st; st = serverthread) {
MT_lock_unset(&GDKthreadLock);
MT_join_thread(st->pid);
MT_lock_set(&GDKthreadLock);
@@ -759,7 +755,7 @@ GDKreset(int status, int doexit)
/* they had their chance, now kill them */
int killed = 0;
MT_lock_set(&GDKthreadLock);
- for (t = GDKthreads, s = t + THREADS; t < s; t++) {
+ for (Thread t = GDKthreads; t < GDKthreads + THREADS; t++) {
if (t->pid) {
MT_Id victim = t->pid;
@@ -783,14 +779,13 @@ GDKreset(int status, int doexit)
}
GDKlog(GET_GDKLOCK(PERSISTENT), GDKLOGOFF);
- for (farmid = 0; farmid < MAXFARMS; farmid++) {
+ for (int farmid = 0; farmid < MAXFARMS; farmid++) {
if (BBPfarms[farmid].dirname != NULL) {
- int skip = 0;
- int j;
- for (j = 0; j < farmid; j++) {
+ bool skip = false;
+ for (int j = 0; j < farmid; j++) {
if (BBPfarms[j].dirname != NULL &&
strcmp(BBPfarms[farmid].dirname,
BBPfarms[j].dirname) == 0) {
- skip = 1;
+ skip = true;
break;
}
}
@@ -820,12 +815,12 @@ GDKreset(int status, int doexit)
GDKnrofthreads = 0;
close_stream((stream *) THRdata[0]);
close_stream((stream *) THRdata[1]);
- for (i = 0; i <= BBP_BATMASK; i++) {
+ for (int i = 0; i <= BBP_BATMASK; i++) {
MT_lock_destroy(&GDKbatLock[i].swap);
MT_lock_destroy(&GDKbatLock[i].hash);
MT_lock_destroy(&GDKbatLock[i].imprints);
}
- for (i = 0; i <= BBP_THREADMASK; i++) {
+ for (int i = 0; i <= BBP_THREADMASK; i++) {
MT_lock_destroy(&GDKbbpLock[i].cache);
MT_lock_destroy(&GDKbbpLock[i].trim);
GDKbbpLock[i].free = 0;
@@ -1017,7 +1012,7 @@ doGDKaddbuf(const char *prefix, const ch
(int) messagelen, message, suffix);
}
fprintf(stderr, "#%s:%s%.*s%s",
- MT_thread_name(),
+ MT_thread_getname(),
prefix[0] == '#' ? prefix + 1 : prefix,
(int) messagelen, message, suffix);
}
@@ -1339,33 +1334,43 @@ THRsp(void)
static Thread
GDK_find_thread(MT_Id pid)
{
- Thread t, s;
-
- for (t = GDKthreads, s = t + THREADS; t < s; t++) {
- if (t->pid && t->pid == pid) {
+ MT_lock_set(&GDKthreadLock);
+ for (Thread t = GDKthreads; t < GDKthreads + THREADS; t++) {
+ if (t->pid == pid) {
+ MT_lock_unset(&GDKthreadLock);
return t;
}
}
+ MT_lock_unset(&GDKthreadLock);
return NULL;
}
+static Thread
+GDK_find_self(void)
+{
+ Thread t;
+
+ if ((t = MT_thread_getdata()) != NULL) /* should succeed */
+ return t;
+ return GDK_find_thread(MT_getpid());
+}
+
_______________________________________________
checkin-list mailing list
[email protected]
https://www.monetdb.org/mailman/listinfo/checkin-list