Commit Diff


commit - 9cb22ba60776e22568798b40560543ddf6c8ed2c
commit + d54ead7fb922cfa661c7b7f0d0b2ba1251cdea7f
blob - 77c4869abf06bdf2246a773dde699d1a5f52a948
blob + 0fdb54b97426d717873ee89310a66c7a8caa03d7
--- src/libthread/FreeBSD.c
+++ src/libthread/FreeBSD.c
@@ -1,11 +1,3 @@
-#include "u.h"
-#include <errno.h>
-#include <sys/types.h>
-#include <sys/wait.h>
-#include <sched.h>
-#include <signal.h>
-#include "libc.h"
-#include "thread.h"
 #include "threadimpl.h"
 
 extern int __isthreaded;
@@ -70,7 +62,6 @@ _spinlock(spinlock_t *lk)
 {
 	lock((Lock*)&lk->access_lock);
 }
-	__isthreaded = 1;
 
 /*
  * sleep and wakeup
@@ -351,6 +342,46 @@ _threadsetproc(Proc *p)
 void
 _pthreadinit(void)
 {
+	__isthreaded = 1;
 	signal(SIGUSR2, sigusr2handler);
 }
+
+/*
+ * FreeBSD 4 and earlier needs the context functions.
+ */
+void
+makecontext(ucontext_t *ucp, void (*func)(void), int argc, ...)
+{
+	int *sp;
 
+	sp = (int*)ucp->uc_stack.ss_sp+ucp->uc_stack.ss_size/4;
+	sp -= argc;
+	memmove(sp, &argc+1, argc*sizeof(int));
+	*--sp = 0;		/* return address */
+	ucp->uc_mcontext.mc_eip = (long)func;
+	ucp->uc_mcontext.mc_esp = (int)sp;
+}
+
+extern int getmcontext(mcontext_t*);
+extern int setmcontext(mcontext_t*);
+
+int
+getcontext(ucontext_t *uc)
+{
+	return getmcontext(&uc->uc_mcontext);
+}
+
+void
+setcontext(ucontext_t *uc)
+{
+	setmcontext(&uc->uc_mcontext);
+}
+
+int
+swapcontext(ucontext_t *oucp, ucontext_t *ucp)
+{
+	if(getcontext(oucp) == 0)
+		setcontext(ucp);
+	return 0;
+}
+
blob - be97f5a79d4aa0ff738c810e17bde4eef189cfd1
blob + 103cb4281c7d516535266fb0681b9beb4dacd3f3
--- src/libthread/Linux.c
+++ src/libthread/Linux.c
@@ -1,11 +1,3 @@
-#include "u.h"
-#include <errno.h>
-#include <sys/types.h>
-#include <sys/wait.h>
-#include <sched.h>
-#include <signal.h>
-#include "libc.h"
-#include "thread.h"
 #include "threadimpl.h"
 
 /*
blob - /dev/null
blob + f9a80255992c290f6ce270eb1278124b484646d0 (mode 644)
--- /dev/null
+++ src/libthread/FreeBSDasm.s
@@ -0,0 +1,54 @@
+.globl _tas
+_tas:
+	movl $0xCAFEBABE, %eax
+	movl 4(%esp), %ecx
+	xchgl %eax, 0(%ecx)
+	ret
+
+.globl setmcontext
+setmcontext:
+	movl 4(%esp), %edx
+	movl	8(%edx), %fs
+	movl	12(%edx), %es
+	movl	16(%edx), %ds
+	movl	76(%edx), %ss
+	movl	20(%edx), %edi
+	movl	24(%edx), %esi
+	movl	28(%edx), %ebp
+	movl	%esp, %ecx		 
+	movl	72(%edx), %esp		 
+	pushl	60(%edx)	/* eip */	 
+	pushl	44(%edx)	/* ecx */	 
+	pushl	48(%edx)	/* eax */	 
+	movl	36(%edx), %ebx		 
+	movl	40(%edx), %edx
+	movl	12(%ecx), %eax		 
+	popl	%eax			 
+	popl	%ecx
+	ret
+
+.globl getmcontext
+getmcontext:
+	pushl	%edx
+	movl	8(%esp), %edx
+	movl	%fs, 8(%edx)
+	movl	%es, 12(%edx)
+	movl	%ds, 16(%edx)
+	movl	%ss, 76(%edx)
+	movl	%edi, 20(%edx)
+	movl	%esi, 24(%edx)
+	movl	%ebp, 28(%edx)
+	movl	%ebx, 36(%edx)
+	movl	$1, 48(%edx)
+	popl	%eax
+	movl	%eax, 40(%edx)		 
+	movl	%ecx, 44(%edx)
+	movl	(%esp), %eax	/* eip */		 
+	movl	%eax, 60(%edx)		 
+	movl	%esp, %eax		 
+	addl	$4, %eax	/* setmcontext will re-push the eip */	 
+	movl	%eax, 72(%edx)
+	movl	40(%edx), %edx		 
+	xorl	%eax, %eax		 
+	ret
+
blob - c8c96796428ae85fb94b89c32d765b7ce403dc4d
blob + 4dc51fcf43423b5b45d0381becdfbe2dd6be92eb
--- src/libthread/channel.c
+++ src/libthread/channel.c
@@ -1,6 +1,3 @@
-#include "u.h"
-#include "libc.h"
-#include "thread.h"
 #include "threadimpl.h"
 
 /*
blob - 65af29ab4efcfdcedc0503ac995ce1041a18b3ec
blob + dab6e42b825f3cd5925366aa907cf2c628b9de58
--- src/libthread/daemonize.c
+++ src/libthread/daemonize.c
@@ -1,8 +1,3 @@
-#include <u.h>
-#include <sys/signal.h>
-#include <sys/wait.h>
-#include <libc.h>
-#include <thread.h>
 #include "threadimpl.h"
 
 #undef pipe
@@ -15,8 +10,10 @@ static void
 child(void)
 {
 	int status;
-	if(wait(&status) == sigpid && WIFEXITED(status))
-		 _exit(WEXITSTATUS(status));
+	if(wait(&status) == sigpid)
+		if(WIFEXITED(status))
+			 _exit(WEXITSTATUS(status));
+	_exit(97);
 }
 
 static void
@@ -83,7 +80,7 @@ _threadsetupdaemonize(void)
 			child();
 		if(n > 0)
 			break;
-		sysfatal("passer pipe read: %r");
+		print("passer read: %r\n");
 	}
 	buf[n] = 0;
 	_exit(atoi(buf));
blob - 6a7ca1dbf151e508c7c0a05d94f2a701170cbe78
blob + a81490b1ae33f98aa3cee216484ce3095cabb87a
--- src/libthread/exec.c
+++ src/libthread/exec.c
@@ -1,7 +1,3 @@
-#include "u.h"
-#include <errno.h>
-#include "libc.h"
-#include "thread.h"
 #include "threadimpl.h"
 
 static Lock thewaitlock;
blob - 259bcff136c2e5778d7d7a4f3987ca86f287d58f
blob + 1e257b3ebc9802c679b48d8a50daaecb9f22973e
--- src/libthread/mkfile
+++ src/libthread/mkfile
@@ -39,3 +39,5 @@ test:V: tprimes tspawn
 
 CLEANFILES=p1.txt p2.txt tp1.txt tp2.txt
 
+
+
blob - c1804a433417dc2687a555a1fa03116aae0c3118
blob + 8d3c7f9accd2f650d620f723bd2dc144e66ca9a8
--- src/libthread/pthread.c
+++ src/libthread/pthread.c
@@ -1,7 +1,3 @@
-#include "u.h"
-#include <errno.h>
-#include "libc.h"
-#include "thread.h"
 #include "threadimpl.h"
 
 static pthread_mutex_t initmutex = PTHREAD_MUTEX_INITIALIZER;
blob - b41f9f341b366be3e7e34545ebef9192c7de0664
blob + 92e9394099b9401aed44562f15eefa99245fb302
--- src/libthread/thread.c
+++ src/libthread/thread.c
@@ -1,6 +1,3 @@
-#include "u.h"
-#include "libc.h"
-#include "thread.h"
 #include "threadimpl.h"
 
 int	_threaddebuglevel;
@@ -95,7 +92,7 @@ threadalloc(void (*fn)(void*), void *arg, uint stack)
 	sigemptyset(&zero);
 	sigprocmask(SIG_BLOCK, &zero, &t->context.uc.uc_sigmask);
 
-	/* on Linux makecontext neglects floating point */
+	/* must initialize with current context */
 	getcontext(&t->context.uc);
 
 	/* call makecontext to do the real work. */
blob - d4acebeb2d2ed417d063483a6bd641f00a546880
blob + 32afa5fecc9446046bd199ea3ad3f50b5b1604b0
--- src/libthread/threadimpl.h
+++ src/libthread/threadimpl.h
@@ -1,5 +1,21 @@
+#include "u.h"
+#include <errno.h>
+#include <sys/time.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+#include <sched.h>
+#include <signal.h>
 #include <ucontext.h>
+#include "libc.h"
+#include "thread.h"
 
+#if defined(__FreeBSD__) && !defined(__FreeBSD5__)
+extern	int		getcontext(ucontext_t*);
+extern	void		setcontext(ucontext_t*);
+extern	int		swapcontext(ucontext_t*, ucontext_t*);
+extern	void		makecontext(ucontext_t*, void(*)(), int, ...);
+#endif
+
 typedef struct Context Context;
 typedef struct Execjob Execjob;
 typedef struct Proc Proc;