Team Fortress 2 Source Code as on 22/4/2020
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.

169 lines
3.3 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. //=============================================================================//
  8. /*
  9. *
  10. * Copyright (c) 1998-9
  11. * Dr John Maddock
  12. *
  13. * Permission to use, copy, modify, distribute and sell this software
  14. * and its documentation for any purpose is hereby granted without fee,
  15. * provided that the above copyright notice appear in all copies and
  16. * that both that copyright notice and this permission notice appear
  17. * in supporting documentation. Dr John Maddock makes no representations
  18. * about the suitability of this software for any purpose.
  19. * It is provided "as is" without express or implied warranty.
  20. *
  21. */
  22. /*
  23. * FILE re_thrd.h
  24. * VERSION 2.12
  25. * Thread synch helper functions, for regular
  26. * expression library.
  27. */
  28. #ifndef RE_THRD_H
  29. #define RE_THRD_H
  30. #ifndef JM_CFG_H
  31. #include <jm/jm_cfg.h>
  32. #endif
  33. #if defined(JM_PLATFORM_W32) && defined(JM_THREADS)
  34. //#include <windows.h>
  35. #endif
  36. #if !defined(JM_PLATFORM_W32) && defined(JM_THREADS)
  37. #include <pthread.h>
  38. #endif
  39. JM_NAMESPACE(__JM)
  40. void RE_CALL re_init_threads();
  41. void RE_CALL re_free_threads();
  42. #ifdef JM_THREADS
  43. #ifndef JM_PLATFORM_W32
  44. typedef pthread_mutex_t CRITICAL_SECTION;
  45. inline void RE_CALL InitializeCriticalSection(CRITICAL_SECTION* ps)
  46. {
  47. pthread_mutex_init(ps, NULL);
  48. }
  49. inline void RE_CALL DeleteCriticalSection(CRITICAL_SECTION* ps)
  50. {
  51. pthread_mutex_destroy(ps);
  52. }
  53. inline void RE_CALL EnterCriticalSection(CRITICAL_SECTION* ps)
  54. {
  55. pthread_mutex_lock(ps);
  56. }
  57. inline void RE_CALL LeaveCriticalSection(CRITICAL_SECTION* ps)
  58. {
  59. pthread_mutex_unlock(ps);
  60. }
  61. #endif
  62. template <class Lock>
  63. class lock_guard
  64. {
  65. typedef Lock lock_type;
  66. public:
  67. lock_guard(lock_type& m, bool aq = true)
  68. : mut(m), owned(false){ acquire(aq); }
  69. ~lock_guard()
  70. { acquire(false); }
  71. void RE_CALL acquire(bool aq = true, DWORD timeout = INFINITE)
  72. {
  73. if(aq && !owned)
  74. {
  75. mut.acquire(true, timeout);
  76. owned = true;
  77. }
  78. else if(!aq && owned)
  79. {
  80. mut.acquire(false);
  81. owned = false;
  82. }
  83. }
  84. private:
  85. lock_type& mut;
  86. bool owned;
  87. };
  88. class critical_section
  89. {
  90. public:
  91. critical_section()
  92. { InitializeCriticalSection(&hmutex);}
  93. critical_section(const critical_section&)
  94. { InitializeCriticalSection(&hmutex);}
  95. const critical_section& RE_CALL operator=(const critical_section&)
  96. {return *this;}
  97. ~critical_section()
  98. {DeleteCriticalSection(&hmutex);}
  99. private:
  100. void RE_CALL acquire(bool aq, DWORD unused = INFINITE)
  101. { if(aq) EnterCriticalSection(&hmutex);
  102. else LeaveCriticalSection(&hmutex);
  103. }
  104. CRITICAL_SECTION hmutex;
  105. public:
  106. typedef lock_guard<critical_section> ro_guard;
  107. typedef lock_guard<critical_section> rw_guard;
  108. friend lock_guard<critical_section>;
  109. };
  110. inline bool RE_CALL operator==(const critical_section&, const critical_section&)
  111. {
  112. return false;
  113. }
  114. inline bool RE_CALL operator<(const critical_section&, const critical_section&)
  115. {
  116. return true;
  117. }
  118. typedef lock_guard<critical_section> cs_guard;
  119. JM_IX_DECL extern critical_section* p_re_lock;
  120. JM_IX_DECL extern unsigned int re_lock_count;
  121. #define JM_GUARD(inst) __JM::critical_section::rw_guard g(inst);
  122. #else // JM_THREADS
  123. #define JM_GUARD(inst)
  124. #endif // JM_THREADS
  125. JM_END_NAMESPACE
  126. #endif // sentry