1 /* Copyright (c) 2011, 2012, 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
22    02110-1301 USA */
23 
24 #include "rpl_gtid.h"
25 
26 #include "my_sys.h"
27 #include "sql_class.h"
28 
29 
Mutex_cond_array(Checkable_rwlock * _global_lock)30 Mutex_cond_array::Mutex_cond_array(Checkable_rwlock *_global_lock)
31   : global_lock(_global_lock)
32 {
33   DBUG_ENTER("Mutex_cond_array::Mutex_cond_array");
34   my_init_dynamic_array(&array, sizeof(Mutex_cond *), 0, 8);
35   DBUG_VOID_RETURN;
36 }
37 
38 
~Mutex_cond_array()39 Mutex_cond_array::~Mutex_cond_array()
40 {
41   DBUG_ENTER("Mutex_cond_array::~Mutex_cond_array");
42   // destructor should only be called when no other thread may access object
43   //global_lock->assert_no_lock();
44   // need to hold lock before calling get_max_sidno
45   global_lock->rdlock();
46   int max_index= get_max_index();
47   for (int i= 0; i <= max_index; i++)
48   {
49     Mutex_cond *mutex_cond= get_mutex_cond(i);
50     if (mutex_cond)
51     {
52       mysql_mutex_destroy(&mutex_cond->mutex);
53       mysql_cond_destroy(&mutex_cond->cond);
54       free(mutex_cond);
55     }
56   }
57   delete_dynamic(&array);
58   global_lock->unlock();
59   DBUG_VOID_RETURN;
60 }
61 
62 
enter_cond(THD * thd,int n,PSI_stage_info * stage,PSI_stage_info * old_stage) const63 void Mutex_cond_array::enter_cond(THD *thd, int n, PSI_stage_info *stage,
64                                   PSI_stage_info *old_stage) const
65 {
66   DBUG_ENTER("Mutex_cond_array::enter_cond");
67   Mutex_cond *mutex_cond= get_mutex_cond(n);
68   thd->ENTER_COND(&mutex_cond->cond, &mutex_cond->mutex, stage, old_stage);
69   DBUG_VOID_RETURN;
70 }
71 
72 
ensure_index(int n)73 enum_return_status Mutex_cond_array::ensure_index(int n)
74 {
75   DBUG_ENTER("Mutex_cond_array::ensure_index");
76   global_lock->assert_some_wrlock();
77   int max_index= get_max_index();
78   if (n > max_index)
79   {
80     if (n > max_index)
81     {
82       if (allocate_dynamic(&array, n + 1))
83         goto error;
84       for (int i= max_index + 1; i <= n; i++)
85       {
86         Mutex_cond *mutex_cond= (Mutex_cond *)my_malloc(sizeof(Mutex_cond), MYF(MY_WME));
87         if (mutex_cond == NULL)
88           goto error;
89         mysql_mutex_init(key_gtid_ensure_index_mutex, &mutex_cond->mutex, NULL);
90         mysql_cond_init(key_gtid_ensure_index_cond, &mutex_cond->cond, NULL);
91         insert_dynamic(&array, &mutex_cond);
92         DBUG_ASSERT(&get_mutex_cond(i)->mutex == &mutex_cond->mutex);
93       }
94     }
95   }
96   RETURN_OK;
97 error:
98   BINLOG_ERROR(("Out of memory."), (ER_OUT_OF_RESOURCES, MYF(0)));
99   RETURN_REPORTED_ERROR;
100 }
101