~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/thr_lock.h

  • Committer: Daniel Nichter
  • Date: 2011-10-23 16:01:37 UTC
  • mto: This revision was merged to the branch mainline in revision 2448.
  • Revision ID: daniel@percona.com-20111023160137-7ac3blgz8z4tf8za
Add Administration Getting Started and Logging.  Capitalize SQL clause keywords.

Show diffs side-by-side

added added

removed removed

Lines of Context:
11
11
 
12
12
   You should have received a copy of the GNU General Public License
13
13
   along with this program; if not, write to the Free Software
14
 
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
15
 
 
16
 
/* For use with thr_lock:s */
17
 
 
18
 
#ifndef _thr_lock_h
19
 
#define _thr_lock_h
20
 
#ifdef  __cplusplus
21
 
extern "C" {
22
 
#endif
23
 
 
24
 
#include <mysys/my_pthread.h>
25
 
#include <mysys/my_list.h>
26
 
#include <mysys/definitions.h>
27
 
 
28
 
struct st_thr_lock;
29
 
extern uint32_t locks_immediate,locks_waited ;
30
 
extern pthread_mutex_t THR_LOCK_lock;
31
 
 
 
14
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
 
15
 
 
16
/* For use with thr_locks */
 
17
 
 
18
#pragma once
 
19
 
 
20
#include <boost/thread/mutex.hpp>
 
21
#include <boost/thread/shared_mutex.hpp>
 
22
#include <boost/thread/condition_variable.hpp>
 
23
 
 
24
#include <drizzled/visibility.h>
 
25
 
 
26
namespace drizzled {
32
27
 
33
28
extern uint64_t max_write_lock_count;
34
29
extern uint64_t table_lock_wait_timeout;
35
 
extern bool thr_lock_inited;
36
 
extern enum thr_lock_type thr_upgraded_concurrent_insert_lock;
37
 
 
 
30
 
 
31
 
 
32
enum thr_lock_type { TL_IGNORE=-1,
 
33
                     /* UNLOCK ANY LOCK */
 
34
                     TL_UNLOCK,
 
35
                     /* Read lock */
 
36
                     TL_READ,
 
37
                     TL_READ_WITH_SHARED_LOCKS,
 
38
                     /* READ, Don't allow concurrent insert */
 
39
                     TL_READ_NO_INSERT,
 
40
                     /*
 
41
                       Write lock, but allow other threads to read / write.
 
42
                       Used by BDB tables in MySQL to mark that someone is
 
43
                       reading/writing to the table.
 
44
                     */
 
45
                     TL_WRITE_ALLOW_WRITE,
 
46
                     /*
 
47
                       Write lock, but allow other threads to read.
 
48
                       Used by ALTER TABLE in MySQL to allow readers
 
49
                       to use the table until ALTER TABLE is finished.
 
50
                     */
 
51
                     TL_WRITE_ALLOW_READ,
 
52
                     /*
 
53
                       WRITE lock used by concurrent insert. Will allow
 
54
                       READ, if one could use concurrent insert on table.
 
55
                     */
 
56
                     TL_WRITE_CONCURRENT_INSERT,
 
57
                     /*
 
58
                       parser only! Late bound low_priority flag.
 
59
                       At open_tables() becomes thd->update_lock_default.
 
60
                     */
 
61
                     TL_WRITE_DEFAULT,
 
62
                     /* Normal WRITE lock */
 
63
                     TL_WRITE,
 
64
                     /* Abort new lock request with an error */
 
65
                     TL_WRITE_ONLY};
 
66
 
 
67
enum enum_thr_lock_result { THR_LOCK_SUCCESS= 0, THR_LOCK_ABORTED= 1,
 
68
                            THR_LOCK_WAIT_TIMEOUT= 2, THR_LOCK_DEADLOCK= 3 };
38
69
/*
39
70
  A description of the thread which owns the lock. The address
40
71
  of an instance of this structure is used to uniquely identify the thread.
41
72
*/
42
73
 
43
 
typedef struct st_thr_lock_info
 
74
struct THR_LOCK_INFO
44
75
{
45
 
  pthread_t thread;
46
 
  my_thread_id thread_id;
 
76
  uint64_t thread_id;
47
77
  uint32_t n_cursors;
48
 
} THR_LOCK_INFO;
 
78
 
 
79
  THR_LOCK_INFO() : 
 
80
    thread_id(0),
 
81
    n_cursors(0)
 
82
  { }
 
83
 
 
84
  void init();
 
85
 
 
86
};
49
87
 
50
88
/*
51
89
  Lock owner identifier. Globally identifies the lock owner within the
53
91
  structure is used as id.
54
92
*/
55
93
 
56
 
typedef struct st_thr_lock_owner
 
94
struct THR_LOCK_OWNER
57
95
{
58
96
  THR_LOCK_INFO *info;
59
 
} THR_LOCK_OWNER;
60
 
 
61
 
 
62
 
typedef struct st_thr_lock_data {
 
97
 
 
98
  THR_LOCK_OWNER() :
 
99
    info(0)
 
100
  { }
 
101
 
 
102
};
 
103
 
 
104
struct THR_LOCK;
 
105
struct THR_LOCK_DATA;
 
106
 
 
107
struct DRIZZLED_API THR_LOCK_DATA {
63
108
  THR_LOCK_OWNER *owner;
64
 
  struct st_thr_lock_data *next,**prev;
65
 
  struct st_thr_lock *lock;
66
 
  pthread_cond_t *cond;
 
109
  struct THR_LOCK_DATA *next,**prev;
 
110
  struct THR_LOCK *lock;
 
111
  boost::condition_variable_any *cond;
67
112
  enum thr_lock_type type;
68
113
  void *status_param;                   /* Param to status functions */
69
 
} THR_LOCK_DATA;
 
114
 
 
115
  THR_LOCK_DATA() :
 
116
    owner(0),
 
117
    next(0),
 
118
    prev(0),
 
119
    lock(0),
 
120
    cond(0),
 
121
    type(TL_UNLOCK),
 
122
    status_param(0)
 
123
  { }
 
124
 
 
125
  void init(THR_LOCK *lock,
 
126
            void *status_param= NULL);
 
127
};
70
128
 
