Drizzled Public API Documentation

thr_lock.h
00001 /* Copyright (C) 2000 MySQL AB
00002 
00003    This program is free software; you can redistribute it and/or modify
00004    it under the terms of the GNU General Public License as published by
00005    the Free Software Foundation; version 2 of the License.
00006 
00007    This program is distributed in the hope that it will be useful,
00008    but WITHOUT ANY WARRANTY; without even the implied warranty of
00009    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00010    GNU General Public License for more details.
00011 
00012    You should have received a copy of the GNU General Public License
00013    along with this program; if not, write to the Free Software
00014    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
00015 
00016 /* For use with thr_locks */
00017 
00018 #pragma once
00019 
00020 #include <boost/thread/mutex.hpp>
00021 #include <boost/thread/shared_mutex.hpp>
00022 #include <boost/thread/condition_variable.hpp>
00023 
00024 #include <drizzled/visibility.h>
00025 
00026 namespace drizzled
00027 {
00028 
00029 extern uint64_t max_write_lock_count;
00030 extern uint64_t table_lock_wait_timeout;
00031 
00032 
00033 enum thr_lock_type { TL_IGNORE=-1,
00034                      /* UNLOCK ANY LOCK */
00035                      TL_UNLOCK,
00036                      /* Read lock */
00037                      TL_READ,
00038                      TL_READ_WITH_SHARED_LOCKS,
00039                      /* READ, Don't allow concurrent insert */
00040                      TL_READ_NO_INSERT,
00041                      /*
00042                        Write lock, but allow other threads to read / write.
00043                        Used by BDB tables in MySQL to mark that someone is
00044                        reading/writing to the table.
00045                      */
00046                      TL_WRITE_ALLOW_WRITE,
00047                      /*
00048                        Write lock, but allow other threads to read.
00049                        Used by ALTER TABLE in MySQL to allow readers
00050                        to use the table until ALTER TABLE is finished.
00051                      */
00052                      TL_WRITE_ALLOW_READ,
00053                      /*
00054                        WRITE lock used by concurrent insert. Will allow
00055                        READ, if one could use concurrent insert on table.
00056                      */
00057                      TL_WRITE_CONCURRENT_INSERT,
00058                      /*
00059                        parser only! Late bound low_priority flag.
00060                        At open_tables() becomes thd->update_lock_default.
00061                      */
00062                      TL_WRITE_DEFAULT,
00063                      /* Normal WRITE lock */
00064                      TL_WRITE,
00065                      /* Abort new lock request with an error */
00066                      TL_WRITE_ONLY};
00067 
00068 enum enum_thr_lock_result { THR_LOCK_SUCCESS= 0, THR_LOCK_ABORTED= 1,
00069                             THR_LOCK_WAIT_TIMEOUT= 2, THR_LOCK_DEADLOCK= 3 };
00070 /*
00071   A description of the thread which owns the lock. The address
00072   of an instance of this structure is used to uniquely identify the thread.
00073 */
00074 
00075 struct THR_LOCK_INFO
00076 {
00077   uint64_t thread_id;
00078   uint32_t n_cursors;
00079 
00080   THR_LOCK_INFO() : 
00081     thread_id(0),
00082     n_cursors(0)
00083   { }
00084 
00085   void init();
00086 
00087 };
00088 
00089 /*
00090   Lock owner identifier. Globally identifies the lock owner within the
00091   thread and among all the threads. The address of an instance of this
00092   structure is used as id.
00093 */
00094 
00095 struct THR_LOCK_OWNER
00096 {
00097   THR_LOCK_INFO *info;
00098 
00099   THR_LOCK_OWNER() :
00100     info(0)
00101   { }
00102 
00103 };
00104 
00105 struct THR_LOCK;
00106 struct THR_LOCK_DATA;
00107 
00108 struct DRIZZLED_API THR_LOCK_DATA {
00109   THR_LOCK_OWNER *owner;
00110   struct THR_LOCK_DATA *next,**prev;
00111   struct THR_LOCK *lock;
00112   boost::condition_variable_any *cond;
00113   enum thr_lock_type type;
00114   void *status_param;     /* Param to status functions */
00115 
00116   THR_LOCK_DATA() :
00117     owner(0),
00118     next(0),
00119     prev(0),
00120     lock(0),
00121     cond(0),
00122     type(TL_UNLOCK),
00123     status_param(0)
00124   { }
00125 
00126   void init(THR_LOCK *lock,
00127             void *status_param= NULL);
00128 };
00129 
00130 struct st_lock_list {
00131   THR_LOCK_DATA *data,**last;
00132 
00133   st_lock_list() :
00134     data(0),
00135     last(0)
00136   { }
00137 };
00138 
00139 struct THR_LOCK {
00140 private:
00141   boost::mutex mutex;
00142 public:
00143   struct st_lock_list read_wait;
00144   struct st_lock_list read;
00145   struct st_lock_list write_wait;
00146   struct st_lock_list write;
00147   /* write_lock_count is incremented for write locks and reset on read locks */
00148   uint32_t write_lock_count;
00149   uint32_t read_no_write_count;
00150 
00151   THR_LOCK() :
00152     write_lock_count(0),
00153     read_no_write_count(0)
00154   { }
00155 
00156   ~THR_LOCK()
00157   { }
00158 
00159   void abort_locks();
00160   bool abort_locks_for_thread(uint64_t thread);
00161 
00162   void lock()
00163   {
00164     mutex.lock();
00165   }
00166 
00167   void unlock()
00168   {
00169     mutex.unlock();
00170   }
00171 
00172   void init()
00173   {
00174   }
00175 
00176   void deinit()
00177   {
00178   }
00179 
00180   boost::mutex *native_handle()
00181   {
00182     return &mutex;
00183   }
00184 };
00185 
00186 class Session; 
00187 
00188 #define thr_lock_owner_init(owner, info_arg) (owner)->info= (info_arg)
00189 DRIZZLED_API void thr_lock_init(THR_LOCK *lock);
00190 enum enum_thr_lock_result thr_multi_lock(Session &session, THR_LOCK_DATA **data,
00191                                          uint32_t count, THR_LOCK_OWNER *owner);
00192 void thr_multi_unlock(THR_LOCK_DATA **data,uint32_t count);
00193 
00194 } /* namespace drizzled */
00195