diff --git a/Makefile b/Makefile index 8178e00..631f595 100644 --- a/Makefile +++ b/Makefile @@ -1,10 +1,15 @@ -CFLAGS = -g -Wall +ifeq ($(shell uname), Darwin) + APPLE_CCFLAGS = -m64 + APPLE_ASFLAGS = -arch x86_64 +endif -gttest: gthr.o swtch.o - $(CC) -o $@ $^ +CFLAGS = $(APPLE_CCFLAGS) -g -Wall + +gttest: gthr.o gtswtch.o + $(CC) $(APPLE_CCFLAGS) -o $@ $^ .S.o: - as -o $@ $^ + as $(APPLE_ASFLAGS) -o $@ $^ .PHONY: clean clean: diff --git a/gthr.c b/gthr.c index 18fc4c1..b017680 100644 --- a/gthr.c +++ b/gthr.c @@ -23,18 +23,16 @@ struct gt { Unused, Running, Ready, - Zombie, } st; - int excode; }; struct gt gttbl[MaxGThreads]; struct gt *gtcur; -bool gttick; +void gtinit(void); void gtret(int ret); void gtswtch(struct gtctx *old, struct gtctx *new); -bool gtyield(bool force); +bool gtyield(void); static void gtstop(void); int gtgo(void (*f)(void)); @@ -49,25 +47,21 @@ void __attribute__((noreturn)) gtret(int ret) { if (gtcur != >tbl[0]) { - gtcur->st = Zombie; - gtcur->excode = ret; - gtyield(true); + gtcur->st = Unused; + gtyield(); assert(!"reachable"); } - while (gtyield(true)) + while (gtyield()) ; exit(ret); } bool -gtyield(bool force) +gtyield(void) { struct gt *p; struct gtctx *old, *new; - if (!gttick && !force) - return false; - gttick = 0; p = gtcur; while (p->st != Ready) { if (++p == >tbl[MaxGThreads]) @@ -76,7 +70,7 @@ gtyield(bool force) return false; } - if (gtcur->st != Zombie) + if (gtcur->st != Unused) gtcur->st = Ready; p->st = Running; old = >cur->ctx; @@ -114,7 +108,7 @@ gtgo(void (*f)(void)) } -/* Now we test this nice library. */ +/* Now, let's run some simple threaded code. */ void f(void) @@ -125,7 +119,7 @@ f(void) id = ++x; for (i = 0; i < 10; i++) { printf("%d %d\n", id, i); - gtyield(true); + gtyield(); } } diff --git a/swtch.S b/gtswtch.S similarity index 94% rename from swtch.S rename to gtswtch.S index 5899b5a..c747da2 100644 --- a/swtch.S +++ b/gtswtch.S @@ -5,8 +5,8 @@ # %rsi contains the context to switch to. # -.code64 -.globl gtswtch +.globl _gtswtch, gtswtch +_gtswtch: gtswtch: mov %rsp, 0x00(%rdi)