~mysql/mysql-server/mysql-6.0

« back to all changes in this revision

Viewing changes to myisam/mi_locking.c

  • Committer: bk at mysql
  • Date: 2000-07-31 19:29:14 UTC
  • Revision ID: sp1r-bk@work.mysql.com-20000731192914-08846
Import changeset

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB
 
2
   
 
3
   This program is free software; you can redistribute it and/or modify
 
4
   it under the terms of the GNU General Public License as published by
 
5
   the Free Software Foundation; either version 2 of the License, or
 
6
   (at your option) any later version.
 
7
   
 
8
   This program is distributed in the hope that it will be useful,
 
9
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
   GNU General Public License for more details.
 
12
   
 
13
   You should have received a copy of the GNU General Public License
 
14
   along with this program; if not, write to the Free Software
 
15
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
 
16
 
 
17
/*
 
18
  locking of isam-tables.
 
19
  reads info from a isam-table. Must be first request before doing any furter
 
20
  calls to any isamfunktion.  Is used to allow many process use the same
 
21
  isamdatabase.
 
22
  */
 
23
 
 
24
#include "myisamdef.h"
 
25
#ifdef  __WIN__
 
26
#include <errno.h>
 
27
#endif
 
28
#if !defined(HAVE_PREAD) && defined(THREAD)
 
29
pthread_mutex_t THR_LOCK_keycache;
 
30
#endif
 
31
 
 
32
        /* lock table by F_UNLCK, F_RDLCK or F_WRLCK */
 
33
 
 
34
int mi_lock_database(MI_INFO *info, int lock_type)
 
