Changeset: 739651dcbd34 for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB/rev/739651dcbd34
Modified Files:
gdk/gdk_system.c
gdk/gdk_utils.c
Branch: default
Log Message:
Code deduplication: combine Windows and Posix thread code.
diffs (truncated from 1474 to 300 lines):
diff --git a/gdk/gdk_system.c b/gdk/gdk_system.c
--- a/gdk/gdk_system.c
+++ b/gdk/gdk_system.c
@@ -182,55 +182,74 @@ GDKlockstatistics(int what)
static void MT_thread_setcondwait(MT_Cond *cond);
-#if !defined(HAVE_PTHREAD_H) && defined(WIN32)
-static struct winthread {
- struct winthread *next;
- HANDLE hdl;
- DWORD tid;
- void (*func) (void *);
- void *data;
+static struct mtthread {
+ struct mtthread *next;
+ void (*func) (void *); /* function to be called */
+ void *data; /* and its data */
MT_Lock *lockwait; /* lock we're waiting for */
MT_Sema *semawait; /* semaphore we're waiting for */
MT_Cond *condwait; /* condition variable we're waiting for */
#ifdef LOCK_OWNER
MT_Lock *mylocks; /* locks we're holding */
#endif
- struct winthread *joinwait; /* process we are joining with */
+ struct mtthread *joinwait; /* process we are joining with */
const char *working; /* what we're currently doing */
char algorithm[512]; /* the algorithm used in the last operation */
size_t algolen; /* length of string in .algorithm */
ATOMIC_TYPE exited;
bool detached:1, waiting:1, limit_override:1;
+ unsigned int refs:20;
char threadname[MT_NAME_LEN];
QryCtx *qry_ctx;
-} *winthreads = NULL;
-static struct winthread mainthread = {
+#ifdef HAVE_PTHREAD_H
+ pthread_t hdl;
+#else
+ HANDLE hdl;
+#endif
+ MT_Id tid;
+} *mtthreads = NULL;
+struct mtthread mainthread = {
.threadname = "main thread",
.exited = ATOMIC_VAR_INIT(0),
+ .refs = 1,
};
-
+#ifdef HAVE_PTHREAD_H
+static pthread_mutex_t posthread_lock = PTHREAD_MUTEX_INITIALIZER;
+static MT_Id MT_thread_id = 1;
+static pthread_key_t threadkey;
+#define thread_lock() pthread_mutex_lock(&posthread_lock)
+#define thread_unlock() pthread_mutex_unlock(&posthread_lock)
+#define thread_self() pthread_getspecific(threadkey)
+#define thread_setself(self) pthread_setspecific(threadkey, self)
+#else
static CRITICAL_SECTION winthread_cs;
static DWORD threadslot = TLS_OUT_OF_INDEXES;
+#define thread_lock() EnterCriticalSection(&winthread_cs)
+#define thread_unlock() LeaveCriticalSection(&winthread_cs)
+#define thread_self() TlsGetValue(threadslot)
+#define thread_setself(self) TlsSetValue(threadslot, self)
+#endif
+static bool thread_initialized = false;
void
dump_threads(void)
{
char buf[1024];
- EnterCriticalSection(&winthread_cs);
- for (struct winthread *w = winthreads; w; w = w->next) {
+ thread_lock();
+ for (struct mtthread *t = mtthreads; t; t = t->next) {
int pos = snprintf(buf, sizeof(buf),
"%s, waiting for %s, working on %.200s",
- w->threadname,
- w->lockwait ? w->lockwait->name :
- w->semawait ? w->semawait->name :
- w->condwait ? w->condwait->name :
- w->joinwait ? w->joinwait->threadname :
+ t->threadname,
+ t->lockwait ? t->lockwait->name :
+ t->semawait ? t->semawait->name :
+ t->condwait ? t->condwait->name :
+ t->joinwait ? t->joinwait->threadname :
"nothing",
- ATOMIC_GET(&w->exited) ? "exiting" :
- w->working ? w->working : "nothing");
+ ATOMIC_GET(&t->exited) ? "exiting" :
+ t->working ? t->working : "nothing");
#ifdef LOCK_OWNER
const char *sep = ", locked: ";
- for (MT_Lock *l = w->mylocks; l && pos < (int) sizeof(buf); l =
l->nxt) {
+ for (MT_Lock *l = t->mylocks; l && pos < (int) sizeof(buf); l =
l->nxt) {
pos += snprintf(buf + pos, sizeof(buf) - pos,
"%s%s(%s)", sep, l->name, l->locker);
sep = ", ";
@@ -241,577 +260,61 @@ dump_threads(void)
else
printf("%s%s\n", buf, pos >= (int) sizeof(buf) ? "..."
: "");
}
- LeaveCriticalSection(&winthread_cs);
+ thread_unlock();
+}
+
+static void
+rm_mtthread(struct mtthread *t)
+{
+ struct mtthread **pt;
+
+ thread_lock();
+ for (pt = &mtthreads; *pt && *pt != t; pt = &(*pt)->next)
+ ;
+ if (*pt)
+ *pt = t->next;
+ ATOMIC_DESTROY(&t->exited);
+ free(t);
+ thread_unlock();
}
bool
MT_thread_init(void)
{
- if (threadslot == TLS_OUT_OF_INDEXES) {
- threadslot = TlsAlloc();
- if (threadslot == TLS_OUT_OF_INDEXES) {
- GDKwinerror("Creating thread-local slot for thread
failed");
- return false;
- }
- mainthread.tid = GetCurrentThreadId();
- if (TlsSetValue(threadslot, &mainthread) == 0) {
- GDKwinerror("Setting thread-local value failed");
- TlsFree(threadslot);
- threadslot = TLS_OUT_OF_INDEXES;
- return false;
- }
- InitializeCriticalSection(&winthread_cs);
- }
- return true;
-}
-
-static void
-rm_winthread(struct winthread *w)
-{
- struct winthread **wp;
-
- EnterCriticalSection(&winthread_cs);
- for (wp = &winthreads; *wp && *wp != w; wp = &(*wp)->next)
- ;
- if (*wp)
- *wp = w->next;
- LeaveCriticalSection(&winthread_cs);
- ATOMIC_DESTROY(&w->exited);
- free(w);
-}
-
-bool
-MT_thread_register(void)
-{
- assert(threadslot != TLS_OUT_OF_INDEXES);
- if (threadslot == TLS_OUT_OF_INDEXES)
- return false;
-
- struct winthread *w;
-
- if ((w = TlsGetValue(threadslot)) != NULL)
- return false;
-
- w = malloc(sizeof(*w));
- if (w == NULL)
- return false;
-
- EnterCriticalSection(&winthread_cs);
- *w = (struct winthread) {
- .detached = false,
- .tid = GetCurrentThreadId(),
- };
- snprintf(w->threadname, sizeof(w->threadname),
- "foreign %zu", (MT_Id) w->tid);
- Thread t = THRnew(w->threadname, w->tid);
- if (t == NULL) {
- free(w);
- LeaveCriticalSection(&winthread_cs);
- return false;
- }
- w->data = t;
- ATOMIC_INIT(&w->exited, 0);
- TlsSetValue(threadslot, w);
- w->next = winthreads;
- winthreads = w;
- LeaveCriticalSection(&winthread_cs);
- return true;
-}
-
-void
-MT_thread_deregister(void)
-{
- struct winthread *w;
-
- if ((w = TlsGetValue(threadslot)) == NULL)
- return;
-
- THRdel(w->data);
-
- rm_winthread(w);
-}
-
-static struct winthread *
-find_winthread(DWORD tid)
-{
- struct winthread *w;
-
- EnterCriticalSection(&winthread_cs);
- for (w = winthreads; w && w->tid != tid; w = w->next)
- ;
- LeaveCriticalSection(&winthread_cs);
- return w;
-}
-
-const char *
-MT_thread_getname(void)
-{
- if (threadslot == TLS_OUT_OF_INDEXES)
- return mainthread.threadname;
- struct winthread *w = TlsGetValue(threadslot);
- return w ? w->threadname : UNKNOWN_THREAD;
-}
-
-void
-MT_thread_setdata(void *data)
-{
- if (threadslot == TLS_OUT_OF_INDEXES)
- return;
- struct winthread *w = TlsGetValue(threadslot);
-
- if (w)
- w->data = data;
-}
-
-void *
-MT_thread_getdata(void)
-{
- if (threadslot == TLS_OUT_OF_INDEXES)
- return NULL;
- struct winthread *w = TlsGetValue(threadslot);
-
- return w ? w->data : NULL;
-}
-
-void
-MT_thread_set_qry_ctx(QryCtx *ctx)
-{
- if (threadslot == TLS_OUT_OF_INDEXES)
- return;
- struct winthread *w = TlsGetValue(threadslot);
-
- if (w)
- w->qry_ctx = ctx;
-}
-
-QryCtx *
-MT_thread_get_qry_ctx(void)
-{
- if (threadslot == TLS_OUT_OF_INDEXES)
- return NULL;
- struct winthread *w = TlsGetValue(threadslot);
-
- return w ? w->qry_ctx : NULL;
-}
-
-void
-MT_thread_setlockwait(MT_Lock *lock)
-{
- if (threadslot == TLS_OUT_OF_INDEXES)
- return;
- struct winthread *w = TlsGetValue(threadslot);
-
- if (w)
- w->lockwait = lock;
-}
-
-void
-MT_thread_setsemawait(MT_Sema *sema)
-{
- if (threadslot == TLS_OUT_OF_INDEXES)
- return;
- struct winthread *w = TlsGetValue(threadslot);
-
- if (w)
- w->semawait = sema;
-}
-
-static void
-MT_thread_setcondwait(MT_Cond *cond)
-{
- if (threadslot == TLS_OUT_OF_INDEXES)
- return;
- struct winthread *w = TlsGetValue(threadslot);
_______________________________________________
checkin-list mailing list -- [email protected]
To unsubscribe send an email to [email protected]