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

  1. /*
  2.     (C) 1995-96 AROS - The Amiga Replacement OS
  3.     $Id: obtainsemaphoreshared.c,v 1.7 1997/01/01 03:46:12 ldp Exp $
  4.     $Log: obtainsemaphoreshared.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:49  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, ObtainSemaphoreShared,
  37.  
  38. /*  SYNOPSIS */
  39.     AROS_LHA(struct SignalSemaphore *, sigSem, A0),
  40.  
  41. /*  LOCATION */
  42.     struct ExecBase *, SysBase, 113, Exec)
  43.  
  44. /*  FUNCTION
  45.     Get a shared lock on a semaphore. If the lock cannot be obtained
  46.     immediately this function waits. There may be more than one shared
  47.     locks at the same time but only one exclusive one. An exclusive
  48.     lock prevents shared locks. Shared locks are released with
  49.     ReleaseSemaphore().
  50.  
  51.     INPUTS
  52.     sigSem - Pointer to semaphore structure
  53.  
  54.     RESULT
  55.  
  56.     NOTES
  57.     This function preserves all registers.
  58.  
  59.     EXAMPLE
  60.  
  61.     BUGS
  62.  
  63.     SEE ALSO
  64.     ReleaseSemaphore()
  65.  
  66.     INTERNALS
  67.  
  68.     HISTORY
  69.     29-10-95    digulla automatically created from
  70.                 exec_lib.fd and clib/exec_protos.h
  71.     21-01-96    fleischer implementation
  72.  
  73. *****************************************************************************/
  74. {
  75.     AROS_LIBFUNC_INIT
  76.     AROS_LIBBASE_EXT_DECL(struct ExecBase *,SysBase)
  77.     struct Task *me;
  78.  
  79.     /* Get pointer to current task */
  80.     me=SysBase->ThisTask;
  81.  
  82.     /* Arbitrate for the semaphore structure */
  83.     Forbid();
  84.  
  85.     /* Check if there's an exclusive lock on the semaphore */
  86.     if(sigSem->ss_NestCount>0)
  87.     {
  88.     /* Yes. Is it owned by the current task? */
  89.     if(sigSem->ss_Owner!=me)
  90.     {
  91.         /* No. Prepare a node for the waiting queue. */
  92.         struct SemaphoreNode sn;
  93.         sn.node.ln_Pri =SN_TYPE_OBTAIN;
  94.         sn.node.ln_Name=(char *)SM_SHARED;
  95.         sn.task       =me;
  96.  
  97.         /* Add it. */
  98.         AddTail((struct List *)&sigSem->ss_WaitQueue,&sn.node);
  99.  
  100.         /* Wait until the semaphore is free */
  101.         Wait(SEMAPHORESIGF);
  102.  
  103.         /* ss_NestCount and ss_Owner are already set by ReleaseSemaphore() */
  104.     }else
  105.         /* Add one nesting level more */
  106.         sigSem->ss_NestCount++;
  107.     }else
  108.     {
  109.     /* There's no exclusive lock on the semaphore. Get a shared one. */
  110.     sigSem->ss_NestCount--;
  111.  
  112.     /* Invalidate the owner field - so no task may think this semaphore is his. */
  113.     sigSem->ss_Owner=NULL;
  114.     }
  115.  
  116.     /* All done. */
  117.     Permit();
  118.     AROS_LIBFUNC_EXIT
  119. } /* ObtainSemaphoreShared */
  120.  
  121.