35
{
 
36
  int error;
 
37
  uint count;
 
38
  MYISAM_SHARE *share=info->s;
 
39
  uint flag;
 
40
  DBUG_ENTER("mi_lock_database");
 
41
 
 
42
  if (share->options & HA_OPTION_READ_ONLY_DATA ||
 
43
      info->lock_type == lock_type)
 
44
    DBUG_RETURN(0);
 
45
  flag=error=0;
 
46
  pthread_mutex_lock(&share->intern_lock);
 
47
  if (share->kfile >= 0)                /* May only be false on windows */
 
48
  {
 
49
    switch (lock_type)
 
50
    {
 
51
    case F_UNLCK:
 
52
      if (info->lock_type == F_RDLCK)
 
53
        count= --share->r_locks;
 
54
      else
 
55
        count= --share->w_locks;
 
56
      if (info->lock_type == F_WRLCK && !share->w_locks &&
 
57
          !share->delay_key_write && flush_key_blocks(share->kfile,FLUSH_KEEP))
 
58
      {
 
59
        error=my_errno;
 
60
        mi_mark_crashed(info);          /* Mark that table must be checked */
 
61
      }
 
62
      if (info->opt_flag & (READ_CACHE_USED | WRITE_CACHE_USED))
 
63
      {
 
64
        if (end_io_cache(&info->rec_cache))
 
65
        {
 
66
          error=my_errno;
 
67
          mi_mark_crashed(info);
 
68
        }
 
69
      }
 
70
      if (!count)
 
71
      {
 
72
        if (share->changed && !share->w_locks)
 
73
        {
 
74
          share->state.process= share->last_process=share->this_process;
 
75
          share->state.unique=   info->last_unique=  info->this_unique;
 
76
#ifndef HAVE_PREAD
 
77
          pthread_mutex_lock(&THR_LOCK_keycache); // QQ; Has to be removed!
 
78
#endif
 
79
          if (mi_state_info_write(share->kfile, &share->state, 1))
 
80
            error=my_errno;
 
81
#ifndef HAVE_PREAD
 
82
          pthread_mutex_unlock(&THR_LOCK_keycache);// QQ; Has to be removed!
 
83
#endif
 
84
          share->changed=0;
 
85
          if (myisam_flush)
 
86
          {
 
87
#if defined(__WIN__)
 
88
            if (_commit(share->kfile))
 
89
              error=errno;
 
90
            if (_commit(info->dfile))
 
91
              error=errno;
 
92
#elif defined(HAVE_FDATASYNC)
 
93
            if (fdatasync(share->kfile))
 
94
              error=errno;
 
95
            if (fdatasync(share->dfile))
 
96
              error=errno;
 
97
#elif defined(HAVE_FSYNC)
 
98
            if (fsync(share->kfile))
 
99
              error=errno;
 
100
            if (fsync(share->dfile))
 
101
              error=errno;
 
102
#endif
 
103
          }
 
104
          else
 
105
            share->not_flushed=1;
 
106
          if (error)
 
107
            mi_mark_crashed(info);
 
108
        }
 
109
        if (share->r_locks)
 
110
        {                                       /* Only read locks left */
 
111
          flag=1;
 
112
          if (my_lock(share->kfile,F_RDLCK,0L,F_TO_EOF,
 
113
                      MYF(MY_WME | MY_SEEK_NOT_DONE)) && !error)
 
114
            error=my_errno;
 
115
        }
 
116
        else if (!share->w_locks)
 
117
        {                                       /* No more locks */
 
118
          flag=1;
 
119
          if (my_lock(share->kfile,F_UNLCK,0L,F_TO_EOF,
 
120
                      MYF(MY_WME | MY_SEEK_NOT_DONE)) && !error)
 
121
            error=my_errno;
 
122
        }
 
123
      }
 
124
      info->opt_flag&= ~(READ_CACHE_USED | WRITE_CACHE_USED);
 
125
      info->lock_type= F_UNLCK;
 
126
      break;
 
127
    case F_RDLCK:
 
128
      if (info->lock_type == F_WRLCK)
 
129
      {                                         /* Change RW to READONLY */
 
130
        if (share->w_locks == 1)
 
131
        {
 
132
          flag=1;
 
133
          if (my_lock(share->kfile,lock_type,0L,F_TO_EOF,
 
134
                      MYF(MY_SEEK_NOT_DONE)))
 
135
          {
 
136
            error=my_errno;
 
137
            break;
 
138
          }
 
139
        }
 
140
        share->w_locks--;
 
141
        share->r_locks++;
 
142
        info->lock_type=lock_type;
 
143
        break;
 
144
      }
 
145
      if (!share->r_locks && !share->w_locks)
 
146
      {
 
147
        flag=1;
 
148
        if (my_lock(share->kfile,lock_type,0L,F_TO_EOF,
 
149
                    info->lock_wait | MY_SEEK_NOT_DONE))
 
150
        {
 
151
          error=my_errno;
 
152
          break;
 
153
        }
 
154
        if (mi_state_info_read_dsk(share->kfile, &share->state, 1))
 
155
        {
 
156
          error=my_errno;
 
157
          VOID(my_lock(share->kfile,F_UNLCK,0L,F_TO_EOF,MYF(MY_SEEK_NOT_DONE)));
 
158
          my_errno=error;
 
159
          break;
 
160
        }
 
161
      }
 
162
      VOID(_mi_test_if_changed(info));
 
163
      share->r_locks++;
 
164
      info->lock_type=lock_type;
 
165
      break;
 
166
    case F_WRLCK:
 
167
      if (info->lock_type == F_RDLCK)
 
168
      {                                         /* Change READONLY to RW */
 
169
        if (share->r_locks == 1)
 
170
        {
 
171
          flag=1;
 
172
          if (my_lock(share->kfile,lock_type,0L,F_TO_EOF,
 
173
                      MYF(info->lock_wait | MY_SEEK_NOT_DONE)))
 
174
          {
 
175
            error=my_errno;
 
176
            break;
 
177
          }
 
178
          share->r_locks--;
 
179
          share->w_locks++;
 
180
          info->lock_type=lock_type;
 
181
          break;
 
182
        }
 
183
      }
 
184
      if (!(share->options & HA_OPTION_READ_ONLY_DATA))
 
185
      {
 
186
        if (!share->w_locks)
 
187
        {
 
188
          flag=1;
 
189
          VOID(my_seek(share->kfile,0L,MY_SEEK_SET,MYF(0)));
 
190
          if (my_lock(share->kfile,lock_type,0L,F_TO_EOF,info->lock_wait))
 
191
          {
 
192
            error=my_errno;
 
193
            break;
 
194
          }
 
195
          if (!share->r_locks)
 
196
          {
 
197
            if (mi_state_info_read_dsk(share->kfile, &share->state, 0))
 
198
            {
 
199
              error=my_errno;
 
200
              VOID(my_lock(share->kfile,F_UNLCK,0L,F_TO_EOF,info->lock_wait));
 
201
              my_errno=error;
 
202
              break;
 
203
            }
 
204
          }
 
205
        }
 
206
      }
 
207
      VOID(_mi_test_if_changed(info));
 
208
      info->lock_type=lock_type;
 
209
      share->w_locks++;
 
210
      break;
 
211
    default:
 
212
      break;                            /* Impossible */
 
213
    }
 
214
  }
 
215
  pthread_mutex_unlock(&share->intern_lock);
 
216
#if defined(FULL_LOG) || defined(_lint)
 
217
  lock_type|=(int) (flag << 8);         /* Set bit to set if real lock */
 
218
  myisam_log_command(MI_LOG_LOCK,info,(byte*) &lock_type,sizeof(lock_type),
 
219
                     error);
 
220
#endif
 
221
  DBUG_RETURN(error);
 
222
} /* mi_lock_database */
 
