home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 568a.lha / OwnDevUnit.library / source / OwnDevUnit.c < prev    next >
C/C++ Source or Header  |  1991-10-28  |  24KB  |  791 lines

  1. /* OwnDevUnit.c
  2.  
  3.    Copyright 1991 by Christopher A. Wichura (caw@miroc.chi.il.us)
  4.    All Rights Reserved.
  5.  
  6.    This implements a fairly simple blocking mechanism for a device/unit
  7.    specification.
  8.  
  9.    Note that this probably won't compile unless using SAS 5.10b.
  10.    It also assumes KickStart 2.0 include files.
  11.  
  12.    This code is freely redistributable.  You may not charge anything
  13.    for it, except for media and/or postage.  In using this code, you
  14.    assume all responsibility for any damage, loss of productivity/money,
  15.    or other disaster that occurs while this code is in use.  C. Wichura
  16.    can not, and will not, be held responsible.
  17. */
  18.  
  19. #include <exec/types.h>
  20. #include <exec/lists.h>
  21. #include <exec/nodes.h>
  22. #include <exec/memory.h>
  23. #include <exec/ports.h>
  24. #include <exec/tasks.h>
  25. #include <exec/libraries.h>
  26. #include <exec/semaphores.h>
  27. #include <devices/timer.h>
  28. #include <rexx/errors.h>
  29.  
  30. #include <clib/exec_protos.h>
  31. extern struct ExecBase *SysBase;
  32. #include <pragmas/exec_lib.h>
  33.  
  34. #include <clib/rexxsyslib_protos.h>
  35. extern struct RxsLib *RexxSysBase;
  36. #include <pragmas/rexxsyslib_lib.h>
  37.  
  38. #include <string.h>
  39. #include <stdlib.h>
  40. #include <clib/alib_protos.h>
  41.  
  42. LONG __stdargs kprintf(STRPTR fmt, ...);
  43.  
  44. /* we keep a list of all device/unit pairs we are currently tracking.
  45.    a semaphore is used to prevent collisions while accessing this
  46.    list. */
  47. struct SignalSemaphore MasterListSema;
  48. struct MinList MasterList;
  49.  
  50. typedef struct {
  51.     struct MinNode    wn_Node;
  52.     struct Task    *wn_Task;
  53. } WaitListNode_t;
  54.  
  55. typedef struct {
  56.     struct MinNode        mn_Node;
  57.     ULONG            mn_SizeOf;
  58.     struct Task        *mn_OwnerTask;
  59.     ULONG            mn_Unit;
  60.     UBYTE            *mn_OwnerName;
  61.     struct MinList        mn_WaitList;
  62.     UBYTE            mn_NotifyBit;
  63.     UBYTE            mn_Device[1];    /* dynamically allocated */
  64. } MasterListNode_t;
  65.  
  66. /* as a failsafe for programs that don't check their notifybit properly,
  67.    we will re-signal them every RE_SIGNAL times through the loop that
  68.    waits for the port to become free. */
  69. #define RE_SIGNAL 10
  70.  
  71. /* strings that can be returned by LockDevUnit() and AttempDevUnit() to
  72.    indicate an internal error.  Note that these all start with a
  73.    special character. */
  74.  
  75. #define ODUERR_LEADCHAR "\x07"
  76.  
  77. #define ODUERR_NOMEM    ODUERR_LEADCHAR "Out of memory"
  78. #define ODUERR_NOTIMER    ODUERR_LEADCHAR "Unable to open timer.device"
  79. #define ODUERR_BADNAME    ODUERR_LEADCHAR "Bogus device name supplied"
  80. #define ODUERR_BADBIT    ODUERR_LEADCHAR "Bogus notify bit supplied"
  81. #define ODUERR_UNKNOWN    ODUERR_LEADCHAR "Unknown"
  82.                 /* returned if owner's name is NULL */
  83.  
  84. /* we keep track of our seglist for LibExpunge() */
  85. ULONG LibrarySeg;
  86.  
  87. /* these are some error return values for the ARexx dispatcher */
  88. #define WRONG_NUM_OF_ARGS ERR10_017
  89. #define NO_MEM_FOR_ARGSTRING ERR10_003
  90. #define NO_REXX_LIB ERR10_014
  91.  
  92. /***************************************************************************/
  93. /***************************************************************************/
  94. /**           Here we prototype the functions found in this code          **/
  95. /***************************************************************************/
  96. /***************************************************************************/
  97.  
  98. struct Library * __saveds __asm LibInit(register __d0 struct Library *LibBase,
  99.                                         register __a0 ULONG LibSegment);
  100. struct Library * __saveds __asm LibOpen(register __a6 struct Library *LibraryBase);
  101. ULONG __saveds __asm LibClose(register __a6 struct Library *LibraryBase);
  102. ULONG __saveds __asm LibExpunge(register __a6 struct Library *LibraryBase);
  103.  
  104. UBYTE * __saveds __asm LockDevUnit(register __a0 UBYTE *Device,
  105.     register __d0 ULONG Unit, register __a1 UBYTE *OwnerName,
  106.     register __d1 UBYTE NotifyBit);
  107. UBYTE * __saveds __asm AttemptDevUnit(register __a0 UBYTE *Device,
  108.     register __d0 ULONG Unit, register __a1 UBYTE *OwnerName,
  109.     register __d1 UBYTE NotifyBit);
  110. void __saveds __asm FreeDevUnit(register __a0 UBYTE *Device,
  111.     register __d0 ULONG Unit);
  112. void __saveds __asm NameDevUnit(register __a0 UBYTE *Device,
  113.     register __d0 ULONG Unit, register __a1 UBYTE *OwnerName);
  114. BOOL __saveds __asm AvailDevUnit(register __a0 UBYTE *Device,
  115.     register __d0 ULONG Unit);
  116. ULONG __saveds __asm RexxQuery(register __a0 struct RexxMsg *RMsg,
  117.                   register __a1 UBYTE **ReturnArgString);
  118.  
  119. UBYTE *DoDevUnit(UBYTE *Device, ULONG Unit, UBYTE *OwnerName,
  120.     UBYTE NotifyBit, ULONG AttemptMaximum, ULONG AttemptDelay);
  121.  
  122. MasterListNode_t *FindMasterNode(UBYTE *Device, ULONG Unit);
  123. MasterListNode_t *CreateMasterNode(UBYTE *Device, ULONG Unit);
  124. void SetMasterNode(MasterListNode_t *Node, UBYTE *OwnerName, UBYTE NotifyBit);
  125.  
  126. BOOL OpenTimer(struct timerequest *TimeRequest, struct MsgPort *ReplyPort);
  127. void CloseTimer(struct timerequest *TimeRequest, struct MsgPort *ReplyPort);
  128. void Sleep(struct timerequest *TimeRequest, struct MsgPort *ReplyPort, ULONG Seconds);
  129.  
  130. /***************************************************************************/
  131. /***************************************************************************/
  132. /**     These are the library base routines used to open/close us, etc    **/
  133. /***************************************************************************/
  134. /***************************************************************************/
  135.  
  136. struct Library * __saveds __asm LibInit(register __d0 struct Library *LibBase,
  137.                                         register __a0 ULONG LibSegment)
  138. {
  139. #ifdef DEBUG
  140.     kprintf("LibInit called Base = %08lx\tSegment = %08lx\n", LibBase, LibSegment);
  141. #endif
  142.     LibrarySeg = LibSegment;
  143.  
  144.     NewList((struct List *)&MasterList);
  145.     InitSemaphore(&MasterListSema);
  146.  
  147.     return LibBase;
  148. }
  149.  
  150. struct Library * __saveds __asm LibOpen(register __a6 struct Library *LibraryBase)
  151. {
  152. #ifdef DEBUG
  153.     kprintf("LibOpen called");
  154. #endif
  155.     LibraryBase->lib_OpenCnt++;
  156.     LibraryBase->lib_Flags &= ~LIBF_DELEXP;
  157.  
  158. #ifdef DEBUG
  159.     kprintf(" OpenCount = %ld\n", LibraryBase->lib_OpenCnt);
  160. #endif
  161.     return LibraryBase;
  162. }
  163.  
  164. ULONG __saveds __asm LibClose(register __a6 struct Library *LibraryBase)
  165. {
  166. #ifdef DEBUG
  167.     kprintf("LibClose called");
  168. #endif
  169.     if (--LibraryBase->lib_OpenCnt == 0)
  170.         if (LibraryBase->lib_Flags & LIBF_DELEXP) {
  171. #ifdef DEBUG
  172.             kprintf("\n");
  173. #endif
  174.             return LibExpunge(LibraryBase);
  175.         }
  176.  
  177. #ifdef DEBUG
  178.     kprintf(" OpenCount = %ld\n", LibraryBase->lib_OpenCnt);
  179. #endif
  180.     return (ULONG) NULL;
  181. }
  182.  
  183. ULONG __saveds __asm LibExpunge(register __a6 struct Library *LibraryBase)
  184. {
  185.     BOOL    KillLib;
  186.     LONG    LibSize;
  187.  
  188. #ifdef DEBUG
  189.     kprintf("LibExpunge called\n");
  190. #endif
  191.  
  192.     KillLib = FALSE;
  193.  
  194.     /* we will refuse to expunge if we are currently tracking devices,
  195.        regardless of our OpenCnt.  We AttemptSemaphore() the MasterList
  196.        before checking it.  If someone else owns it then obviously we
  197.        can't try to shut down. */
  198.  
  199.     if (LibraryBase->lib_OpenCnt == 0 && AttemptSemaphore(&MasterListSema)) {
  200. #ifdef DEBUG
  201.         kprintf("LIBEXP: Checking if MasterList is empty\n");
  202. #endif
  203.         if (MasterList.mlh_TailPred == (struct MinNode *)&MasterList.mlh_Head) {
  204.             Remove((struct Node *)LibraryBase);
  205.             KillLib = TRUE;
  206.         }
  207.  
  208.         ReleaseSemaphore(&MasterListSema);
  209.     }
  210.  
  211.     if (KillLib) {
  212. #ifdef DEBUG
  213.         kprintf("LIBEXP: Removing this library!\n");
  214. #endif
  215.         LibSize = LibraryBase->lib_NegSize + LibraryBase->lib_PosSize;
  216.         FreeMem((char *)LibraryBase - LibraryBase->lib_NegSize, LibSize);
  217.  
  218.         return LibrarySeg;
  219.     }
  220.  
  221. #ifdef DEBUG
  222.     kprintf("LIBEXP: Can't remove library, setting LIBF_DELEXP\n");
  223. #endif
  224.     LibraryBase->lib_Flags |= LIBF_DELEXP;
  225.     return (ULONG) NULL;
  226. }
  227.  
  228. /***************************************************************************/
  229. /***************************************************************************/
  230. /**       Now we have the routines that acutally make up the library      **/
  231. /***************************************************************************/
  232. /***************************************************************************/
  233.  
  234. /* LockDevUnit() will block until it gets hold of the requested
  235.    device and unit
  236. */
  237.  
  238. UBYTE * __saveds __asm LockDevUnit(register __a0 UBYTE *Device,
  239.     register __d0 ULONG Unit, register __a1 UBYTE *OwnerName,
  240.     register __d1 UBYTE NotifyBit)
  241. {
  242.     return DoDevUnit(Device, Unit, OwnerName, NotifyBit, -1L, 3L);
  243. }
  244.  
  245. /* AttemptDevUnit() will try to grab the requested device and unit.
  246.    We allow up to five seconds for an owner of the lock to let it
  247.    go before we return a failure.
  248. */
  249.  
  250. UBYTE *__saveds __asm AttemptDevUnit(register __a0 UBYTE *Device,
  251.     register __d0 ULONG Unit, register __a1 UBYTE *OwnerName,
  252.     register __d1 UBYTE NotifyBit)
  253. {
  254.     return DoDevUnit(Device, Unit, OwnerName, NotifyBit, 5L, 1L);
  255. }
  256.  
  257. /* FreeDevUnit() releases a lock on a device/unit that was previously
  258.    attained by a call to LockDevUnit() or AttempDevUnit()
  259.  
  260.    if there is stuff hanging off the node's waitlist then we just
  261.    null the owner task field so that LockDevUnit() and AttemptDevUnit()
  262.    know that the port is free.  if there isn't anything on the waitlist
  263.    then no one else wants this node so remove the node from the master
  264.    list and free the memory used by it.
  265. */
  266.  
  267. void __saveds __asm FreeDevUnit(register __a0 UBYTE *Device,
  268.     register __d0 ULONG Unit)
  269. {
  270.     MasterListNode_t *Master;
  271.  
  272. #ifdef DEBUG
  273.     kprintf("FreeDevUnit called \"%s\" %ld\n", Device, Unit);
  274. #endif
  275.  
  276.     ObtainSemaphore(&MasterListSema);
  277.  
  278.     if (Master = FindMasterNode(Device, Unit)) {
  279. #ifdef DEBUG
  280.         kprintf("FDU: Master = %08lx\n", Master);
  281. #endif
  282.         if (Master->mn_OwnerTask == FindTask(0L)) {
  283. #ifdef DEBUG
  284.             kprintf("FDU: Master->mn_OwnerTask matches\n");
  285. #endif
  286.             if (Master->mn_WaitList.mlh_TailPred == (struct MinNode *)
  287.                &Master->mn_WaitList.mlh_Head) {
  288. #ifdef DEBUG
  289.                 kprintf("Removing this Master from MasterList\n");
  290. #endif
  291.                 Remove((struct Node *)Master);
  292.                 FreeMem((char *)Master, Master->mn_SizeOf);
  293.             } else {
  294. #ifdef DEBUG
  295.                 kprintf("Setting Master->mn_OwnerTask to NULL\n");
  296. #endif
  297.                 Master->mn_OwnerTask = (struct Task *)NULL;
  298.                 Master->mn_OwnerName = (UBYTE *)NULL;
  299.             }
  300.         }
  301.     }
  302.  
  303.     ReleaseSemaphore(&MasterListSema);
  304.     return;
  305. }
  306.  
  307. /* NameDevUnit() will allow the owner of a node to change the owner
  308.    name that is returned when an AttemptDevUnit() times out and fails.
  309.    Useful for programs that sit on the modem and start others when
  310.    someone calls in (i.e., "Getty" becomes "Getty started <program name>").
  311. */
  312.  
  313. void __saveds __asm NameDevUnit(register __a0 UBYTE *Device,
  314.     register __d0 ULONG Unit, register __a1 UBYTE *OwnerName)
  315. {
  316.     MasterListNode_t *Master;
  317.  
  318. #ifdef DEBUG
  319.     kprintf("NameDevUnit called \"%s\" %ld \"%s\"\n", Device,
  320.         Unit, OwnerName);
  321. #endif
  322.     ObtainSemaphore(&MasterListSema);
  323.  
  324.     if (Master = FindMasterNode(Device, Unit)) {
  325.         if (Master->mn_OwnerTask == FindTask(0L)) {
  326. #ifdef DEBUG
  327.             kprintf("NDU: Master owned by this task.  Setting name\n");
  328. #endif
  329.             Master->mn_OwnerName = OwnerName;
  330.         }
  331.     }
  332.  
  333.     ReleaseSemaphore(&MasterListSema);
  334. }
  335.  
  336. /* AvailDevUnit() will return the availability status of a node very
  337.    quickly.  This may be preferable over AttemptDevUnit(), which can
  338.    take up 5 seconds.
  339. */
  340.  
  341. BOOL __saveds __asm AvailDevUnit(register __a0 UBYTE *Device,
  342.     register __d0 ULONG Unit)
  343. {
  344.     MasterListNode_t *Master;
  345.  
  346. #ifdef DEBUG
  347.     kprintf("AvailDevUnit called \"%s\" %ld\n", Device, Unit);
  348. #endif
  349.     ObtainSemaphore(&MasterListSema);
  350.  
  351.     /* we simply check to see if a node for this device/unit
  352.        exists.  if it does, we return FALSE (not available)
  353.        because it is either owned or someone else will camp
  354.        on it very soon. */
  355.     Master = FindMasterNode(Device, Unit);
  356.  
  357.     ReleaseSemaphore(&MasterListSema);
  358.  
  359.     if (Master)
  360.         return FALSE;
  361.     else
  362.         return TRUE;
  363. }
  364.  
  365. /* RexxQuery function handles ARexx requests when OwnDevUnit.library has
  366.    been added as an ARexx function host */
  367.  
  368. ULONG __saveds __asm RexxQuery(register __a0 struct RexxMsg *RMsg,
  369.                   register __a1 UBYTE **ReturnArgString)
  370. {
  371.     UBYTE *Error = NULL;
  372.     UBYTE NumArgs;
  373.     BOOL  DidAFunc = FALSE;
  374.     BOOL  WeDidALock = FALSE;
  375.     BOOL  NoRexxLib = FALSE;
  376.  
  377.     ULONG Unit;
  378.     UBYTE NotifyBit;
  379.  
  380.     struct RxsLib *RexxSysBase;
  381.  
  382. #ifdef DEBUG
  383.     kprintf("RexxQuery called\n");
  384. #endif
  385.  
  386.     /* set ReturnArgString to NULL so that if we return early
  387.        ARexx will know it doesn't point at anything */
  388.     *ReturnArgString = NULL;
  389.  
  390.     /* grab the number of arguments they passed us */
  391.     NumArgs = RMsg->rm_Action & 0xFF;
  392.  
  393.     /* now look and see if this is a function we can do.  Note that
  394.        we don't use a lookup table here as we have very few funcs
  395.            to implement and this is easier... signed, his laziness */
  396.  
  397.     if (!stricmp(RMsg->rm_Args[0], "LockDevUnit")) {
  398.         if (NumArgs == 3) {
  399.             /* this is a LockDevUnit call */
  400.             Unit = atoi(RMsg->rm_Args[2]);
  401.             NotifyBit = atoi(RMsg->rm_Args[3]);
  402.             Error = LockDevUnit(RMsg->rm_Args[1], Unit,
  403.                         "ARexx", NotifyBit);
  404.             WeDidALock = DidAFunc = TRUE;
  405.         } else
  406.             return WRONG_NUM_OF_ARGS;
  407.     } else
  408.     if (!stricmp(RMsg->rm_Args[0], "AttemptDevUnit")) {
  409.         if (NumArgs == 3) {
  410.             /* this is an AttemptDevUnit call */
  411.             Unit = atoi(RMsg->rm_Args[2]);
  412.             NotifyBit = atoi(RMsg->rm_Args[3]);
  413.             Error = AttemptDevUnit(RMsg->rm_Args[1], Unit,
  414.                            "ARexx", NotifyBit);
  415.             WeDidALock = DidAFunc = TRUE;
  416.         } else
  417.             return WRONG_NUM_OF_ARGS;
  418.     } else
  419.     if (!stricmp(RMsg->rm_Args[0], "FreeDevUnit")) {
  420.         if (NumArgs == 2) {
  421.             /* this is a FreeDevUnit call */
  422.             Unit = atoi(RMsg->rm_Args[2]);
  423.             FreeDevUnit(RMsg->rm_Args[1], Unit);
  424.             DidAFunc = TRUE;
  425.         } else
  426.             return WRONG_NUM_OF_ARGS;
  427.     } else
  428.     if (!stricmp(RMsg->rm_Args[0], "AvailDevUnit")) {
  429.         if (NumArgs == 2) {
  430.             /* this is an AvailDevUnit call */
  431.             Unit = atoi(RMsg->rm_Args[2]);
  432.             if (AvailDevUnit(RMsg->rm_Args[1], Unit))
  433.                 Error = "1";
  434.             else
  435.                 Error = "0";
  436.             DidAFunc = TRUE;
  437.         } else
  438.             return WRONG_NUM_OF_ARGS;
  439.     }
  440.  
  441.     if (!DidAFunc)
  442.         return 1L;    /* not a function we recognize */
  443.  
  444.     /* if we get here then we did a function.  try an allocate an
  445.        argstring to return the result string in.  if that fails
  446.        then check if we did a lock.  if we did, and it was succesful
  447.        then we want to free the lock before returning an error saying
  448.        we couldn't allocate the argstring. */
  449.  
  450.     /* try and get rexxsyslib.library */
  451.     if (RexxSysBase = (struct RxsLib *)OpenLibrary("rexxsyslib.library", 0)) {
  452.         {
  453.             ULONG Length;
  454.  
  455.             if (Error)
  456.                 Length = strlen(Error);
  457.             else
  458.                 Length = 0;
  459.  
  460.             *ReturnArgString = CreateArgstring(Error, Length);
  461.         }
  462.  
  463.         CloseLibrary((struct Library *)RexxSysBase);
  464.  
  465.         if (*ReturnArgString)
  466.             return 0L;    /* no low level error so return argstring */
  467.     } else
  468.         NoRexxLib = TRUE;
  469.  
  470.     /* if we did a lock successfully then free it before returning */
  471.     if (WeDidALock && Error == NULL)
  472.         FreeDevUnit(RMsg->rm_Args[1], Unit);
  473.  
  474.     if (NoRexxLib)
  475.         return NO_REXX_LIB;
  476.     else
  477.         return NO_MEM_FOR_ARGSTRING;
  478. }
  479.  
  480. /***************************************************************************/
  481. /***************************************************************************/
  482. /**     These are the support routines used by the top level functions    **/
  483. /***************************************************************************/
  484. /***************************************************************************/
  485.  
  486. /* DoDevUnit() is by in large the most important support routine.  it is
  487.    what actually handles the LockDevUnit() and AttemptDevUnit() requests.
  488.    The difference in the above two calls is the MaxAttempts and
  489.    AttemptDelay that they use.
  490.  
  491.    LockDevUnit() passes -1 in MaxAttempts.  It would take near eternity
  492.    to reach $FFFFFFFF so LockDevUnit() basically blocks forever.  It uses
  493.    an AttemptDelay of 3 seconds to keep from hammering the list constantly.
  494.  
  495.    AttemptDevUnit() passes 5 in MaxAttempts.  Since AttemptDelay is 1
  496.    second for AttemptDevUnit(), this effectively means that this call will
  497.    wait no longer than five seconds for a lock and will return the name
  498.    of the current owner if it can't get it within time.
  499. */
  500.  
  501. UBYTE *DoDevUnit(UBYTE *Device, ULONG Unit, UBYTE *OwnerName,
  502.     UBYTE NotifyBit, ULONG MaxAttempts, ULONG AttemptDelay)
  503. {
  504.     MasterListNode_t *Master;
  505.     WaitListNode_t WaitNode;
  506.  
  507.     struct MsgPort ReplyPort;
  508.     struct timerequest TimeRequest;
  509.  
  510.     UBYTE AttemptNumber;
  511.     UBYTE ReSignalCount;
  512.  
  513.     UBYTE *Error = NULL;
  514.  
  515. #ifdef DEBUG
  516.     kprintf("DoDevUnit called \"%s\" %ld \"%s\" %ld %ld %ld\n",
  517.         Device, Unit, OwnerName, NotifyBit, MaxAttempts, AttemptDelay);
  518. #endif
  519.  
  520.     if (!Device)
  521.         return ODUERR_BADNAME;
  522.  
  523.     if (NotifyBit == -1)
  524.         return ODUERR_BADBIT;
  525.  
  526.     ObtainSemaphore(&MasterListSema);
  527.  
  528.     /* if the node doesn't exist then we try and create it */
  529.     if (!(Master = FindMasterNode(Device, Unit))) {
  530.         if (Master = CreateMasterNode(Device, Unit))
  531.             SetMasterNode(Master, OwnerName, NotifyBit);
  532.         else
  533.             Error = ODUERR_NOMEM;
  534.  
  535.         ReleaseSemaphore(&MasterListSema);
  536.  
  537.         return Error;
  538.     }
  539.  
  540.     /* if we already own this task then simply return that the
  541.        device is successfully locked.  */
  542.     if (Master->mn_OwnerTask == FindTask(0L)) {
  543. #ifdef DEBUG
  544.         kprintf("DDU: Task already owns this Master\n");
  545. #endif
  546.         ReleaseSemaphore(&MasterListSema);
  547.         return (UBYTE *)NULL;
  548.     }
  549.  
  550.     /* we are going to need the timer to do things right so open
  551.        it up.  If we can't init the timer for use then return
  552.        the appropriate lock failed message */
  553.     if (!OpenTimer(&TimeRequest, &ReplyPort)) {
  554.         ReleaseSemaphore(&MasterListSema);
  555.         return ODUERR_NOTIMER;
  556.     }
  557.  
  558.     /* setup up our wait node and add it onto the wait list */
  559.     WaitNode.wn_Task = FindTask(0L);
  560.     AddTail((struct List *)&Master->mn_WaitList, (struct Node *)&WaitNode);
  561.  
  562.     /* if the current owner has provided a NotifyBit then we
  563.        signal the task to let it know someone wants the list */
  564.     if (Master->mn_OwnerTask && Master->mn_NotifyBit) {
  565. #ifdef DEBUG
  566.         kprintf("DDU: Notifying %08lx on SigBit %ld\n",
  567.             Master->mn_OwnerTask, Master->mn_NotifyBit);
  568. #endif
  569.         Signal(Master->mn_OwnerTask, 1L << Master->mn_NotifyBit);
  570.     }
  571.  
  572.     /* now we wait for the node to become free (mn_OwnerTask = NULL)
  573.        and then check to see if we are the head of the wait list.
  574.        if we are the head then we can own the device.  else someone
  575.        else gets to use it before us so continue waiting.
  576.  
  577.        we must release the MasterListSema and wait before checking
  578.        or the owner will never be able to unlock the node! */
  579.     ReleaseSemaphore(&MasterListSema);
  580.  
  581.     Sleep(&TimeRequest, &ReplyPort, 1L);    /* our initial wait is very short */
  582.  
  583.     for (AttemptNumber = ReSignalCount = 0; AttemptNumber < MaxAttempts;
  584.         ReSignalCount++, AttemptNumber++) {
  585.         ObtainSemaphore(&MasterListSema);
  586.  
  587.         if (Master->mn_OwnerTask) {    /* still owned by someone */
  588.             if (ReSignalCount > RE_SIGNAL) {
  589.                 if (Master->mn_NotifyBit)
  590.                     Signal(Master->mn_OwnerTask,
  591.                         1L << Master->mn_NotifyBit);
  592.                 ReSignalCount = 0;
  593.             }
  594.  
  595.             ReleaseSemaphore(&MasterListSema);
  596.             Sleep(&TimeRequest, &ReplyPort, AttemptDelay);
  597.             continue;
  598.         }
  599.  
  600.         /* device is free.  if we aren't the head of the wait
  601.            list we must let someone else go first */
  602.         if (Master->mn_WaitList.mlh_Head != (struct MinNode *)&WaitNode) {
  603.             ReleaseSemaphore(&MasterListSema);
  604.             Sleep(&TimeRequest, &ReplyPort, AttemptDelay);
  605.             continue;
  606.         }
  607.  
  608.         /* we can own the node!  yeah! */
  609.         RemHead((struct List *)&Master->mn_WaitList);
  610.         SetMasterNode(Master, OwnerName, NotifyBit);
  611.  
  612.         /* if this new owner supplied a NotifyBit and there are
  613.            other people waiting on this node then it is only
  614.            fair to trigger the NotifyBit. */
  615.         if (Master->mn_WaitList.mlh_TailPred != (struct MinNode *)
  616.            &Master->mn_WaitList.mlh_Head)
  617.             if (NotifyBit)
  618.                 Signal(FindTask(0L), 1L << NotifyBit);
  619.  
  620.         /* finally, shut things down and return a successful lock */
  621.         ReleaseSemaphore(&MasterListSema);
  622.         CloseTimer(&TimeRequest, &ReplyPort);
  623.  
  624.         return (UBYTE *)NULL;
  625.     }
  626.  
  627.     /* if we get here then someone else has the device and refuses to
  628.        give it up.  so we return a pointer to the owner's name.
  629.            we also need to remove our WaitNode from the Master's waitlist. */
  630.  
  631.     ObtainSemaphore(&MasterListSema);
  632.  
  633.     Remove((struct Node *)&WaitNode);
  634.  
  635.     Error = Master->mn_OwnerName;
  636.  
  637.     if (!Error)
  638.         Error = ODUERR_UNKNOWN;
  639.  
  640.     ReleaseSemaphore(&MasterListSema);
  641.     CloseTimer(&TimeRequest, &ReplyPort);
  642.  
  643.     return Error;
  644. }
  645.  
  646. /* attempt to find a node in the master list.  returns NULL if not found.
  647.    does not arbitrate for access to the master list.  This must be done
  648.    by the caller! */
  649. MasterListNode_t *FindMasterNode(UBYTE *Device, ULONG Unit)
  650. {
  651.     MasterListNode_t *MasterNode;
  652.  
  653.     for (MasterNode = (MasterListNode_t *)MasterList.mlh_Head;
  654.         MasterNode->mn_Node.mln_Succ; MasterNode = (MasterListNode_t *)
  655.         MasterNode->mn_Node.mln_Succ) {
  656.  
  657. #ifdef DEBUG
  658.         kprintf("FMN: compare \"%s\" %ld with requested \"%s\" %ld\n",
  659.             MasterNode->mn_Device, MasterNode->mn_Unit, Device, Unit);
  660. #endif
  661.         if (strcmp(MasterNode->mn_Device, Device) == 0 &&
  662.             MasterNode->mn_Unit == Unit) {
  663. #ifdef DEBUG
  664.             kprintf("FMN: Match found Master = %08lx\n", MasterNode);
  665. #endif
  666.             return MasterNode;
  667.         }
  668.     }
  669.  
  670.     return (MasterListNode_t *)NULL;
  671. }
  672.  
  673. /* this will try to create a new MasterListNode.  returns NULL if it
  674.    fails (mainly due to an out of memory error).  does not arbitrate
  675.    for access to the master list */
  676. MasterListNode_t *CreateMasterNode(UBYTE *Device, ULONG Unit)
  677. {
  678.     MasterListNode_t *MasterNode;
  679.     ULONG DevNameSize;
  680.     ULONG AllocSize;
  681.  
  682. #ifdef DEBUG
  683.     kprintf("CreateMasterNode called \"%s\" %ld\n", Device, Unit);
  684. #endif
  685.  
  686.     /* failsafe:  if the node already exists then return a
  687.        pointer to it instead of creating a new one. */
  688.  
  689.     if (MasterNode = FindMasterNode(Device, Unit))
  690.         return MasterNode;
  691.  
  692.     /* alloc some memory to hold the node.  return NULL if the
  693.        alloc fails. we copy the device name onto the end of the
  694.        node so that an original owner can quit and we don't
  695.        lose the device name */
  696.  
  697.     DevNameSize = strlen(Device);
  698.     AllocSize = sizeof(MasterListNode_t) + DevNameSize;
  699.  
  700. #ifdef DEBUG
  701.     kprintf("CMN: DevNameSize = %ld\tAllocSize = %ld\n", DevNameSize, AllocSize);
  702. #endif
  703.  
  704.     if (!(MasterNode = (MasterListNode_t *)AllocMem(AllocSize,
  705.         MEMF_PUBLIC|MEMF_CLEAR)))
  706.         return (MasterListNode_t *)NULL;
  707.  
  708.     /* copy device name over and fill in a few fields */
  709.  
  710.     MasterNode->mn_SizeOf = AllocSize;
  711.     MasterNode->mn_Unit = Unit;
  712.     CopyMem(Device, MasterNode->mn_Device, DevNameSize + 1);
  713.     NewList((struct List *)&MasterNode->mn_WaitList);
  714.  
  715.     /* add the master node to the master list */
  716.     AddTail((struct List *)&MasterList, (struct Node *)MasterNode);
  717.  
  718.     /* return a pointer to the newly added node */
  719.  
  720. #ifdef DEBUG
  721.     kprintf("CMN: Master %08lx created\n", MasterNode);
  722. #endif
  723.  
  724.     return MasterNode;
  725. }
  726.  
  727. void SetMasterNode(MasterListNode_t *Node, UBYTE *OwnerName, UBYTE NotifyBit)
  728. {
  729. #ifdef DEBUG
  730.     kprintf("SetMasterNode called Master %08lx \"%s\" %ld\n", Node, OwnerName, NotifyBit);
  731. #endif
  732.     
  733.     Node->mn_OwnerTask = FindTask(0L);
  734.     Node->mn_OwnerName = OwnerName;
  735.     Node->mn_NotifyBit = NotifyBit;
  736. }
  737.  
  738. /***************************************************************************/
  739. /***************************************************************************/
  740. /**        These routines manage the timer.device for doing delays        **/
  741. /***************************************************************************/
  742. /***************************************************************************/
  743.  
  744. BOOL OpenTimer(struct timerequest *TimeRequest, struct MsgPort *ReplyPort)
  745. {
  746.     ULONG SigBit;
  747.  
  748.     memset((char *)TimeRequest, 0, sizeof(struct timerequest));
  749.     memset((char *)ReplyPort, 0, sizeof(struct MsgPort));
  750.  
  751.     if ((SigBit = AllocSignal(-1L)) == -1L)
  752.         return FALSE;
  753.  
  754.     ReplyPort->mp_Node.ln_Type = NT_MSGPORT;
  755.  
  756.     ReplyPort->mp_Flags   = PA_SIGNAL;
  757.     ReplyPort->mp_SigBit  = SigBit;
  758.     ReplyPort->mp_SigTask = FindTask(0L);
  759.  
  760.     NewList(&(ReplyPort->mp_MsgList));
  761.  
  762.     if (OpenDevice(TIMERNAME, UNIT_VBLANK, (struct IORequest *)
  763.         TimeRequest, 0)) {
  764.         FreeSignal(SigBit);
  765.         ReplyPort->mp_SigTask         = (struct Task *) -1;
  766.         ReplyPort->mp_MsgList.lh_Head = (struct Node *) -1;
  767.         return FALSE;
  768.     }
  769.  
  770.     TimeRequest->tr_node.io_Message.mn_ReplyPort = ReplyPort;
  771.  
  772.     return TRUE;
  773. }
  774.  
  775. void CloseTimer(struct timerequest *TimeRequest, struct MsgPort *ReplyPort)
  776. {
  777.     CloseDevice((struct IORequest *)TimeRequest);
  778.     FreeSignal(ReplyPort->mp_SigBit);
  779.     ReplyPort->mp_SigTask         = (struct Task *) -1;
  780.     ReplyPort->mp_MsgList.lh_Head = (struct Node *) -1;
  781. }
  782.  
  783. void Sleep(struct timerequest *TimeRequest, struct MsgPort *ReplyPort, ULONG Seconds)
  784. {
  785.     TimeRequest->tr_node.io_Command = TR_ADDREQUEST;
  786.     TimeRequest->tr_time.tv_secs = Seconds;
  787.     TimeRequest->tr_time.tv_micro = 0;
  788.  
  789.     DoIO((struct IORequest *)TimeRequest);
  790. }
  791.