home *** CD-ROM | disk | FTP | other *** search
/ Internet Publisher's Toolbox 1.0 / Image.iso / toolbox / ntserver / wtsource / win32loc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-02-17  |  7.0 KB  |  194 lines

  1. /*
  2.  *  win32lock.c
  3.  *
  4.  *  This contains locking code for WAIS, called from irfiles.c.
  5.  *
  6.  *  (C) Copyright 1994 The University Court of the University of Edinburgh
  7.  *
  8.  *  Author:  Chris Adie <C.J.Adie@ed.ac.uk> 
  9.  *
  10.  */
  11.  
  12. /******************************************************************************/
  13. /* INCLUDE FILES                                                              */
  14. /******************************************************************************/
  15.  
  16. #include <windows.h>
  17. #include "cutil.h"
  18.  
  19. /******************************************************************************/
  20. /* CONSTANT DEFINITIONS                                                       */
  21. /******************************************************************************/
  22.  
  23. /* This section deliberately left blank */
  24.  
  25. /******************************************************************************/
  26. /* MACRO FUNCTION DEFINITIONS                                                 */
  27. /******************************************************************************/
  28.  
  29. #define MAXQUERIES  5       /* Max no of simultaneour queries on a database */
  30.  
  31. /******************************************************************************/
  32. /* TYPE DEFINITIONS                                                           */
  33. /******************************************************************************/
  34.  
  35. /* This section deliberately left blank */
  36.  
  37. /******************************************************************************/
  38. /* GLOBAL VARIABLES AND FUNCTIONS IMPORTED                                    */
  39. /******************************************************************************/
  40.  
  41. /* This section deliberately left blank */
  42. /* Consider whether these should appear in a header file */
  43.  
  44. /******************************************************************************/
  45. /* GLOBAL VARIABLES EXPORTED                                                  */
  46. /******************************************************************************/
  47.  
  48. /* This section deliberately left blank */
  49.  
  50. /******************************************************************************/
  51. /* VARIABLES PRIVATE TO THIS FILE                                             */
  52. /******************************************************************************/
  53.  
  54. static int LockCount = 0;   /* How many times the Query sem has been acquired */
  55. static HANDLE hIndex = INVALID_HANDLE_VALUE;   /* Handle of Index mutex */ 
  56. static HANDLE hQuery = INVALID_HANDLE_VALUE;   /* Handle of Query sem */ 
  57.  
  58. /******************************************************************************/
  59. /* FUNCTIONS PRIVATE TO THIS FILE                                             */
  60. /******************************************************************************/
  61.  
  62. /* This section deliberately left blank */
  63.  
  64. /******************************************************************************/
  65. /* GLOBAL FUNCTIONS EXPORTED                                                  */
  66. /******************************************************************************/
  67.  
  68. void ResetInterlock(void) {
  69.     if (hIndex!=INVALID_HANDLE_VALUE) {
  70.         /* We own the Index mutex */
  71.         CloseHandle(hIndex);
  72.         hIndex = INVALID_HANDLE_VALUE;
  73.     }
  74.     if (hQuery!=INVALID_HANDLE_VALUE) {
  75.         if (LockCount>0) {
  76.             ReleaseSemaphore(hQuery,LockCount,NULL);
  77.             LockCount = 0;
  78.         }
  79.         CloseHandle(hQuery);
  80.         hQuery = INVALID_HANDLE_VALUE;
  81.     }
  82. }
  83.  
  84. boolean SetInterlock(char *name,boolean initialize,boolean for_search) {
  85. char *p;
  86. char IndexName[MAX_PATH];
  87. char QueryName[MAX_PATH];
  88. int i;
  89. boolean bResult;
  90. DWORD dwResult;
  91.  
  92.     /* Get the last component of the database name to construct the semaphore names */
  93.     i = strlen(name);
  94.     while (i>0) {
  95.         p = name + i - 1;
  96.         if (*p=='\\' || *p=='/') break;
  97.         i--;
  98.     }
  99.     p = name + i;
  100.     
  101.     /* Put the name into upper case and copy to both semaphore names */
  102.     strncpy(IndexName,p,MAX_PATH-1);
  103.     IndexName[MAX_PATH-1] = '\0';
  104.     p = IndexName;
  105.     while (*p) {
  106.         toupper(*p);
  107.         p++;
  108.     }
  109.     strncpy(QueryName,IndexName,MAX_PATH-1);
  110.     QueryName[MAX_PATH-1] = '\0';
  111.  
  112.     /* Distinguish the strings */
  113.     strncat(IndexName,"-WAIS-Index-mutex",MAX_PATH-1);
  114.     IndexName[MAX_PATH-1] = '\0';
  115.     strncat(QueryName,"-WAIS-Query-semaphore",MAX_PATH-1);
  116.     IndexName[MAX_PATH-1] = '\0';
  117.   
  118.     /* Open or create the Query semaphore */
  119.     hQuery = CreateSemaphore(NULL,MAXQUERIES,MAXQUERIES,QueryName);
  120.     if (hQuery==0 || hQuery==INVALID_HANDLE_VALUE) {
  121.         hQuery = INVALID_HANDLE_VALUE;
  122.         waislog(WLOG_HIGH, WLOG_ERROR,"can't create Query semaphore");
  123.         return(false);
  124.     }
  125.  
  126.     bResult = true;
  127.     
  128.     if (for_search == true) {
  129.   
  130.         dwResult = WaitForSingleObject(hQuery,0);
  131.         if(dwResult==WAIT_OBJECT_0) {
  132.             /* Got the query semaphore */
  133.             LockCount = 1;
  134.         } else {
  135.             /* Either an update is running, or there are too many queries */ 
  136.             waislog(WLOG_HIGH, WLOG_ERROR,
  137.                 "can't search the database as an update is currently running");
  138.             bResult = false;
  139.         }
  140.  
  141.     } else {
  142.  
  143.         /* Can we get ownership of the Index mutex? */
  144.         hIndex = CreateMutex(NULL,FALSE,IndexName);
  145.         dwResult = WaitForSingleObject(hIndex,0);
  146.         if (dwResult==WAIT_ABANDONED) {
  147.             /* Clear the abandoned flag and try again */
  148.             ReleaseMutex(hIndex);
  149.             dwResult = WaitForSingleObject(hIndex,0);
  150.         }
  151.         if(dwResult==WAIT_TIMEOUT || dwResult==WAIT_FAILED) {
  152.             waislog(WLOG_HIGH, WLOG_ERROR,
  153.                 "an indexing is currently running on the database. Try again later.");
  154.             CloseHandle(hIndex);
  155.             hIndex = INVALID_HANDLE_VALUE;
  156.             bResult = false;
  157.         }
  158.     
  159.         if ( bResult==true && initialize==true ) {
  160.             /* Wait for all queries to finish */
  161.             for (i=0;i<MAXQUERIES;i++) {
  162.                 /* Get query semaphore */
  163.                 dwResult = WaitForSingleObject(hQuery,30000); /* Thirty secs */
  164.                 if (dwResult==WAIT_OBJECT_0) {
  165.                     LockCount++;
  166.                 } else {
  167.                     /* timed out */
  168.                     break;
  169.                 }
  170.             }
  171.             if ( i<MAXQUERIES ) {
  172.                 waislog(WLOG_HIGH, WLOG_ERROR,
  173.                     "timed out in waiting for queries to finish. Try again later.");
  174.                 if (i>0) ReleaseSemaphore(hQuery,i,NULL);
  175.                 bResult = false;
  176.             }
  177.         }
  178.  
  179.     }
  180.  
  181.     if (bResult==false) {
  182.         if (hQuery!=INVALID_HANDLE_VALUE) {
  183.             CloseHandle(hQuery);
  184.             hQuery = INVALID_HANDLE_VALUE;
  185.         }
  186.         if (hIndex!=INVALID_HANDLE_VALUE) {
  187.             ReleaseMutex(hIndex);
  188.             CloseHandle(hIndex);
  189.             hIndex = INVALID_HANDLE_VALUE;
  190.         }
  191.     }
  192.     return(bResult);
  193. }
  194.