~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to storage/myisam/mi_locking.c

  • Committer: Brian Aker
  • Date: 2008-10-06 06:47:29 UTC
  • Revision ID: brian@tangent.org-20081006064729-2i9mhjkzyvow9xsm
RemoveĀ uint.

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
  isamdatabase.
21
21
*/
22
22
 
23
 
#include "myisam_priv.h"
24
 
#include "drizzled/charset_info.h"
25
 
#include <drizzled/util/test.h>
26
 
 
27
 
using namespace std;
28
 
using namespace drizzled;
 
23
#include "myisamdef.h"
 
24
#include <mystrings/m_ctype.h>
 
25
#include <mysys/my_tree.h>
 
26
#include <mysys/queues.h>
29
27
 
30
28
        /* lock table by F_UNLCK, F_RDLCK or F_WRLCK */
31
29
 
36
34
  MYISAM_SHARE *share=info->s;
37
35
  uint32_t flag;
38
36
 
39
 
  pthread_mutex_lock(&share->intern_lock);
40
 
  if (!info->s->in_use)
41
 
    info->s->in_use= new list<Session *>;
42
 
 
 
37
  if (share->options & HA_OPTION_READ_ONLY_DATA ||
 
38
      info->lock_type == lock_type)
 
39
    return(0);
43
40
  if (lock_type == F_EXTRA_LCK)                 /* Used by TMP tables */
44
41
  {
45
 
    pthread_mutex_unlock(&share->intern_lock);
46
42
    ++share->w_locks;
47
43
    ++share->tot_locks;
48
44
    info->lock_type= lock_type;
49
 
    info->s->in_use->push_front(info->in_use);
 
45
    info->s->in_use= list_add(info->s->in_use, &info->in_use);
50
46
    return(0);
51
47
  }
52
48
 
53
49
  flag=error=0;
 
50
  pthread_mutex_lock(&share->intern_lock);
54
51
  if (share->kfile >= 0)                /* May only be false on windows */
