~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/thr_lock.h

  • Committer: Andrew Hutchings
  • Date: 2010-11-01 22:14:18 UTC
  • mto: This revision was merged to the branch mainline in revision 1907.
  • Revision ID: andrew@linuxjedi.co.uk-20101101221418-9n9gmm4ms7fl8vo5
Fix copyright

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