9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
* [9fans] Questions about libthread and setjmp
@ 2005-02-21 12:15 Gorka Guardiola
  2005-02-21 15:41 ` Russ Cox
  0 siblings, 1 reply; 2+ messages in thread
From: Gorka Guardiola @ 2005-02-21 12:15 UTC (permalink / raw)
  To: 9fans


I have some questions regarding the man pages of setjmp
and the thread library implementation (the commentaries).

In setjmp(2):

The commentary "-2 leaves room for old pc and new pc in frame"
in the setlabel definition (there is also a missing bracket
in  label[JMPBUFSP but that is another story).

What does it mean?. I simply don't understand it. Maybe I am
missing something...


There is another strange commentary too in the same fashion.
Probably for the same reason I don't understand the first one
I don't understand this one.


	t->sched[JMPBUFSP] = (ulong)tos - 8;		/* old PC and new PC */

Can anyone explain please?.



						G.



^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [9fans] Questions about libthread and setjmp
  2005-02-21 12:15 [9fans] Questions about libthread and setjmp Gorka Guardiola
@ 2005-02-21 15:41 ` Russ Cox
  0 siblings, 0 replies; 2+ messages in thread
From: Russ Cox @ 2005-02-21 15:41 UTC (permalink / raw)
  To: Fans of the OS Plan 9 from Bell Labs

>         t->sched[JMPBUFSP] = (ulong)tos - 8;            /* old PC and new PC */

Taking this as a concrete example, the thread library is
trying to make it look like launcher386(f, arg) called setjmp
at the very beginning of its execution.  If it had, the stack
would look like:

    arg
    f
    pc of caller of launcher386 ("old pc")
    pc of caller of setjmp (== launcher386, "new pc")

and the saved SP is the one pointing at the bottom of this stack.

When longjmp comes along, it restores SP to the same place,
writes the jmp_buf PC over the slot marked "pc of caller of setjmp"
(in normal setjmp/longjmp use that memory word is no longer valid)
and executes a return instruction, ending up at the beginning of
launcher386 with f, arg as the arguments on the stack.

Normal setjmp-produced stacks look a little different, because
there is a pointer to the jump buffer, and perhaps then a stack frame
full of local variables, between the two pcs.  But here we are not
returning to a function that has called setjmp.  We are "returning"
to the beginning of a function, so we want the stack to look like
it does on entry, not like it does in the middle.

Russ


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2005-02-21 15:41 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-02-21 12:15 [9fans] Questions about libthread and setjmp Gorka Guardiola
2005-02-21 15:41 ` Russ Cox

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).