libthread: handle spurious _procsleep wakeups, fix $LIBTHREAD handling
This commit is contained in:
parent
baef953da2
commit
162d0d5cd9
2 changed files with 26 additions and 6 deletions
|
|
@ -50,13 +50,15 @@ _threadunlock(Lock *lk, ulong pc)
|
||||||
abort();
|
abort();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* note: _procsleep can have spurious wakeups, like pthread_cond_wait */
|
||||||
void
|
void
|
||||||
_procsleep(_Procrendez *r)
|
_procsleep(_Procrendez *r)
|
||||||
{
|
{
|
||||||
/* r is protected by r->l, which we hold */
|
/* r is protected by r->l, which we hold */
|
||||||
pthread_cond_init(&r->cond, 0);
|
pthread_cond_init(&r->cond, 0);
|
||||||
r->asleep = 1;
|
r->asleep = 1;
|
||||||
pthread_cond_wait(&r->cond, &r->l->mutex);
|
if(pthread_cond_wait(&r->cond, &r->l->mutex) != 0)
|
||||||
|
sysfatal("pthread_cond_wait: %r");
|
||||||
pthread_cond_destroy(&r->cond);
|
pthread_cond_destroy(&r->cond);
|
||||||
r->asleep = 0;
|
r->asleep = 0;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -54,9 +54,9 @@ _threaddebug(char *fmt, ...)
|
||||||
va_end(arg);
|
va_end(arg);
|
||||||
t = proc()->thread;
|
t = proc()->thread;
|
||||||
if(t)
|
if(t)
|
||||||
fprint(fd, "%d.%d: %s\n", getpid(), t->id, buf);
|
fprint(fd, "%p %d.%d: %s\n", proc(), getpid(), t->id, buf);
|
||||||
else
|
else
|
||||||
fprint(fd, "%d._: %s\n", getpid(), buf);
|
fprint(fd, "%p %d._: %s\n", proc(), getpid(), buf);
|
||||||
}
|
}
|
||||||
|
|
||||||
static _Thread*
|
static _Thread*
|
||||||
|
|
@ -219,20 +219,28 @@ proccreate(void (*fn)(void*), void *arg, uint stack)
|
||||||
static void
|
static void
|
||||||
procswitch(Proc *p, _Thread *from, _Thread *to)
|
procswitch(Proc *p, _Thread *from, _Thread *to)
|
||||||
{
|
{
|
||||||
// fprint(2, "procswitch %p %d %d\n", p, from?from->id:-1, to?to->id:-1);
|
_threaddebug("procswitch %p %d %d", p, from?from->id:-1, to?to->id:-1);
|
||||||
lock(&p->schedlock);
|
lock(&p->schedlock);
|
||||||
from->schedrend.l = &p->schedlock;
|
from->schedrend.l = &p->schedlock;
|
||||||
if(to) {
|
if(to) {
|
||||||
p->schedthread = to;
|
p->schedthread = to;
|
||||||
to->schedrend.l = &p->schedlock;
|
to->schedrend.l = &p->schedlock;
|
||||||
|
_threaddebug("procswitch wakeup %p %d", p, to->id);
|
||||||
_procwakeup(&to->schedrend);
|
_procwakeup(&to->schedrend);
|
||||||
}
|
}
|
||||||
if(p->schedthread != from) {
|
if(p->schedthread != from) {
|
||||||
if(from->exiting) {
|
if(from->exiting) {
|
||||||
unlock(&p->schedlock);
|
unlock(&p->schedlock);
|
||||||
_threadpexit();
|
_threadpexit();
|
||||||
|
_threaddebug("procswitch exit wakeup!!!\n");
|
||||||
}
|
}
|
||||||
_procsleep(&from->schedrend);
|
while(p->schedthread != from) {
|
||||||
|
_threaddebug("procswitch sleep %p %d", p, from->id);
|
||||||
|
_procsleep(&from->schedrend);
|
||||||
|
_threaddebug("procswitch awake %p %d", p, from->id);
|
||||||
|
}
|
||||||
|
if(p->schedthread != from)
|
||||||
|
sysfatal("_procswitch %p %p oops", p->schedthread, from);
|
||||||
}
|
}
|
||||||
unlock(&p->schedlock);
|
unlock(&p->schedlock);
|
||||||
}
|
}
|
||||||
|
|
@ -448,6 +456,7 @@ Top:
|
||||||
procswitch(p, p->thread0, t);
|
procswitch(p, p->thread0, t);
|
||||||
else
|
else
|
||||||
contextswitch(&p->schedcontext, &t->context);
|
contextswitch(&p->schedcontext, &t->context);
|
||||||
|
_threaddebug("back in scheduler");
|
||||||
/*print("back in scheduler\n"); */
|
/*print("back in scheduler\n"); */
|
||||||
goto Top;
|
goto Top;
|
||||||
}
|
}
|
||||||
|
|
@ -589,6 +598,10 @@ threadqlock(QLock *l, int block, ulong pc)
|
||||||
if(l->owner == nil){
|
if(l->owner == nil){
|
||||||
l->owner = (*threadnow)();
|
l->owner = (*threadnow)();
|
||||||
/*print("qlock %p @%#x by %p\n", l, pc, l->owner); */
|
/*print("qlock %p @%#x by %p\n", l, pc, l->owner); */
|
||||||
|
if(l->owner == nil) {
|
||||||
|
fprint(2, "%s: qlock uncontended owner=nil oops\n", argv0);
|
||||||
|
abort();
|
||||||
|
}
|
||||||
unlock(&l->l);
|
unlock(&l->l);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
@ -613,6 +626,11 @@ threadqlock(QLock *l, int block, ulong pc)
|
||||||
argv0, pc, l->owner, (*threadnow)());
|
argv0, pc, l->owner, (*threadnow)());
|
||||||
abort();
|
abort();
|
||||||
}
|
}
|
||||||
|
if(l->owner == nil) {
|
||||||
|
fprint(2, "%s: qlock threadswitch owner=nil oops\n", argv0);
|
||||||
|
abort();
|
||||||
|
}
|
||||||
|
|
||||||
/*print("qlock wakeup %p @%#x by %p\n", l, pc, (*threadnow)()); */
|
/*print("qlock wakeup %p @%#x by %p\n", l, pc, (*threadnow)()); */
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
@ -818,7 +836,7 @@ main(int argc, char **argv)
|
||||||
// Easier to just run in pthread-per-thread mode.
|
// Easier to just run in pthread-per-thread mode.
|
||||||
pthreadperthread = 1;
|
pthreadperthread = 1;
|
||||||
#endif
|
#endif
|
||||||
if(strstr(opts, "nodaemon") || getenv("NOLIBTHREADDAEMONIZE") == nil)
|
if(strstr(opts, "nodaemon") == nil && getenv("NOLIBTHREADDAEMONIZE") == nil)
|
||||||
_threadsetupdaemonize();
|
_threadsetupdaemonize();
|
||||||
|
|
||||||
threadargc = argc;
|
threadargc = argc;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue