jmutex.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*
  2. This file is a part of the JThread package, which contains some object-
  3. oriented thread wrappers for different thread implementations.
  4. Copyright (c) 2000-2011 Jori Liesenborgs (jori.liesenborgs@gmail.com)
  5. Permission is hereby granted, free of charge, to any person obtaining a
  6. copy of this software and associated documentation files (the "Software"),
  7. to deal in the Software without restriction, including without limitation
  8. the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. and/or sell copies of the Software, and to permit persons to whom the
  10. Software is furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  16. THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  18. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  19. DEALINGS IN THE SOFTWARE.
  20. */
  21. #ifndef JTHREAD_JMUTEX_H
  22. #define JTHREAD_JMUTEX_H
  23. #include "jthreadconfig.h"
  24. #ifdef JTHREAD_CONFIG_WIN32THREADS
  25. #ifndef _WIN32_WCE
  26. #include <process.h>
  27. #endif // _WIN32_WCE
  28. #include <winsock2.h>
  29. #include <windows.h>
  30. #else // using pthread
  31. #include <pthread.h>
  32. #endif // JTHREAD_CONFIG_WIN32THREADS
  33. #define ERR_JMUTEX_ALREADYINIT -1
  34. #define ERR_JMUTEX_NOTINIT -2
  35. #define ERR_JMUTEX_CANTCREATEMUTEX -3
  36. namespace jthread
  37. {
  38. class JTHREAD_IMPORTEXPORT JMutex
  39. {
  40. public:
  41. JMutex();
  42. ~JMutex();
  43. int Init();
  44. int Lock();
  45. int Unlock();
  46. bool IsInitialized() { return initialized; }
  47. private:
  48. #ifdef JTHREAD_CONFIG_WIN32THREADS
  49. #ifdef JTHREAD_CONFIG_JMUTEXCRITICALSECTION
  50. CRITICAL_SECTION mutex;
  51. #else // Use standard mutex
  52. HANDLE mutex;
  53. #endif // JTHREAD_CONFIG_JMUTEXCRITICALSECTION
  54. #else // pthread mutex
  55. pthread_mutex_t mutex;
  56. #endif // JTHREAD_CONFIG_WIN32THREADS
  57. bool initialized;
  58. };
  59. } // end namespace
  60. #endif // JTHREAD_JMUTEX_H