home *** CD-ROM | disk | FTP | other *** search
/ Gold Fish 2 / goldfish_vol2_cd1.bin / files / util / wb / forceicon / source / rendezvous.h < prev    next >
C/C++ Source or Header  |  1994-08-19  |  3KB  |  153 lines

  1.  
  2. /**********************************************************************/
  3. /*                Try to find Semaphore or create it                  */
  4. /**********************************************************************/
  5. static struct FIconSema *FindFIconSema(void)
  6. {
  7.     struct    FIconSema    *FIconSema;
  8.  
  9.  
  10.         // Semaphore already created ???
  11.  
  12.     Forbid();
  13.     if((FIconSema = (struct FIconSema *)FindSemaphore("ForceIcon rendezvous")))
  14.     {
  15.             // Already launched ???
  16.  
  17. #ifndef PREFSRUN
  18.         if(!FIconSema->ServerTask)
  19.             FIconSema->ServerTask    = FindTask(NULL);
  20.         else
  21.         {
  22.             Signal(FIconSema->ServerTask, SIGBREAKF_CTRL_C);
  23.             FIconSema    = NULL;
  24.         }
  25. #else
  26.         if(!FIconSema->PrefsTask)
  27.             FIconSema->PrefsTask    = FindTask(NULL);
  28. #endif
  29.  
  30.         if(FIconSema)
  31.         {
  32.                 // One user more
  33.  
  34.             FIconSema->UseCount++;
  35.         }
  36.  
  37.         Permit();
  38.     }
  39.     else
  40.     {
  41.         if((FIconSema = AllocVec(sizeof(struct FIconSema), MEMF_CLEAR | MEMF_PUBLIC)))
  42.         {
  43.                 // Try to create pool
  44.  
  45.             if((FIconSema->FIconPool = AsmCreatePool(MEMF_CLEAR | MEMF_PUBLIC, 4096, 2048, SysBase)))
  46.             {
  47.                     // Copy name of semaphore
  48.  
  49.                 strcpy(FIconSema->Name, "ForceIcon rendezvous");
  50.  
  51.                     // Set up semaphore
  52.  
  53.                 FIconSema->FIconSema.ss_Link.ln_Name    = FIconSema->Name;
  54.                 FIconSema->UseCount            = 1;
  55.  
  56.  
  57.                     // Add semaphore to list of public semaphores
  58.  
  59.                 AddSemaphore(&FIconSema->FIconSema);
  60.  
  61.                     // Add address of calling task
  62.  
  63. #ifndef PREFSRUN
  64.                 FIconSema->ServerTask            = FindTask(NULL);
  65. #endif
  66.  
  67.                     // Initialize VolumeList
  68.  
  69.                 NewList(&FIconSema->VolumeList);
  70.  
  71.                     // Load in preferences
  72.  
  73.                 ObtainSemaphore(&FIconSema->FIconSema);
  74.                 Permit();
  75.                 LoadPrefs(FIconSema);
  76.                 ReleaseSemaphore(&FIconSema->FIconSema);
  77.                 Forbid();
  78.             }
  79.             else
  80.             {
  81.                 FreeVec(FIconSema);
  82.                 FIconSema = NULL;
  83.             }
  84.         }
  85.  
  86.             // Display error on failure
  87.  
  88.         if(!FIconSema)
  89.             DisplayError(ERR_NOMEM, NULL);
  90.  
  91.         Permit();
  92.     }
  93.  
  94.     return(FIconSema);
  95. }
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105. /**********************************************************************/
  106. /*                    Release/remove FIconSemaphore                   */
  107. /**********************************************************************/
  108. static void RemoveFIconSema(struct FIconSema *FIconSema)
  109. {
  110.     if(FIconSema)
  111.     {
  112.             // Remove Semaphore from memory, or simply "log" out ???
  113.  
  114.         ObtainSemaphore(&FIconSema->FIconSema);
  115.         Forbid();
  116.         ReleaseSemaphore(&FIconSema->FIconSema);
  117.  
  118.  
  119.             // We`re no longer available
  120.  
  121. #ifndef PREFSRUN
  122.         FIconSema->ServerTask    = NULL;
  123. #endif
  124.  
  125.  
  126.             // Semaphore still in use ???
  127.  
  128.         if(!--FIconSema->UseCount)
  129.         {
  130.             if(FIconSema->FIconPool)
  131.             {
  132.                     // Free list of patches
  133.  
  134.                 FreeDevVolList(&FIconSema->VolumeList);
  135.  
  136.                     // Remove Pool
  137.  
  138.                 AsmDeletePool(FIconSema->FIconPool, SysBase);
  139.             }
  140.  
  141.                 // Remove Semaphore
  142.  
  143.             RemSemaphore(&FIconSema->FIconSema);
  144.  
  145.                 // Free memory associated
  146.  
  147.             FreeVec(FIconSema);
  148.         }
  149.  
  150.         Permit();
  151.     }
  152. }
  153.