ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libcoro/coro.c
(Generate patch)

Comparing libcoro/coro.c (file contents):
Revision 1.29 by root, Sun Mar 2 16:10:22 2008 UTC vs.
Revision 1.36 by root, Wed Nov 5 01:54:34 2008 UTC

43#if !defined(STACK_ADJUST_PTR) 43#if !defined(STACK_ADJUST_PTR)
44/* IRIX is decidedly NON-unix */ 44/* IRIX is decidedly NON-unix */
45# if __sgi 45# if __sgi
46# define STACK_ADJUST_PTR(sp,ss) ((char *)(sp) + (ss) - 8) 46# define STACK_ADJUST_PTR(sp,ss) ((char *)(sp) + (ss) - 8)
47# define STACK_ADJUST_SIZE(sp,ss) ((ss) - 8) 47# define STACK_ADJUST_SIZE(sp,ss) ((ss) - 8)
48# elif __i386__ && CORO_LINUX 48# elif (__i386__ && CORO_LINUX) || (_M_IX86 && CORO_LOSER)
49# define STACK_ADJUST_PTR(sp,ss) ((char *)(sp) + (ss)) 49# define STACK_ADJUST_PTR(sp,ss) ((char *)(sp) + (ss))
50# define STACK_ADJUST_SIZE(sp,ss) (ss) 50# define STACK_ADJUST_SIZE(sp,ss) (ss)
51# elif __amd64__ && CORO_LINUX 51# elif (__amd64__ && CORO_LINUX) || ((_M_AMD64 || _M_IA64) && CORO_LOSER)
52# define STACK_ADJUST_PTR(sp,ss) ((char *)(sp) + (ss) - 8) 52# define STACK_ADJUST_PTR(sp,ss) ((char *)(sp) + (ss) - 8)
53# define STACK_ADJUST_SIZE(sp,ss) (ss) 53# define STACK_ADJUST_SIZE(sp,ss) (ss)
54# else 54# else
55# define STACK_ADJUST_PTR(sp,ss) (sp) 55# define STACK_ADJUST_PTR(sp,ss) (sp)
56# define STACK_ADJUST_SIZE(sp,ss) (ss) 56# define STACK_ADJUST_SIZE(sp,ss) (ss)
120# endif 120# endif
121 121
122#endif 122#endif
123 123
124#if CORO_ASM 124#if CORO_ASM
125void __attribute__((__noinline__, __fastcall__)) 125asm (
126coro_transfer (struct coro_context *prev, struct coro_context *next) 126 ".text\n"
127{ 127 ".globl coro_transfer\n"
128 asm volatile ( 128 ".type coro_transfer, @function\n"
129 "coro_transfer:\n"
129#if __amd64 130#if __amd64
131# define NUM_ALIGN 1
130# define NUM_CLOBBERED 5 132# define NUM_SAVED 5
131 "push %%rbx\n\t" 133 "\tpush %rbx\n"
132 "push %%r12\n\t" 134 "\tpush %r12\n"
133 "push %%r13\n\t" 135 "\tpush %r13\n"
134 "push %%r14\n\t" 136 "\tpush %r14\n"
135 "push %%r15\n\t" 137 "\tpush %r15\n"
136 "mov %%rsp, %0\n\t" 138 "\tmov %rsp, (%rdi)\n"
137 "mov %1, %%rsp\n\t" 139 "\tmov (%rsi), %rsp\n"
138 "pop %%r15\n\t" 140 "\tpop %r15\n"
139 "pop %%r14\n\t" 141 "\tpop %r14\n"
140 "pop %%r13\n\t" 142 "\tpop %r13\n"
141 "pop %%r12\n\t" 143 "\tpop %r12\n"
142 "pop %%rbx\n\t" 144 "\tpop %rbx\n"
143#elif __i386 145#elif __i386
146# define NUM_ALIGN 1
144# define NUM_CLOBBERED 4 147# define NUM_SAVED 4
145 "push %%ebx\n\t" 148 "\tpush %ebx\n"
146 "push %%esi\n\t" 149 "\tpush %esi\n"
147 "push %%edi\n\t" 150 "\tpush %edi\n"
148 "push %%ebp\n\t" 151 "\tpush %ebp\n"
149 "mov %%esp, %0\n\t" 152 "\tmov %esp, (%eax)\n"
150 "mov %1, %%esp\n\t" 153 "\tmov (%edx), %esp\n"
151 "pop %%ebp\n\t" 154 "\tpop %ebp\n"
152 "pop %%edi\n\t" 155 "\tpop %edi\n"
153 "pop %%esi\n\t" 156 "\tpop %esi\n"
154 "pop %%ebx\n\t" 157 "\tpop %ebx\n"
155#else 158#else
156# error unsupported architecture 159# error unsupported architecture
157#endif 160#endif
158 : "=m" (prev->sp) 161 "\tret\n"
159 : "m" (next->sp) 162);
160 ); 163#endif
164
165#if CORO_PTHREAD
166
167struct coro_init_args {
168 coro_func func;
169 void *arg;
170 coro_context *self, *main;
171};
172
173pthread_mutex_t coro_mutex = PTHREAD_MUTEX_INITIALIZER;
174
175static void *
176trampoline (void *args_)
177{
178 struct coro_init_args *args = (struct coro_init_args *)args_;
179 coro_func func = args->func;
180 void *arg = args->arg;
181
182 pthread_mutex_lock (&coro_mutex);
183 pthread_cond_destroy (&args->self->c);
184 coro_transfer (args->self, args->main);
185 func (arg);
186 pthread_mutex_unlock (&coro_mutex);
187
188 return 0;
161} 189}
190
191asm("");
192
193void coro_transfer(coro_context *prev, coro_context *next)
194{
195 pthread_cond_init (&prev->c, 0);
196 pthread_cond_signal (&next->c);
197 pthread_cond_wait (&prev->c, &coro_mutex);
198 pthread_cond_destroy (&prev->c);
199}
200
162#endif 201#endif
163 202
164/* initialize a machine state */ 203/* initialize a machine state */
165void coro_create (coro_context *ctx, 204void coro_create (coro_context *ctx,
166 coro_func coro, void *arg, 205 coro_func coro, void *arg,
170 209
171 getcontext (&(ctx->uc)); 210 getcontext (&(ctx->uc));
172 211
173 ctx->uc.uc_link = 0; 212 ctx->uc.uc_link = 0;
174 ctx->uc.uc_stack.ss_sp = STACK_ADJUST_PTR (sptr,ssize); 213 ctx->uc.uc_stack.ss_sp = STACK_ADJUST_PTR (sptr,ssize);
175 ctx->uc.uc_stack.ss_size = (size_t) STACK_ADJUST_SIZE (sptr,ssize); 214 ctx->uc.uc_stack.ss_size = (size_t)STACK_ADJUST_SIZE (sptr,ssize);
176 ctx->uc.uc_stack.ss_flags = 0; 215 ctx->uc.uc_stack.ss_flags = 0;
177 216
178 makecontext (&(ctx->uc), (void (*)()) coro, 1, arg); 217 makecontext (&(ctx->uc), (void (*)()) coro, 1, arg);
179 218
180#elif CORO_SJLJ || CORO_LOSER || CORO_LINUX || CORO_IRIX || CORO_ASM 219#elif CORO_SJLJ || CORO_LOSER || CORO_LINUX || CORO_IRIX || CORO_ASM
245 284
246# elif CORO_LOSER 285# elif CORO_LOSER
247 286
248 setjmp (ctx->env); 287 setjmp (ctx->env);
249#if __CYGWIN__ 288#if __CYGWIN__
250 ctx->env[7] = (long)((char *)sptr + ssize); 289 ctx->env[7] = (long)((char *)sptr + ssize) - sizeof (long);
251 ctx->env[8] = (long)coro_init; 290 ctx->env[8] = (long)coro_init;
252#elif defined(_M_IX86) 291#elif defined(_M_IX86)
253 ((_JUMP_BUFFER *)&ctx->env)->Eip = (long)coro_init; 292 ((_JUMP_BUFFER *)&ctx->env)->Eip = (long)coro_init;
254 ((_JUMP_BUFFER *)&ctx->env)->Esp = (long)STACK_ADJUST_PTR (sptr,ssize); 293 ((_JUMP_BUFFER *)&ctx->env)->Esp = (long)STACK_ADJUST_PTR (sptr, ssize) - sizeof (long);
255#elif defined(_M_AMD64) 294#elif defined(_M_AMD64)
256 ((_JUMP_BUFFER *)&ctx->env)->Rip = (__int64)coro_init; 295 ((_JUMP_BUFFER *)&ctx->env)->Rip = (__int64)coro_init;
257 ((_JUMP_BUFFER *)&ctx->env)->Rsp = (__int64)STACK_ADJUST_PTR (sptr,ssize); 296 ((_JUMP_BUFFER *)&ctx->env)->Rsp = (__int64)STACK_ADJUST_PTR (sptr, ssize) - sizeof (long);
258#elif defined(_M_IA64) 297#elif defined(_M_IA64)
259 ((_JUMP_BUFFER *)&ctx->env)->StIIP = (__int64)coro_init; 298 ((_JUMP_BUFFER *)&ctx->env)->StIIP = (__int64)coro_init;
260 ((_JUMP_BUFFER *)&ctx->env)->IntSp = (__int64)STACK_ADJUST_PTR (sptr,ssize); 299 ((_JUMP_BUFFER *)&ctx->env)->IntSp = (__int64)STACK_ADJUST_PTR (sptr, ssize) - sizeof (long);
261#else 300#else
262#error "microsoft libc or architecture not supported" 301# error "microsoft libc or architecture not supported"
263#endif 302#endif
264 303
265# elif CORO_LINUX 304# elif CORO_LINUX
266 305
267 _setjmp (ctx->env); 306 _setjmp (ctx->env);
268#if __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 0 && defined (JB_PC) && defined (JB_SP) 307#if __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 0 && defined (JB_PC) && defined (JB_SP)
269 ctx->env[0].__jmpbuf[JB_PC] = (long)coro_init; 308 ctx->env[0].__jmpbuf[JB_PC] = (long)coro_init;
270 ctx->env[0].__jmpbuf[JB_SP] = (long)STACK_ADJUST_PTR (sptr, ssize); 309 ctx->env[0].__jmpbuf[JB_SP] = (long)STACK_ADJUST_PTR (sptr, ssize) - sizeof (long);
271#elif __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 0 && defined (__mc68000__) 310#elif __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 0 && defined (__mc68000__)
272 ctx->env[0].__jmpbuf[0].__aregs[0] = (long int)coro_init; 311 ctx->env[0].__jmpbuf[0].__aregs[0] = (long int)coro_init;
273 ctx->env[0].__jmpbuf[0].__sp = (int *)((char *)sptr + ssize); 312 ctx->env[0].__jmpbuf[0].__sp = (int *)((char *)sptr + ssize) - sizeof (long);
274#elif defined (__GNU_LIBRARY__) && defined (__i386__) 313#elif defined (__GNU_LIBRARY__) && defined (__i386__)
275 ctx->env[0].__jmpbuf[0].__pc = (char *)coro_init; 314 ctx->env[0].__jmpbuf[0].__pc = (char *)coro_init;
276 ctx->env[0].__jmpbuf[0].__sp = (void *)((char *)sptr + ssize); 315 ctx->env[0].__jmpbuf[0].__sp = (void *)((char *)sptr + ssize) - sizeof (long);
277#elif defined (__GNU_LIBRARY__) && defined (__amd64__) 316#elif defined (__GNU_LIBRARY__) && defined (__amd64__)
278 ctx->env[0].__jmpbuf[JB_PC] = (long)coro_init; 317 ctx->env[0].__jmpbuf[JB_PC] = (long)coro_init;
279 ctx->env[0].__jmpbuf[JB_RSP] = (long)STACK_ADJUST_PTR (sptr, ssize); 318 ctx->env[0].__jmpbuf[0].__sp = (void *)((char *)sptr + ssize) - sizeof (long);
280#else 319#else
281# error "linux libc or architecture not supported" 320# error "linux libc or architecture not supported"
282#endif 321#endif
283 322
284# elif CORO_IRIX 323# elif CORO_IRIX
285 324
286 setjmp (ctx->env); 325 setjmp (ctx->env);
287 ctx->env[JB_PC] = (__uint64_t)coro_init; 326 ctx->env[JB_PC] = (__uint64_t)coro_init;
288 ctx->env[JB_SP] = (__uint64_t)STACK_ADJUST_PTR (sptr, ssize); 327 ctx->env[JB_SP] = (__uint64_t)STACK_ADJUST_PTR (sptr, ssize) - sizeof (long);
289 328
290# elif CORO_ASM 329# elif CORO_ASM
291 330
292 ctx->sp = (volatile void **)(ssize + (char *)sptr); 331 ctx->sp = (volatile void **)(ssize + (char *)sptr);
332 ctx->sp -= NUM_ALIGN;
293 *--ctx->sp = (void *)coro_init; 333 *--ctx->sp = (void *)coro_init;
294 *--ctx->sp = (void *)coro_init; // this is needed when the prologue saves ebp
295 ctx->sp -= NUM_CLOBBERED; 334 ctx->sp -= NUM_SAVED;
296 335
297# endif 336# endif
298 337
299 coro_transfer ((coro_context *)create_coro, (coro_context *)new_coro); 338 coro_transfer ((coro_context *)create_coro, (coro_context *)new_coro);
300 339
340# elif CORO_PTHREAD
341
342 pthread_t id;
343 pthread_attr_t attr;
344 coro_context nctx;
345 struct coro_init_args args;
346 static int once;
347
348 if (!once)
349 {
350 pthread_mutex_lock (&coro_mutex);
351 once = 1;
352 }
353
354 args.func = coro;
355 args.arg = arg;
356 args.self = ctx;
357 args.main = &nctx;
358
359 pthread_attr_init (&attr);
360 pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
361 pthread_attr_setstack (&attr, sptr, (size_t)ssize);
362 pthread_create (&id, &attr, trampoline, &args);
363
364 pthread_cond_init (&args.self->c, 0);
365 coro_transfer (args.main, args.self);
366
301#else 367#else
302# error unsupported architecture 368# error unsupported backend
303#endif 369#endif
304} 370}
305 371

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines