[comp.sys.amiga] Semaphores..

dillon@CORY.BERKELEY.EDU (Matt Dillon) (11/02/87)

>Whether Forbid/Permit or ObtainSemaphore/ReleaseSemaphore (which is
>LockIBase() amounts to) are used to arbitrate shared data between

	Speaking of which, the documentation on the new Semaphore calls
is seriously lacking.  It amounts to something like:

	ObtainSemaphore(): This function obtains a semaphore.

	Right... That really tells me how to use the function.  How about 
some better docs on these functions??  I must have added two hundred lines to
my autodocs since I got them.... every time I figure out how an inadequately
explained function works, I add it to my manual set.  The EXEC IO system is
the worst of the lot.

	I can finally understand some of the original RKM stuff... I realized
that the author was talking about what the ROM routines need to do, not what
the original program or destination device driver needs to do.  Great..
Lovely, just what I didn't need to know.

					-Matt

carolyn@cbmvax.UUCP (Carolyn Scheppner CATS) (11/02/87)

In article <8711012250.AA16257@cory.Berkeley.EDU> dillon@CORY.BERKELEY.EDU (Matt Dillon) writes:
>
>	Speaking of which, the documentation on the new Semaphore calls
>is seriously lacking.  It amounts to something like:
>
>	ObtainSemaphore(): This function obtains a semaphore.
>
>	Right... That really tells me how to use the function.  How about 
>some better docs on these functions??

   Here's an example.  Note that 1.2 Amiga.lib and Manx semaphore bindings
are bad.  This code contains workarounds.

----cut--here----
/** semaphore.c *************************************************************
*
*   Playing with public semaphores.
*
****************************************************************************/

#include <exec/types.h>
#include <exec/memory.h>
#include <exec/execbase.h>
#include <exec/semaphores.h>
#include <libraries/dos.h>

#ifdef AZTEC_C
#include <functions.h>
#endif

#include <stdio.h>

/* 
 * Use our versions of the semaphore functions
 */
#define RemSemaphore        myRemSemaphore  
#define FindSemaphore       myFindSemaphore 
#define AddSemaphore        myAddSemaphore


#define SSNAME	"mysemaphore"
#define SSSIZE  11

extern struct ExecBase *SysBase;

struct SignalSemaphore *FindSemaphore();

/* The bindings for "AddSemaphore" are broken in 1.2 Amiga.lib
 *
 * Dale's handcrafted AddSemaphore(). 
 */
VOID myAddSemaphore(ss)
struct SignalSemaphore *ss;
{
    InitSemaphore(ss);
    Forbid();
    Enqueue(&SysBase->SemaphoreList,ss);
    Permit();
}

#if AZTEC_C
VOID _cli_parse() {  }  /* save space, since we are not handling args */
#endif

main()
{
 struct SignalSemaphore *mysemaphore=0,*mysema=0;
 UBYTE	*ssname=0;
 
 mysema = (struct SignalSemaphore *) FindSemaphore(SSNAME);  
 if(mysema)
 {
    printf("Found semaphore: %s\n",mysema->ss_Link.ln_Name);
    RemSemaphore(mysema);
    *ssname='\0';
    FreeMem(mysema->ss_Link.ln_Name, (ULONG) SSSIZE+1);
    FreeMem(mysema, (ULONG) sizeof(*mysemaphore));
    exit(TRUE);
 }

 ssname = (UBYTE *) AllocMem( (ULONG) SSSIZE+1,MEMF_PUBLIC | MEMF_CLEAR); 
 if(!ssname)
 {
    printf("No space for name!\n");
    exit(TRUE);
 }
 	 
 mysemaphore = 
   (struct SignalSemaphore *) AllocMem( (ULONG) sizeof(*mysemaphore),MEMF_PUBLIC | MEMF_CLEAR);

 if(!mysemaphore)
 {
    FreeMem(ssname, (ULONG) SSSIZE+1);
    printf("No space for semaphore structure\n");
    exit(TRUE);
 }

 printf("Have memory...Now initializing and adding semaphore to system list.\n");

 strcpy(ssname,SSNAME);  
 mysemaphore->ss_Link.ln_Name = ( char * ) ssname;
 mysemaphore->ss_Link.ln_Pri  = 0;
 
 printf("AddSemaphore(%s)\n",mysemaphore->ss_Link.ln_Name); 

 AddSemaphore(mysemaphore);
 
 /* print name of semaphore */
 printf("FindSemaphore(%s)\n",mysemaphore->ss_Link.ln_Name); 

 mysema = (struct SignalSemaphore *) FindSemaphore(SSNAME);  
 if(!mysema)
  {
   printf("Not found semaphore: %s\n",ssname);
   *ssname='\0';
   FreeMem(ssname, (ULONG) SSSIZE+1);
   FreeMem(mysemaphore, (ULONG) sizeof(*mysemaphore));
   exit(TRUE);
  }

  printf("Found semaphore: %s\n",mysema->ss_Link.ln_Name);
  
  printf("ObtainSemaphore(%s)\n",mysemaphore->ss_Link.ln_Name); 
  
  ObtainSemaphore(mysema);  /* I won't come back until I have it... */

  printf("I have the semaphore: %s\n",mysema->ss_Link.ln_Name);
  
  printf("ReleaseSemaphore(%s)\n",mysemaphore->ss_Link.ln_Name); 

  ReleaseSemaphore(mysema);

  printf("RemSemaphore(%s)\n",mysemaphore->ss_Link.ln_Name);
   
  RemSemaphore(mysema);
  
  puts("Freeing memory used by semaphore.\n");

  *ssname='\0';
  FreeMem(ssname, (ULONG) SSSIZE+1);
  FreeMem(mysemaphore, (ULONG) sizeof(*mysemaphore));
  
  puts("All done.");
  	
}


