* [9fans] portable 9-style threads libraries
@ 2003-09-26 18:43 rog
2003-09-26 18:57 ` Russ Cox
0 siblings, 1 reply; 6+ messages in thread
From: rog @ 2003-09-26 18:43 UTC (permalink / raw)
To: 9fans
i've just been queried by a friend (and i'd like to know anyway):
a) has anyone bundled up the plan9 libthread into a package portable
to other systems?
b) i seem to remember another very lightweight portable thread library
produced by someone plan 9 related, but can't seem to find any
references. any ideas?
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [9fans] portable 9-style threads libraries
2003-09-26 18:43 [9fans] portable 9-style threads libraries rog
@ 2003-09-26 18:57 ` Russ Cox
2003-09-26 19:21 ` Russ Cox
0 siblings, 1 reply; 6+ messages in thread
From: Russ Cox @ 2003-09-26 18:57 UTC (permalink / raw)
To: 9fans
> a) has anyone bundled up the plan9 libthread into a package portable
> to other systems?
just yesterday, in fact, i packaged up libthread
and the current versions of the other libraries.
i haven't put them out for playing yet. i'm a little
worried that my unix implementation of the rendezvous
system call is too heavyweight for switch-intensive jobs.
> b) i seem to remember another very lightweight portable thread library
> produced by someone plan 9 related, but can't seem to find any
> references. any ideas?
if all you want is user-level threads (only one proc)
then i have an even lighter-weight library that works
better for programs you're willing to tailor to unix.
you can designate an idle function. on unix it works
well to make this a wrapper around select. sadly.
the original tra sources (and perhaps the ones still in
the anonymous cvs) used a stack-copying thread implementation
that was fairly portable. the stack copying got around
stack overflow -- you were always on the system stack --
though it introduced the plan 9 "all stack variables are
thread local" complication/feature. the initial plan 9 stab
at this version is below.
russ
/*
* Stack-copying context switching.
* Assumes stack grows toward lower addresses.
*/
#include <u.h>
#include <libc.h>
jmp_buf mainjmp;
typedef struct Label Label;
struct Label
{
void *stk;
jmp_buf jb;
};
void*
emalloc(int n)
{
void *a;
a = mallocz(n, 1);
if(a==nil)
sysfatal("out of memory");
return a;
}
static void swtch0(Label*, Label*);
static void swtch1(Label*);
static void swtch2(Label*);
/* the abort() calls keep smart compilers from optimizing the tail recursion */
void
switchlabel(Label *cur, Label *nxt)
{
/*
* save the current stack position and registers
* the first time through. swtch0 won't return.
*/
if(setjmp(cur->jb)==0){
swtch0(cur, nxt);
abort();
}
}
static void
swtch0(Label *cur, Label *nxt)
{
uchar *lo, *hi;
int n;
/* save the stack for the current thread */
lo = (uchar*)cur->jb[JMPBUFSP];
hi = (uchar*)mainjmp[JMPBUFSP];
n = hi-lo;
cur->stk = emalloc(n);
memmove(cur->stk, lo, n);
/* switch to the next thread */
swtch1(nxt);
abort();
}
static void
swtch1(Label *nxt)
{
uchar buf[64]; /* take up stack space */
/* recurse until we're out of the area the new stack will occupy. */
if(nxt->jb[JMPBUFSP] < (ulong)buf)
swtch1(nxt);
/* do the copy */
swtch2(nxt);
abort();
}
static void
swtch2(Label *nxt)
{
uchar *lo, *hi;
int n;
/* put the next stack in place */
lo = (uchar*)nxt->jb[JMPBUFSP];
hi = (uchar*)mainjmp[JMPBUFSP];
n = hi-lo;
memmove(lo, nxt->stk, n);
free(nxt->stk);
nxt->stk = nil;
/* hop to it */
longjmp(nxt->jb, 1);
abort();
}
void
initlabel(Label *l, void (*fn)(void*), void *arg)
{
ulong *stk;
int n;
n = 16;
l->stk = emalloc(n);
stk = (ulong*)((uchar*)l->stk + n);
--stk;
*--stk = (ulong)arg;
*--stk = 0; /* return address */
*--stk = 0; /* new pc */
l->jb[JMPBUFPC] = (ulong)fn+JMPBUFDPC;
l->jb[JMPBUFSP] = mainjmp[JMPBUFSP]-n;
}
Label ml, pl;
void
printthread(void *a)
{
print("printthread runs, arg is %d\n", (int)a);
switchlabel(&pl, &ml);
}
void
main(int argc, char **argv)
{
setjmp(mainjmp);
initlabel(&pl, printthread, (void*)12345);
print("main runs\n");
switchlabel(&ml, &pl);
print("main runs again\n");
exits(nil);
}
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [9fans] portable 9-style threads libraries
2003-09-26 18:57 ` Russ Cox
@ 2003-09-26 19:21 ` Russ Cox
2003-09-26 21:57 ` goga
0 siblings, 1 reply; 6+ messages in thread
From: Russ Cox @ 2003-09-26 19:21 UTC (permalink / raw)
To: 9fans
http://pdos.lcs.mit.edu/~rsc/software/
has current libutf, libfmt, libbio, libregexp,
lib9 (libc bits that don't fit anywhere else),
libthread, mk, and sam4e (thanks to scott schwartz).
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [9fans] portable 9-style threads libraries
2003-09-26 21:57 ` goga
@ 2003-09-26 21:36 ` Russ Cox
2003-09-27 7:54 ` goga
0 siblings, 1 reply; 6+ messages in thread
From: Russ Cox @ 2003-09-26 21:36 UTC (permalink / raw)
To: 9fans
> I couldn't download about half of the files -- the server gives me 404.
Whoops. The ones that did download were old.
I had named today's files .tar.gz but the links .tgz.
It should be fixed now.
Thanks.
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [9fans] portable 9-style threads libraries
2003-09-26 19:21 ` Russ Cox
@ 2003-09-26 21:57 ` goga
2003-09-26 21:36 ` Russ Cox
0 siblings, 1 reply; 6+ messages in thread
From: goga @ 2003-09-26 21:57 UTC (permalink / raw)
To: 9fans
On Fri, Sep 26, 2003 at 03:21:23PM -0400, Russ Cox wrote:
> http://pdos.lcs.mit.edu/~rsc/software/
> has current libutf, libfmt, libbio, libregexp,
> lib9 (libc bits that don't fit anywhere else),
> libthread, mk, and sam4e (thanks to scott schwartz).
>
I couldn't download about half of the files -- the server gives me 404.
goga
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [9fans] portable 9-style threads libraries
2003-09-26 21:36 ` Russ Cox
@ 2003-09-27 7:54 ` goga
0 siblings, 0 replies; 6+ messages in thread
From: goga @ 2003-09-27 7:54 UTC (permalink / raw)
To: 9fans
On Fri, Sep 26, 2003 at 05:36:58PM -0400, Russ Cox wrote:
> > I couldn't download about half of the files -- the server gives me 404.
>
> Whoops. The ones that did download were old.
> I had named today's files .tar.gz but the links .tgz.
> It should be fixed now.
>
> Thanks.
>
Now it works. Thank you!
goga
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2003-09-27 7:54 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-09-26 18:43 [9fans] portable 9-style threads libraries rog
2003-09-26 18:57 ` Russ Cox
2003-09-26 19:21 ` Russ Cox
2003-09-26 21:57 ` goga
2003-09-26 21:36 ` Russ Cox
2003-09-27 7:54 ` goga
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).