Mutex.cpp 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /*
  2. * Mutex.cpp
  3. * Copyright © 2012 kbinani
  4. *
  5. * This file is part of vConnect-STAND.
  6. *
  7. * vConnect-STAND is free software; you can redistribute it and/or
  8. * modify it under the terms of the GPL License.
  9. *
  10. * vConnect-STAND 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.
  13. */
  14. #include "Mutex.h"
  15. namespace vconnect
  16. {
  17. #ifdef _WIN32
  18. Mutex::Mutex()
  19. {
  20. this->mutex = CreateMutex( NULL, FALSE, NULL );
  21. }
  22. Mutex::~Mutex()
  23. {
  24. if( this->mutex ){
  25. CloseHandle( this->mutex );
  26. }
  27. this->mutex = NULL;
  28. }
  29. void Mutex::lock()
  30. {
  31. WaitForSingleObject( this->mutex, INFINITE );
  32. }
  33. void Mutex::unlock()
  34. {
  35. ReleaseMutex( this->mutex );
  36. }
  37. #else
  38. Mutex::Mutex()
  39. {
  40. this->mutex = (pthread_mutex_t *)malloc( sizeof( pthread_mutex_t ) );
  41. pthread_mutex_init( this->mutex, NULL );
  42. }
  43. Mutex::~Mutex()
  44. {
  45. if( this->mutex ){
  46. pthread_mutex_destroy( this->mutex );
  47. free( this->mutex );
  48. }
  49. this->mutex = NULL;
  50. }
  51. void Mutex::lock()
  52. {
  53. pthread_mutex_lock( this->mutex );
  54. }
  55. void Mutex::unlock()
  56. {
  57. pthread_mutex_unlock( mutex );
  58. }
  59. #endif
  60. }