3 #include <linux/linkage.h>
11 #include "cothreads.h"
13 pthread_key_t _cothread_key = -1;
15 cothread_state *cothread_create(cothread_context *ctx) {
18 printf("pthread_self() %ld\n",pthread_self());
19 //if (pthread_self() == 0) {
21 s = (cothread_state *)malloc(sizeof(int) * COTHREAD_STACKSIZE);
22 printf("new stack at %p\n",s);
24 char *sp = CURRENT_STACK_FRAME;
25 unsigned long *stack_end = (unsigned long *)((unsigned long)sp &
27 s = (cothread_state *)(stack_end + ((ctx->nthreads - 1) *
29 if (mmap((char *)s,COTHREAD_STACKSIZE*(sizeof(int)),
30 PROT_READ|PROT_WRITE|PROT_EXEC,MAP_PRIVATE|MAP_ANONYMOUS,
32 perror("mmap'ing cothread stack space");
38 s->threadnum = ctx->nthreads;
40 s->sp = ((int *)s + COTHREAD_STACKSIZE);
42 ctx->threads[ctx->nthreads++] = s;
44 printf("created cothread at %p %p\n",s, s->sp);
49 void cothread_setfunc(cothread_state *thread,cothread_func func,int argc,char **argv) {
53 thread->pc = (int *)func;
56 cothread_context *cothread_init() {
57 cothread_context *ctx = (cothread_context *)malloc(sizeof(cothread_context));
59 if (_cothread_key == -1) {
60 if (pthread_key_create(&_cothread_key,NULL) != 0) {
61 perror("pthread_key_create");
65 pthread_setspecific(_cothread_key,ctx);
67 memset(ctx->threads,0,sizeof(ctx->threads));
69 ctx->threads[0] = (cothread_state *)malloc(sizeof(cothread_state));
70 ctx->threads[0]->ctx = ctx;
71 ctx->threads[0]->threadnum = 0;
72 ctx->threads[0]->func = NULL;
73 ctx->threads[0]->argc = 0;
74 ctx->threads[0]->argv = NULL;
75 ctx->threads[0]->flags = COTHREAD_STARTED;
76 ctx->threads[0]->sp = (int *)CURRENT_STACK_FRAME;
77 ctx->threads[0]->pc = 0;
79 fprintf(stderr,"0th thread is at %p %p\n",ctx->threads[0], ctx->threads[0]->sp);
81 // we consider the initiating process to be cothread 0
88 cothread_state *cothread_main(cothread_context *ctx) {
89 // fprintf(stderr,"returning %p, the 0th cothread\n",ctx->threads[0]);
90 return ctx->threads[0];
93 void cothread_stub() {
94 cothread_context *ctx = pthread_getspecific(_cothread_key);
95 register cothread_state *thread = ctx->threads[ctx->current];
97 printf("cothread_stub() entered\n");
98 thread->flags |= COTHREAD_STARTED;
100 thread->func(thread->argc,thread->argv);
101 thread->flags &= ~COTHREAD_STARTED;
103 //printf("uh, yeah, we shouldn't be here, but we should deal anyway\n");
106 void cothread_switch(cothread_state *thread) {
107 cothread_context *ctx;
108 cothread_state *current;
117 current = ctx->threads[ctx->current];
118 if (current == NULL) {
119 fprintf(stderr,"there's no current thread, help!\n");
123 if (current == thread) {
124 fprintf(stderr,"trying to switch to same thread, legal but not necessary\n");
128 // find the number of the thread to switch to
129 ctx->current = thread->threadnum;
130 fprintf(stderr,"about to switch to thread #%d\n",ctx->current);
132 /* save the current stack pointer, frame pointer, and pc */
133 __asm__("movl %%esp, %0" : "=m"(current->sp) : : "esp", "ebp");
134 enter = setjmp(current->jmp);
139 fprintf(stderr,"set stack to %p\n", thread->sp);
140 /* restore stack pointer and other stuff of new cothread */
141 if (thread->flags & COTHREAD_STARTED) {
142 fprintf(stderr,"in thread \n");
143 __asm__("movl %0, %%esp\n" : "=m"(thread->sp));
145 longjmp(thread->jmp,1);
147 __asm__("movl %0, %%esp\n" : "=m"(thread->sp));
149 __asm__("jmp " SYMBOL_NAME_STR(cothread_stub));