55
52
  {
56
53
    switch (lock_type) {
64
61
          !share->delay_key_write && flush_key_blocks(share->key_cache,
65
62
                                                      share->kfile,FLUSH_KEEP))
66
63
      {
67
 
        error=errno;
 
64
        error=my_errno;
68
65
        mi_print_error(info->s, HA_ERR_CRASHED);
69
66
        mi_mark_crashed(info);          /* Mark that table must be checked */
70
67
      }
72
69
      {
73
70
        if (end_io_cache(&info->rec_cache))
74
71
        {
75
 
          error=errno;
 
72
          error=my_errno;
76
73
          mi_print_error(info->s, HA_ERR_CRASHED);
77
74
          mi_mark_crashed(info);
78
75
        }
81
78
      {
82
79
        if (share->changed && !share->w_locks)
83
80
        {
 
81
#ifdef HAVE_MMAP
84
82
    if ((info->s->mmaped_length != info->s->state.state.data_file_length) &&
85
83
        (info->s->nonmmaped_inserts > MAX_NONMAPPED_INSERTS))
86
84
    {
87
85
      if (info->s->concurrent_insert)
88
 
        pthread_rwlock_wrlock(&info->s->mmap_lock);
 
86
        rw_wrlock(&info->s->mmap_lock);
89
87
      mi_remap_file(info, info->s->state.state.data_file_length);
90
88
      info->s->nonmmaped_inserts= 0;
91
89
      if (info->s->concurrent_insert)
92
 
        pthread_rwlock_unlock(&info->s->mmap_lock);
 
90
        rw_unlock(&info->s->mmap_lock);
93
91
    }
 
92
#endif
94
93
          share->state.process= share->last_process=share->this_process;
95
94
          share->state.unique=   info->last_unique=  info->this_unique;
96
95
          share->state.update_count= info->last_loop= ++info->this_loop;
97
96
          if (mi_state_info_write(share->kfile, &share->state, 1))
98
 
            error=errno;
 
97
            error=my_errno;
99
98
          share->changed=0;
100
 
          share->not_flushed=1;
 
99
          if (myisam_flush)
 
100
          {
 
101
            if (my_sync(share->kfile, MYF(0)))
 
102
              error= my_errno;
 
103
            if (my_sync(info->dfile, MYF(0)))
 
104
              error= my_errno;
 
105
          }
 
106
          else
 
107
            share->not_flushed=1;
101
108
          if (error)
102
109
          {
103
110
            mi_print_error(info->s, HA_ERR_CRASHED);
118
125
      }
119
126
      info->opt_flag&= ~(READ_CACHE_USED | WRITE_CACHE_USED);
120
127
      info->lock_type= F_UNLCK;
121
 
      info->s->in_use->remove(info->in_use);
 
128
      info->s->in_use= list_delete(info->s->in_use, &info->in_use);
122
129
      break;
123
130
    case F_RDLCK:
124
131
      if (info->lock_type == F_WRLCK)
143
150
        flag=1;
144
151
        if (mi_state_info_read_dsk(share->kfile, &share->state, 1))
145
152
        {
146
 
          error=errno;
 
153
          error=my_errno;
147
154
          break;
148
155
        }
149
156
        if (mi_state_info_read_dsk(share->kfile, &share->state, 1))
150
157
        {
151
 
          error=errno;
152
 
          errno=error;
 
158
          error=my_errno;
 
159
          my_errno=error;
153
160
          break;
154
161
        }
155
162
      }
157
164
      share->r_locks++;
158
165
      share->tot_locks++;
159
166
      info->lock_type=lock_type;
160
 
      info->s->in_use->push_front(info->in_use);
 
167
      info->s->in_use= list_add(info->s->in_use, &info->in_use);
161
168
      break;
162
169
    case F_WRLCK:
163
170
      if (info->lock_type == F_RDLCK)
180
187
          {
181
188
            if (mi_state_info_read_dsk(share->kfile, &share->state, 1))
182
189
            {
183
 
              error=errno;
184
 
              errno=error;
 
190
              error=my_errno;
 
191
              my_errno=error;
185
192
              break;
186
193
            }
187
194
          }
188
195
        }
189
196
      }
190
197
      _mi_test_if_changed(info);
191
 
 
 
198
        
192
199
      info->lock_type=lock_type;
 
200
      info->invalidator=info->s->invalidator;
193
201
      share->w_locks++;
194
202
      share->tot_locks++;
195
 
      info->s->in_use->push_front(info->in_use);
 
203
      info->s->in_use= list_add(info->s->in_use, &info->in_use);
196
204
      break;
197
205
    default:
198
206
      break;                            /* Impossible */
204
212
    /*
205
213
       Check for bad file descriptors if this table is part
206
214
       of a merge union. Failing to capture this may cause
207
 
       a crash on windows if the table is renamed and
 
215
       a crash on windows if the table is renamed and 
208
216
       later on referenced by the merge table.
209
217
     */
210
218
    if( info->owned_by_merge && (info->s)->kfile < 0 )
299
307
 
300
308
  IMPLEMENTATION
301
309
    Allow concurrent inserts if we don't have a hole in the table or
302
 
    if there is no active write lock and there is active read locks and
 
310
    if there is no active write lock and there is active read locks and 
303
311
    myisam_concurrent_insert == 2. In this last case the new
304
312
    row('s) are inserted at end of file instead of filling up the hole.
305
313
 
342
350
    {
343
351
      if (mi_state_info_read_dsk(share->kfile, &share->state, 1))
344
352
      {
345
 
        int error=errno ? errno : -1;
346
 
        errno=error;
 
353
        int error=my_errno ? my_errno : -1;
 
354
        my_errno=error;
347
355
        return(1);
348
356
      }
349
357
    }
350
358
    if (check_keybuffer)
351
359
      _mi_test_if_changed(info);
 
360
    info->invalidator=info->s->invalidator;
352
361
  }
353
362
  else if (lock_type == F_WRLCK && info->lock_type == F_RDLCK)
354
363
  {
355
 
    errno=EACCES;                               /* Not allowed to change */
 
364
    my_errno=EACCES;                            /* Not allowed to change */
356
365
    return(-1);                         /* when have read_lock() */
357
366
  }
358
367
  return(0);
372
381
  error=0;
373
382
  if (share->tot_locks == 0)
374
383
  {
375
 
    olderror=errno;                     /* Remember last error */
 
384
    olderror=my_errno;                  /* Remember last error */
376
385
    if (operation)
377
386
    {                                   /* Two threads can't be here */
378
387
      share->state.process= share->last_process=   share->this_process;
379
388
      share->state.unique=  info->last_unique=     info->this_unique;
380
389
      share->state.update_count= info->last_loop= ++info->this_loop;
381
390
      if ((error=mi_state_info_write(share->kfile, &share->state, 1)))
382
 
        olderror=errno;
 
391
        olderror=my_errno;
 
392
#ifdef __WIN__
 
393
      if (myisam_flush)
 
394
      {
 
395
        _commit(share->kfile);
 
396
        _commit(info->dfile);
 
397
      }
 
398
#endif
383
399
    }
384
 
    errno=olderror;
 
400
    my_errno=olderror;
385
401
  }
386
402
  else if (operation)
387
403
    share->changed= 1;                  /* Mark keyfile changed */