SimpleMutex.cpp 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. /*
  2. * Copyright (C) by Michael Schuster <michael.schuster@nextcloud.com>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  11. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. * for more details.
  13. */
  14. #include "NCTools.h"
  15. #include "SimpleMutex.h"
  16. SimpleMutex::SimpleMutex()
  17. {
  18. }
  19. bool SimpleMutex::create(const std::wstring &name)
  20. {
  21. release();
  22. // Mutex
  23. _hMutex = CreateMutex(nullptr, TRUE, name.data());
  24. if (GetLastError() == ERROR_ALREADY_EXISTS) {
  25. CloseHandle(_hMutex);
  26. _hMutex = nullptr;
  27. return false;
  28. }
  29. return true;
  30. }
  31. void SimpleMutex::release()
  32. {
  33. // Release mutex
  34. if (_hMutex) {
  35. ReleaseMutex(_hMutex);
  36. CloseHandle(_hMutex);
  37. _hMutex = nullptr;
  38. }
  39. }