home *** CD-ROM | disk | FTP | other *** search
/ CD Actual Thematic 25: Programming / pc_actual_25.iso / C_C++ / BorlandCompiler / freecommandLinetools.exe / Include / mspthrd.h < prev    next >
Encoding:
C/C++ Source or Header  |  2000-06-27  |  2.5 KB  |  105 lines

  1. /*++
  2.  
  3. Copyright (c) 1998-1999 Microsoft Corporation
  4.  
  5. Module Name:
  6.  
  7.     mspthrd.h
  8.  
  9. Abstract:
  10.  
  11.     Definitions for MSP thread management classes.
  12.  
  13. --*/
  14.  
  15. #ifndef __MSPTHRD_H
  16. #pragma option push -b -a8 -pc -A- /*P_O_Push*/
  17. #define __MSPTHRD_H
  18.  
  19. //
  20. // Commands that the worker worker thread can handle.
  21. //
  22.  
  23. typedef enum
  24. {
  25.     WORK_ITEM,          // process a work item
  26.     STOP,               // kill the worker thread.
  27.  
  28. } COMMAND;
  29.  
  30. typedef struct
  31. {
  32.     COMMAND                cmd;
  33.     LPTHREAD_START_ROUTINE pfn;
  34.     PVOID                  pContext;
  35.     HANDLE                 hEvent;
  36.  
  37. } COMMAND_NODE;
  38.  
  39. typedef struct
  40. {
  41.     LIST_ENTRY  link;
  42.     COMMAND_NODE node;
  43.  
  44. } COMMAND_QUEUE_ITEM;
  45.  
  46. class CMSPThread
  47. {
  48. public:
  49.     CMSPThread()
  50.     {
  51.         InitializeListHead(&m_CommandQueue);
  52.  
  53.         m_hCommandEvent = NULL;
  54.         m_hThread       = NULL;
  55.  
  56.         m_iStartCount = 0;
  57.     }
  58.  
  59.     ~CMSPThread() { };
  60.  
  61.     HRESULT Start();
  62.     HRESULT Stop();
  63.  
  64.     // Shutdown is used to clean up the thread unconditionally. This can be
  65.     // used as an alternative to matched Start() / Stop() calls.
  66.  
  67.     HRESULT Shutdown();
  68.  
  69.     HRESULT ThreadProc();
  70.  
  71.     HRESULT QueueWorkItem(
  72.         LPTHREAD_START_ROUTINE Function,
  73.         PVOID Context,
  74.         BOOL  fSynchronous
  75.         );
  76.  
  77. private:
  78.     BOOL SignalThreadProc() { return SetEvent(m_hCommandEvent); }
  79.  
  80. private:
  81.  
  82.     CMSPCritSection         m_CountLock;     // Protects start count
  83.     CMSPCritSection         m_QueueLock;     // Protects command queue
  84.     int                     m_iStartCount;   // number of times we've been
  85.                                              // started minus number of times
  86.                                              // we've been stopped. If == 0
  87.                                              // then we actually stop thread.
  88.     LIST_ENTRY              m_CommandQueue;  // Queue of commands for thread
  89.                                              // to process.
  90.     HANDLE                  m_hCommandEvent; // Signaled to tell us to do
  91.                                              // something.
  92.  
  93.     HANDLE                  m_hThread;       // The thread handle. We need to
  94.                                              // save it so that we can wait
  95.                                              // for it when stopping the
  96.                                              // thread.
  97. };
  98.  
  99. extern CMSPThread g_Thread;
  100.  
  101. #pragma option pop /*P_O_Pop*/
  102. #endif // __MSPTHRD_H
  103.  
  104. // eof
  105.