From: gor@cs.strath.ac.uk (Gordon Russell)
Newsgroups: comp.sys.sun.misc
Subject: Lightweight Tasking
Date: 1 Sep 92 08:55:39 GMT
Organization: Comp. Sci. Dept., Strathclyde Univ., Glasgow, Scotland.


 Hello all SPARC gurus....

   I have an interesting problem with a (almost) portable lightweight
thread management system on the SPARCs. What I have been working on is a
simulator of a particular machine using the SPARCs, but I have to use a number
of SPARC instructions per simulated instruction. Now, I also want to prevent
task-switching during a simulated instruction (ie no simulated instruction can
be interrupted). One way to do this using lwp would be to use a
mon_enter(mon)...mon_exit(mon) combination around each of the pseudo
instructions, but the simulation must be fast, and mon_enter has more executed
instructions than those used to represent the simulated ones! Anyway, lwp
is not very portable, so back to the drawing board.

   I design a system using SIGNAL and SETITIMER, with the aim of performing
a context switch in the signal handler. Sounds great. I inhibit the
ALRM signal by using SIGSETMASK (which is small). Now, when the ALARM
signal is generated, I can context switch. Well I think I could, however
a number of problems have arose, and I am hoping someone can help me with them:

 o the sigcontext structure does not contain the y register. Surely this is
   a mistake. If so, what is the easiest way of rectifying this problem?
 o the sigcontext structure contains a number of fields which are not explained
   in the manual. Does someone have an explination (eg what is npc, the
   next program counter)?
 o Any good ways to set up a task initially? My approach is dire!
 o Any way I can convince the compiler to pull in some library routines
   as inline code (ie sigsetmask() ), as this would certainly improve the
   performance significantly (although it is already a leaf procedure).

Incidentally, does anyone have any thoughts on an alternative approach?
I would be happy to hear them, and I don't mind writing some code using
the .il approach. 

One alternative scheme I have considered is to use one of the global
registers, and set it to one when I enter a simulated instruction.
On leaving, I perform an exchange on the register with a local register,
setting the global reg to zero, and cheching the local reg. If it is
one, then ok, but if it was zero, then the timer must have expired,
so do an task switch.   In the timer handler, if the global register
is zero, then ok to task switch, otherwise set the register to zero and return
(thus allowing the instruction to instigate the switch). This is the
most efficient scheme I know.

All help would be appreciated. This is for a research purposes only (I am not
making money from it), and full acknowlegments will be given in my thesis
(I am a PhD-er). Once this is working, I will post a message to the group
allowing them to get a copy if they wish.

  Roll on solaris

PS I use vi all the time, and not just as a last resort... Its great!

+--------------------------------+-------------------------------------------+
|  Gordon Russell                |  EMAIL     : gor@cs.strath.ac.uk          |
|  L13.13a, Livingstone Tower,   |  TELEPHONE : 041-552-4400   Ex 3703       |
|  University Of Strathclyde,    |  FAX       : 041-552-0775                 |
|  26 Richmond Street,           +-------------------------------------------+
|  Glasgow, G1 1XH               | Spelling mistakes within this document are|
|  Scotland, UK                  | caused by internet compaction algorithms. |
+--------------------------------+-------------------------------------------+
Newsgroups: comp.sys.sun.misc