/* The "C" interface code for the follow semaphore routines is broken in
 * Amiga.lib and in the Aztec C release 3.4a.
 *
 * @ Lattice people should cut and paste the assembler into a separate file.
 */
 
#if AZTEC_C     
#asm
; The exec.library function "AddSemaphore" is broken in KickStart rel. 33.180
;
        
        XREF    _SysBase
        XREF    _LVOFindSemaphore
;	XREF    _LVOAddSemaphore
	XREF    _LVORemSemaphore


	XDEF    _myFindSemaphore
;       XDEF    _myAddSemaphore
	XDEF    _myRemSemaphore

_myFindSemaphore:
	move.l 	4(sp),a1
        move.l 	_SysBase,a6
	jmp 	_LVOFindSemaphore(a6)
;
;_myAddSemaphore:
;	move.l 	4(sp),a1
;       move.l 	_SysBase,a6
;	jmp 	_LVOAddSemaphore(a6)

_myRemSemaphore:
	move.l 	4(sp),a1
        move.l 	_SysBase,a6
	jmp 	_LVORemSemaphore(a6)
#endasm
#endif

-- 
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Carolyn Scheppner -- CATS   >>Commodore Amiga Technical Support<<
                     UUCP  ...{allegra,ihnp4,rutgers}!cbmvax!carolyn 
                     PHONE 215-431-9180
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

peter@sugar.UUCP (Peter da Silva) (11/05/87)

Any chance of the Enhanced Dillon Autodocs being one of the forthcoming
Dillon Sample Software series?

You oughta write a book, "Tricks of the AmigaDOS Masters". :-> for the name.
Serious on the concept.
-- 
-- Peter da Silva  `-_-'  ...!hoptoad!academ!uhnix1!sugar!peter
-- Disclaimer: These U aren't mere opinions... these are *values*.

dillon@CORY.BERKELEY.EDU (Matt Dillon) (11/07/87)

>Any chance of the Enhanced Dillon Autodocs being one of the forthcoming
>Dillon Sample Software series?
>
>You oughta write a book, "Tricks of the AmigaDOS Masters". :-> for the name.
>Serious on the concept.

	Ahh shoot... if you keep on suggesting book names I'll never figure
a good one out for myself!

	Autodocs:  I don't think I can unless I get permission from 
Commodore...

	DME:  *great* new command in the next release.  I will also finally
fix the keyboard problems (DME doesn't recognize some keys on WestGerman
A2000's and there other problems with forward-single-quote with keymaps 
which don't implement them).  The command?  Oh yah... 

	How often are you programming around and need to lookup a structure?
For instance, you forgot what the RastPort structure looked like.  How would
you like to be able to hit a single key and have that structure pop-up in
a DME window?  I thought you would!  It's mainly a feature for programmers
as some cross reference files need to be set up, but after that it's fully
automatic and quite fast.   And we are not limited to structures... how 
'bout looking up library calls with a keystroke.   Of course, this does
mean you need to have the autodocs and commented include files on-line,
but it works well on a two-drive system and is much faster than looking it
up in a manual.

				-Matt

john13@garfield.UUCP (11/10/87)

In article <8711070723.AA05065@cory.Berkeley.EDU> dillon@CORY.BERKELEY.EDU (Matt Dillon) writes:

>	Autodocs:  I don't think I can unless I get permission from 
>Commodore...

...and Larry Niven :-).

>	How often are you programming around and need to lookup a structure?
>For instance, you forgot what the RastPort structure looked like.  How would
>you like to be able to hit a single key and have that structure pop-up in
>a DME window?  

There is a PD program which does this (haven't tried it, although I've got
it somewhere around here). I hope it's optional, or at least takes up very
little memory... what I do in a pinch is have a key mapped to
escimm `newwindow newfile sys1:include/'. That way it's one key, followed
by "graphics/rastport.h" or whatever it is.

No autodocs 'round these parts anyway :-(.

