3 #include <linux/linkage.h>
11 #include "cothreads.h"
14 pthread_key_t _cothread_key = -1;
16 cothread_state *cothread_create(cothread_context *ctx) {
19 printf("pthread_self() %ld\n",pthread_self());
20 //if (pthread_self() == 0) {
22 s = (cothread_state *)malloc(sizeof(int) * COTHREAD_STACKSIZE);
23 printf("new stack at %p\n",s);
25 char *sp = CURRENT_STACK_FRAME;
26 unsigned long *stack_end = (unsigned long *)((unsigned long)sp &
28 s = (cothread_state *)(stack_end + ((ctx->nthreads - 1) *
30 if (mmap((char *)s,COTHREAD_STACKSIZE*(sizeof(int)),
31 PROT_READ|PROT_WRITE|PROT_EXEC,MAP_PRIVATE|MAP_ANONYMOUS,
33 perror("mmap'ing cothread stack space");
39 s->threadnum = ctx->nthreads;
41 s->sp = ((int *)s + COTHREAD_STACKSIZE);
43 ctx->threads[ctx->nthreads++] = s;
45 printf("created cothread at %p %p\n",s, s->sp);
50 void cothread_setfunc(cothread_state *thread,cothread_func func,int argc,char **argv) {
54 thread->pc = (int *)func;
57 cothread_context *cothread_init() {
58 cothread_context *ctx = (cothread_context *)malloc(sizeof(cothread_context));
60 if (_cothread_key == -1) {
61 if (pthread_key_create(&_cothread_key,NULL) != 0) {
62 perror("pthread_key_create");
66 pthread_setspecific(_cothread_key,ctx);
68 memset(ctx->threads,0,sizeof(ctx->threads));
70 ctx->threads[0] = (cothread_state *)malloc(sizeof(cothread_state));
71 ctx->threads[0]->ctx = ctx;
72 ctx->threads[0]->threadnum = 0;
73 ctx->threads[0]->func = NULL;
74 ctx->threads[0]->argc = 0;
75 ctx->threads[0]->argv = NULL;
76 ctx->threads[0]->flags = COTHREAD_STARTED;
77 ctx->threads[0]->sp = (int *)CURRENT_STACK_FRAME;
78 ctx->threads[0]->pc = 0;
80 fprintf(stderr,"0th thread is at %p %p\n",ctx->threads[0], ctx->threads[0]->sp);
82 // we consider the initiating process to be cothread 0
89 cothread_state *cothread_main(cothread_context *ctx) {
90 // fprintf(stderr,"returning %p, the 0th cothread\n",ctx->threads[0]);
91 return ctx->threads[0];
94 void cothread_stub() {
95 cothread_context *ctx = pthread_getspecific(_cothread_key);
96 register cothread_state *thread = ctx->threads[ctx->current];
98 printf("cothread_stub() entered\n");
99 thread->flags |= COTHREAD_STARTED;
101 thread->func(thread->argc,thread->argv);
102 thread->flags &= ~COTHREAD_STARTED;
104 //printf("uh, yeah, we shouldn't be here, but we should deal anyway\n");
107 void cothread_switch(cothread_state *thread) {
108 cothread_context *ctx;
109 cothread_state *current;
118 current = ctx->threads[ctx->current];
119 if (current == NULL) {
120 fprintf(stderr,"there's no current thread, help!\n");
124 if (current == thread) {
125 fprintf(stderr,"trying to switch to same thread, legal but not necessary\n");
129 // find the number of the thread to switch to
130 ctx->current = thread->threadnum;
131 fprintf(stderr,"about to switch to thread #%d\n",ctx->current);
133 /* save the current stack pointer, frame pointer, and pc */
135 enter = setjmp(current->jmp);
140 fprintf(stderr,"set stack to %p\n", thread->sp);
141 /* restore stack pointer and other stuff of new cothread */
142 if (thread->flags & COTHREAD_STARTED) {
143 fprintf(stderr,"in thread \n");
146 longjmp(thread->jmp,1);