~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to mysys/thr_lock.h

Removed dependency for hex convert, fixed a few Protocol class issues for Solaris.

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., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
15
 
 
16
 
/* For use with thr_locks */
17
 
 
18
 
#ifndef DRIZZLED_THR_LOCK_H
19
 
#define DRIZZLED_THR_LOCK_H
20
 
 
21
 
#include <boost/thread/mutex.hpp>
22
 
#include <boost/thread/shared_mutex.hpp>
23
 
#include <boost/thread/condition_variable.hpp>
24
 
 
25
 
namespace drizzled
26
 
{
 
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/definitions.h>
 
26
 
 
27
struct st_thr_lock;
 
28
extern uint32_t locks_immediate,locks_waited ;
 
29
extern pthread_mutex_t THR_LOCK_lock;
 
30
 
27
31
 
28
32
extern uint64_t max_write_lock_count;
29
33
extern uint64_t table_lock_wait_timeout;
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 };
 
34
extern bool thr_lock_inited;
 
35
extern enum thr_lock_type thr_upgraded_concurrent_insert_lock;
 
36
 
69
37
/*
70
38
  A description of the thread which owns the lock. The address
71
39
  of an instance of this structure is used to uniquely identify the thread.
72
40
*/
73
41
 
74
 
struct THR_LOCK_INFO
 
42
typedef struct st_thr_lock_info
75
43
{
76
 
  uint64_t thread_id;
 
44
  pthread_t thread;
 
45
  my_thread_id thread_id;
77
46
  uint32_t n_cursors;
78
 
 
79
 
  THR_LOCK_INFO() : 
80
 
    thread_id(0),
81
 
    n_cursors(0)
82
 
  { }
83
 
 
84
 
  void init();
85
 
 
86
 
};
 
47
} THR_LOCK_INFO;
87
48
 
88
49
/*
89
50
  Lock owner identifier. Globally identifies the lock owner within the
91
52
  structure is used as id.
92
53
*/
93
54
 
94
 
struct THR_LOCK_OWNER
 
55
typedef struct st_thr_lock_owner
95
56
{
96
57
  THR_LOCK_INFO *info;
97
 
 
98
 
  THR_LOCK_OWNER() :
99
 
    info(0)
100
 
  { }
101
 
 
102
 
};
103
 
 
104
 
struct THR_LOCK;
105
 
struct THR_LOCK_DATA;
106
 
 
107
 
struct THR_LOCK_DATA {
 
58
} THR_LOCK_OWNER;
 
59
 
 
60
 
 
61
typedef struct st_thr_lock_data {
108
62
  THR_LOCK_OWNER *owner;
109
 
  struct THR_LOCK_DATA *next,**prev;
110
 
  struct THR_LOCK *lock;
111
 
  boost::condition_variable_any *cond;
 
63
  struct st_thr_lock_data *next,**prev;
 
64
  struct st_thr_lock *lock;
 
65
  pthread_cond_t *cond;
112
66
  enum thr_lock_type type;
113
67
  void *status_param;                   /* Param to status functions */
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);
 
68
} THR_LOCK_DATA;
 
69
 
 
70
/* A helper type for transactional locking. */
 
71
struct st_table_lock_info
 
72
{
 
73
  enum thr_lock_type lock_type;
 
74
  int           lock_timeout;
 
75
  bool          lock_transactional;
127
76
};
128
77
 
129
78
struct st_lock_list {
130
79
  THR_LOCK_DATA *data,**last;
131
 
 
132
 
  st_lock_list() :
133
 
    data(0),
134
 
    last(0)
135
 
  { }
136
80
};
137
81
 
138
 
struct THR_LOCK {
139
 
private:
140
 
  boost::mutex mutex;
141
 
public:
 
82
typedef struct st_thr_lock {
 
83
  pthread_mutex_t mutex;
142
84
  struct st_lock_list read_wait;
143
85
  struct st_lock_list read;
144
86
  struct st_lock_list write_wait;
146
88
  /* write_lock_count is incremented for write locks and reset on read locks */
147
89
  uint32_t write_lock_count;
148
90
  uint32_t read_no_write_count;
149
 
 
150
 
  THR_LOCK() :
151
 
    write_lock_count(0),
152
 
    read_no_write_count(0)
153
 
  { }
154
 
 
155
 
  ~THR_LOCK()
156
 
  { }
157
 
 
158
 
  void abort_locks();
159
 
  bool abort_locks_for_thread(uint64_t thread);
160
 
 
161
 
  void lock()
162
 
  {
163
 
    mutex.lock();
164
 
  }
165
 
 
166
 
  void unlock()
167
 
  {
168
 
    mutex.unlock();
169
 
  }
170
 
 
171
 
  void init()
172
 
  {
173
 
  }
174
 
 
175
 
  void deinit()
176
 
  {
177
 
  }
178
 
 
179
 
  boost::mutex *native_handle()
180
 
  {
181
 
    return &mutex;
182
 
  }
183
 
};
184
 
 
185
 
class Session; 
186
 
 
 
91
  void (*get_status)(void*, int);       /* When one gets a lock */
 
92
  void (*copy_status)(void*,void*);
 
93
  void (*update_status)(void*);         /* Before release of write */
 
94
  void (*restore_status)(void*);         /* Before release of read */
 
95
  bool (*check_status)(void *);
 
96
} THR_LOCK;
 
97
 
 
98
 
 
99
bool init_thr_lock(void);               /* Must be called once/thread */
187
100
#define thr_lock_owner_init(owner, info_arg) (owner)->info= (info_arg)
 
101
void thr_lock_info_init(THR_LOCK_INFO *info);
188
102
void thr_lock_init(THR_LOCK *lock);
189
 
enum enum_thr_lock_result thr_multi_lock(Session &session, THR_LOCK_DATA **data,
 
103
void thr_lock_delete(THR_LOCK *lock);
 
104
void thr_lock_data_init(THR_LOCK *lock,THR_LOCK_DATA *data,
 
105
                        void *status_param);
 
106
enum enum_thr_lock_result thr_lock(THR_LOCK_DATA *data,
 
107
                                   THR_LOCK_OWNER *owner,
 
108
                                   enum thr_lock_type lock_type);
 
109
void thr_unlock(THR_LOCK_DATA *data);
 
110
enum enum_thr_lock_result thr_multi_lock(THR_LOCK_DATA **data,
190
111
                                         uint32_t count, THR_LOCK_OWNER *owner);
191
112
void thr_multi_unlock(THR_LOCK_DATA **data,uint32_t count);
192
 
 
193
 
} /* namespace drizzled */
194
 
 
195
 
#endif /* DRIZZLED_THR_LOCK_H */
 
113
void thr_abort_locks(THR_LOCK *lock, bool upgrade_lock);
 
114
bool thr_abort_locks_for_thread(THR_LOCK *lock, my_thread_id thread);
 
115
bool thr_upgrade_write_delay_lock(THR_LOCK_DATA *data);
 
116
void    thr_downgrade_write_lock(THR_LOCK_DATA *data,
 
117
                                 enum thr_lock_type new_lock_type);
 
118
bool thr_reschedule_write_lock(THR_LOCK_DATA *data);
 
119
#ifdef  __cplusplus
 
120
}
 
121
#endif
 
122
#endif /* _thr_lock_h */