i got it to work. i am the hack-master of the moment. check out test-pth-pthreads2.c.

Original commit message from CVS:
i got it to work. i am the hack-master of the moment. check out test-pth-pthreads2.c.
This commit is contained in:
Andy Wingo 2002-01-23 03:14:19 +00:00
parent 5e48d598ea
commit 3c798cc8a0
4 changed files with 111 additions and 4 deletions

View file

@ -8,11 +8,12 @@ noinst_LTLIBRARIES = libpth-mctx.la
libpth_mctx_la_SOURCES = pth_mctx.c pth_p.h pth_vers.c pth.h pth_acdef.h pth_acmac.h
noinst_PROGRAMS = test-pth test-pth-pthreads test-pthreads
noinst_PROGRAMS = test-pth test-pth-pthreads test-pthreads test-pth-pthreads2
test_pth_LDADD = libpth-mctx.la
test_pth_pthreads_LDADD = libpth-mctx.la -lpthread
test_pthreads_LDADD = -lpthread
test_pth_pthreads2_LDADD = -lpthread libpth-mctx.la
BUILT_SOURCES = pth_p.h

View file

@ -9,7 +9,7 @@
#endif /* CURRENT_STACK_FRAME */
#ifndef STACK_SIZE
#define STACK_SIZE 0x20000 /* 2 M linuxthreads default stack size */
#define STACK_SIZE 0x200000 /* 2 M linuxthreads default stack size */
#endif
typedef void * pthread_descr;

View file

@ -0,0 +1,90 @@
#define __USE_GNU /* non-posix functions */
#include <pthread.h>
#undef __USE_GNU
#include <stdio.h>
#include "linuxthreads-internals.h"
#include "pth_p.h"
/* this function is only really necessary to get the main thread's
* pthread_descr, as the other threads store the pthread_descr (actually the
* first member of struct _pthread_descr_struct, which points to itself for the
* default (non-indirected) case) at the top of the stack. */
static _pthread_descr linuxthreads_self()
{
pthread_mutexattr_t mutexattr;
pthread_mutex_t mutex;
_pthread_descr self;
pthread_mutexattr_init (&mutexattr);
pthread_mutexattr_setkind_np (&mutexattr, PTHREAD_MUTEX_ERRORCHECK_NP);
pthread_mutex_init (&mutex, &mutexattr);
pthread_mutex_lock (&mutex);
self = mutex.__m_owner;
pthread_mutex_unlock (&mutex);
printf ("pthread_self: %d\n", pthread_self());
printf ("descr: %p\n", self);
printf ("*descr: %p\n", *(int*)self);
return self;
}
pth_mctx_t main_context;
int threadnum = 0;
void cothread (void *unused)
{
printf ("1.%d: current stack frame: %p\n", threadnum, CURRENT_STACK_FRAME);
printf ("1.%d: sleeping 2s...\n", threadnum);
sleep (2);
printf ("1.%d: returning to cothread 1.0\n");
pth_mctx_restore (&main_context);
}
void *pthread (void *unused)
{
char *sp = CURRENT_STACK_FRAME;
char *cothread_stack;
pth_mctx_t ctx;
_pthread_descr descr;
descr = linuxthreads_self();
printf ("sp: %p\n", sp);
printf ("STACK_SIZE: %p\n", STACK_SIZE);
printf ("sp | STACK_SIZE: 0x%x\n", (int) sp | STACK_SIZE );
printf ("(sp | (STACK_SIZE-1))+1 - 1K: 0x%x\n", ((((int)sp | (STACK_SIZE-1))+1) - 1024));
printf ("*(sp | (STACK_SIZE-1))+1 - 1K: %p\n", *(int*)((((long int)sp | (STACK_SIZE-1))+1) - 1024));
while (threadnum < 10) {
if (posix_memalign (&cothread_stack, STACK_SIZE, STACK_SIZE)) {
printf ("could not malloc a chunk of aligned memory\n");
exit (-1);
}
printf ("1: setting *%p = %p\n", cothread_stack + STACK_SIZE - 1024, descr);
memcpy(cothread_stack + STACK_SIZE - 1024, descr, 1024);
pth_mctx_save (&main_context);
printf ("1: spawning new thread, bottom = %p\n", cothread_stack);
pth_mctx_set (&ctx, cothread, cothread_stack, cothread_stack + STACK_SIZE - 1024);
printf ("1: switching to cothread %d...\n", ++threadnum);
pth_mctx_switch (&main_context, &ctx);
}
printf ("1: back, returning...\n");
return NULL;
}
int main (int argc, char *argv[])
{
pthread_t tid;
pthread_create (&tid, NULL, pthread, NULL);
pthread_join (tid, NULL);
linuxthreads_self();
exit (0);
}

View file

@ -2,6 +2,7 @@
#include <pthread.h>
#undef __USE_GNU
#include <stdio.h>
#include "linuxthreads-internals.h"
// the thread_self algorithm:
/*
@ -19,6 +20,10 @@
#endif
*/
/* this function is only really necessary to get the main thread's
* pthread_descr, as the other threads store the pthread_descr (actually the
* first member of struct _pthread_descr_struct, which points to itself for the
* default (non-indirected) case) at the top of the stack. */
static _pthread_descr linuxthreads_self()
{
pthread_mutexattr_t mutexattr;
@ -35,22 +40,33 @@ static _pthread_descr linuxthreads_self()
printf ("pthread_self: %d\n", pthread_self());
printf ("descr: %p\n", self);
printf ("*descr: %p\n", *(int*)self);
return self;
}
void *pthread (void *unused)
{
char *sp = CURRENT_STACK_FRAME;
linuxthreads_self();
printf ("sp: %p\n", sp);
printf ("sp | 0x020000: 0x%x\n", (int) sp | 0x020000 );
printf ("(sp | (0x020000-1))+1 - 1K: 0x%x\n", ((((int)sp | (STACK_SIZE-1))+1) - 1024));
printf ("*(sp | (0x020000-1))+1 - 1K: %p\n", *(int*)((((long int)sp | (STACK_SIZE-1))+1) - 1024));
printf ("(sp &~ (0x020000-1))+1: 0x%x\n", (((int)sp &~ (STACK_SIZE-1))+1));
return NULL;
}
int main (int argc, char *argv[])
{
pthread_t tid;
int i;
pthread_create (&tid, NULL, pthread, NULL);
pthread_join (tid, NULL);
for (i=0; i<10; i++) {
pthread_create (&tid, NULL, pthread, NULL);
sleep(2);
}
linuxthreads_self();
exit (0);