223
 
 
224
 
 
225
/****************************************************************************
 
226
** The following functions are called by thr_lock() in threaded applications
 
227
****************************************************************************/
 
228
 
 
229
void mi_get_status(void* param)
 
230
{
 
231
  MI_INFO *info=(MI_INFO*) param;
 
232
  DBUG_ENTER("mi_get_status");
 
233
  DBUG_PRINT("info",("key_file: %ld  data_file: %ld",
 
234
                     (long) info->s->state.state.key_file_length,
 
235
                     (long) info->s->state.state.data_file_length));
 
236
#ifndef DBUG_OFF
 
237
  if (info->state->key_file_length > info->s->state.state.key_file_length ||
 
238
      info->state->data_file_length > info->s->state.state.data_file_length)
 
239
    DBUG_PRINT("warning",("old info:  key_file: %ld  data_file: %ld",
 
240
                          (long) info->state->key_file_length,
 
241
                          (long) info->state->data_file_length));
 
242
#endif
 
243
  info->save_state=info->s->state.state;
 
244
  info->state= &info->save_state;
 
245
  DBUG_VOID_RETURN;
 
246
}
 
247
 
 
248
void mi_update_status(void* param)
 
249
{
 
250
  MI_INFO *info=(MI_INFO*) param;
 
251
  /*
 
252
    Because someone may have closed the table we point at, we only
 
253
    update the state if its our own state.  This isn't a problem as
 
254
    we are always pointing at our own lock or at a read lock.
 
255
    (This is enforced by thr_multi_lock.c)
 
256
  */
 
257
  if (info->state == &info->save_state)
 
258
  {
 
259
#ifndef DBUG_OFF
 
260
    DBUG_PRINT("info",("updating status:  key_file: %ld  data_file: %ld",
 
261
                       (long) info->state->key_file_length,
 
262
                       (long) info->state->data_file_length));
 
263
    if (info->state->key_file_length < info->s->state.state.key_file_length ||
 
264
        info->state->data_file_length < info->s->state.state.data_file_length)
 
265
      DBUG_PRINT("warning",("old info:  key_file: %ld  data_file: %ld",
 
266
                            (long) info->s->state.state.key_file_length,
 
267
                            (long) info->s->state.state.data_file_length));
 
268
#endif
 
269
    info->s->state.state= *info->state;
 
270
    info->state= &info->s->state.state;
 
271
  }
 
272
 
 
273
  /*
 
274
    We have to flush the write cache here as other threads may start
 
275
    reading the table before mi_lock_database() is called
 
276
  */
 
277
  if (info->opt_flag & WRITE_CACHE_USED)
 
278
  {
 
279
    if (end_io_cache(&info->rec_cache))
 
280
    {
 
281
      mi_mark_crashed(info);
 
282
    }
 
283
    info->opt_flag&= ~WRITE_CACHE_USED;
 
284
  }
 
285
}
 
