Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

75 lines
2.3 KiB

  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) 1997, Microsoft Corp. All rights reserved.
  4. //
  5. // FILE
  6. //
  7. // Dispatcher.h
  8. //
  9. // SYNOPSIS
  10. //
  11. // This file describes the class Dispatcher.
  12. //
  13. // MODIFICATION HISTORY
  14. //
  15. // 7/31/1997 Original version.
  16. // 4/16/1998 Added 'empty' event.
  17. // 8/07/1998 Added 'last out' thread handle.
  18. //
  19. ///////////////////////////////////////////////////////////////////////////////
  20. #ifndef _DISPATCHER_H_
  21. #define _DISPATCHER_H_
  22. #include <guard.h>
  23. #include <nocopy.h>
  24. //////////
  25. // Default number of milliseconds that a thread will wait for work.
  26. //////////
  27. const DWORD DEFAULT_MAX_IDLE = 5 * 60 * 1000;
  28. ///////////////////////////////////////////////////////////////////////////////
  29. //
  30. // CLASS
  31. //
  32. // Dispatcher
  33. //
  34. // DESCRIPTION
  35. //
  36. // This class maintains an upper-bounded recycle bin of threads. Instead
  37. // of calling the Win32 CreateThread() function, clients instead call
  38. // RequestThread(). If a thread is available it will be dispatched
  39. // immediately, otherwise the request is placed in a FIFO queue until a
  40. // thread becomes available.
  41. //
  42. ///////////////////////////////////////////////////////////////////////////////
  43. class Dispatcher
  44. : Guardable, NonCopyable
  45. {
  46. public:
  47. BOOL initialize(DWORD dwMaxThreads = 0,
  48. DWORD dwMaxIdle = DEFAULT_MAX_IDLE) throw ();
  49. void finalize() throw ();
  50. BOOL requestThread(PIAS_CALLBACK OnStart) throw ();
  51. DWORD setMaxNumberOfThreads(DWORD dwMaxThreads) throw ();
  52. DWORD setMaxThreadIdle(DWORD dwMilliseconds) throw ();
  53. protected:
  54. // Main loop to dispatch thread requests.
  55. void fillRequests() throw ();
  56. DWORD numThreads; // Current number of threads.
  57. DWORD maxThreads; // Maximum size of the thread pool.
  58. LONG available; // Number of threads available for work (may be < 0).
  59. DWORD maxIdle; // Max. time (in msec) that a thread will idle.
  60. HANDLE hPort; // I/O Completion Port used as the queue.
  61. HANDLE hEmpty; // Event indicating that the pool is empty.
  62. HANDLE hLastOut; // The last thread to exit from the pool.
  63. // Start routine for all threads.
  64. static unsigned __stdcall startRoutine(void* pArg) throw ();
  65. };
  66. #endif _DISPATCHER_H_