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