Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
24 changes: 9 additions & 15 deletions gthr.c
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand All @@ -49,25 +47,21 @@ void __attribute__((noreturn))
gtret(int ret)
{
if (gtcur != &gttbl[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 == &gttbl[MaxGThreads])
Expand All @@ -76,7 +70,7 @@ gtyield(bool force)
return false;
}

if (gtcur->st != Zombie)
if (gtcur->st != Unused)
gtcur->st = Ready;
p->st = Running;
old = &gtcur->ctx;
Expand Down Expand Up @@ -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)
Expand All @@ -125,7 +119,7 @@ f(void)
id = ++x;
for (i = 0; i < 10; i++) {
printf("%d %d\n", id, i);
gtyield(true);
gtyield();
}
}

Expand Down
4 changes: 2 additions & 2 deletions swtch.S → gtswtch.S
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
# %rsi contains the context to switch to.
#

.code64
.globl gtswtch
.globl _gtswtch, gtswtch
_gtswtch:
gtswtch:

mov %rsp, 0x00(%rdi)
Expand Down