286
 
 
287
void mi_copy_status(void* to,void *from)
 
288
{
 
289
  ((MI_INFO*) to)->state= &((MI_INFO*) from)->save_state;
 
290
}
 
291
 
 
292
my_bool mi_check_status(void* param)
 
293
{
 
294
  MI_INFO *info=(MI_INFO*) param;
 
295
  return (my_bool) (info->s->state.dellink != HA_OFFSET_ERROR);
 
296
}
 
297
 
 
298
 
 
299
/****************************************************************************
 
300
 ** functions to read / write the state
 
301
****************************************************************************/
 
302
 
 
303
int _mi_readinfo(register MI_INFO *info, int lock_type, int check_keybuffer)
 
304
{
 
305
  MYISAM_SHARE *share;
 
306
  DBUG_ENTER("_mi_readinfo");
 
307
 
 
308
  share=info->s;
 
309
  if (info->lock_type == F_UNLCK)
 
310
  {
 
311
    if (!share->r_locks && !share->w_locks)
 
312
    {
 
313
      if ((info->tmp_lock_type=lock_type) != F_RDLCK)
 
314
        if (my_lock(share->kfile,lock_type,0L,F_TO_EOF,
 
315
                    info->lock_wait | MY_SEEK_NOT_DONE))
 
316
          DBUG_RETURN(1);
 
317
      if (mi_state_info_read_dsk(share->kfile, &share->state, 1))
 
318
      {
 
319
        int error=my_errno ? my_errno : -1;
 
320
        VOID(my_lock(share->kfile,F_UNLCK,0L,F_TO_EOF,
 
321
                     MYF(MY_SEEK_NOT_DONE)));
 
322
        my_errno=error;
 
323
        DBUG_RETURN(1);
 
324
      }
 
325
    }
 
326
    if (check_keybuffer)
 
327
      VOID(_mi_test_if_changed(info));
 
328
  }
 
329
  else if (lock_type == F_WRLCK && info->lock_type == F_RDLCK)
 
330
  {
 
331
    my_errno=EACCES;                            /* Not allowed to change */
 
332
    DBUG_RETURN(-1);                            /* when have read_lock() */
 
333
  }
 
334
  DBUG_RETURN(0);
 
335
} /* _mi_readinfo */
 
336
 
 
337
 
 
338
        /* Every isam-function that uppdates the isam-database must! end */
 
339
        /* with this request */
 
340
        /* ARGSUSED */
 
341
 
 
342
int _mi_writeinfo(register MI_INFO *info, uint operation)
 
343
{
 
344
  int error,olderror;
 
345
  MYISAM_SHARE *share;
 
346
  DBUG_ENTER("_mi_writeinfo");
 
347
 
 
348
  error=0;
 
349
  share=info->s;
 
350
  if (share->r_locks == 0 && share->w_locks == 0)
 
351
  {
 
352
    olderror=my_errno;                  /* Remember last error */
 
353
    if (operation)
 
354
    {                                   /* Two threads can't be here */
 
355
      share->state.process= share->last_process=   share->this_process;
 
356
      share->state.unique=  info->last_unique=     info->this_unique;
 
357
      if ((error=mi_state_info_write(share->kfile, &share->state, 1)))
 
358
        olderror=my_errno;
 
359
#ifdef __WIN__
 
360
      if (myisam_flush)
 
361
      {
 
362
        _commit(share->kfile);
 
363
        _commit(info->dfile);
 
364
      }
 
365
#endif
 
366
    }
 
367
    if (!(operation & WRITEINFO_NO_UNLOCK) &&
 
368
        my_lock(share->kfile,F_UNLCK,0L,F_TO_EOF,
 
369
                MYF(MY_WME | MY_SEEK_NOT_DONE)) && !error)
 
370
      DBUG_RETURN(1);
 
371
    my_errno=olderror;
 
372
  }
 
373
  else if (operation)
 
374
  {
 
375
    share->changed= 1;                  /* Mark keyfile changed */
 
376
  }
 
377
    DBUG_RETURN(error);
 
378
} /* _mi_writeinfo */
 
