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 #include <drizzled/common_fwd.h>
00026 
00027 namespace drizzled {
00028 
00029 extern uint64_t max_write_lock_count;
00030 extern uint64_t table_lock_wait_timeout;
00031 
00032 enum thr_lock_type 
00033 { 
00034                      TL_IGNORE=-1,
00035                      /* UNLOCK ANY LOCK */
00036                      TL_UNLOCK,
00037                      /* Read lock */
00038                      TL_READ,
00039                      TL_READ_WITH_SHARED_LOCKS,
00040                      /* READ, Don't allow concurrent insert */
00041                      TL_READ_NO_INSERT,
00042                      /*
00043                        Write lock, but allow other threads to read / write.
00044                        Used by BDB tables in MySQL to mark that someone is
00045                        reading/writing to the table.
00046                      */
00047                      TL_WRITE_ALLOW_WRITE,
00048                      /*
00049                        Write lock, but allow other threads to read.
00050                        Used by ALTER TABLE in MySQL to allow readers
00051                        to use the table until ALTER TABLE is finished.
00052                      */
00053                      TL_WRITE_ALLOW_READ,
00054                      /*
00055                        WRITE lock used by concurrent insert. Will allow
00056                        READ, if one could use concurrent insert on table.
00057                      */
00058                      TL_WRITE_CONCURRENT_INSERT,
00059                      /*
00060                        parser only! Late bound low_priority flag.
00061                        At open_tables() becomes thd->update_lock_default.
00062                      */
00063                      TL_WRITE_DEFAULT,
00064                      /* Normal WRITE lock */
00065                      TL_WRITE,
00066                      /* Abort new lock request with an error */
00067                      TL_WRITE_ONLY
00068 };
00069 
00070 enum enum_thr_lock_result 
00071 { 
00072   THR_LOCK_SUCCESS= 0, 
00073   THR_LOCK_ABORTED= 1,
00074   THR_LOCK_WAIT_TIMEOUT= 2, 
00075   THR_LOCK_DEADLOCK= 3 
00076 };
00077 
00078 /*
00079   A description of the thread which owns the lock. The address
00080   of an instance of this structure is used to uniquely identify the thread.
00081 */
00082 
00083 struct THR_LOCK_INFO
00084 {
00085   uint64_t thread_id;
00086   uint32_t n_cursors;
00087 
00088   THR_LOCK_INFO() : 
00089     thread_id(0),
00090     n_cursors(0)
00091   { }
00092 
00093   void init();
00094 
00095 };
00096 
00097 /*
00098   Lock owner identifier. Globally identifies the lock owner within the
00099   thread and among all the threads. The address of an instance of this
00100   structure is used as id.
00101 */
00102 
00103 struct THR_LOCK_OWNER
00104 {
00105   THR_LOCK_INFO *info;
00106 
00107   THR_LOCK_OWNER() :
00108     info(NULL)
00109   { }
00110 
00111 };
00112 
00113 struct THR_LOCK;
00114 struct THR_LOCK_DATA;
00115 
00116 struct DRIZZLED_API THR_LOCK_DATA 
00117 {
00118   THR_LOCK_OWNER *owner;
00119   struct THR_LOCK_DATA *next,**prev;
00120   struct THR_LOCK *lock;
00121   boost::condition_variable_any *cond;
00122   enum thr_lock_type type;
00123   void *status_param;     /* Param to status functions */
00124 
00125   THR_LOCK_DATA() :
00126     owner(0),
00127     next(0),
00128     prev(0),
00129     lock(0),
00130     cond(0),
00131     type(TL_UNLOCK),
00132     status_param(0)
00133   { }
00134 
00135   void init(THR_LOCK*, void *status_param= NULL);
00136 };
00137 
00138 struct st_lock_list 
00139 {
00140   THR_LOCK_DATA *data,**last;
00141 
00142   st_lock_list() :
00143     data(0),
00144     last(0)
00145   { }
00146 };
00147 
00148 struct THR_LOCK 
00149 {
00150 private:
00151   boost::mutex mutex;
00152 public:
00153   struct st_lock_list read_wait;
00154   struct st_lock_list read;
00155   struct st_lock_list write_wait;
00156   struct st_lock_list write;
00157   /* write_lock_count is incremented for write locks and reset on read locks */
00158   uint32_t write_lock_count;
00159   uint32_t read_no_write_count;
00160 
00161   THR_LOCK() :
00162     write_lock_count(0),
00163     read_no_write_count(0)
00164   { }
00165 
00166   void abort_locks();
00167   bool abort_locks_for_thread(uint64_t thread);
00168 
00169   void lock()
00170   {
00171     mutex.lock();
00172   }
00173 
00174   void unlock()
00175   {
00176     mutex.unlock();
00177   }
00178 
00179   boost::mutex *native_handle()
00180   {
00181     return &mutex;
00182   }
00183 };
00184 
00185 DRIZZLED_API void thr_lock_init(THR_LOCK *lock);
00186 enum_thr_lock_result thr_multi_lock(Session&, THR_LOCK_DATA**, uint32_t count, THR_LOCK_OWNER*);
00187 void thr_multi_unlock(THR_LOCK_DATA**, uint32_t count);
00188 
00189 } /* namespace drizzled */
00190