Various tweaks to make things run on Mac OS.

The main change is the use of pthread to implement ffork.
This commit is contained in:
rsc 2003-10-01 02:50:57 +00:00
parent e182749a98
commit a995e477ff
12 changed files with 84 additions and 12 deletions

View file

@ -46,6 +46,11 @@ extern "C" {
# undef _NEEDUINT # undef _NEEDUINT
# endif # endif
#endif #endif
#if defined(__APPLE__)
# include <sys/types.h>
# undef _NEEDUSHORT
# undef _NEEDUINT
#endif
typedef signed char schar; typedef signed char schar;
typedef unsigned int u32int; typedef unsigned int u32int;
@ -213,7 +218,8 @@ extern int errstr(char*, uint);
/* command line */ /* command line */
extern char *argv0; extern char *argv0;
#define ARGBEGIN for((argv0||(argv0=*argv)),argv++,argc--;\ extern void __fixargv0(void);
#define ARGBEGIN for((argv0||(argv0=(__fixargv0(),*argv))),argv++,argc--;\
argv[0] && argv[0][0]=='-' && argv[0][1];\ argv[0] && argv[0][0]=='-' && argv[0][1];\
argc--, argv++) {\ argc--, argv++) {\
char *_args, *_argt;\ char *_args, *_argt;\

View file

@ -270,3 +270,39 @@ int create(char *name, int omode, int perm)
return fd; return fd;
} }
/* SHOULD BE ELSEWHERE */
#ifdef __APPLE__
#include <lib9.h>
Lock plk;
ulong
pread(int fd, void *buf, ulong n, ulong off)
{
ulong rv;
lock(&plk);
if (lseek(fd, off, 0) != off)
return -1;
rv = read(fd, buf, n);
unlock(&plk);
return rv;
}
ulong
pwrite(int fd, void *buf, ulong n, ulong off)
{
ulong rv;
lock(&plk);
if (lseek(fd, off, 0) != off)
return -1;
rv = write(fd, buf, n);
unlock(&plk);
return rv;
}
#endif

View file

@ -19,7 +19,7 @@ OFILES=\
nrand.$O\ nrand.$O\
qlock.$O\ qlock.$O\
readn.$O\ readn.$O\
rendez.$O\ rendez-$(SYSNAME).$O\
strecpy.$O\ strecpy.$O\
sysfatal.$O\ sysfatal.$O\
tas-$(OBJTYPE).$O\ tas-$(OBJTYPE).$O\
@ -31,7 +31,7 @@ OFILES=\
werrstr.$O\ werrstr.$O\
HFILES=\ HFILES=\
lib9.h\ $(9SRC)/include/lib9.h\
include $(9SRC)/Makesyslib include $(9SRC)/Makesyslib

View file

@ -2,3 +2,8 @@
char *argv0; char *argv0;
/*
* Mac OS can't deal with files that only declare data.
* ARGBEGIN mentions this function so that this file gets pulled in.
*/
void __fixargv0(void) { }

View file

@ -1,5 +1,6 @@
#include <signal.h> #include <signal.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/time.h>
#include <sys/resource.h> #include <sys/resource.h>
#include <sys/wait.h> #include <sys/wait.h>
#include <sys/time.h> #include <sys/time.h>

View file

@ -31,3 +31,9 @@ _spinlock(spinlock_t *lk)
lock((Lock*)&lk->access_lock); lock((Lock*)&lk->access_lock);
} }
int
getfforkid(void)
{
return getpid();
}

View file

@ -37,3 +37,9 @@ ffork(int flags, void (*fn)(void*), void *arg)
return pid; return pid;
} }
int
getfforkid(void)
{
return getpid();
}

View file

@ -8,12 +8,9 @@ _xtas(void *v)
{ {
int x; int x;
_ntas++; _ntas++;
x = _tas(v); x = _tas(v);
if(x == 0 || x == 0xCAFEBABE)
return x; return x;
fprint(2, "%d: tas %p got %ux\n", getpid(), v, x);
abort();
} }
int int

View file

@ -14,6 +14,7 @@ sysfatal(char *fmt, ...)
vseprint(buf, buf+sizeof buf, fmt, arg); vseprint(buf, buf+sizeof buf, fmt, arg);
va_end(arg); va_end(arg);
__fixargv0();
fprint(2, "%s; %s\n", argv0 ? argv0 : "<prog>", buf); fprint(2, "%s; %s\n", argv0 ? argv0 : "<prog>", buf);
exits("fatal"); exits("fatal");
} }

View file

@ -1,8 +1,7 @@
#include "threadimpl.h" #include "threadimpl.h"
#include <unistd.h> extern int getfforkid(void);
int int
_threadgetpid(void) _threadgetpid(void)
{ {
return getpid(); return getfforkid();
} }

View file

@ -17,6 +17,21 @@ struct Label
ulong si; ulong si;
ulong di; ulong di;
}; };
#elif defined(__APPLE__)
struct Label
{
ulong pc; /* lr */
ulong cr; /* mfcr */
ulong ctr; /* mfcr */
ulong xer; /* mfcr */
ulong sp; /* callee saved: r1 */
ulong toc; /* callee saved: r2 */
ulong gpr[19]; /* callee saved: r13-r31 */
// XXX: currently do not save vector registers or floating-point state
// ulong pad;
// uvlong fpr[18]; /* callee saved: f14-f31 */
// ulong vr[4*12]; /* callee saved: v20-v31, 256-bits each */
};
#else #else
#error "Unknown or unsupported architecture" #error "Unknown or unsupported architecture"
#endif #endif

View file

@ -25,7 +25,7 @@ threadmain(int argc, char **argv)
c[1] = chancreate(sizeof(ulong), 1); c[1] = chancreate(sizeof(ulong), 1);
c[2] = c[0]; c[2] = c[0];
threadcreate(pingpong, c, 16384); proccreate(pingpong, c, 16384);
threadcreate(pingpong, c+1, 16384); threadcreate(pingpong, c+1, 16384);
sendul(c[0], atoi(argv[1])); sendul(c[0], atoi(argv[1]));
} }