2002-01-21 05:09:17 +00:00
|
|
|
#include "pth_p.h"
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2002-01-27 20:11:58 +00:00
|
|
|
#include "linuxthreads.h"
|
2002-01-21 05:09:17 +00:00
|
|
|
|
|
|
|
pth_mctx_t main_context;
|
|
|
|
int threadnum = 0;
|
2002-01-21 22:21:19 +00:00
|
|
|
|
2002-02-02 19:07:10 +00:00
|
|
|
void cothread (void)
|
2002-01-21 05:09:17 +00:00
|
|
|
{
|
|
|
|
printf ("1.1: current stack frame: %p\n", CURRENT_STACK_FRAME);
|
|
|
|
printf ("1.1: sleeping 2s in thread %d...\n", threadnum);
|
|
|
|
sleep (2);
|
|
|
|
printf ("1.1: current stack frame: %p\n", CURRENT_STACK_FRAME);
|
|
|
|
printf ("1.1: returning to cothread 0\n");
|
|
|
|
pth_mctx_restore (&main_context);
|
|
|
|
}
|
|
|
|
|
2002-02-02 19:07:10 +00:00
|
|
|
void *pthread (void* unused)
|
2002-01-21 05:09:17 +00:00
|
|
|
{
|
|
|
|
pth_mctx_t ctx;
|
2002-01-22 21:48:26 +00:00
|
|
|
char *skaddr;
|
2002-01-21 05:09:17 +00:00
|
|
|
|
|
|
|
printf ("1: saving the main context\n");
|
|
|
|
printf ("1: current stack frame: %p\n", CURRENT_STACK_FRAME);
|
|
|
|
pth_mctx_save (&main_context);
|
|
|
|
|
|
|
|
while (1) {
|
2002-01-22 21:48:26 +00:00
|
|
|
skaddr = alloca (64 * 1024);
|
2002-01-21 05:09:17 +00:00
|
|
|
|
|
|
|
printf ("1: current stack frame: %p\n", CURRENT_STACK_FRAME);
|
|
|
|
printf ("1: spawning a new cothread\n");
|
2002-01-21 22:21:19 +00:00
|
|
|
pth_mctx_set (&ctx, cothread, skaddr, skaddr + 64 * 1024);
|
2002-01-21 05:09:17 +00:00
|
|
|
printf ("1: new thread's stack frame will be in the heap at %p\n", skaddr);
|
|
|
|
|
|
|
|
printf ("1: current stack frame: %p\n", CURRENT_STACK_FRAME);
|
|
|
|
printf ("1: switching to cothread %d...\n", ++threadnum);
|
|
|
|
|
|
|
|
printf ("1: current stack frame: %p\n", CURRENT_STACK_FRAME);
|
|
|
|
pth_mctx_switch (&main_context, &ctx);
|
|
|
|
|
|
|
|
printf ("1: current stack frame: %p\n", CURRENT_STACK_FRAME);
|
|
|
|
printf ("1: back now, looping\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int main (int argc, char *argv[])
|
|
|
|
{
|
|
|
|
pthread_t tid;
|
|
|
|
|
|
|
|
printf ("0: current stack frame: %p\n", CURRENT_STACK_FRAME);
|
|
|
|
printf ("0: creating the pthread\n");
|
2002-02-02 19:07:10 +00:00
|
|
|
// pthread_create (&tid, NULL, pthread, NULL);
|
|
|
|
// printf ("0: %d\n", pthread_self());
|
|
|
|
pthread(NULL);
|
2002-01-21 05:09:17 +00:00
|
|
|
// printf ("joining the pthread\n");
|
2002-02-02 19:07:10 +00:00
|
|
|
// pthread_join (tid, NULL);
|
2002-01-21 05:09:17 +00:00
|
|
|
|
|
|
|
printf ("0: current stack frame: %p\n", CURRENT_STACK_FRAME);
|
|
|
|
printf ("0: take five...\n");
|
|
|
|
sleep(5);
|
|
|
|
|
|
|
|
printf ("0 current stack frame: %p\n", CURRENT_STACK_FRAME);
|
|
|
|
printf ("exiting\n");
|
|
|
|
|
|
|
|
exit (0);
|
|
|
|
}
|
|
|
|
|