John
-- 
"Operating systems room, prepare for shutdown."
"I never thought I'd be HAPPY to see our ratings do DOWN!"
		-- lots of these were sprinkled throughout the 
		   last broadcast episode of _Max_Headroom_

sutherla@qtp.ufl.edu (scott sutherland) (04/10/89)

	Maybe I am just dense, but I have read most of the Amiga books
(with the exception of the Addison-Wesley REFERENCE books) and I have
seen many references to Semaphores. There have also been several postings
to this group in the last few days which also mention Semaphores. So I have
three SIMPLE :) questions:

	1) WHAT IS A SEMAPHORE??

	2) WHEN WOULD I EVER USE A SEMAPHORE IN MY PROGRAMS?

	3) HOW DO I USE THEM (e.g., code, examples...)?





					Thanks,

				A confused Scott Sutherland


"Where did the name Semaphore come from? It sounds like some sort of musical
term"

dinsdale%liaison@Sun.COM (Tom van Peer) (04/11/89)

Try any book on operating systems and you will get your answers.  The word
semaphore you might want to look up in a so called "dictionary", a book filled
with explanations of words.  I am sure your university has one.  
The term semaphore in relation to computers has been discussed several
times in comp.misc.  You might still find some traces there of the last
discussion which was a copy of the previous discussions on that subject.

Tom van Peer.

tom@pcg.philips.nl or
dinsdale@liaison.sun.com (whichever is closer to you)

lively@sunybcs.uucp (Richard S. Lively) (04/11/89)

You must be thinking of SEMIFORKS which are used as a solution to the
Dining Philosophers Problem.  Seriously, any book on Operating Systems
should have s description: try Operating Systems: Design and Implementation
by Andrew Tannenbaum for a good, readable introduction.  Semaphores were
first used by Dijkstra (the famous P and V) for implementing mutual exclusion
in critical sections.

doug@homxc.ATT.COM (D.SULPY) (04/12/89)

In article <98248@sun.Eng.Sun.COM>, dinsdale%liaison@Sun.COM (Tom van Peer) writes:
> 
> Try any book on operating systems and you will get your answers.  The word
> semaphore you might want to look up in a so called "dictionary", a book filled
> with explanations of words.  I am sure your university has one.  
> The term semaphore in relation to computers has been discussed several
> times in comp.misc.  You might still find some traces there of the last
> discussion which was a copy of the previous discussions on that subject.
> 
> Tom van Peer.
> 

------

Hmm. Well, I guess USENET is not an acceptable avenue for questions unless
ALL other resources have been thoroughly checked, including an archive
of the past year's postings. Since you're so obviously acquainted with
the dictionary, why not use one to look up some archaic technical terms,
like 'courteous'?


DISCLAIMER: 
I can say without fear of contradiction that AT&T doesn't care in the
slightest bit what I say, unless I make the mistake of saying something
they care about :-). 

arc@desire.wright.edu (06/30/90)

  
 
 
 There is a utility called Stat in the v2.0 of the Kramden utilities.
It has a way to let you list all the semaphores in your system.  There
are only 2 programs that I've seen that use there own semaphores and these
are Tracksalve and some other program (I forget it;s's name).  How come
more people don't use semaphrores?  Wouldn't they help out a lot in some
areas?  I know that semaphorses in a Vax is useful for synchronizing tasks...
Is this what they are used for on the Amiga? Would someone PLEASE elaborate?
 
 /Bryan
 
 
------------------------------------------------------------------------
=     //           | Bryan K. Fite             | Arc@Desire.Wright.edu =
=    // Amiga!     | ^Service Engineer^        |         -or-          =
= \\// The One     | Arc Electronics, Inc.     |    Arc@WSU.BITNET     =
=  \/ & Only...    | Wright State University   |                       =
=                  | Dayton, Ohio              |  Depeche' Mode, Now!  =
========================================================================

vinsci@soft.fi (Leonard Norrgard) (07/01/90)

  One of the reasons is that the exec.library:AddSemaphore() is broken in
the V33 & V34 ROMS, see the RKM Includes & Autodocs BUGS description for
AddSemaphore() for more info and a workaround. I bet that bug has caused much
frustration...
  Another reason is of course that you can often achieve the same thing
using other methods, like public message ports etc., depending on your needs
of course.

>are Tracksalve and some other program (I forget it;s's name).  How come
>more people don't use semaphrores?  Wouldn't they help out a lot in some
>areas?  I know that semaphorses in a Vax is useful for synchronizing tasks...
>Is this what they are used for on the Amiga? Would someone PLEASE elaborate?

  In PostDriver (our PostScript printer driver), we use a semaphore to
make sure that the printer driver task gets a genuine set of the
PostDriver preferences, either from the in memory settings, from the
saved settings or as a last resort from the default settings. The in
memory and saved-to-disk settings are protected by the semaphore so as to
protect the printer driver to access the settings while the setup program
is updating them.

  Use a semaphore instead of a Forbid() where you can, and the world will
be happier. :-)

-- Leonard