2 #include <linux/linkage.h>
11 #include "cothreads.h"
13 pthread_key_t _cothread_key = -1;
15 cothread_state *cothread_create(cothread_context *ctx) {
18 if (pthread_self() == 0) {
19 s = (cothread_state *)malloc(sizeof(int) * COTHREAD_STACKSIZE);
21 char *sp = CURRENT_STACK_FRAME;
22 unsigned long *stack_end = (unsigned long *)((unsigned long)sp &
24 s = (cothread_state *)(stack_end + ((ctx->nthreads - 1) *
26 if (mmap((char *)s,COTHREAD_STACKSIZE*(sizeof(int)),
27 PROT_READ|PROT_WRITE|PROT_EXEC,MAP_PRIVATE|MAP_ANONYMOUS,
29 perror("mmap'ing cothread stack space");
35 s->threadnum = ctx->nthreads;
37 s->sp = (int *)(s + COTHREAD_STACKSIZE);
39 ctx->threads[ctx->nthreads++] = s;
41 // printf("created cothread at %p\n",s);
46 void cothread_setfunc(cothread_state *thread,cothread_func func,int argc,char **argv) {
50 thread->pc = (int *)func;
53 cothread_context *cothread_init() {
54 cothread_context *ctx = (cothread_context *)malloc(sizeof(cothread_context));
56 if (_cothread_key == -1) {
57 if (pthread_key_create(&_cothread_key,NULL) != 0) {
58 perror("pthread_key_create");
62 pthread_setspecific(_cothread_key,ctx);
64 memset(ctx->threads,0,sizeof(ctx->threads));
66 ctx->threads[0] = (cothread_state *)malloc(sizeof(cothread_state));
67 ctx->threads[0]->ctx = ctx;
68 ctx->threads[0]->threadnum = 0;
69 ctx->threads[0]->func = NULL;
70 ctx->threads[0]->argc = 0;
71 ctx->threads[0]->argv = NULL;
72 ctx->threads[0]->flags = COTHREAD_STARTED;
73 ctx->threads[0]->sp = CURRENT_STACK_FRAME;
74 ctx->threads[0]->pc = 0;
76 // fprintf(stderr,"0th thread is at %p\n",ctx->threads[0]);
78 // we consider the initiating process to be cothread 0
85 cothread_state *cothread_main(cothread_context *ctx) {
86 // fprintf(stderr,"returning %p, the 0th cothread\n",ctx->threads[0]);
87 return ctx->threads[0];
90 void cothread_stub() {
91 cothread_context *ctx = pthread_getspecific(_cothread_key);
92 register cothread_state *thread = ctx->threads[ctx->current];
94 thread->flags |= COTHREAD_STARTED;
95 thread->func(thread->argc,thread->argv);
96 thread->flags &= ~COTHREAD_STARTED;
98 // printf("uh, yeah, we shouldn't be here, but we should deal anyway\n");
101 void cothread_switch(cothread_state *thread) {
102 cothread_context *ctx;
103 cothread_state *current;
112 current = ctx->threads[ctx->current];
113 if (current == NULL) {
114 fprintf(stderr,"there's no current thread, help!\n");
118 if (current == thread) {
119 fprintf(stderr,"trying to switch to same thread, legal but not necessary\n");
123 // find the number of the thread to switch to
124 ctx->current = thread->threadnum;
125 // fprintf(stderr,"about to switch to thread #%d\n",ctx->current);
127 /* save the current stack pointer, frame pointer, and pc */
128 __asm__("movl %%esp, %0" : "=m"(current->sp) : : "esp", "ebp");
129 enter = setjmp(current->jmp);
134 /* restore stack pointer and other stuff of new cothread */
135 __asm__("movl %0, %%esp\n" : "=m"(thread->sp));
136 if (thread->flags & COTHREAD_STARTED) {
138 longjmp(thread->jmp,1);
141 __asm__("jmp " SYMBOL_NAME_STR(cothread_stub));