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>
103#include <boost/thread/shared_mutex.hpp>
104#include <boost/thread/condition_variable.hpp>
111static boost::mutex LOCK_global_read_lock;
112static boost::condition_variable_any COND_global_read_lock;
119static void print_lock_error(
int error,
const char *);
143static drizzled::error_t thr_lock_errno_to_mysql[]=
144{ EE_OK, EE_ERROR_FIRST, ER_LOCK_WAIT_TIMEOUT, ER_LOCK_DEADLOCK };
174DrizzleLock *Session::lockTables(Table **tables, uint32_t count, uint32_t flags)
176 DrizzleLock *sql_lock;
177 Table *write_lock_used;
178 vector<plugin::StorageEngine *> involved_engines;
182 if (! (sql_lock=
get_lock_data(tables, count,
true, &write_lock_used)))
185 if (global_read_lock && write_lock_used and (not (flags & DRIZZLE_LOCK_IGNORE_GLOBAL_READ_LOCK)))
191 if (wait_if_global_read_lock(1, 1))
198 if (open_tables.version != g_refresh_version)
211 if (sql_lock->sizeTable())
213 size_t num_tables= sql_lock->sizeTable();
214 plugin::StorageEngine *engine;
215 std::set<size_t> involved_slots;
217 for (
size_t x= 1; x <= num_tables; x++, tables++)
219 engine= (*tables)->cursor->getEngine();
221 if (involved_slots.count(engine->getId()) > 0)
224 involved_engines.push_back(engine);
225 involved_slots.insert(engine->getId());
228 for_each(involved_engines.begin(),
229 involved_engines.end(),
230 std::bind(std::mem_fn(&plugin::StorageEngine::startStatement),
231 std::placeholders::_1,
this));
241 if (sql_lock->sizeTable() && lock_external(sql_lock->getTable(), sql_lock->sizeTable()))
249 memcpy(sql_lock->getLocks() + sql_lock->sizeLock(),
250 sql_lock->getLocks(),
251 sql_lock->sizeLock() *
sizeof(*sql_lock->getLocks()));
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(),
262 if (sql_lock->sizeTable())
265 my_error(rc, MYF(0));
275 unlockTables(sql_lock);
280 times.set_time_after_lock();
286int Session::lock_external(
Table **tables, uint32_t count)
289 for (uint32_t i= 1 ; i <= count ; i++, tables++)
291 assert((*tables)->reginfo.lock_type >= TL_READ);
293 if ((*tables)->db_stat & HA_READ_ONLY ||
294 ((*tables)->reginfo.lock_type >= TL_READ &&
295 (*tables)->reginfo.lock_type <= TL_READ_NO_INSERT))
298 if ((error=(*tables)->cursor->ha_external_lock(
this,lock_type)))
300 print_lock_error(error, (*tables)->cursor->getEngine()->getName().c_str());
304 (*tables)->cursor->ha_external_lock(
this, F_UNLCK);
305 (*tables)->current_lock=F_UNLCK;
311 (*tables)->db_stat &= ~ HA_BLOCK_LOCK;
312 (*tables)->current_lock= lock_type;
321 if (sql_lock->sizeLock())
322 sql_lock->unlock(sql_lock->sizeLock());
323 if (sql_lock->sizeTable())
337 Table *write_lock_used;
340 unlockTables(sql_lock);
354 for (i=found=0 ; i < sql_lock->sizeLock(); i++)
356 if (sql_lock->getLocks()[i]->type >= TL_WRITE_ALLOW_READ)
358 std::swap(*lock_local, sql_lock->getLocks()[i]);
366 thr_multi_unlock(lock_local, i - found);
367 sql_lock->setLock(found);
372 Table **table= sql_lock->getTable();
373 for (i=found=0 ; i < sql_lock->sizeTable() ; i++)
376 if ((uint32_t) sql_lock->getTable()[i]->reginfo.lock_type >= TL_WRITE_ALLOW_READ)
378 std::swap(*table, sql_lock->getTable()[i]);
387 sql_lock->resizeTable(found);
390 table= sql_lock->getTable();
392 for (i= 0; i < sql_lock->sizeTable(); i++)
434 Table *write_lock_used;
439 for (uint32_t x= 0; x < locked->sizeLock(); x++)
440 locked->getLocks()[x]->lock->abort_locks();
461 Table* write_lock_used;
464 for (uint32_t i= 0; i < locked->sizeLock(); i++)
466 if (locked->getLocks()[i]->lock->abort_locks_for_thread(table->in_use->thread_id))
483 if ((*table)->current_lock != F_UNLCK)
485 (*table)->current_lock = F_UNLCK;
486 if ((error=(*table)->cursor->ha_external_lock(
this, F_UNLCK)))
489 print_lock_error(error_code, (*table)->cursor->getEngine()->getName().c_str());
510 bool should_lock,
Table **write_lock_used)
514 Table **to, **table_buf;
517 for (uint32_t i= lock_count= 0 ; i < count ; i++)
519 Table *t= table_ptr[i];
521 if (! (t->getEngine()->check_flag(HTON_BIT_SKIP_STORE_LOCK)))
538 locks= locks_buf= sql_lock->getLocks();
539 to= table_buf= sql_lock->getTable();
541 for (uint32_t i= 0; i < count ; i++)
544 thr_lock_type lock_type;
546 if (table_ptr[i]->getEngine()->check_flag(HTON_BIT_SKIP_STORE_LOCK))
550 lock_type= table->reginfo.lock_type;
551 assert (lock_type != TL_WRITE_DEFAULT);
552 if (lock_type >= TL_WRITE_ALLOW_WRITE)
554 *write_lock_used=table;
555 if (table->db_stat & HA_READ_ONLY)
557 my_error(ER_OPEN_AS_READONLY, MYF(0), table->getAlias());
559 sql_lock->setLock(locks - sql_lock->getLocks());
565 locks= table->cursor->store_lock(
this, locks, should_lock ? lock_type : TL_IGNORE);
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);
589 sql_lock->setLock(locks - locks_buf);
624 table_list->getSchemaName(),
625 table_list->getTableName());
628 table::CacheRange ppp= table::getCache().equal_range(identifier.getKey());
629 for (table::CacheMap::const_iterator iter= ppp.first; iter != ppp.second; ++iter)
631 Table *table= iter->second;
632 if (table->reginfo.lock_type < TL_WRITE)
634 if (table->in_use ==
this)
636 table->getMutableShare()->resetVersion();
637 table->locked_by_name=
true;
644 table_list->
table=
reinterpret_cast<Table*
>(table);
647 return (test(table::Cache::removeTable(*
this, identifier, RTFC_NO_FLAG)));
651void TableList::unlock_table_name()
661static bool locked_named_table(
TableList *table_list)
663 for (; table_list; table_list=table_list->next_local)
665 Table *table= table_list->table;
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);
680bool Session::wait_for_locked_table_names(
TableList *table_list)
685 assert(ownership of table::Cache::mutex());
688 while (locked_named_table(table_list))
695 wait_for_condition(table::Cache::mutex(), COND_refresh);
696 table::Cache::mutex().lock();
718 bool got_all_locks=
true;
728 got_all_locks=
false;
732 if (not got_all_locks && wait_for_locked_table_names(table_list))
770 table->table->open_placeholder= 1;
800 for (TableList *table_iter=
this; table_iter != last_table; table_iter= table_iter->
next_local)
802 table_iter->unlock_table_name();
808static void print_lock_error(
int error,
const char *table)
810 drizzled::error_t textno;
813 case HA_ERR_LOCK_WAIT_TIMEOUT:
814 textno=ER_LOCK_WAIT_TIMEOUT;
816 case HA_ERR_READ_ONLY_TRANSACTION:
817 textno=ER_READ_ONLY_TRANSACTION;
819 case HA_ERR_LOCK_DEADLOCK:
820 textno=ER_LOCK_DEADLOCK;
822 case HA_ERR_WRONG_COMMAND:
823 textno=ER_ILLEGAL_HA;
830 if ( textno == ER_ILLEGAL_HA )
831 my_error(textno, MYF(ME_BELL+ME_OLDWIN+ME_WAITTANG), table);
833 my_error(textno, MYF(ME_BELL+ME_OLDWIN+ME_WAITTANG), error);
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;
917bool Session::lockGlobalReadLock()
919 if (isGlobalReadLock() == Session::NONE)
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");
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();
934 exit_cond(old_message);
937 setGlobalReadLock(Session::GOT_GLOBAL_READ_LOCK);
938 global_read_lock= global_read_lock + 1;
939 exit_cond(old_message);
954void Session::unlockGlobalReadLock(
void)
958 if (not isGlobalReadLock())
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;
971 COND_global_read_lock.notify_all();
973 setGlobalReadLock(Session::NONE);
976static inline bool must_wait(
bool is_not_commit)
978 return (global_read_lock &&
980 global_read_lock_blocks_commit));
983bool Session::wait_if_global_read_lock(
bool abort_on_refresh,
bool is_not_commit)
985 const char *old_message= NULL;
986 bool result= 0, need_exit_cond;
993 safe_mutex_assert_not_owner(table::Cache::mutex().native_handle());
995 LOCK_global_read_lock.lock();
996 if ((need_exit_cond= must_wait(is_not_commit)))
998 if (isGlobalReadLock())
1001 my_message(ER_CANT_UPDATE_WITH_READLOCK,
1002 ER(ER_CANT_UPDATE_WITH_READLOCK), MYF(0));
1003 LOCK_global_read_lock.unlock();
1009 return is_not_commit;
1011 old_message= enter_cond(COND_global_read_lock, LOCK_global_read_lock,
1012 "Waiting for release of readlock");
1014 while (must_wait(is_not_commit) && not getKilled() &&
1015 (!abort_on_refresh || open_tables.version == g_refresh_version))
1017 boost::mutex::scoped_lock scoped(LOCK_global_read_lock, boost::adopt_lock_t());
1018 COND_global_read_lock.wait(scoped);
1025 if (not abort_on_refresh && not result)
1026 protect_against_global_read_lock= protect_against_global_read_lock + 1;
1032 if (DRIZZLE_UNLIKELY(need_exit_cond))
1034 exit_cond(old_message);
1038 LOCK_global_read_lock.unlock();
1045void Session::startWaitingGlobalReadLock()
1047 if (DRIZZLE_UNLIKELY(isGlobalReadLock()))
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();
1056 COND_global_read_lock.notify_all();
1060bool Session::makeGlobalReadLockBlockCommit()
1063 const char *old_message;
1068 if (isGlobalReadLock() != Session::GOT_GLOBAL_READ_LOCK)
1070 LOCK_global_read_lock.lock();
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())
1077 boost::mutex::scoped_lock scopedLock(LOCK_global_read_lock, boost::adopt_lock_t());
1078 COND_global_read_lock.wait(scopedLock);
1079 scopedLock.release();
1081 if ((error= test(getKilled())))
1083 global_read_lock_blocks_commit= global_read_lock_blocks_commit - 1;
1087 setGlobalReadLock(Session::MADE_GLOBAL_READ_LOCK_BLOCK_COMMIT);
1090 exit_cond(old_message);
1117 COND_refresh.notify_all();
1118 COND_global_read_lock.notify_all();
table::Placeholder & table_cache_insert_placeholder(const identifier::Table &)
void set_proc_info(const char *info)
Table * table
opened table
DrizzleLock * get_lock_data(Table **table_ptr, uint32_t count, bool should_lock, Table **write_lock_used)
void unlockReadTables(DrizzleLock *sql_lock)
bool abortLockForThread(Table *table)
int unlock_external(Table **table, uint32_t count)
int lock_table_name(TableList *table_list)
void removeLock(Table *table)
void unlock_table_names(TableList *last_table=NULL)
bool lock_table_names(TableList *table_list)
void unlockSomeTables(Table **table, uint32_t count)
bool lock_table_names_exclusively(TableList *table_list)
void abortLock(Table *table)
static void reset_lock_data_and_free(DrizzleLock *&lock)
void broadcast_refresh(void)
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.