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 */
14
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
17
17
locking of isam-tables.
23
#include "myisam_priv.h"
24
#include <drizzled/charset_info.h>
23
#include "myisamdef.h"
24
#include <mystrings/m_ctype.h>
25
#include <mysys/my_tree.h>
26
#include <mysys/queues.h>
25
27
#include <drizzled/util/test.h>
28
using namespace drizzled;
30
29
/* lock table by F_UNLCK, F_RDLCK or F_WRLCK */
32
31
int mi_lock_database(MI_INFO *info, int lock_type)
36
35
MYISAM_SHARE *share=info->s;
40
info->s->in_use= new list<Session *>;
38
if (share->options & HA_OPTION_READ_ONLY_DATA ||
39
info->lock_type == lock_type)
42
41
if (lock_type == F_EXTRA_LCK) /* Used by TMP tables */
45
44
++share->tot_locks;
46
45
info->lock_type= lock_type;
47
info->s->in_use->push_front(info->in_use);
46
info->s->in_use= list_add(info->s->in_use, &info->in_use);
51
pthread_mutex_lock(&share->intern_lock);
52
52
if (share->kfile >= 0) /* May only be false on windows */
54
54
switch (lock_type) {
59
59
count= --share->w_locks;
60
60
--share->tot_locks;
61
61
if (info->lock_type == F_WRLCK && !share->w_locks &&
62
!share->delay_key_write && flush_key_blocks(share->getKeyCache(),
62
!share->delay_key_write && flush_key_blocks(share->key_cache,
63
63
share->kfile,FLUSH_KEEP))
66
66
mi_print_error(info->s, HA_ERR_CRASHED);
67
67
mi_mark_crashed(info); /* Mark that table must be checked */
69
69
if (info->opt_flag & (READ_CACHE_USED | WRITE_CACHE_USED))
71
if (info->rec_cache.end_io_cache())
71
if (end_io_cache(&info->rec_cache))
74
74
mi_print_error(info->s, HA_ERR_CRASHED);
75
75
mi_mark_crashed(info);
80
80
if (share->changed && !share->w_locks)
82
83
if ((info->s->mmaped_length != info->s->state.state.data_file_length) &&
83
84
(info->s->nonmmaped_inserts > MAX_NONMAPPED_INSERTS))
86
if (info->s->concurrent_insert)
87
pthread_rwlock_wrlock(&info->s->mmap_lock);
85
88
mi_remap_file(info, info->s->state.state.data_file_length);
86
89
info->s->nonmmaped_inserts= 0;
90
if (info->s->concurrent_insert)
91
pthread_rwlock_unlock(&info->s->mmap_lock);
88
94
share->state.process= share->last_process=share->this_process;
89
95
share->state.unique= info->last_unique= info->this_unique;
90
96
share->state.update_count= info->last_loop= ++info->this_loop;
91
97
if (mi_state_info_write(share->kfile, &share->state, 1))
102
if (my_sync(share->kfile, MYF(0)))
104
if (my_sync(info->dfile, MYF(0)))
108
share->not_flushed=1;
97
111
mi_print_error(info->s, HA_ERR_CRASHED);
219
234
/****************************************************************************
235
The following functions are called by thr_lock() in threaded applications
236
****************************************************************************/
239
Create a copy of the current status for the table
243
param Pointer to Myisam handler
244
concurrent_insert Set to 1 if we are going to do concurrent inserts
245
(THR_WRITE_CONCURRENT_INSERT was used)
248
void mi_get_status(void* param, int concurrent_insert)
250
MI_INFO *info=(MI_INFO*) param;
252
info->save_state=info->s->state.state;
253
info->state= &info->save_state;
254
info->append_insert_at_end= concurrent_insert;
259
void mi_update_status(void* param)
261
MI_INFO *info=(MI_INFO*) param;
263
Because someone may have closed the table we point at, we only
264
update the state if its our own state. This isn't a problem as
265
we are always pointing at our own lock or at a read lock.
266
(This is enforced by thr_multi_lock.c)
268
if (info->state == &info->save_state)
270
info->s->state.state= *info->state;
271
info->state= &info->s->state.state;
273
info->append_insert_at_end= 0;
276
We have to flush the write cache here as other threads may start
277
reading the table before mi_lock_database() is called
279
if (info->opt_flag & WRITE_CACHE_USED)
281
if (end_io_cache(&info->rec_cache))
283
mi_print_error(info->s, HA_ERR_CRASHED);
284
mi_mark_crashed(info);
286
info->opt_flag&= ~WRITE_CACHE_USED;
291
void mi_restore_status(void *param)
293
MI_INFO *info= (MI_INFO*) param;
294
info->state= &info->s->state.state;
295
info->append_insert_at_end= 0;
299
void mi_copy_status(void* to,void *from)
301
((MI_INFO*) to)->state= &((MI_INFO*) from)->save_state;
306
Check if should allow concurrent inserts
309
Allow concurrent inserts if we don't have a hole in the table or
310
if there is no active write lock and there is active read locks and
311
myisam_concurrent_insert == 2. In this last case the new
312
row('s) are inserted at end of file instead of filling up the hole.
314
The last case is to allow one to inserts into a heavily read-used table
315
even if there is holes.
318
If there is a an rtree indexes in the table, concurrent inserts are
319
disabled in mi_open()
322
0 ok to use concurrent inserts
326
bool mi_check_status(void *param)
328
MI_INFO *info=(MI_INFO*) param;
330
The test for w_locks == 1 is here because this thread has already done an
331
external lock (in other words: w_locks == 1 means no other threads has
334
return (bool) !(info->s->state.dellink == HA_OFFSET_ERROR ||
335
(myisam_concurrent_insert == 2 && info->s->r_locks &&
336
info->s->w_locks == 1));
340
/****************************************************************************
220
341
** functions to read / write the state
221
342
****************************************************************************/
260
381
if (share->tot_locks == 0)
262
olderror=errno; /* Remember last error */
383
olderror=my_errno; /* Remember last error */
264
385
{ /* Two threads can't be here */
265
386
share->state.process= share->last_process= share->this_process;
266
387
share->state.unique= info->last_unique= info->this_unique;
267
388
share->state.update_count= info->last_loop= ++info->this_loop;
268
389
if ((error=mi_state_info_write(share->kfile, &share->state, 1)))
394
_commit(share->kfile);
395
_commit(info->dfile);
273
401
else if (operation)
274
402
share->changed= 1; /* Mark keyfile changed */
287
415
share->state.update_count != info->last_loop)
288
416
{ /* Keyfile has changed */
289
417
if (share->state.process != share->this_process)
290
flush_key_blocks(share->getKeyCache(), share->kfile, FLUSH_RELEASE);
418
flush_key_blocks(share->key_cache, share->kfile, FLUSH_RELEASE);
291
419
share->last_process=share->state.process;
292
420
info->last_unique= share->state.unique;
293
421
info->last_loop= share->state.update_count;