379
 
 
380
 
 
381
        /* Test if someone has changed the database */
 
382
        /* (Should be called after readinfo) */
 
383
 
 
384
int _mi_test_if_changed(register MI_INFO *info)
 
385
{
 
386
  MYISAM_SHARE *share=info->s;
 
387
  if (share->state.process != share->last_process ||
 
388
      share->state.unique  != info->last_unique)
 
389
  {                                             /* Keyfile has changed */
 
390
    if (share->state.process != share->this_process)
 
391
      VOID(flush_key_blocks(share->kfile,FLUSH_RELEASE));
 
392
    share->last_process=share->state.process;
 
393
    info->last_unique=  share->state.unique;
 
394
    info->update|=      HA_STATE_WRITTEN;       /* Must use file on next */
 
395
    info->data_changed= 1;                      /* For mi_is_changed */
 
396
    return 1;
 
397
  }
 
398
  return (!(info->update & HA_STATE_AKTIV) ||
 
399
          (info->update & (HA_STATE_WRITTEN | HA_STATE_DELETED |
 
400
                           HA_STATE_KEY_CHANGED)));
 
401
} /* _mi_test_if_changed */
 
402
 
 
403
 
 
404
/* Put a mark in the .ISM file that someone is updating the table */
 
405
 
 
406
int _mi_mark_file_changed(MI_INFO *info)
 
407
{
 
408
  char buff[3];
 
409
  register MYISAM_SHARE *share=info->s;
 
410
  if (!share->state.changed || ! share->global_changed)
 
411
  {
 
412
    share->state.changed|=1;
 
413
    if (!share->global_changed)
 
414
    {
 
415
      share->global_changed=1;
 
416
      share->state.open_count++;
 
417
    }
 
418
    mi_int2store(buff,share->state.open_count);
 
419
    buff[2]=1;                                  /* Mark that it's changed */
 
420
    return (my_pwrite(share->kfile,buff,sizeof(buff),
 
421
                      sizeof(share->state.header),
 
422
                      MYF(MY_NABP)));
 
423
  }
 
424
  return 0;
 
425
}
 
426
 
 
427
 
 
428
/*
 
429
  This is only called by close or by extra(HA_FLUSH) if the OS has the pwrite()
 
430
  call.  In these context the following code should be safe!
 
431
 */
 
432
 
 
433
int _mi_decrement_open_count(MI_INFO *info)
 
434
{
 
435
  char buff[2];
 
436
  register MYISAM_SHARE *share=info->s;
 
437
  int lock_error=0,write_error=0;
 
438
  if (share->global_changed)
 
439
  {
 
440
    uint old_lock=info->lock_type;
 
441
    share->global_changed=0;
 
442
    lock_error=mi_lock_database(info,F_WRLCK);
 
443
    /* Its not fatal even if we couldn't get the lock ! */
 
444
    if (share->state.open_count > 0)
 
445
    {
 
446
      share->state.open_count--;
 
447
      mi_int2store(buff,share->state.open_count);
 
448
      write_error=my_pwrite(share->kfile,buff,sizeof(buff),
 
449
                            sizeof(share->state.header),
 
450
                            MYF(MY_NABP));
 
451
    }
 
452
    if (!lock_error)
 
453
      lock_error=mi_lock_database(info,old_lock);
 
454
  }
 
455
  return test(lock_error || write_error);
 
456
}