critsec.h 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /*
  2. ** Command & Conquer Generals(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #ifndef CRITSEC_HEADER
  19. #define CRITSEC_HEADER
  20. #include "wstypes.h"
  21. #ifdef _WIN32
  22. #include <windows.h>
  23. #include <winbase.h>
  24. #elif defined(_UNIX)
  25. #include <pthread.h>
  26. #include <errno.h>
  27. #endif
  28. // Windows headers have a tendency to redefine IN
  29. #ifdef IN
  30. #undef IN
  31. #endif
  32. #define IN const
  33. //
  34. // Critical Section built either on a POSIX Mutex, or a Win32 Critical Section
  35. //
  36. // POSIX version is done by keeping a thread_id and a reference count. Win32 version
  37. // just calls the built in functions.
  38. //
  39. class CritSec
  40. {
  41. public:
  42. CritSec();
  43. ~CritSec();
  44. sint32 lock(int *refcount=NULL) RO;
  45. sint32 unlock(void) RO;
  46. protected:
  47. #ifdef _WIN32
  48. mutable CRITICAL_SECTION CritSec_;
  49. #else
  50. mutable pthread_mutex_t Mutex_; // Mutex lock
  51. mutable pthread_t ThreadId_; // Owner of mutex
  52. mutable int RefCount_; // Reference count
  53. #endif
  54. };
  55. #endif