home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 9 / IOPROG_9.ISO / contrib / iis4 / iis4_07.cab / ThreadPool.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-09-05  |  1.9 KB  |  102 lines

  1. #define _WIN32_WINNT 0x0400
  2.  
  3. #include <windows.h>
  4. #include <httpext.h>
  5. #include "threadpool.h"
  6.  
  7.  
  8. // Structure to create simple linked list
  9.  
  10. typedef struct {
  11.     EXTENSION_CONTROL_BLOCK * pECB; // Data for list entry
  12.     DWORD dwNextEntry;    // Pointer to next entry
  13. } ECB_QUEUE_ENTRY;
  14.  
  15.  
  16. // Array that is a simple linked list
  17.  
  18. ECB_QUEUE_ENTRY ECBqueue[WORK_QUEUE_ENTRIES];
  19.  
  20.  
  21. // Index of next ECBqueue entry to use, and last Entry in use.
  22.  
  23. DWORD dwCurrentEntry, dwLastEntry;
  24.  
  25.  
  26. // Flag to indicate that there are no other entries in the ECBqueue
  27.  
  28. BOOL fQueueEmpty;
  29.  
  30.  
  31. BOOL InitThreadPool(void)
  32. {
  33.     DWORD       i;
  34.     DWORD       dwThreadID;
  35.  
  36.     // Create Semaphore in nonsignaled state
  37.  
  38.     if( (hWorkSem = CreateSemaphore( NULL, 0, 0x7fffffff, NULL )) ==NULL)
  39.         return FALSE;
  40.  
  41.     InitializeCriticalSection(&csQueueLock );
  42.             
  43.     fQueueEmpty=TRUE;
  44.  
  45.     // Create Pool Threads
  46.     
  47.     for(i=0; i<POOL_THREADS; i++)
  48.     {    
  49.         if(CreateThread(NULL, 0, WorkerFunction, (LPVOID)i, 0, &dwThreadID) == NULL)
  50.         return FALSE;
  51.     }
  52.  
  53.     // Clear work queue
  54.     
  55.     ZeroMemory(ECBqueue, WORK_QUEUE_ENTRIES*sizeof(ECB_QUEUE_ENTRY) );
  56.  
  57.  
  58.     return TRUE;
  59. }
  60.  
  61.  
  62. BOOL AddWorkQueueEntry(EXTENSION_CONTROL_BLOCK * pECB)
  63. {
  64.     DWORD i;
  65.  
  66.     for(i=0; i<WORK_QUEUE_ENTRIES; i++)
  67.     {
  68.         if (ECBqueue[i].pECB==NULL)
  69.         {
  70.             if(fQueueEmpty)
  71.             {
  72.                 dwCurrentEntry=i;
  73.                 fQueueEmpty = FALSE;
  74.             }
  75.             else
  76.                 ECBqueue[dwLastEntry].dwNextEntry=i;
  77.             ECBqueue[i].pECB=pECB;
  78.             dwLastEntry=i;
  79.             return TRUE;
  80.         }
  81.     }
  82.  
  83.     // If no NULL queue entry found, indicate failure
  84.     return FALSE;
  85. }
  86.  
  87. BOOL GetWorkQueueEntry(EXTENSION_CONTROL_BLOCK ** ppECB)
  88. {
  89.     if(    (*ppECB=ECBqueue[dwCurrentEntry].pECB) == NULL)
  90.         return FALSE;
  91.     else
  92.     {
  93.         ECBqueue[dwCurrentEntry].pECB = NULL;
  94.         if(dwCurrentEntry == dwLastEntry) // If this is only pending item
  95.             fQueueEmpty = TRUE;
  96.         else
  97.             dwCurrentEntry = ECBqueue[dwCurrentEntry].dwNextEntry;
  98.     }
  99.     
  100.     return TRUE;
  101. }
  102.