drizzle
C++ MySQL-protocol-compatible database server
Loading...
Searching...
No Matches
global.cc
Go to the documentation of this file.
1/*
2 Copyright (C) 2011 Brian Aker
3 Copyright (C) 2000-2006 MySQL AB
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; version 2 of the License.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
17
18
79#include <config.h>
80
81#include <fcntl.h>
82
83#include <drizzled/error.h>
84#include <drizzled/thr_lock.h>
85#include <drizzled/session.h>
86#include <drizzled/session/times.h>
87#include <drizzled/sql_base.h>
88#include <drizzled/lock.h>
89#include <drizzled/pthread_globals.h>
90#include <drizzled/internal/my_sys.h>
91#include <drizzled/pthread_globals.h>
92#include <drizzled/plugin/storage_engine.h>
93#include <drizzled/util/test.h>
94#include <drizzled/open_tables_state.h>
95#include <drizzled/table/cache.h>
96#include <drizzled/catalog/instance.h>
97
98#include <set>
99#include <vector>
100#include <algorithm>
101#include <functional>
102
103#include <boost/thread/shared_mutex.hpp>
104#include <boost/thread/condition_variable.hpp>
105
106using namespace std;
107
108namespace drizzled
109{
110
111static boost::mutex LOCK_global_read_lock;
112static boost::condition_variable_any COND_global_read_lock;
113
118
119static void print_lock_error(int error, const char *);
120
121/*
122 Lock tables.
123
124 SYNOPSIS
125 lockTables()
126 tables An array of pointers to the tables to lock.
127 count The number of tables to lock.
128 flags Options:
129 DRIZZLE_LOCK_IGNORE_GLOBAL_READ_LOCK Ignore a global read lock
130 DRIZZLE_LOCK_IGNORE_FLUSH Ignore a flush tables.
131 DRIZZLE_LOCK_NOTIFY_IF_NEED_REOPEN Instead of reopening altered
132 or dropped tables by itself,
133 lockTables() should
134 notify upper level and rely
135 on caller doing this.
136
137 RETURN
138 A lock structure pointer on success.
139 NULL on error or if some tables should be reopen.
140*/
141
142/* Map the return value of thr_lock to an error from errmsg.txt */
143static drizzled::error_t thr_lock_errno_to_mysql[]=
144{ EE_OK, EE_ERROR_FIRST, ER_LOCK_WAIT_TIMEOUT, ER_LOCK_DEADLOCK };
145
146
166
168{
169 lock->reset();
170 delete lock;
171 lock= NULL;
172}
173
174DrizzleLock *Session::lockTables(Table **tables, uint32_t count, uint32_t flags)
175{
176 DrizzleLock *sql_lock;
177 Table *write_lock_used;
178 vector<plugin::StorageEngine *> involved_engines;
179
180 do
181 {
182 if (! (sql_lock= get_lock_data(tables, count, true, &write_lock_used)))
183 break;
184
185 if (global_read_lock && write_lock_used and (not (flags & DRIZZLE_LOCK_IGNORE_GLOBAL_READ_LOCK)))
186 {
187 /*
188 Someone has issued LOCK ALL TABLES FOR READ and we want a write lock
189 Wait until the lock is gone
190 */
191 if (wait_if_global_read_lock(1, 1))
192 {
193 /* Clear the lock type of all lock data to avoid reusage. */
194 reset_lock_data_and_free(sql_lock);
195 break;
196 }
197
198 if (open_tables.version != g_refresh_version)
199 {
200 /* Clear the lock type of all lock data to avoid reusage. */
201 reset_lock_data_and_free(sql_lock);
202 break;
203 }
204 }
205
206 set_proc_info("Notify start statement");
207 /*
208 * Here, we advise all storage engines involved in the
209 * statement that we are starting a new statement
210 */
211 if (sql_lock->sizeTable())
212 {
213 size_t num_tables= sql_lock->sizeTable();
214 plugin::StorageEngine *engine;
215 std::set<size_t> involved_slots;
216
217 for (size_t x= 1; x <= num_tables; x++, tables++)
218 {
219 engine= (*tables)->cursor->getEngine();
220
221 if (involved_slots.count(engine->getId()) > 0)
222 continue; /* already added to involved engines */
223
224 involved_engines.push_back(engine);
225 involved_slots.insert(engine->getId());
226 }
227
228 for_each(involved_engines.begin(),
229 involved_engines.end(),
230 std::bind(std::mem_fn(&plugin::StorageEngine::startStatement),
231 std::placeholders::_1, this));
232 }
233
234 set_proc_info("External lock");
235 /*
236 * Here, the call to lock_external() informs the
237 * all engines for all tables used in this statement
238 * of the type of lock that Drizzle intends to take on a
239 * specific table.
240 */
241 if (sql_lock->sizeTable() && lock_external(sql_lock->getTable(), sql_lock->sizeTable()))
242 {
243 /* Clear the lock type of all lock data to avoid reusage. */
244 reset_lock_data_and_free(sql_lock);
245 break;
246 }
247 set_proc_info("Table lock");
248 /* Copy the lock data array. thr_multi_lock() reorders its contens. */
249 memcpy(sql_lock->getLocks() + sql_lock->sizeLock(),
250 sql_lock->getLocks(),
251 sql_lock->sizeLock() * sizeof(*sql_lock->getLocks()));
252
253 /* Lock on the copied half of the lock data array. */
254 drizzled::error_t rc;
255 rc= thr_lock_errno_to_mysql[(int) thr_multi_lock(*this,
256 sql_lock->getLocks() +
257 sql_lock->sizeLock(),
258 sql_lock->sizeLock(),
259 this->lock_id)];
260 if (rc) /* a timeout or a deadlock */
261 {
262 if (sql_lock->sizeTable())
263 unlock_external(sql_lock->getTable(), sql_lock->sizeTable());
264 reset_lock_data_and_free(sql_lock);
265 my_error(rc, MYF(0));
266 }
267 } while(0);
268
269 set_proc_info(0);
270 if (getKilled())
271 {
272 send_kill_message();
273 if (sql_lock)
274 {
275 unlockTables(sql_lock);
276 sql_lock= NULL;
277 }
278 }
279
280 times.set_time_after_lock();
281
282 return sql_lock;
283}
284
285
286int Session::lock_external(Table **tables, uint32_t count)
287{
288 int lock_type,error;
289 for (uint32_t i= 1 ; i <= count ; i++, tables++)
290 {
291 assert((*tables)->reginfo.lock_type >= TL_READ);
292 lock_type=F_WRLCK; /* Lock exclusive */
293 if ((*tables)->db_stat & HA_READ_ONLY ||
294 ((*tables)->reginfo.lock_type >= TL_READ &&
295 (*tables)->reginfo.lock_type <= TL_READ_NO_INSERT))
296 lock_type=F_RDLCK;
297
298 if ((error=(*tables)->cursor->ha_external_lock(this,lock_type)))
299 {
300 print_lock_error(error, (*tables)->cursor->getEngine()->getName().c_str());
301 while (--i)
302 {
303 tables--;
304 (*tables)->cursor->ha_external_lock(this, F_UNLCK);
305 (*tables)->current_lock=F_UNLCK;
306 }
307 return error;
308 }
309 else
310 {
311 (*tables)->db_stat &= ~ HA_BLOCK_LOCK;
312 (*tables)->current_lock= lock_type;
313 }
314 }
315 return 0;
316}
317
318
319void Session::unlockTables(DrizzleLock *sql_lock)
320{
321 if (sql_lock->sizeLock())
322 sql_lock->unlock(sql_lock->sizeLock());
323 if (sql_lock->sizeTable())
324 unlock_external(sql_lock->getTable(), sql_lock->sizeTable());
325 delete sql_lock;
326}
327
333
334void Session::unlockSomeTables(Table **table, uint32_t count)
335{
336 DrizzleLock *sql_lock;
337 Table *write_lock_used;
338 if ((sql_lock= get_lock_data(table, count, false,
339 &write_lock_used)))
340 unlockTables(sql_lock);
341}
342
343
347
349{
350 uint32_t i,found;
351
352 /* Move all write locks first */
353 THR_LOCK_DATA **lock_local= sql_lock->getLocks();
354 for (i=found=0 ; i < sql_lock->sizeLock(); i++)
355 {
356 if (sql_lock->getLocks()[i]->type >= TL_WRITE_ALLOW_READ)
357 {
358 std::swap(*lock_local, sql_lock->getLocks()[i]);
359 lock_local++;
360 found++;
361 }
362 }
363 /* unlock the read locked tables */
364 if (i != found)
365 {
366 thr_multi_unlock(lock_local, i - found);
367 sql_lock->setLock(found);
368 }
369
370 /* Then do the same for the external locks */
371 /* Move all write locked tables first */
372 Table **table= sql_lock->getTable();
373 for (i=found=0 ; i < sql_lock->sizeTable() ; i++)
374 {
375 assert(sql_lock->getTable()[i]->lock_position == i);
376 if ((uint32_t) sql_lock->getTable()[i]->reginfo.lock_type >= TL_WRITE_ALLOW_READ)
377 {
378 std::swap(*table, sql_lock->getTable()[i]);
379 table++;
380 found++;
381 }
382 }
383 /* Unlock all read locked tables */
384 if (i != found)
385 {
386 unlock_external(table, i - found);
387 sql_lock->resizeTable(found);
388 }
389 /* Fix the lock positions in Table */
390 table= sql_lock->getTable();
391 found= 0;
392 for (i= 0; i < sql_lock->sizeTable(); i++)
393 {
394 Table *tbl= *table;
395 tbl->lock_position= table - sql_lock->getTable();
396 tbl->lock_data_start= found;
397 found+= tbl->lock_count;
398 table++;
399 }
400}
401
402
422
424{
425 unlockSomeTables(&table, /* table count */ 1);
426}
427
428
430
432{
433 DrizzleLock *locked;
434 Table *write_lock_used;
435
436 if ((locked= get_lock_data(&table, 1, false,
437 &write_lock_used)))
438 {
439 for (uint32_t x= 0; x < locked->sizeLock(); x++)
440 locked->getLocks()[x]->lock->abort_locks();
441 delete locked;
442 }
443}
444
445
457
459{
460 bool result= false;
461 Table* write_lock_used;
462 if (DrizzleLock* locked= get_lock_data(&table, 1, false, &write_lock_used))
463 {
464 for (uint32_t i= 0; i < locked->sizeLock(); i++)
465 {
466 if (locked->getLocks()[i]->lock->abort_locks_for_thread(table->in_use->thread_id))
467 result= true;
468 }
469 delete locked;
470 }
471 return result;
472}
473
475
476int Session::unlock_external(Table **table, uint32_t count)
477{
478 int error;
479
480 int error_code=0;
481 do
482 {
483 if ((*table)->current_lock != F_UNLCK)
484 {
485 (*table)->current_lock = F_UNLCK;
486 if ((error=(*table)->cursor->ha_external_lock(this, F_UNLCK)))
487 {
488 error_code=error;
489 print_lock_error(error_code, (*table)->cursor->getEngine()->getName().c_str());
490 }
491 }
492 table++;
493 } while (--count);
494 return error_code;
495}
496
497
508
509DrizzleLock *Session::get_lock_data(Table **table_ptr, uint32_t count,
510 bool should_lock, Table **write_lock_used)
511{
512 uint32_t lock_count;
513 THR_LOCK_DATA **locks, **locks_buf, **locks_start;
514 Table **to, **table_buf;
515
516 *write_lock_used=0;
517 for (uint32_t i= lock_count= 0 ; i < count ; i++)
518 {
519 Table *t= table_ptr[i];
520
521 if (! (t->getEngine()->check_flag(HTON_BIT_SKIP_STORE_LOCK)))
522 {
523 lock_count++;
524 }
525 }
526
527 /*
528 Allocating twice the number of pointers for lock data for use in
529 thr_mulit_lock(). This function reorders the lock data, but cannot
530 update the table values. So the second part of the array is copied
531 from the first part immediately before calling thr_multi_lock().
532 */
533 DrizzleLock *sql_lock= new DrizzleLock(lock_count);
534
535 if (not sql_lock)
536 return NULL;
537
538 locks= locks_buf= sql_lock->getLocks();
539 to= table_buf= sql_lock->getTable();
540
541 for (uint32_t i= 0; i < count ; i++)
542 {
543 Table *table;
544 thr_lock_type lock_type;
545
546 if (table_ptr[i]->getEngine()->check_flag(HTON_BIT_SKIP_STORE_LOCK))
547 continue;
548
549 table= table_ptr[i];
550 lock_type= table->reginfo.lock_type;
551 assert (lock_type != TL_WRITE_DEFAULT);
552 if (lock_type >= TL_WRITE_ALLOW_WRITE)
553 {
554 *write_lock_used=table;
555 if (table->db_stat & HA_READ_ONLY)
556 {
557 my_error(ER_OPEN_AS_READONLY, MYF(0), table->getAlias());
558 /* Clear the lock type of the lock data that are stored already. */
559 sql_lock->setLock(locks - sql_lock->getLocks());
560 reset_lock_data_and_free(sql_lock);
561 return NULL;
562 }
563 }
564 locks_start= locks;
565 locks= table->cursor->store_lock(this, locks, should_lock ? lock_type : TL_IGNORE);
566 if (should_lock)
567 {
568 table->lock_position= (uint32_t) (to - table_buf);
569 table->lock_data_start= (uint32_t) (locks_start - locks_buf);
570 table->lock_count= (uint32_t) (locks - locks_start);
571 assert(table->lock_count == 1);
572 }
573 *to++= table;
574 }
575 /*
576 We do not use 'tables', because there are cases where store_lock()
577 returns less locks than lock_count() claimed. This can happen when
578 a FLUSH TABLES tries to abort locks from a MERGE table of another
579 thread. When that thread has just opened the table, but not yet
580 attached its children, it cannot return the locks. lock_count()
581 always returns the number of locks that an attached table has.
582 This is done to avoid the reverse situation: If lock_count() would
583 return 0 for a non-attached MERGE table, and that table becomes
584 attached between the calls to lock_count() and store_lock(), then
585 we would have allocated too little memory for the lock data. Now
586 we may allocate too much, but better safe than memory overrun.
587 And in the FLUSH case, the memory is released quickly anyway.
588 */
589 sql_lock->setLock(locks - locks_buf);
590
591 return sql_lock;
592}
593
594
620
622{
623 identifier::Table identifier(catalog().identifier(),
624 table_list->getSchemaName(),
625 table_list->getTableName());
626 {
627 /* Only insert the table if we haven't insert it already */
628 table::CacheRange ppp= table::getCache().equal_range(identifier.getKey());
629 for (table::CacheMap::const_iterator iter= ppp.first; iter != ppp.second; ++iter)
630 {
631 Table *table= iter->second;
632 if (table->reginfo.lock_type < TL_WRITE)
633 continue;
634 if (table->in_use == this)
635 {
636 table->getMutableShare()->resetVersion(); // Ensure no one can use this
637 table->locked_by_name= true;
638 return 0;
639 }
640 }
641 }
642
644 table_list->table= reinterpret_cast<Table*>(table);
645
646 /* Return 1 if table is in use */
647 return (test(table::Cache::removeTable(*this, identifier, RTFC_NO_FLAG)));
648}
649
650
651void TableList::unlock_table_name()
652{
653 if (table)
654 {
655 table::remove_table(static_cast<table::Concurrent *>(table));
657 }
658}
659
660
661static bool locked_named_table(TableList *table_list)
662{
663 for (; table_list; table_list=table_list->next_local)
664 {
665 Table *table= table_list->table;
666 if (table)
667 {
668 Table *save_next= table->getNext();
669 table->setNext(NULL);
670 bool result= table::Cache::areTablesUsed(table_list->table, 0);
671 table->setNext(save_next);
672 if (result)
673 return 1;
674 }
675 }
676 return 0; // All tables are locked
677}
678
679
680bool Session::wait_for_locked_table_names(TableList *table_list)
681{
682 bool result= false;
683
684#if 0
685 assert(ownership of table::Cache::mutex());
686#endif
687
688 while (locked_named_table(table_list))
689 {
690 if (getKilled())
691 {
692 result= true;
693 break;
694 }
695 wait_for_condition(table::Cache::mutex(), COND_refresh);
696 table::Cache::mutex().lock(); /* Wait for a table to unlock and then lock it */
697 }
698 return result;
699}
700
701
715
717{
718 bool got_all_locks= true;
719 for (TableList* lock_table= table_list; lock_table; lock_table= lock_table->next_local)
720 {
721 int got_lock= lock_table_name(lock_table);
722 if (got_lock < 0)
723 {
724 table_list->unlock_table_names(table_list);
725 return true; // Fatal error
726 }
727 if (got_lock)
728 got_all_locks= false; // Someone is using table
729 }
730
731 /* If some table was in use, wait until we got the lock */
732 if (not got_all_locks && wait_for_locked_table_names(table_list))
733 {
734 table_list->unlock_table_names(table_list);
735 return true;
736 }
737 return false;
738}
739
740
758
760{
761 if (lock_table_names(table_list))
762 return true;
763
764 /*
765 Upgrade the table name locks from semi-exclusive to exclusive locks.
766 */
767 for (TableList *table= table_list; table; table= table->next_global)
768 {
769 if (table->table)
770 table->table->open_placeholder= 1;
771 }
772 return false;
773}
774
775
797
798void TableList::unlock_table_names(TableList *last_table)
799{
800 for (TableList *table_iter= this; table_iter != last_table; table_iter= table_iter->next_local)
801 {
802 table_iter->unlock_table_name();
803 }
805}
806
807
808static void print_lock_error(int error, const char *table)
809{
810 drizzled::error_t textno;
811 switch (error)
812 {
813 case HA_ERR_LOCK_WAIT_TIMEOUT:
814 textno=ER_LOCK_WAIT_TIMEOUT;
815 break;
816 case HA_ERR_READ_ONLY_TRANSACTION:
817 textno=ER_READ_ONLY_TRANSACTION;
818 break;
819 case HA_ERR_LOCK_DEADLOCK:
820 textno=ER_LOCK_DEADLOCK;
821 break;
822 case HA_ERR_WRONG_COMMAND:
823 textno=ER_ILLEGAL_HA;
824 break;
825 default:
826 textno=ER_CANT_LOCK;
827 break;
828 }
829
830 if ( textno == ER_ILLEGAL_HA )
831 my_error(textno, MYF(ME_BELL+ME_OLDWIN+ME_WAITTANG), table);
832 else
833 my_error(textno, MYF(ME_BELL+ME_OLDWIN+ME_WAITTANG), error);
834}
835
836
837/****************************************************************************
838 Handling of global read locks
839
840 Taking the global read lock is TWO steps (2nd step is optional; without
841 it, COMMIT of existing transactions will be allowed):
842 lock_global_read_lock() THEN make_global_read_lock_block_commit().
843
844 The global locks are handled through the global variables:
845 global_read_lock
846 count of threads which have the global read lock (i.e. have completed at
847 least the first step above)
848 global_read_lock_blocks_commit
849 count of threads which have the global read lock and block
850 commits (i.e. are in or have completed the second step above)
851 waiting_for_read_lock
852 count of threads which want to take a global read lock but cannot
853 protect_against_global_read_lock
854 count of threads which have set protection against global read lock.
855
856 access to them is protected with a mutex LOCK_global_read_lock
857
858 (XXX: one should never take table::Cache::mutex() if LOCK_global_read_lock is
859 taken, otherwise a deadlock may occur. Other mutexes could be a
860 problem too - grep the code for global_read_lock if you want to use
861 any other mutex here) Also one must not hold table::Cache::mutex() when calling
862 wait_if_global_read_lock(). When the thread with the global read lock
863 tries to close its tables, it needs to take table::Cache::mutex() in
864 close_thread_table().
865
866 How blocking of threads by global read lock is achieved: that's
867 advisory. Any piece of code which should be blocked by global read lock must
868 be designed like this:
869 - call to wait_if_global_read_lock(). When this returns 0, no global read
870 lock is owned; if argument abort_on_refresh was 0, none can be obtained.
871 - job
872 - if abort_on_refresh was 0, call to session->startWaitingGlobalReadLock() to
873 allow other threads to get the global read lock. I.e. removal of the
874 protection.
875 (Note: it's a bit like an implementation of rwlock).
876
877 [ I am sorry to mention some SQL syntaxes below I know I shouldn't but found
878 no better descriptive way ]
879
880 Why does FLUSH TABLES WITH READ LOCK need to block COMMIT: because it's used
881 to read a non-moving SHOW MASTER STATUS, and a COMMIT writes to the binary
882 log.
883
884 Why getting the global read lock is two steps and not one. Because FLUSH
885 TABLES WITH READ LOCK needs to insert one other step between the two:
886 flushing tables. So the order is
887 1) lock_global_read_lock() (prevents any new table write locks, i.e. stalls
888 all new updates)
889 2) close_cached_tables() (the FLUSH TABLES), which will wait for tables
890 currently opened and being updated to close (so it's possible that there is
891 a moment where all new updates of server are stalled *and* FLUSH TABLES WITH
892 READ LOCK is, too).
893 3) session::makeGlobalReadLockBlockCommit().
894 If we have merged 1) and 3) into 1), we would have had this deadlock:
895 imagine thread 1 and 2, in non-autocommit mode, thread 3, and an InnoDB
896 table t.
897 session1: SELECT * FROM t FOR UPDATE;
898 session2: UPDATE t SET a=1; # blocked by row-level locks of session1
899 session3: FLUSH TABLES WITH READ LOCK; # blocked in close_cached_tables() by the
900 table instance of session2
901 session1: COMMIT; # blocked by session3.
902 session1 blocks session2 which blocks session3 which blocks session1: deadlock.
903
904 Note that we need to support that one thread does
905 FLUSH TABLES WITH READ LOCK; and then COMMIT;
906 (that's what innobackup does, for some good reason).
907 So in this exceptional case the COMMIT should not be blocked by the FLUSH
908 TABLES WITH READ LOCK.
909
910****************************************************************************/
911
912volatile uint32_t global_read_lock=0;
913volatile uint32_t global_read_lock_blocks_commit=0;
914static volatile uint32_t protect_against_global_read_lock=0;
915static volatile uint32_t waiting_for_read_lock=0;
916
917bool Session::lockGlobalReadLock()
918{
919 if (isGlobalReadLock() == Session::NONE)
920 {
921 const char *old_message;
922 LOCK_global_read_lock.lock();
923 old_message= enter_cond(COND_global_read_lock, LOCK_global_read_lock,
924 "Waiting to get readlock");
925
926 waiting_for_read_lock= waiting_for_read_lock + 1;
927 boost::mutex::scoped_lock scopedLock(LOCK_global_read_lock, boost::adopt_lock_t());
928 while (protect_against_global_read_lock && not getKilled())
929 COND_global_read_lock.wait(scopedLock);
930 waiting_for_read_lock= waiting_for_read_lock - 1;
931 scopedLock.release();
932 if (getKilled())
933 {
934 exit_cond(old_message);
935 return true;
936 }
937 setGlobalReadLock(Session::GOT_GLOBAL_READ_LOCK);
938 global_read_lock= global_read_lock + 1;
939 exit_cond(old_message); // this unlocks LOCK_global_read_lock
940 }
941
942 /*
943 We DON'T set global_read_lock_blocks_commit now, it will be set after
944 tables are flushed (as the present function serves for FLUSH TABLES WITH
945 READ LOCK only). Doing things in this order is necessary to avoid
946 deadlocks (we must allow COMMIT until all tables are closed; we should not
947 forbid it before, or we can have a 3-thread deadlock if 2 do SELECT FOR
948 UPDATE and one does FLUSH TABLES WITH READ LOCK).
949 */
950 return false;
951}
952
953
954void Session::unlockGlobalReadLock(void)
955{
956 uint32_t tmp;
957
958 if (not isGlobalReadLock()) // If we have no personal stake in the global lock, just return
959 return;
960
961 {
962 boost::mutex::scoped_lock scopedLock(LOCK_global_read_lock);
963 global_read_lock= global_read_lock - 1;
964 tmp= global_read_lock;
965 if (isGlobalReadLock() == Session::MADE_GLOBAL_READ_LOCK_BLOCK_COMMIT)
966 global_read_lock_blocks_commit= global_read_lock_blocks_commit - 1;
967 }
968 /* Send the signal outside the mutex to avoid a context switch */
969 if (not tmp)
970 {
971 COND_global_read_lock.notify_all();
972 }
973 setGlobalReadLock(Session::NONE);
974}
975
976static inline bool must_wait(bool is_not_commit)
977{
978 return (global_read_lock &&
979 (is_not_commit ||
980 global_read_lock_blocks_commit));
981}
982
983bool Session::wait_if_global_read_lock(bool abort_on_refresh, bool is_not_commit)
984{
985 const char *old_message= NULL;
986 bool result= 0, need_exit_cond;
987
988 /*
989 Assert that we do not own table::Cache::mutex(). If we would own it, other
990 threads could not close their tables. This would make a pretty
991 deadlock.
992 */
993 safe_mutex_assert_not_owner(table::Cache::mutex().native_handle());
994
995 LOCK_global_read_lock.lock();
996 if ((need_exit_cond= must_wait(is_not_commit)))
997 {
998 if (isGlobalReadLock()) // This thread had the read locks
999 {
1000 if (is_not_commit)
1001 my_message(ER_CANT_UPDATE_WITH_READLOCK,
1002 ER(ER_CANT_UPDATE_WITH_READLOCK), MYF(0));
1003 LOCK_global_read_lock.unlock();
1004 /*
1005 We allow FLUSHer to COMMIT; we assume FLUSHer knows what it does.
1006 This allowance is needed to not break existing versions of innobackup
1007 which do a BEGIN; INSERT; FLUSH TABLES WITH READ LOCK; COMMIT.
1008 */
1009 return is_not_commit;
1010 }
1011 old_message= enter_cond(COND_global_read_lock, LOCK_global_read_lock,
1012 "Waiting for release of readlock");
1013
1014 while (must_wait(is_not_commit) && not getKilled() &&
1015 (!abort_on_refresh || open_tables.version == g_refresh_version))
1016 {
1017 boost::mutex::scoped_lock scoped(LOCK_global_read_lock, boost::adopt_lock_t());
1018 COND_global_read_lock.wait(scoped);
1019 scoped.release();
1020 }
1021 if (getKilled())
1022 result=1;
1023 }
1024
1025 if (not abort_on_refresh && not result)
1026 protect_against_global_read_lock= protect_against_global_read_lock + 1;
1027
1028 /*
1029 The following is only true in case of a global read locks (which is rare)
1030 and if old_message is set
1031 */
1032 if (DRIZZLE_UNLIKELY(need_exit_cond))
1033 {
1034 exit_cond(old_message); // this unlocks LOCK_global_read_lock
1035 }
1036 else
1037 {
1038 LOCK_global_read_lock.unlock();
1039 }
1040
1041 return result;
1042}
1043
1044
1045void Session::startWaitingGlobalReadLock()
1046{
1047 if (DRIZZLE_UNLIKELY(isGlobalReadLock()))
1048 return;
1049
1050 LOCK_global_read_lock.lock();
1051 protect_against_global_read_lock= protect_against_global_read_lock - 1;
1052 bool tmp= (!protect_against_global_read_lock && (waiting_for_read_lock || global_read_lock_blocks_commit));
1053 LOCK_global_read_lock.unlock();
1054
1055 if (tmp)
1056 COND_global_read_lock.notify_all();
1057}
1058
1059
1060bool Session::makeGlobalReadLockBlockCommit()
1061{
1062 bool error;
1063 const char *old_message;
1064 /*
1065 If we didn't succeed lock_global_read_lock(), or if we already suceeded
1066 Session::makeGlobalReadLockBlockCommit(), do nothing.
1067 */
1068 if (isGlobalReadLock() != Session::GOT_GLOBAL_READ_LOCK)
1069 return false;
1070 LOCK_global_read_lock.lock();
1071 /* increment this BEFORE waiting on cond (otherwise race cond) */
1072 global_read_lock_blocks_commit= global_read_lock_blocks_commit + 1;
1073 old_message= enter_cond(COND_global_read_lock, LOCK_global_read_lock,
1074 "Waiting for all running commits to finish");
1075 while (protect_against_global_read_lock && not getKilled())
1076 {
1077 boost::mutex::scoped_lock scopedLock(LOCK_global_read_lock, boost::adopt_lock_t());
1078 COND_global_read_lock.wait(scopedLock);
1079 scopedLock.release();
1080 }
1081 if ((error= test(getKilled())))
1082 {
1083 global_read_lock_blocks_commit= global_read_lock_blocks_commit - 1; // undo what we did
1084 }
1085 else
1086 {
1087 setGlobalReadLock(Session::MADE_GLOBAL_READ_LOCK_BLOCK_COMMIT);
1088 }
1089
1090 exit_cond(old_message); // this unlocks LOCK_global_read_lock
1091
1092 return error;
1093}
1094
1095
1114
1116{
1117 COND_refresh.notify_all();
1118 COND_global_read_lock.notify_all();
1119}
1120
1121} /* namespace drizzled */
table::Placeholder & table_cache_insert_placeholder(const identifier::Table &)
Definition sql_base.cc:714
void set_proc_info(const char *info)
Definition session.h:605
TableList * next_global
Definition table_list.h:110
TableList * next_local
Definition table_list.h:107
Table * table
opened table
Definition table_list.h:145
uint32_t lock_data_start
DrizzleLock * get_lock_data(Table **table_ptr, uint32_t count, bool should_lock, Table **write_lock_used)
Definition global.cc:509
void unlockReadTables(DrizzleLock *sql_lock)
Definition global.cc:348
bool abortLockForThread(Table *table)
Definition global.cc:458
int unlock_external(Table **table, uint32_t count)
Definition global.cc:476
int lock_table_name(TableList *table_list)
Definition global.cc:621
void removeLock(Table *table)
Definition global.cc:423
void unlock_table_names(TableList *last_table=NULL)
Definition global.cc:798
bool lock_table_names(TableList *table_list)
Definition global.cc:716
void unlockSomeTables(Table **table, uint32_t count)
Definition global.cc:334
bool lock_table_names_exclusively(TableList *table_list)
Definition global.cc:759
void abortLock(Table *table)
Definition global.cc:431
static void reset_lock_data_and_free(DrizzleLock *&lock)
Definition global.cc:167
void broadcast_refresh(void)
Definition global.cc:1115
UNIV_INTERN ulint lock_table(ulint flags, dict_table_t *table, enum lock_mode mode, que_thr_t *thr)
TODO: Rename this file - func.h is stupid.
STL namespace.