*BSD News Article 22475


Return to BSD News archive

Path: sserve!newshost.anu.edu.au!munnari.oz.au!network.ucsd.edu!library.ucla.edu!agate!spool.mu.edu!uwm.edu!daffy!uwvax!sinetnews!news.u-tokyo.ac.jp!graco!nitta
From: nitta@c.u-tokyo.ac.jp (Yoshihisa Nitta)
Newsgroups: comp.unix.bsd
Subject: Re: thread-implementation details
Message-ID: <NITTA.93Oct17021419@davinci.c.u-tokyo.ac.jp>
Date: 16 Oct 93 17:14:19 GMT
References: <CEw92y.GoA@ms.uky.edu>
Sender: news@graco.c.u-tokyo.ac.jp
Organization: Coll. Arts & Sci., University of Tokyo, Japan.
Lines: 51
In-reply-to: nimrod@ms.uky.edu's message of 14 Oct 93 15:44:56 GMT


  In article <CEw92y.GoA@ms.uky.edu> nimrod@ms.uky.edu (B. A Krishna) writes:
  >I am attempting to implement a user-level thread library on sun4. I need to
  >be able to execute a thread in its own allocated stack and for this reason,
  >I need to be able to modify the stack pointer to point to the top of the new
  >thread's stack at thread creation time. Is there any way of achieving this

How about using _setjmp/_longjmp functions?
With them, thread can be implemented portable to work on 
Sun4, Sun3, 386bsd, ...
Be sure the address of new stack pointer (newsp in the below)
must be aligned to 8 bytes (last 3 bits are 0) on SPARC.

#include <setjmp.h>
/* jmp_buf index */
#if defined(sun)
#  define SP		2
#  define PC		3
#  if defined(sparc)
#    define NPC		4
#  endif /* sparc */
#elif defined(_386BSD_) /* !sun*/
#define intel386
#define SP		2
#define PC		0
#else  /* sony cisc-news */
#define SP		9
#define PC		10
#endif /* !sun */

create_thread()
{
   jmp_buf env, newenv;
   i = _setjmp(env);
   if (!i) {
       bcopy(env,newenv,sizeof(jmp_buf));
       newenv[SP]=(int)newsp; /* new stack */
       newenv[PC]=(int)func;  /* C function executed in the new thread */
#if defined(sparc)
       newenv[NPC]=newenv[PC]+4; /* only SPARC */
#endif
       _longjmp(newenv,1);
       /* NOT REACHED */
   }
   /* continue point with _longjmp(env) */
   ...
}
--
Yoshihisa Nitta, Univ. of Tokyo, Japan
[Tel] +81-3-3469-5388(dialin) or +81-3-3467-1171(ex)542 [Fax] +81-3-3465-2896
[e-mail] nitta@c.u-tokyo.ac.jp