mutex_lock.h 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /* Copyright (c) 2014, 2017, Oracle and/or its affiliates. All rights reserved.
  2. This program is free software; you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License, version 2.0,
  4. as published by the Free Software Foundation.
  5. This program is also distributed with certain software (including
  6. but not limited to OpenSSL) that is licensed under separate terms,
  7. as designated in a particular file or component or in included license
  8. documentation. The authors of MySQL hereby grant you an additional
  9. permission to link the program and your derivative works with the
  10. separately licensed software that they have included with MySQL.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License, version 2.0, for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
  18. #ifndef MUTEX_LOCK_INCLUDED
  19. #define MUTEX_LOCK_INCLUDED
  20. /**
  21. @file include/mutex_lock.h
  22. */
  23. #include <mysql/psi/mysql_mutex.h>
  24. /**
  25. A simple wrapper around a mutex:
  26. Grabs the mutex in the CTOR, releases it in the DTOR.
  27. The mutex may be NULL, in which case this is a no-op.
  28. */
  29. class Mutex_lock {
  30. public:
  31. explicit Mutex_lock(mysql_mutex_t *mutex, const char *src_file, int src_line)
  32. : m_mutex(mutex), m_src_file(src_file), m_src_line(src_line) {
  33. if (m_mutex) {
  34. mysql_mutex_lock_with_src(m_mutex, m_src_file, m_src_line);
  35. }
  36. }
  37. ~Mutex_lock() {
  38. if (m_mutex) {
  39. mysql_mutex_unlock_with_src(m_mutex, m_src_file, m_src_line);
  40. }
  41. }
  42. private:
  43. mysql_mutex_t *m_mutex;
  44. const char *m_src_file;
  45. int m_src_line;
  46. Mutex_lock(const Mutex_lock &); /* Not copyable. */
  47. void operator=(const Mutex_lock &); /* Not assignable. */
  48. };
  49. #define MUTEX_LOCK(NAME, X) Mutex_lock NAME(X, __FILE__, __LINE__)
  50. #endif // MUTEX_LOCK_INCLUDED