home *** CD-ROM | disk | FTP | other *** search
/ Amiga GigaPD 3 / Amiga_GigaPD_v3_3of3.iso / fred_fish / fish_601-700_lha / fish_650.lha / OWNDEVUNIT / SOURCE / OwnDevUnit.c < prev    next >
C/C++ Source or Header  |  1993-06-02  |  25KB  |  822 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. UBYTE *BaseName(UBYTE *Path);
  126.  
  127. BOOL OpenTimer(struct timerequest *TimeRequest, struct MsgPort *ReplyPort);
  128. void CloseTimer(struct timerequest *TimeRequest, struct MsgPort *ReplyPort);
  129. void Sleep(struct timerequest *TimeRequest, struct MsgPort *ReplyPort, ULONG Seconds);
  130.  
  131. /***************************************************************************/
  132. /***************************************************************************/
  133. /**     These are the library base routines used to open/close us, etc    **/
  134. /***************************************************************************/
  135. /***************************************************************************/
  136.  
  137. struct Library * __saveds __asm LibInit(register __d0 struct Library *LibBase,
  138.                                         register __a0 ULONG LibSegment)
  139. {
  140. #ifdef DEBUG
  141.     kprintf("LibInit called Base = %08lx\tSegment = %08lx\n", LibBase, LibSegment);
  142. #endif
  143.     LibrarySeg = LibSegment;
  144.  
  145.     NewList((struct List *)&MasterList);
  146.     InitSemaphore(&MasterListSema);
  147.  
  148.     return LibBase;
  149. }
  150.  
  151. struct Library * __saveds __asm LibOpen(register __a6 struct Library *LibraryBase)
  152. {
  153. #ifdef DEBUG
  154.     kprintf("LibOpen called");
  155. #endif
  156.     LibraryBase->lib_OpenCnt++;
  157.     LibraryBase->lib_Flags &= ~LIBF_DELEXP;
  158.  
  159. #ifdef DEBUG
  160.     kprintf(" OpenCount = %ld\n", LibraryBase->lib_OpenCnt);
  161. #endif
  162.     return LibraryBase;
  163. }
  164.  
  165. ULONG __saveds __asm LibClose(register __a6 struct Library *LibraryBase)
  166. {
  167. #ifdef DEBUG
  168.     kprintf("LibClose called");
  169. #endif
  170.     if (--LibraryBase->lib_OpenCnt == 0)
  171.         if (LibraryBase->lib_Flags & LIBF_DELEXP) {
  172. #ifdef DEBUG
  173.             kprintf("\n");
  174. #endif
  175.             return LibExpunge(LibraryBase);
  176.         }
  177.  
  178. #ifdef DEBUG
  179.     kprintf(" OpenCount = %ld\n", LibraryBase->lib_OpenCnt);
  180. #endif
  181.     return (ULONG) NULL;
  182. }
  183.  
  184. ULONG __saveds __asm LibExpunge(register __a6 struct Library *LibraryBase)
  185. {
  186.     BOOL    KillLib;
  187.     LONG    LibSize;
  188.  
  189. #ifdef DEBUG
  190.     kprintf("LibExpunge called\n");
  191. #endif
  192.  
  193.     KillLib = FALSE;
  194.  
  195.     /* we will refuse to expunge if we are currently tracking devices,
  196.        regardless of our OpenCnt.  We AttemptSemaphore() the MasterList
  197.        before checking it.  If someone else owns it then obviously we
  198.        can't try to shut down. */
  199.  
  200.     if (LibraryBase->lib_OpenCnt == 0 && AttemptSemaphore(&MasterListSema)) {
  201. #ifdef DEBUG
  202.         kprintf("LIBEXP: Checking if MasterList is empty\n");
  203. #endif
  204.         if (MasterList.mlh_TailPred == (struct MinNode *)&MasterList.mlh_Head) {
  205.             Remove((struct Node *)LibraryBase);
  206.             KillLib = TRUE;
  207.         }
  208.  
  209.         ReleaseSemaphore(&MasterListSema);
  210.     }
  211.  
  212.     if (KillLib) {
  213. #ifdef DEBUG
  214.         kprintf("LIBEXP: Removing this library!\n");
  215. #endif
  216.         LibSize = LibraryBase->lib_NegSize + LibraryBase->lib_PosSize;
  217.         FreeMem((char *)LibraryBase - LibraryBase->lib_NegSize, LibSize);
  218.  
  219.         return LibrarySeg;
  220.     }
  221.  
  222. #ifdef DEBUG
  223.     kprintf("LIBEXP: Can't remove library, setting LIBF_DELEXP\n");
  224. #endif
  225.     LibraryBase->lib_Flags |= LIBF_DELEXP;
  226.     return (ULONG) NULL;
  227. }
  228.  
  229. /***************************************************************************/
  230. /***************************************************************************/
  231. /**       Now we have the routines that acutally make up the library      **/
  232. /***************************************************************************/
  233. /***************************************************************************/
  234.  
  235. /* LockDevUnit() will block until it gets hold of the requested
  236.    device and unit
  237. */
  238.  
  239. UBYTE * __saveds __asm LockDevUnit(register __a0 UBYTE *Device,
  240.     register __d0 ULONG Unit, register __a1 UBYTE *OwnerName,
  241.     register __d1 UBYTE NotifyBit)
  242. {
  243.     return DoDevUnit(Device, Unit, OwnerName, NotifyBit, -1L, 3L);
  244. }
  245.  
  246. /* AttemptDevUnit() will try to grab the requested device and unit.
  247.    We allow up to five seconds for an owner of the lock to let it
  248.    go before we return a failure.
  249. */
  250.  
  251. UBYTE *__saveds __asm AttemptDevUnit(register __a0 UBYTE *Device,
  252.     register __d0 ULONG Unit, register __a1 UBYTE *OwnerName,
  253.     register __d1 UBYTE NotifyBit)
  254. {
  255.     return DoDevUnit(Device, Unit, OwnerName, NotifyBit, 5L, 1L);
  256. }
  257.  
  258. /* FreeDevUnit() releases a lock on a device/unit that was previously
  259.    attained by a call to LockDevUnit() or AttempDevUnit()
  260.  
  261.    if there is stuff hanging off the node's waitlist then we just
  262.    null the owner task field so that LockDevUnit() and AttemptDevUnit()
  263.    know that the port is free.  if there isn't anything on the waitlist
  264.    then no one else wants this node so remove the node from the master
  265.    list and free the memory used by it.
  266. */
  267.  
  268. void __saveds __asm FreeDevUnit(register __a0 UBYTE *Device,
  269.     register __d0 ULONG Unit)
  270. {
  271.     MasterListNode_t *Master;
  272.  
  273. #ifdef DEBUG
  274.     kprintf("FreeDevUnit called \"%s\" %ld\n", Device, Unit);
  275. #endif
  276.  
  277.     if (!Device)
  278.         return;
  279.  
  280.     ObtainSemaphore(&MasterListSema);
  281.  
  282.     if (Master = FindMasterNode(Device, Unit)) {
  283. #ifdef DEBUG
  284.         kprintf("FDU: Master = %08lx\n", Master);
  285. #endif
  286.         if (Master->mn_OwnerTask == FindTask(0L)) {
  287. #ifdef DEBUG
  288.             kprintf("FDU: Master->mn_OwnerTask matches\n");
  289. #endif
  290.             if (Master->mn_WaitList.mlh_TailPred == (struct MinNode *)
  291.                &Master->mn_WaitList.mlh_Head) {
  292. #ifdef DEBUG
  293.                 kprintf("Removing this Master from MasterList\n");
  294. #endif
  295.                 Remove((struct Node *)Master);
  296.                 FreeMem((char *)Master, Master->mn_SizeOf);
  297.             } else {
  298. #ifdef DEBUG
  299.                 kprintf("Setting Master->mn_OwnerTask to NULL\n");
  300. #endif
  301.                 Master->mn_OwnerTask = (struct Task *)NULL;
  302.                 Master->mn_OwnerName = (UBYTE *)NULL;
  303.             }
  304.         }
  305.     }
  306.  
  307.     ReleaseSemaphore(&MasterListSema);
  308.     return;
  309. }
  310.  
  311. /* NameDevUnit() will allow the owner of a node to change the owner
  312.    name that is returned when an AttemptDevUnit() times out and fails.
  313.    Useful for programs that sit on the modem and start others when
  314.    someone calls in (i.e., "Getty" becomes "Getty started <program name>").
  315. */
  316.  
  317. void __saveds __asm NameDevUnit(register __a0 UBYTE *Device,
  318.     register __d0 ULONG Unit, register __a1 UBYTE *OwnerName)
  319. {
  320.     MasterListNode_t *Master;
  321.  
  322. #ifdef DEBUG
  323.     kprintf("NameDevUnit called \"%s\" %ld \"%s\"\n", Device,
  324.         Unit, OwnerName);
  325. #endif
  326.  
  327.     if (!Device)
  328.         return;
  329.  
  330.     ObtainSemaphore(&MasterListSema);
  331.  
  332.     if (Master = FindMasterNode(Device, Unit)) {
  333.         if (Master->mn_OwnerTask == FindTask(0L)) {
  334. #ifdef DEBUG
  335.             kprintf("NDU: Master owned by this task.  Setting name\n");
  336. #endif
  337.             Master->mn_OwnerName = OwnerName;
  338.         }
  339.     }
  340.  
  341.     ReleaseSemaphore(&MasterListSema);
  342. }
  343.  
  344. /* AvailDevUnit() will return the availability status of a node very
  345.    quickly.  This may be preferable over AttemptDevUnit(), which can
  346.    take up 5 seconds.
  347. */
  348.  
  349. BOOL __saveds __asm AvailDevUnit(register __a0 UBYTE *Device,
  350.     register __d0 ULONG Unit)
  351. {
  352.     MasterListNode_t *Master;
  353.  
  354.     if (!Device)
  355.         return FALSE;
  356.  
  357. #ifdef DEBUG
  358.     kprintf("AvailDevUnit called \"%s\" %ld\n", Device, Unit);
  359. #endif
  360.     ObtainSemaphore(&MasterListSema);
  361.  
  362.     /* we simply check to see if a node for this device/unit
  363.        exists.  if it does, we return FALSE (not available)
  364.        because it is either owned or someone else will camp
  365.        on it very soon. */
  366.     Master = FindMasterNode(Device, Unit);
  367.  
  368.     ReleaseSemaphore(&MasterListSema);
  369.  
  370.     if (Master)
  371.         return FALSE;
  372.     else
  373.         return TRUE;
  374. }
  375.  
  376. /* RexxQuery function handles ARexx requests when OwnDevUnit.library has
  377.    been added as an ARexx function host */
  378.  
  379. ULONG __saveds __asm RexxQuery(register __a0 struct RexxMsg *RMsg,
  380.                   register __a1 UBYTE **ReturnArgString)
  381. {
  382.     UBYTE *Error = NULL;
  383.     UBYTE NumArgs;
  384.     BOOL  DidAFunc = FALSE;
  385.     BOOL  WeDidALock = FALSE;
  386.     BOOL  NoRexxLib = FALSE;
  387.  
  388.     ULONG Unit;
  389.     UBYTE NotifyBit;
  390.  
  391.     struct RxsLib *RexxSysBase;
  392.  
  393. #ifdef DEBUG
  394.     kprintf("RexxQuery called\n");
  395. #endif
  396.  
  397.     /* set ReturnArgString to NULL so that if we return early
  398.        ARexx will know it doesn't point at anything */
  399.     *ReturnArgString = NULL;
  400.  
  401.     /* grab the number of arguments they passed us */
  402.     NumArgs = RMsg->rm_Action & 0xFF;
  403.  
  404.     /* now look and see if this is a function we can do.  Note that
  405.        we don't use a lookup table here as we have very few funcs
  406.            to implement and this is easier... signed, his laziness */
  407.  
  408.     if (!stricmp(RMsg->rm_Args[0], "LockDevUnit")) {
  409.         if (NumArgs == 3) {
  410.             /* this is a LockDevUnit call */
  411.             Unit = atoi(RMsg->rm_Args[2]);
  412.             NotifyBit = atoi(RMsg->rm_Args[3]);
  413.             Error = LockDevUnit(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], "AttemptDevUnit")) {
  420.         if (NumArgs == 3) {
  421.             /* this is an AttemptDevUnit call */
  422.             Unit = atoi(RMsg->rm_Args[2]);
  423.             NotifyBit = atoi(RMsg->rm_Args[3]);
  424.             Error = AttemptDevUnit(RMsg->rm_Args[1], Unit,
  425.                            "ARexx", NotifyBit);
  426.             WeDidALock = DidAFunc = TRUE;
  427.         } else
  428.             return WRONG_NUM_OF_ARGS;
  429.     } else
  430.     if (!stricmp(RMsg->rm_Args[0], "FreeDevUnit")) {
  431.         if (NumArgs == 2) {
  432.             /* this is a FreeDevUnit call */
  433.             Unit = atoi(RMsg->rm_Args[2]);
  434.             FreeDevUnit(RMsg->rm_Args[1], Unit);
  435.             DidAFunc = TRUE;
  436.         } else
  437.             return WRONG_NUM_OF_ARGS;
  438.     } else
  439.     if (!stricmp(RMsg->rm_Args[0], "AvailDevUnit")) {
  440.         if (NumArgs == 2) {
  441.             /* this is an AvailDevUnit call */
  442.             Unit = atoi(RMsg->rm_Args[2]);
  443.             if (AvailDevUnit(RMsg->rm_Args[1], Unit))
  444.                 Error = "1";
  445.             else
  446.                 Error = "0";
  447.             DidAFunc = TRUE;
  448.         } else
  449.             return WRONG_NUM_OF_ARGS;
  450.     }
  451.  
  452.     if (!DidAFunc)
  453.         return 1L;    /* not a function we recognize */
  454.  
  455.     /* if we get here then we did a function.  try an allocate an
  456.        argstring to return the result string in.  if that fails
  457.        then check if we did a lock.  if we did, and it was succesful
  458.        then we want to free the lock before returning an error saying
  459.        we couldn't allocate the argstring. */
  460.  
  461.     /* try and get rexxsyslib.library */
  462.     if (RexxSysBase = (struct RxsLib *)OpenLibrary("rexxsyslib.library", 0)) {
  463.         {
  464.             ULONG Length;
  465.  
  466.             if (Error)
  467.                 Length = strlen(Error);
  468.             else
  469.                 Length = 0;
  470.  
  471.             *ReturnArgString = CreateArgstring(Error, Length);
  472.         }
  473.  
  474.         CloseLibrary((struct Library *)RexxSysBase);
  475.  
  476.         if (*ReturnArgString)
  477.             return 0L;    /* no low level error so return argstring */
  478.     } else
  479.         NoRexxLib = TRUE;
  480.  
  481.     /* if we did a lock successfully then free it before returning */
  482.     if (WeDidALock && Error == NULL)
  483.         FreeDevUnit(RMsg->rm_Args[1], Unit);
  484.  
  485.     if (NoRexxLib)
  486.         return NO_REXX_LIB;
  487.     else
  488.         return NO_MEM_FOR_ARGSTRING;
  489. }
  490.  
  491. /***************************************************************************/
  492. /***************************************************************************/
  493. /**     These are the support routines used by the top level functions    **/
  494. /***************************************************************************/
  495. /***************************************************************************/
  496.  
  497. /* DoDevUnit() is by in large the most important support routine.  it is
  498.    what actually handles the LockDevUnit() and AttemptDevUnit() requests.
  499.    The difference in the above two calls is the MaxAttempts and
  500.    AttemptDelay that they use.
  501.  
  502.    LockDevUnit() passes -1 in MaxAttempts.  It would take near eternity
  503.    to reach $FFFFFFFF so LockDevUnit() basically blocks forever.  It uses
  504.    an AttemptDelay of 3 seconds to keep from hammering the list constantly.
  505.  
  506.    AttemptDevUnit() passes 5 in MaxAttempts.  Since AttemptDelay is 1
  507.    second for AttemptDevUnit(), this effectively means that this call will
  508.    wait no longer than five seconds for a lock and will return the name
  509.    of the current owner if it can't get it within time.
  510. */
  511.  
  512. UBYTE *DoDevUnit(UBYTE *Device, ULONG Unit, UBYTE *OwnerName,
  513.     UBYTE NotifyBit, ULONG MaxAttempts, ULONG AttemptDelay)
  514. {
  515.     MasterListNode_t *Master;
  516.     WaitListNode_t WaitNode;
  517.  
  518.     struct MsgPort ReplyPort;
  519.     struct timerequest TimeRequest;
  520.  
  521.     UBYTE AttemptNumber;
  522.     UBYTE ReSignalCount;
  523.  
  524.     UBYTE *Error = NULL;
  525.  
  526. #ifdef DEBUG
  527.     kprintf("DoDevUnit called \"%s\" %ld \"%s\" %ld %ld %ld\n",
  528.         Device, Unit, OwnerName, NotifyBit, MaxAttempts, AttemptDelay);
  529. #endif
  530.  
  531.     if (!Device || !*BaseName(Device))
  532.         return ODUERR_BADNAME;
  533.  
  534.     if (NotifyBit == -1)
  535.         return ODUERR_BADBIT;
  536.  
  537.     ObtainSemaphore(&MasterListSema);
  538.  
  539.     /* if the node doesn't exist then we try and create it */
  540.     if (!(Master = FindMasterNode(Device, Unit))) {
  541.         if (Master = CreateMasterNode(Device, Unit))
  542.             SetMasterNode(Master, OwnerName, NotifyBit);
  543.         else
  544.             Error = ODUERR_NOMEM;
  545.  
  546.         ReleaseSemaphore(&MasterListSema);
  547.  
  548.         return Error;
  549.     }
  550.  
  551.     /* if we already own this task then simply return that the
  552.        device is successfully locked.  */
  553.     if (Master->mn_OwnerTask == FindTask(0L)) {
  554. #ifdef DEBUG
  555.         kprintf("DDU: Task already owns this Master\n");
  556. #endif
  557.         ReleaseSemaphore(&MasterListSema);
  558.         return (UBYTE *)NULL;
  559.     }
  560.  
  561.     /* we are going to need the timer to do things right so open
  562.        it up.  If we can't init the timer for use then return
  563.        the appropriate lock failed message */
  564.     if (!OpenTimer(&TimeRequest, &ReplyPort)) {
  565.         ReleaseSemaphore(&MasterListSema);
  566.         return ODUERR_NOTIMER;
  567.     }
  568.  
  569.     /* setup up our wait node and add it onto the wait list */
  570.     WaitNode.wn_Task = FindTask(0L);
  571.     AddTail((struct List *)&Master->mn_WaitList, (struct Node *)&WaitNode);
  572.  
  573.     /* if the current owner has provided a NotifyBit then we
  574.        signal the task to let it know someone wants the list */
  575.     if (Master->mn_OwnerTask && Master->mn_NotifyBit) {
  576. #ifdef DEBUG
  577.         kprintf("DDU: Notifying %08lx on SigBit %ld\n",
  578.             Master->mn_OwnerTask, Master->mn_NotifyBit);
  579. #endif
  580.         Signal(Master->mn_OwnerTask, 1L << Master->mn_NotifyBit);
  581.     }
  582.  
  583.     /* now we wait for the node to become free (mn_OwnerTask = NULL)
  584.        and then check to see if we are the head of the wait list.
  585.        if we are the head then we can own the device.  else someone
  586.        else gets to use it before us so continue waiting.
  587.  
  588.        we must release the MasterListSema and wait before checking
  589.        or the owner will never be able to unlock the node! */
  590.     ReleaseSemaphore(&MasterListSema);
  591.  
  592.     Sleep(&TimeRequest, &ReplyPort, 1L);    /* our initial wait is very short */
  593.  
  594.     for (AttemptNumber = ReSignalCount = 0; AttemptNumber < MaxAttempts;
  595.         ReSignalCount++, AttemptNumber++) {
  596.         ObtainSemaphore(&MasterListSema);
  597.  
  598.         if (Master->mn_OwnerTask) {    /* still owned by someone */
  599.             if (ReSignalCount > RE_SIGNAL) {
  600.                 if (Master->mn_NotifyBit)
  601.                     Signal(Master->mn_OwnerTask,
  602.                         1L << Master->mn_NotifyBit);
  603.                 ReSignalCount = 0;
  604.             }
  605.  
  606.             ReleaseSemaphore(&MasterListSema);
  607.             Sleep(&TimeRequest, &ReplyPort, AttemptDelay);
  608.             continue;
  609.         }
  610.  
  611.         /* device is free.  if we aren't the head of the wait
  612.            list we must let someone else go first */
  613.         if (Master->mn_WaitList.mlh_Head != (struct MinNode *)&WaitNode) {
  614.             ReleaseSemaphore(&MasterListSema);
  615.             Sleep(&TimeRequest, &ReplyPort, AttemptDelay);
  616.             continue;
  617.         }
  618.  
  619.         /* we can own the node!  yeah! */
  620.         RemHead((struct List *)&Master->mn_WaitList);
  621.         SetMasterNode(Master, OwnerName, NotifyBit);
  622.  
  623.         /* if this new owner supplied a NotifyBit and there are
  624.            other people waiting on this node then it is only
  625.            fair to trigger the NotifyBit. */
  626.         if (Master->mn_WaitList.mlh_TailPred != (struct MinNode *)
  627.            &Master->mn_WaitList.mlh_Head)
  628.             if (NotifyBit)
  629.                 Signal(FindTask(0L), 1L << NotifyBit);
  630.  
  631.         /* finally, shut things down and return a successful lock */
  632.         ReleaseSemaphore(&MasterListSema);
  633.         CloseTimer(&TimeRequest, &ReplyPort);
  634.  
  635.         return (UBYTE *)NULL;
  636.     }
  637.  
  638.     /* if we get here then someone else has the device and refuses to
  639.        give it up.  so we return a pointer to the owner's name.
  640.            we also need to remove our WaitNode from the Master's waitlist. */
  641.  
  642.     ObtainSemaphore(&MasterListSema);
  643.  
  644.     Remove((struct Node *)&WaitNode);
  645.  
  646.     Error = Master->mn_OwnerName;
  647.  
  648.     if (!Error)
  649.         Error = ODUERR_UNKNOWN;
  650.  
  651.     ReleaseSemaphore(&MasterListSema);
  652.     CloseTimer(&TimeRequest, &ReplyPort);
  653.  
  654.     return Error;
  655. }
  656.  
  657. /* attempt to find a node in the master list.  returns NULL if not found.
  658.    does not arbitrate for access to the master list.  This must be done
  659.    by the caller! */
  660. MasterListNode_t *FindMasterNode(UBYTE *Device, ULONG Unit)
  661. {
  662.     MasterListNode_t *MasterNode;
  663.  
  664.     Device = BaseName(Device);
  665.  
  666.     for (MasterNode = (MasterListNode_t *)MasterList.mlh_Head;
  667.         MasterNode->mn_Node.mln_Succ; MasterNode = (MasterListNode_t *)
  668.         MasterNode->mn_Node.mln_Succ) {
  669.  
  670. #ifdef DEBUG
  671.         kprintf("FMN: compare \"%s\" %ld with requested \"%s\" %ld\n",
  672.             MasterNode->mn_Device, MasterNode->mn_Unit, Device, Unit);
  673. #endif
  674.         if (strcmp(MasterNode->mn_Device, Device) == 0 &&
  675.             MasterNode->mn_Unit == Unit) {
  676. #ifdef DEBUG
  677.             kprintf("FMN: Match found Master = %08lx\n", MasterNode);
  678. #endif
  679.             return MasterNode;
  680.         }
  681.     }
  682.  
  683.     return (MasterListNode_t *)NULL;
  684. }
  685.  
  686. /* this will try to create a new MasterListNode.  returns NULL if it
  687.    fails (mainly due to an out of memory error).  does not arbitrate
  688.    for access to the master list */
  689. MasterListNode_t *CreateMasterNode(UBYTE *Device, ULONG Unit)
  690. {
  691.     MasterListNode_t *MasterNode;
  692.     ULONG DevNameSize;
  693.     ULONG AllocSize;
  694.  
  695. #ifdef DEBUG
  696.     kprintf("CreateMasterNode called \"%s\" %ld\n", Device, Unit);
  697. #endif
  698.  
  699.     /* failsafe:  if the node already exists then return a
  700.        pointer to it instead of creating a new one. */
  701.  
  702.     if (MasterNode = FindMasterNode(Device, Unit))
  703.         return MasterNode;
  704.  
  705.     /* only store the basename of the device */
  706.     Device = BaseName(Device);
  707.  
  708.     /* alloc some memory to hold the node.  return NULL if the
  709.        alloc fails. we copy the device name onto the end of the
  710.        node so that an original owner can quit and we don't
  711.        lose the device name */
  712.  
  713.     DevNameSize = strlen(Device);
  714.     AllocSize = sizeof(MasterListNode_t) + DevNameSize;
  715.  
  716. #ifdef DEBUG
  717.     kprintf("CMN: DevNameSize = %ld\tAllocSize = %ld\n", DevNameSize, AllocSize);
  718. #endif
  719.  
  720.     if (!(MasterNode = (MasterListNode_t *)AllocMem(AllocSize,
  721.         MEMF_PUBLIC|MEMF_CLEAR)))
  722.         return (MasterListNode_t *)NULL;
  723.  
  724.     /* copy device name over and fill in a few fields */
  725.  
  726.     MasterNode->mn_SizeOf = AllocSize;
  727.     MasterNode->mn_Unit = Unit;
  728.     CopyMem(Device, MasterNode->mn_Device, DevNameSize + 1);
  729.     NewList((struct List *)&MasterNode->mn_WaitList);
  730.  
  731.     /* add the master node to the master list */
  732.     AddTail((struct List *)&MasterList, (struct Node *)MasterNode);
  733.  
  734.     /* return a pointer to the newly added node */
  735.  
  736. #ifdef DEBUG
  737.     kprintf("CMN: Master %08lx created\n", MasterNode);
  738. #endif
  739.  
  740.     return MasterNode;
  741. }
  742.  
  743. void SetMasterNode(MasterListNode_t *Node, UBYTE *OwnerName, UBYTE NotifyBit)
  744. {
  745. #ifdef DEBUG
  746.     kprintf("SetMasterNode called Master %08lx \"%s\" %ld\n", Node, OwnerName, NotifyBit);
  747. #endif
  748.     
  749.     Node->mn_OwnerTask = FindTask(0L);
  750.     Node->mn_OwnerName = OwnerName;
  751.     Node->mn_NotifyBit = NotifyBit;
  752. }
  753.  
  754. /* find the filename part of a full path spec */
  755. UBYTE *BaseName(UBYTE *Path)
  756. {
  757.     register UBYTE *Ptr;
  758.     register UBYTE Char;
  759.  
  760.     Ptr = Path;
  761.  
  762.     while (Char = *Ptr++)
  763.         if (Char == '/' || Char == ':')
  764.             Path = Ptr;
  765.  
  766.     return Path;
  767. }
  768.  
  769. /***************************************************************************/
  770. /***************************************************************************/
  771. /**        These routines manage the timer.device for doing delays        **/
  772. /***************************************************************************/
  773. /***************************************************************************/
  774.  
  775. BOOL OpenTimer(struct timerequest *TimeRequest, struct MsgPort *ReplyPort)
  776. {
  777.     ULONG SigBit;
  778.  
  779.     memset((char *)TimeRequest, 0, sizeof(struct timerequest));
  780.     memset((char *)ReplyPort, 0, sizeof(struct MsgPort));
  781.  
  782.     if ((SigBit = AllocSignal(-1L)) == -1L)
  783.         return FALSE;
  784.  
  785.     ReplyPort->mp_Node.ln_Type = NT_MSGPORT;
  786.  
  787.     ReplyPort->mp_Flags   = PA_SIGNAL;
  788.     ReplyPort->mp_SigBit  = SigBit;
  789.     ReplyPort->mp_SigTask = FindTask(0L);
  790.  
  791.     NewList(&(ReplyPort->mp_MsgList));
  792.  
  793.     if (OpenDevice(TIMERNAME, UNIT_VBLANK, (struct IORequest *)
  794.         TimeRequest, 0)) {
  795.         FreeSignal(SigBit);
  796.         ReplyPort->mp_SigTask         = (struct Task *) -1;
  797.         ReplyPort->mp_MsgList.lh_Head = (struct Node *) -1;
  798.         return FALSE;
  799.     }
  800.  
  801.     TimeRequest->tr_node.io_Message.mn_ReplyPort = ReplyPort;
  802.  
  803.     return TRUE;
  804. }
  805.  
  806. void CloseTimer(struct timerequest *TimeRequest, struct MsgPort *ReplyPort)
  807. {
  808.     CloseDevice((struct IORequest *)TimeRequest);
  809.     FreeSignal(ReplyPort->mp_SigBit);
  810.     ReplyPort->mp_SigTask         = (struct Task *) -1;
  811.     ReplyPort->mp_MsgList.lh_Head = (struct Node *) -1;
  812. }
  813.  
  814. void Sleep(struct timerequest *TimeRequest, struct MsgPort *ReplyPort, ULONG Seconds)
  815. {
  816.     TimeRequest->tr_node.io_Command = TR_ADDREQUEST;
  817.     TimeRequest->tr_time.tv_secs = Seconds;
  818.     TimeRequest->tr_time.tv_micro = 0;
  819.  
  820.     DoIO((struct IORequest *)TimeRequest);
  821. }
  822.