Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!uunet!seismo!sundc!pitstop!sun!amdcad!ames!oliveb!amiga!cbmvax!carolyn From: carolyn@cbmvax.UUCP (Carolyn Scheppner CATS) Newsgroups: comp.sys.amiga Subject: Re: Semaphores. Message-ID: <2677@cbmvax.UUCP> Date: Mon, 2-Nov-87 11:08:51 EST Article-I.D.: cbmvax.2677 Posted: Mon Nov 2 11:08:51 1987 Date-Received: Fri, 6-Nov-87 03:29:03 EST References: <8711012250.AA16257@cory.Berkeley.EDU> Reply-To: carolyn@cbmvax.UUCP (Carolyn Scheppner CATS) Organization: Commodore Technology, West Chester, PA Lines: 191 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 #include #include #include #include #ifdef AZTEC_C #include #endif #include /* * 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 =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=