home *** CD-ROM | disk | FTP | other *** search
/ Aminet 18 / aminetcdnumber181997.iso / Aminet / misc / emu / AROSdev.lha / AROS / rom / exec / obtainsemaphore.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-01-09  |  2.8 KB  |  117 lines

  1. /*
  2.     (C) 1995-96 AROS - The Amiga Replacement OS
  3.     $Id: obtainsemaphore.c,v 1.7 1997/01/01 03:46:12 ldp Exp $
  4.     $Log: obtainsemaphore.c,v $
  5.     Revision 1.7  1997/01/01 03:46:12  ldp
  6.     Committed Amiga native (support) code
  7.  
  8.     Changed clib to proto
  9.  
  10.     Revision 1.6  1996/12/10 13:51:48  aros
  11.     Moved all #include's in the first column so makedepend can see it.
  12.  
  13.     Revision 1.5  1996/10/24 15:50:52  aros
  14.     Use the official AROS macros over the __AROS versions.
  15.  
  16.     Revision 1.4  1996/08/13 13:56:04  digulla
  17.     Replaced AROS_LA by AROS_LHA
  18.     Replaced some AROS_LH*I by AROS_LH*
  19.     Sorted and added includes
  20.  
  21.     Revision 1.3  1996/08/01 17:41:14  digulla
  22.     Added standard header for all files
  23.  
  24.     Desc:
  25.     Lang: english
  26. */
  27. #include "exec_intern.h"
  28. #include "semaphores.h"
  29. #include <exec/semaphores.h>
  30. #include <proto/exec.h>
  31.  
  32. /*****************************************************************************
  33.  
  34.     NAME */
  35.  
  36.     AROS_LH1(void, ObtainSemaphore,
  37.  
  38. /*  SYNOPSIS */
  39.     AROS_LHA(struct SignalSemaphore *, sigSem, A0),
  40.  
  41. /*  LOCATION */
  42.     struct ExecBase *, SysBase, 94, Exec)
  43.  
  44. /*  FUNCTION
  45.     Obtain an exclusive lock on a semaphore. If the semaphore is already
  46.     in use by another task this function will wait until the semaphore
  47.     becomes free.
  48.  
  49.     INPUTS
  50.     sigSem - Pointer to semaphore structure
  51.  
  52.     RESULT
  53.  
  54.     NOTES
  55.     This function preserves all registers.
  56.  
  57.     EXAMPLE
  58.  
  59.     BUGS
  60.  
  61.     SEE ALSO
  62.     ReleaseSemaphore()
  63.  
  64.     INTERNALS
  65.  
  66.     HISTORY
  67.     29-10-95    digulla automatically created from
  68.                 exec_lib.fd and clib/exec_protos.h
  69.     21-01-96    fleischer implementation
  70.  
  71. *****************************************************************************/
  72. {
  73.     AROS_LIBFUNC_INIT
  74.     AROS_LIBBASE_EXT_DECL(struct ExecBase *,SysBase)
  75.     struct Task *me;
  76.  
  77.     /* Get pointer to current task */
  78.     me=SysBase->ThisTask;
  79.  
  80.     /* Arbitrate for the semaphore structure */
  81.     Forbid();
  82.  
  83.     /* Check if the semaphore is in use by another task */
  84.     if(sigSem->ss_NestCount&&sigSem->ss_Owner!=me)
  85.     {
  86.     /*
  87.         I wish there was some shared memory available as a semaphore node.
  88.         Unfortunately it isn't - and I cannot rely on being able to get
  89.         some from AllocMem() at this point either, so I have to use a part
  90.         of the stack instead :-(.
  91.     */
  92.     struct SemaphoreNode sn;
  93.  
  94.     sn.node.ln_Pri =SN_TYPE_OBTAIN;
  95.     sn.node.ln_Name=(char *)SM_EXCLUSIVE;
  96.     sn.task        =me;
  97.  
  98.     /* Add the node to the semaphore's waiting queue. */
  99.     AddTail((struct List *)&sigSem->ss_WaitQueue,&sn.node);
  100.  
  101.     /* Wait until the semaphore is free */
  102.     Wait(SEMAPHORESIGF);
  103.  
  104.     /* ss_NestCount and ss_Owner are already set by ReleaseSemaphore() */
  105.     }else
  106.     {
  107.     /* The semaphore is free - take it over */
  108.     sigSem->ss_NestCount++;
  109.     sigSem->ss_Owner=me;
  110.     }
  111.  
  112.     /* All done */
  113.     Permit();
  114.     AROS_LIBFUNC_EXIT
  115. } /* ObtainSemaphore */
  116.  
  117.