MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
rpl_gtid_mutex_cond_array.cc
1 /* Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
2 
3  This program is free software; you can redistribute it and/or
4  modify it under the terms of the GNU General Public License as
5  published by the Free Software Foundation; version 2 of the
6  License.
7 
8  This program is distributed in the hope that it will be useful, but
9  WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  General Public License for more details.
12 
13  You should have received a copy of the GNU General Public License
14  along with this program; if not, write to the Free Software
15  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
16  02110-1301 USA */
17 
18 #include "rpl_gtid.h"
19 
20 #include "my_sys.h"
21 #include "sql_class.h"
22 
23 
25  : global_lock(_global_lock)
26 {
27  DBUG_ENTER("Mutex_cond_array::Mutex_cond_array");
28  my_init_dynamic_array(&array, sizeof(Mutex_cond *), 0, 8);
29  DBUG_VOID_RETURN;
30 }
31 
32 
34 {
35  DBUG_ENTER("Mutex_cond_array::~Mutex_cond_array");
36  // destructor should only be called when no other thread may access object
37  //global_lock->assert_no_lock();
38  // need to hold lock before calling get_max_sidno
39  global_lock->rdlock();
40  int max_index= get_max_index();
41  for (int i= 0; i <= max_index; i++)
42  {
43  Mutex_cond *mutex_cond= get_mutex_cond(i);
44  if (mutex_cond)
45  {
46  mysql_mutex_destroy(&mutex_cond->mutex);
47  mysql_cond_destroy(&mutex_cond->cond);
48  free(mutex_cond);
49  }
50  }
51  delete_dynamic(&array);
52  global_lock->unlock();
53  DBUG_VOID_RETURN;
54 }
55 
56 
57 void Mutex_cond_array::enter_cond(THD *thd, int n, PSI_stage_info *stage,
58  PSI_stage_info *old_stage) const
59 {
60  DBUG_ENTER("Mutex_cond_array::enter_cond");
61  Mutex_cond *mutex_cond= get_mutex_cond(n);
62  thd->ENTER_COND(&mutex_cond->cond, &mutex_cond->mutex, stage, old_stage);
63  DBUG_VOID_RETURN;
64 }
65 
66 
67 enum_return_status Mutex_cond_array::ensure_index(int n)
68 {
69  DBUG_ENTER("Mutex_cond_array::ensure_index");
70  global_lock->assert_some_wrlock();
71  int max_index= get_max_index();
72  if (n > max_index)
73  {
74  if (n > max_index)
75  {
76  if (allocate_dynamic(&array, n + 1))
77  goto error;
78  for (int i= max_index + 1; i <= n; i++)
79  {
80  Mutex_cond *mutex_cond= (Mutex_cond *)my_malloc(sizeof(Mutex_cond), MYF(MY_WME));
81  if (mutex_cond == NULL)
82  goto error;
83  mysql_mutex_init(key_gtid_ensure_index_mutex, &mutex_cond->mutex, NULL);
84  mysql_cond_init(key_gtid_ensure_index_cond, &mutex_cond->cond, NULL);
85  insert_dynamic(&array, &mutex_cond);
86  DBUG_ASSERT(&get_mutex_cond(i)->mutex == &mutex_cond->mutex);
87  }
88  }
89  }
90  RETURN_OK;
91 error:
92  BINLOG_ERROR(("Out of memory."), (ER_OUT_OF_RESOURCES, MYF(0)));
93  RETURN_REPORTED_ERROR;
94 }