Commit Diff


commit - fd4655403059a7b5c042e3ba206b9f3f94b9f831
commit + 4dbefdd41ca866a10cee633a3ee9a67d9204b052
blob - 0be8aec42a106b0e5857af4fd3b616b1c785f217
blob + a301d48f122092d8bd34d14b495abd352ce1a62b
--- src/libthread/mkfile
+++ src/libthread/mkfile
@@ -1,12 +1,14 @@
 <$PLAN9/src/mkhdr
 
+SYSOFILES=`{sh ./sysofiles.sh}
+
 LIB=libthread.a
 OFILES=\
+	$SYSOFILES\
 	channel.$O\
 	exec.$O\
 	ioproc.$O\
 	iorw.$O\
-	pthread.$O\
 	ref.$O\
 	thread.$O\
 
blob - 6540605d373cdf3efbbe70b2baad09a828ebbd2b
blob + 2ddd8c72777c7d410ba56c615e54b4725aa727bb
--- src/libthread/pthread.c
+++ src/libthread/pthread.c
@@ -82,8 +82,8 @@ startprocfn(void *v)
 	fn = a[0];
 	p = a[1];
 	free(a);
-	p->tid = pthread_self();
-	pthread_detach(p->tid);
+	p->osprocid = pthread_self();
+	pthread_detach(p->osprocid);
 
 	(*fn)(p);
 
@@ -101,7 +101,7 @@ _procstart(Proc *p, void (*fn)(Proc*))
 	a[0] = fn;
 	a[1] = p;
 
-	if(pthread_create(&p->tid, nil, (void*(*)(void*))startprocfn, (void*)a) < 0){
+	if(pthread_create(&p->osprocid, nil, (void*(*)(void*))startprocfn, (void*)a) < 0){
 		fprint(2, "pthread_create: %r\n");
 		abort();
 	}
blob - 91af5a73318cb73acd0c51a826a23a0868546ff5
blob + 8fcfa2e2c8577d9c632bbcb58aba01ebda9f9b46
--- src/libthread/test/tprimes.c
+++ src/libthread/test/tprimes.c
@@ -77,4 +77,5 @@ threadmain(int argc, char **argv)
 	c = chancreate(sizeof(ulong), nbuf);
 	mk(countthread, c, STACK);
 	mk(filterthread, c, STACK);
+	recvp(chancreate(sizeof(void*), 0));
 }
blob - 158f6e9c708c43d73b00c5e1530c86217df75664
blob + 4116ac22ea4f9f0bccfd02fa313b97bb9bc26174
--- src/libthread/thread.c
+++ src/libthread/thread.c
@@ -1,18 +1,18 @@
 #include "u.h"
-#include <linux/unistd.h>
 #include "libc.h"
 #include "thread.h"
 #include "threadimpl.h"
 
-       _syscall0(pid_t,gettid)
-
 int	_threaddebuglevel;
 
 static	uint		threadnproc;
 static	uint		threadnsysproc;
 static	Lock		threadnproclock;
 static	Ref		threadidref;
+static	Proc		*threadmainproc;
 
+static	void		addproc(Proc*);
+static	void		delproc(Proc*);
 static	void		addthread(_Threadlist*, _Thread*);
 static	void		delthread(_Threadlist*, _Thread*);
 static	void		addthreadinproc(Proc*, _Thread*);
@@ -36,6 +36,7 @@ procalloc(void)
 	if(p == nil)
 		sysfatal("procalloc malloc: %r");
 	memset(p, 0, sizeof *p);
+	addproc(p);
 	lock(&threadnproclock);
 	threadnproc++;
 	unlock(&threadnproclock);
@@ -49,7 +50,7 @@ threadstart(void *v)
 
 	t = v;
 	t->startfn(t->startarg);
-	_threadexit();
+	threadexits(nil);
 }
 
 static _Thread*
@@ -115,9 +116,7 @@ proccreate(void (*fn)(void*), void *arg, uint stack)
 	Proc *p;
 
 	p = procalloc();
-//print("pa %p\n", p);
 	t = _threadcreate(p, fn, arg, stack);
-//print("ps %p\n", p);
 	_procstart(p, scheduler);
 	return t->id;
 }
@@ -151,32 +150,18 @@ threadyield(void)
 }
 
 void
-_threadexit(void)
-{
-	proc()->thread->exiting = 1;
-	_threadswitch();
-}
-
-void
 threadexits(char *msg)
 {
-/*
 	Proc *p;
 
 	p = proc();
+	if(msg == nil)
+		msg = "";
 	utfecpy(p->msg, p->msg+sizeof p->msg, msg);
-*/
-	_threadexit();
+	proc()->thread->exiting = 1;
+	_threadswitch();
 }
 
-void
-threadexitsall(char *msg)
-{
-	if(msg && msg[0])
-		exit(1);
-	exit(0);
-}
-
 static void
 contextswitch(Context *from, Context *to)
 {
@@ -216,11 +201,12 @@ scheduler(Proc *p)
 	}
 
 Out:
+	delproc(p);
 	lock(&threadnproclock);
 	if(p->sysproc)
 		--threadnsysproc;
 	if(--threadnproc == threadnsysproc)
-		exit(0);
+		threadexitsall(p->msg);
 	unlock(&threadnproclock);
 	unlock(&p->lock);
 	free(p);
@@ -237,6 +223,19 @@ _threadsetsysproc(void)
 	proc()->sysproc = 1;
 }
 
+void**
+procdata(void)
+{
+	return &proc()->udata;
+}
+
+extern Jmp *(*_notejmpbuf)(void);
+static Jmp*
+threadnotejmp(void)
+{
+	return &proc()->sigjmp;
+}
+
 /*
  * debugging
  */
@@ -422,6 +421,55 @@ threadrwakeup(Rendez *r, int all, ulong pc)
 }
 
 /*
+ * startup
+ */
+
+static int threadargc;
+static char **threadargv;
+int mainstacksize;
+
+static void
+threadmainstart(void *v)
+{
+	USED(v);
+	threadmainproc = proc();
+	threadmain(threadargc, threadargv);
+}
+
+int
+main(int argc, char **argv)
+{
+	Proc *p;
+
+	threadargc = argc;
+	threadargv = argv;
+
+	/*
+	 * Install locking routines into C library.
+	 */
+	_lock = _threadlock;
+	_unlock = _threadunlock;
+	_qlock = threadqlock;
+	_qunlock = threadqunlock;
+	_rlock = threadrlock;
+	_runlock = threadrunlock;
+	_wlock = threadwlock;
+	_wunlock = threadwunlock;
+	_rsleep = threadrsleep;
+	_rwakeup = threadrwakeup;
+	_notejmpbuf = threadnotejmp;
+
+	_pthreadinit();
+	p = procalloc();
+	_threadsetproc(p);
+	if(mainstacksize == 0)
+		mainstacksize = 65536;
+	_threadcreate(p, threadmainstart, nil, mainstacksize);
+	scheduler(p);
+	return 0;	/* not reached */
+}
+
+/*
  * hooray for linked lists
  */
 static void
@@ -484,58 +532,37 @@ delthreadinproc(Proc *p, _Thread *t)
 		l->tail = t->allprev;
 }
 
-void**
-procdata(void)
-{
-	return &proc()->udata;
-}
+Proc *_threadprocs;
+Lock _threadprocslock;
+static Proc *_threadprocstail;
 
-static int threadargc;
-static char **threadargv;
-int mainstacksize;
-
 static void
-threadmainstart(void *v)
+addproc(Proc *p)
 {
-	USED(v);
-	threadmain(threadargc, threadargv);
+	lock(&_threadprocslock);
+	if(_threadprocstail){
+		_threadprocstail->next = p;
+		p->prev = _threadprocstail;
+	}else{
+		_threadprocs = p;
+		p->prev = nil;
+	}
+	_threadprocstail = p;
+	p->next = nil;
+	unlock(&_threadprocslock);
 }
 
-extern Jmp *(*_notejmpbuf)(void);
-static Jmp*
-threadnotejmp(void)
+static void
+delproc(Proc *p)
 {
-	return &proc()->sigjmp;
+	lock(&_threadprocslock);
+	if(p->prev)
+		p->prev->next = p->next;
+	else
+		_threadprocs = p->next;
+	if(p->next)
+		p->next->prev = p->prev;
+	else
+		_threadprocstail = p->prev;
+	unlock(&_threadprocslock);
 }
-
-int
-main(int argc, char **argv)
-{
-	Proc *p;
-
-	threadargc = argc;
-	threadargv = argv;
-
-	/*
-	 * Install locking routines into C library.
-	 */
-	_lock = _threadlock;
-	_unlock = _threadunlock;
-	_qlock = threadqlock;
-	_qunlock = threadqunlock;
-	_rlock = threadrlock;
-	_runlock = threadrunlock;
-	_wlock = threadwlock;
-	_wunlock = threadwunlock;
-	_rsleep = threadrsleep;
-	_rwakeup = threadrwakeup;
-	_notejmpbuf = threadnotejmp;
-
-	_pthreadinit();
-	p = procalloc();
-	if(mainstacksize == 0)
-		mainstacksize = 65536;
-	_threadcreate(p, threadmainstart, nil, mainstacksize);
-	scheduler(p);
-	return 0;	/* not reached */
-}
blob - e88e321cd7cbedae4b2eaf8dd88ebff8bec33aee
blob + d30d58b6e5a1ab1fde33970803c7a4211dae847d
--- src/libthread/threadimpl.h
+++ src/libthread/threadimpl.h
@@ -42,7 +42,11 @@ struct _Procrendez
 {
 	Lock		*l;
 	int		asleep;
+#ifdef PLAN9PORT_USING_PTHREADS
 	pthread_cond_t	cond;
+#else
+	int		pid;
+#endif
 };
 
 extern	void	_procsleep(_Procrendez*);
@@ -50,7 +54,14 @@ extern	void	_procwakeup(_Procrendez*);
 
 struct Proc
 {
-	pthread_t	tid;
+	Proc		*next;
+	Proc		*prev;
+	char		msg[128];
+#ifdef PLAN9PORT_USING_PTHREADS
+	pthread_t	osprocid;
+#else
+	uint		osprocid;
+#endif
 	Lock		lock;
 	_Thread		*thread;
 	_Threadlist	runqueue;
@@ -63,10 +74,12 @@ struct Proc
 	Jmp		sigjmp;
 };
 
-extern Proc *xxx;
 #define proc() _threadproc()
 #define setproc(p) _threadsetproc(p)
 
+extern Proc *_threadprocs;
+extern Lock _threadprocslock;
+
 extern void _procstart(Proc*, void (*fn)(Proc*));
 extern _Thread *_threadcreate(Proc*, void(*fn)(void*), void*, uint);
 extern void _threadexit(void);