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.

101 lines
2.2 KiB

  1. // Copyright (c) 1997-1999 Microsoft Corporation
  2. //
  3. // DLL object instance and server lock utility class
  4. //
  5. // 8-19-97 sburns
  6. // concept stolen from DavidMun, who stole it from RaviR, who ...
  7. #ifndef __DLLREF_HPP_INCLUDED
  8. #define __DLLREF_HPP_INCLUDED
  9. // Maintains a instance counter and lock counter, with methods to manipulate
  10. // both in a thread-safe fashion. This is useful in implementing
  11. // DllCanUnloadNow.
  12. //
  13. // Each class that is instanciated and passed outside of the control of the
  14. // DLL should contain an instance of ComServerReference. This will automatically
  15. // increment and decrement the ComServerLockState instance counter in step with the
  16. // instance's lifetime. Typically, this includes all class factory instances,
  17. // and instances of classes that are created by those class factories.
  18. //
  19. // Calls to a class factory's LockServer method should be deferred to
  20. // this class.
  21. //
  22. // Calls to DllCanUnloadNow should defer to this class' CanUnloadNow method.
  23. class ComServerLockState
  24. {
  25. public:
  26. static
  27. void
  28. IncrementInstanceCounter()
  29. {
  30. ::InterlockedIncrement(&instanceCount);
  31. }
  32. static
  33. void
  34. DecrementInstanceCounter()
  35. {
  36. ::InterlockedDecrement(&instanceCount);
  37. }
  38. static
  39. void
  40. LockServer(bool lock)
  41. {
  42. lock
  43. ? ::InterlockedIncrement(&lockCount)
  44. : ::InterlockedDecrement(&lockCount);
  45. }
  46. static
  47. bool
  48. CanUnloadNow()
  49. {
  50. return (instanceCount == 0 && lockCount == 0) ? true : false;
  51. }
  52. private:
  53. static long instanceCount;
  54. static long lockCount;
  55. // not implemented
  56. ComServerLockState();
  57. ComServerLockState(const ComServerLockState&);
  58. const ComServerLockState& operator=(const ComServerLockState&);
  59. };
  60. class ComServerReference
  61. {
  62. public:
  63. ComServerReference()
  64. {
  65. ComServerLockState::IncrementInstanceCounter();
  66. }
  67. ~ComServerReference()
  68. {
  69. ComServerLockState::DecrementInstanceCounter();
  70. }
  71. private:
  72. // not implemented; no instance copying allowed.
  73. ComServerReference(const ComServerReference&);
  74. const ComServerReference& operator=(const ComServerReference&);
  75. };
  76. #endif // __DLLREF_HPP_INCLUDED