sp_exlock.cpp 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #include "sp_exlock.h"
  2. #include<iostream>
  3. #include<memory>
  4. #include<map>
  5. #include <winpr/windows.h>
  6. #include <winpr/synch.h>
  7. using namespace std;
  8. map<int, CRITICAL_SECTION*> g_modLockArr;
  9. void sp_mod_mgr_lockEx(int EntityId)
  10. {
  11. auto lock = g_modLockArr.find(EntityId);
  12. if (lock != g_modLockArr.end())
  13. {
  14. //can find it
  15. EnterCriticalSection(lock->second);
  16. }
  17. else
  18. {
  19. CRITICAL_SECTION* cs = new CRITICAL_SECTION();
  20. InitializeCriticalSection(cs);
  21. EnterCriticalSection(cs);
  22. g_modLockArr.insert(std::make_pair(EntityId, cs));
  23. }
  24. }
  25. void sp_mod_mgr_unlockEx(int EntityId)
  26. {
  27. auto lock = g_modLockArr.find(EntityId);
  28. if (lock != g_modLockArr.end())
  29. LeaveCriticalSection(lock->second);
  30. }
  31. void sp_mod_mgr_lockArrClean()
  32. {
  33. #if defined(_MSC_VER)
  34. for each (auto it in g_modLockArr) {
  35. DeleteCriticalSection(it.second);
  36. delete it.second;
  37. it.second = NULL;
  38. }
  39. #else
  40. for (auto it : g_modLockArr) {
  41. DeleteCriticalSection(it.second);
  42. delete it.second;
  43. it.second = NULL;
  44. }
  45. #endif //_MSC_VER
  46. g_modLockArr.clear();
  47. }