71
129
struct st_lock_list {
72
130
  THR_LOCK_DATA *data,**last;
 
131
 
 
132
  st_lock_list() :
 
133
    data(0),
 
134
    last(0)
 
135
  { }
73
136
};
74
137
 
75
 
typedef struct st_thr_lock {
76
 
  LIST list;
77
 
  pthread_mutex_t mutex;
 
138
struct THR_LOCK {
 
139
private:
 
140
  boost::mutex mutex;
 
141
public:
78
142
  struct st_lock_list read_wait;
79
143
  struct st_lock_list read;
80
144
  struct st_lock_list write_wait;
82
146
  /* write_lock_count is incremented for write locks and reset on read locks */
83
147
  uint32_t write_lock_count;
84
148
  uint32_t read_no_write_count;
85
 
  void (*get_status)(void*, int);       /* When one gets a lock */
86
 
  void (*copy_status)(void*,void*);
87
 
  void (*update_status)(void*);         /* Before release of write */
88
 
  void (*restore_status)(void*);         /* Before release of read */
89
 
  bool (*check_status)(void *);
90
 
} THR_LOCK;
91
 
 
92
 
 
93
 
extern LIST *thr_lock_thread_list;
94
 
 
95
 
bool init_thr_lock(void);               /* Must be called once/thread */
 
149
 
 
150
  THR_LOCK() :
 
151
    write_lock_count(0),
 
152
    read_no_write_count(0)
 
153
  { }
 
154
 
 
155
  void abort_locks();
 
156
  bool abort_locks_for_thread(uint64_t thread);
 
157
 
 
158
  void lock()
 
159
  {
 
160
    mutex.lock();
 
161
  }
 
162
 
 
163
  void unlock()
 
164
  {
 
165
    mutex.unlock();
 
166
  }
 
167
 
 
168
  boost::mutex *native_handle()
 
169
  {
 
170
    return &mutex;
 
171
  }
 
172
};
 
173
 
96
174
#define thr_lock_owner_init(owner, info_arg) (owner)->info= (info_arg)
97
 
void thr_lock_info_init(THR_LOCK_INFO *info);
98
 
void thr_lock_init(THR_LOCK *lock);
99
 
void thr_lock_delete(THR_LOCK *lock);
100
 
void thr_lock_data_init(THR_LOCK *lock,THR_LOCK_DATA *data,
101
 
                        void *status_param);
102
 
enum enum_thr_lock_result thr_lock(THR_LOCK_DATA *data,
103
 
                                   THR_LOCK_OWNER *owner,
104
 
                                   enum thr_lock_type lock_type);
105
 
void thr_unlock(THR_LOCK_DATA *data);
106
 
enum enum_thr_lock_result thr_multi_lock(THR_LOCK_DATA **data,
 
175
DRIZZLED_API void thr_lock_init(THR_LOCK *lock);
 
176
enum enum_thr_lock_result thr_multi_lock(Session &session, THR_LOCK_DATA **data,
107
177
                                         uint32_t count, THR_LOCK_OWNER *owner);
108
178
void thr_multi_unlock(THR_LOCK_DATA **data,uint32_t count);
109
 
void thr_abort_locks(THR_LOCK *lock, bool upgrade_lock);
110
 
bool thr_abort_locks_for_thread(THR_LOCK *lock, my_thread_id thread);
111
 
bool thr_upgrade_write_delay_lock(THR_LOCK_DATA *data);
112
 
void    thr_downgrade_write_lock(THR_LOCK_DATA *data,
113
 
                                 enum thr_lock_type new_lock_type);
114
 
bool thr_reschedule_write_lock(THR_LOCK_DATA *data);
115
 
#ifdef  __cplusplus
116
 
}
117
 
#endif
118
 
#endif /* _thr_lock_h */
 
179
 
 
180
} /* namespace drizzled */
 
181