~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to mysys/thr_lock.c

  • Committer: Monty Taylor
  • Date: 2008-07-05 11:20:18 UTC
  • mto: This revision was merged to the branch mainline in revision 62.
  • Revision ID: monty@inaugust.com-20080705112018-fr12kkmgphtu7m29
Changes so that removal of duplicate curr_dir from my_sys.h work.

Show diffs side-by-side

added added

removed removed

Lines of Context:
70
70
 
71
71
*/
72
72
 
 
73
#if !defined(MAIN) && !defined(DBUG_OFF) && !defined(EXTRA_DEBUG)
 
74
#define FORCE_DBUG_OFF
 
75
#endif
 
76
 
73
77
#include "mysys_priv.h"
74
78
 
75
79
#include "thr_lock.h"
76
 
#include <mystrings/m_string.h>
 
80
#include <m_string.h>
77
81
#include <errno.h>
78
82
 
79
 
bool thr_lock_inited=0;
80
 
uint32_t locks_immediate = 0L, locks_waited = 0L;
 
83
my_bool thr_lock_inited=0;
 
84
ulong locks_immediate = 0L, locks_waited = 0L;
81
85
ulong table_lock_wait_timeout;
82
86
enum thr_lock_type thr_upgraded_concurrent_insert_lock = TL_WRITE;
83
87
 
98
102
** For the future (now the thread specific cond is alloced by my_pthread.c)
99
103
*/
100
104
 
101
 
bool init_thr_lock()
 
105
my_bool init_thr_lock()
102
106
{
103
107
  thr_lock_inited=1;
104
108
  return 0;
105
109
}
106
110
 
107
 
static inline bool
 
111
static inline my_bool
108
112
thr_lock_owner_equal(THR_LOCK_OWNER *rhs, THR_LOCK_OWNER *lhs)
109
113
{
110
114
  return rhs == lhs;
111
115
}
112
116
 
 
117
 
113
118
#ifdef EXTRA_DEBUG
114
119
#define MAX_FOUND_ERRORS        10              /* Report 10 first errors */
115
 
static uint32_t found_errors=0;
 
120
static uint found_errors=0;
116
121
 
117
122
static int check_lock(struct st_lock_list *list, const char* lock_type,
118
 
                      const char *where, bool same_owner, bool no_cond)
 
123
                      const char *where, my_bool same_owner, my_bool no_cond)
119
124
{
120
125
  THR_LOCK_DATA *data,**prev;
121
 
  uint32_t count=0;
 
126
  uint count=0;
122
127
  THR_LOCK_OWNER *first_owner;
123
128
 
124
129
  prev= &list->data;
175
180
 
176
181
 
177
182
static void check_locks(THR_LOCK *lock, const char *where,
178
 
                        bool allow_no_locks)
 
183
                        my_bool allow_no_locks)
179
184
{
180
 
  uint32_t old_found_errors=found_errors;
 
185
  uint old_found_errors=found_errors;
 
186
  DBUG_ENTER("check_locks");
181
187
 
182
188
  if (found_errors < MAX_FOUND_ERRORS)
183
189
  {
189
195
 
190
196
    if (found_errors < MAX_FOUND_ERRORS)
191
197
    {
192
 
      uint32_t count=0;
 
198
      uint count=0;
193
199
      THR_LOCK_DATA *data;
194
200
      for (data=lock->read.data ; data ; data=data->next)
195
201
      {
196
202
        if ((int) data->type == (int) TL_READ_NO_INSERT)
197
203
          count++;
198
204
        /* Protect against infinite loop. */
199
 
        assert(count <= lock->read_no_write_count);
 
205
        DBUG_ASSERT(count <= lock->read_no_write_count);
200
206
      }
201
207
      if (count != lock->read_no_write_count)
202
208
      {
269
275
            fprintf(stderr,
270
276
                    "Warning at '%s': Found lock of type %d that is write and read locked\n",
271
277
                    where, lock->write.data->type);
 
278
            DBUG_PRINT("warning",("At '%s': Found lock of type %d that is write and read locked\n",
 
279
                    where, lock->write.data->type));
 
280
 
272
281
          }
273
282
        }
274
283
        if (lock->read_wait.data)
286
295
        }
287
296
      }
288
297
    }
 
298
    if (found_errors != old_found_errors)
 
299
    {
 
300
      DBUG_PRINT("error",("Found wrong lock"));
 
301
    }
289
302
  }
290
 
  return;
 
303
  DBUG_VOID_RETURN;
291
304
}
292
305
 
293
306
#else /* EXTRA_DEBUG */
299
312
 
300
313
void thr_lock_init(THR_LOCK *lock)
301
314
{
302
 
  memset(lock, 0, sizeof(*lock));
303
 
  pthread_mutex_init(&lock->mutex,MY_MUTEX_INIT_FAST);
 
315
  DBUG_ENTER("thr_lock_init");
 
316
  bzero((char*) lock,sizeof(*lock));
 
317
  VOID(pthread_mutex_init(&lock->mutex,MY_MUTEX_INIT_FAST));
304
318
  lock->read.last= &lock->read.data;
305
319
  lock->read_wait.last= &lock->read_wait.data;
306
320
  lock->write_wait.last= &lock->write_wait.data;
310
324
  lock->list.data=(void*) lock;
311
325
  thr_lock_thread_list=list_add(thr_lock_thread_list,&lock->list);
312
326
  pthread_mutex_unlock(&THR_LOCK_lock);
313
 
  return;
 
327
  DBUG_VOID_RETURN;
314
328
}
315
329
 
316
330
 
317
331
void thr_lock_delete(THR_LOCK *lock)
318
332
{
319
 
  pthread_mutex_destroy(&lock->mutex);
 
333
  DBUG_ENTER("thr_lock_delete");
 
334
  VOID(pthread_mutex_destroy(&lock->mutex));
320
335
  pthread_mutex_lock(&THR_LOCK_lock);
321
336
  thr_lock_thread_list=list_delete(thr_lock_thread_list,&lock->list);
322
337
  pthread_mutex_unlock(&THR_LOCK_lock);
323
 
  return;
 
338
  DBUG_VOID_RETURN;
324
339
}
325
340
 
326
341
 
344
359
}
345
360
 
346
361
 
347
 
static inline bool
 
362
static inline my_bool
348
363
have_old_read_lock(THR_LOCK_DATA *data, THR_LOCK_OWNER *owner)
349
364
{
350
365
  for ( ; data ; data=data->next)
355
370
  return 0;
356
371
}
357
372
 
358
 
static inline bool have_specific_lock(THR_LOCK_DATA *data,
 
373
static inline my_bool have_specific_lock(THR_LOCK_DATA *data,
359
374
                                         enum thr_lock_type type)
360
375
{
361
376
  for ( ; data ; data=data->next)
372
387
 
373
388
static enum enum_thr_lock_result
374
389
wait_for_lock(struct st_lock_list *wait, THR_LOCK_DATA *data,
375
 
              bool in_wait_list)
 
390
              my_bool in_wait_list)
376
391
{
377
392
  struct st_my_thread_var *thread_var= my_thread_var;
378
393
  pthread_cond_t *cond= &thread_var->suspend;
379
394
  struct timespec wait_timeout;
380
395
  enum enum_thr_lock_result result= THR_LOCK_ABORTED;
381
 
  bool can_deadlock= test(data->owner->info->n_cursors);
 
396
  my_bool can_deadlock= test(data->owner->info->n_cursors);
 
397
  DBUG_ENTER("wait_for_lock");
382
398
 
383
399
  if (!in_wait_list)
384
400
  {
417
433
    */
418
434
    if (data->cond == 0)
419
435
    {
 
436
      DBUG_PRINT("thr_lock", ("lock granted/aborted"));
420
437
      break;
421
438
    }
422
439
    if (rc == ETIMEDOUT || rc == ETIME)
423
440
    {
424
441
      /* purecov: begin inspected */
 
442
      DBUG_PRINT("thr_lock", ("lock timed out"));
425
443
      result= THR_LOCK_WAIT_TIMEOUT;
426
444
      break;
427
445
      /* purecov: end */
428
446
    }
429
447
  }
 
448
  DBUG_PRINT("thr_lock", ("aborted: %d  in_wait_list: %d",
 
449
                          thread_var->abort, in_wait_list));
 
450
 
430
451
  if (data->cond || data->type == TL_UNLOCK)
431
452
  {
432
453
    if (data->cond)                             /* aborted or timed out */
441
462
    }
442
463
    else
443
464
    {
 
465
      DBUG_PRINT("thr_lock", ("lock aborted"));
444
466
      check_locks(data->lock, "aborted wait_for_lock", 0);
445
467
    }
446
468
  }
458
480
  thread_var->current_mutex= 0;
459
481
  thread_var->current_cond=  0;
460
482
  pthread_mutex_unlock(&thread_var->mutex);
461
 
  return(result);
 
483
  DBUG_RETURN(result);
462
484
}
463
485
 
464
486
 
470
492
  enum enum_thr_lock_result result= THR_LOCK_SUCCESS;
471
493
  struct st_lock_list *wait_queue;
472
494
  THR_LOCK_DATA *lock_owner;
 
495
  DBUG_ENTER("thr_lock");
473
496
 
474
497
  data->next=0;
475
498
  data->cond=0;                                 /* safety */
476
499
  data->type=lock_type;
477
500
  data->owner= owner;                           /* Must be reset ! */
478
 
  pthread_mutex_lock(&lock->mutex);
 
501
  VOID(pthread_mutex_lock(&lock->mutex));
 
502
  DBUG_PRINT("lock",("data: 0x%lx  thread: 0x%lx  lock: 0x%lx  type: %d",
 
503
                     (long) data, data->owner->info->thread_id,
 
504
                     (long) lock, (int) lock_type));
479
505
  check_locks(lock,(uint) lock_type <= (uint) TL_READ_NO_INSERT ?
480
506
              "enter read_lock" : "enter write_lock",0);
481
507
  if ((int) lock_type <= (int) TL_READ_NO_INSERT)
492
518
           and the read lock is not TL_READ_NO_INSERT
493
519
      */
494
520
 
 
521
      DBUG_PRINT("lock",("write locked 1 by thread: 0x%lx",
 
522
                         lock->write.data->owner->info->thread_id));
495
523
      if (thr_lock_owner_equal(data->owner, lock->write.data->owner) ||
496
524
          (lock->write.data->type <= TL_WRITE_DELAYED &&
497
525
           (((int) lock_type <= (int) TL_READ_HIGH_PRIORITY) ||
603
631
          We have already got a write lock or all locks are
604
632
          TL_WRITE_ALLOW_WRITE
605
633
        */
 
634
        DBUG_PRINT("info", ("write_wait.data: 0x%lx  old_type: %d",
 
635
                            (ulong) lock->write_wait.data,
 
636
                            lock->write.data->type));
606
637
 
607
638
        (*lock->write.last)=data;       /* Add to running fifo */
608
639
        data->prev=lock->write.last;
613
644
        statistic_increment(locks_immediate,&THR_LOCK_lock);
614
645
        goto end;
615
646
      }
 
647
      DBUG_PRINT("lock",("write locked 2 by thread: 0x%lx",
 
648
                         lock->write.data->owner->info->thread_id));
616
649
    }
617
650
    else
618
651
    {
 
652
      DBUG_PRINT("info", ("write_wait.data: 0x%lx",
 
653
                          (ulong) lock->write_wait.data));
619
654
      if (!lock->write_wait.data)
620
655
      {                                         /* no scheduled write locks */
621
 
        bool concurrent_insert= 0;
 
656
        my_bool concurrent_insert= 0;
622
657
        if (lock_type == TL_WRITE_CONCURRENT_INSERT)
623
658
        {
624
659
          concurrent_insert= 1;
645
680
          goto end;
646
681
        }
647
682
      }
 
683
      DBUG_PRINT("lock",("write locked 3 by thread: 0x%lx  type: %d",
 
684
                         lock->read.data->owner->info->thread_id, data->type));
648
685
    }
649
686
    wait_queue= &lock->write_wait;
650
687
  }
657
694
  lock_owner= lock->read.data ? lock->read.data : lock->write.data;
658
695
  if (lock_owner && lock_owner->owner->info == owner->info)
659
696
  {
 
697
    DBUG_PRINT("lock",("deadlock"));
660
698
    result= THR_LOCK_DEADLOCK;
661
699
    goto end;
662
700
  }
663
701
  /* Can't get lock yet;  Wait for it */
664
 
  return(wait_for_lock(wait_queue, data, 0));
 
702
  DBUG_RETURN(wait_for_lock(wait_queue, data, 0));
665
703
end:
666
704
  pthread_mutex_unlock(&lock->mutex);
667
 
  return(result);
 
705
  DBUG_RETURN(result);
668
706
}
669
707
 
670
708
 
671
709
static inline void free_all_read_locks(THR_LOCK *lock,
672
 
                                       bool using_concurrent_insert)
 
710
                                       my_bool using_concurrent_insert)
673
711
{
674
712
  THR_LOCK_DATA *data=lock->read_wait.data;
675
713
 
705
743
      }
706
744
      lock->read_no_write_count++;
707
745
    }      
 
746
    /* purecov: begin inspected */
 
747
    DBUG_PRINT("lock",("giving read lock to thread: 0x%lx",
 
748
                       data->owner->info->thread_id));
 
749
    /* purecov: end */
708
750
    data->cond=0;                               /* Mark thread free */
709
 
    pthread_cond_signal(cond);
 
751
    VOID(pthread_cond_signal(cond));
710
752
  } while ((data=data->next));
711
753
  *lock->read_wait.last=0;
712
754
  if (!lock->read_wait.data)
720
762
{
721
763
  THR_LOCK *lock=data->lock;
722
764
  enum thr_lock_type lock_type=data->type;
 
765
  DBUG_ENTER("thr_unlock");
 
766
  DBUG_PRINT("lock",("data: 0x%lx  thread: 0x%lx  lock: 0x%lx",
 
767
                     (long) data, data->owner->info->thread_id, (long) lock));
723
768
  pthread_mutex_lock(&lock->mutex);
724
769
  check_locks(lock,"start of release lock",0);
725
770
 
753
798
  check_locks(lock,"after releasing lock",1);
754
799
  wake_up_waiters(lock);
755
800
  pthread_mutex_unlock(&lock->mutex);
756
 
  return;
 
801
  DBUG_VOID_RETURN;
757
802
}
758
803
 
759
804
 
770
815
  THR_LOCK_DATA *data;
771
816
  enum thr_lock_type lock_type;
772
817
 
 
818
  DBUG_ENTER("wake_up_waiters");
 
819
 
773
820
  if (!lock->write.data)                        /* If no active write locks */
774
821
  {
775
822
    data=lock->write_wait.data;
786
833
          lock->write_lock_count=0;
787
834
          if (lock->read_wait.data)
788
835
          {
 
836
            DBUG_PRINT("info",("Freeing all read_locks because of max_write_lock_count"));
789
837
            free_all_read_locks(lock,0);
790
838
            goto end;
791
839
          }
803
851
          if (data->type == TL_WRITE_CONCURRENT_INSERT &&
804
852
              (*lock->check_status)(data->status_param))
805
853
            data->type=TL_WRITE;                        /* Upgrade lock */
 
854
          /* purecov: begin inspected */
 
855
          DBUG_PRINT("lock",("giving write lock of type %d to thread: 0x%lx",
 
856
                             data->type, data->owner->info->thread_id));
 
857
          /* purecov: end */
806
858
          {
807
859
            pthread_cond_t *cond=data->cond;
808
860
            data->cond=0;                               /* Mark thread free */
809
 
            pthread_cond_signal(cond);  /* Start waiting thread */
 
861
            VOID(pthread_cond_signal(cond));    /* Start waiting thread */
810
862
          }
811
863
          if (data->type != TL_WRITE_ALLOW_WRITE ||
812
864
              !lock->write_wait.data ||
823
875
                            data &&
824
876
                            (data->type == TL_WRITE_CONCURRENT_INSERT ||
825
877
                             data->type == TL_WRITE_ALLOW_WRITE));
 
878
      else
 
879
      {
 
880
        DBUG_PRINT("lock",("No waiting read locks to free"));
 
881
      }
826
882
    }
827
883
    else if (data &&
828
884
             (lock_type=data->type) <= TL_WRITE_DELAYED &&
853
909
        lock->write.last= &data->next;
854
910
        data->next=0;                           /* Only one write lock */
855
911
        data->cond=0;                           /* Mark thread free */
856
 
        pthread_cond_signal(cond);      /* Start waiting thread */
 
912
        VOID(pthread_cond_signal(cond));        /* Start waiting thread */
857
913
      } while (lock_type == TL_WRITE_ALLOW_WRITE &&
858
914
               (data=lock->write_wait.data) &&
859
915
               data->type == TL_WRITE_ALLOW_WRITE);
867
923
  }
868
924
end:
869
925
  check_locks(lock, "after waking up waiters", 0);
870
 
  return;
 
926
  DBUG_VOID_RETURN;
871
927
}
872
928
 
873
929
 
878
934
*/
879
935
 
880
936
 
881
 
#define LOCK_CMP(A,B) ((unsigned char*) (A->lock) - (uint) ((A)->type) < (unsigned char*) (B->lock)- (uint) ((B)->type))
 
937
#define LOCK_CMP(A,B) ((uchar*) (A->lock) - (uint) ((A)->type) < (uchar*) (B->lock)- (uint) ((B)->type))
882
938
 
883
 
static void sort_locks(THR_LOCK_DATA **data,uint32_t count)
 
939
static void sort_locks(THR_LOCK_DATA **data,uint count)
884
940
{
885
941
  THR_LOCK_DATA **pos,**end,**prev,*tmp;
886
942
 
902
958
 
903
959
 
904
960
enum enum_thr_lock_result
905
 
thr_multi_lock(THR_LOCK_DATA **data, uint32_t count, THR_LOCK_OWNER *owner)
 
961
thr_multi_lock(THR_LOCK_DATA **data, uint count, THR_LOCK_OWNER *owner)
906
962
{
907
963
  THR_LOCK_DATA **pos,**end;
 
964
  DBUG_ENTER("thr_multi_lock");
 
965
  DBUG_PRINT("lock",("data: 0x%lx  count: %d", (long) data, count));
908
966
  if (count > 1)
909
967
    sort_locks(data,count);
910
968
  /* lock everything */
914
972
    if (result != THR_LOCK_SUCCESS)
915
973
    {                                           /* Aborted */
916
974
      thr_multi_unlock(data,(uint) (pos-data));
917
 
      return(result);
 
975
      DBUG_RETURN(result);
918
976
    }
919
977
#ifdef MAIN
920
978
    printf("Thread: %s  Got lock: 0x%lx  type: %d\n",my_thread_name(),
968
1026
    } while (pos != data);
969
1027
  }
970
1028
#endif
971
 
  return(THR_LOCK_SUCCESS);
 
1029
  DBUG_RETURN(THR_LOCK_SUCCESS);
972
1030
}
973
1031
 
974
1032
  /* free all locks */
975
1033
 
976
 
void thr_multi_unlock(THR_LOCK_DATA **data,uint32_t count)
 
1034
void thr_multi_unlock(THR_LOCK_DATA **data,uint count)
977
1035
{
978
1036
  THR_LOCK_DATA **pos,**end;
 
1037
  DBUG_ENTER("thr_multi_unlock");
 
1038
  DBUG_PRINT("lock",("data: 0x%lx  count: %d", (long) data, count));
979
1039
 
980
1040
  for (pos=data,end=data+count; pos < end ; pos++)
981
1041
  {
986
1046
#endif
987
1047
    if ((*pos)->type != TL_UNLOCK)
988
1048
      thr_unlock(*pos);
 
1049
    else
 
1050
    {
 
1051
      DBUG_PRINT("lock",("Free lock: data: 0x%lx  thread: 0x%lx  lock: 0x%lx",
 
1052
                         (long) *pos, (*pos)->owner->info->thread_id,
 
1053
                         (long) (*pos)->lock));
 
1054
    }
989
1055
  }
990
 
  return;
 
1056
  DBUG_VOID_RETURN;
991
1057
}
992
1058
 
993
1059
/*
995
1061
  TL_WRITE_ONLY to abort any new accesses to the lock
996
1062
*/
997
1063
 
998
 
void thr_abort_locks(THR_LOCK *lock, bool upgrade_lock)
 
1064
void thr_abort_locks(THR_LOCK *lock, my_bool upgrade_lock)
999
1065
{
1000
1066
  THR_LOCK_DATA *data;
 
1067
  DBUG_ENTER("thr_abort_locks");
1001
1068
  pthread_mutex_lock(&lock->mutex);
1002
1069
 
1003
1070
  for (data=lock->read_wait.data; data ; data=data->next)
1019
1086
  if (upgrade_lock && lock->write.data)
1020
1087
    lock->write.data->type=TL_WRITE_ONLY;
1021
1088
  pthread_mutex_unlock(&lock->mutex);
1022
 
  return;
 
1089
  DBUG_VOID_RETURN;
1023
1090
}
1024
1091
 
1025
1092
 
1029
1096
  This is used to abort all locks for a specific thread
1030
1097
*/
1031
1098
 
1032
 
bool thr_abort_locks_for_thread(THR_LOCK *lock, my_thread_id thread_id)
 
1099
my_bool thr_abort_locks_for_thread(THR_LOCK *lock, my_thread_id thread_id)
1033
1100
{
1034
1101
  THR_LOCK_DATA *data;
1035
 
  bool found= false;
 
1102
  my_bool found= FALSE;
 
1103
  DBUG_ENTER("thr_abort_locks_for_thread");
1036
1104
 
1037
1105
  pthread_mutex_lock(&lock->mutex);
1038
1106
  for (data= lock->read_wait.data; data ; data= data->next)
1039
1107
  {
1040
1108
    if (data->owner->info->thread_id == thread_id)    /* purecov: tested */
1041
1109
    {
 
1110
      DBUG_PRINT("info",("Aborting read-wait lock"));
1042
1111
      data->type= TL_UNLOCK;                    /* Mark killed */
1043
1112
      /* It's safe to signal the cond first: we're still holding the mutex. */
1044
 
      found= true;
 
1113
      found= TRUE;
1045
1114
      pthread_cond_signal(data->cond);
1046
1115
      data->cond= 0;                            /* Removed from list */
1047
1116
 
1055
1124
  {
1056
1125
    if (data->owner->info->thread_id == thread_id) /* purecov: tested */
1057
1126
    {
 
1127
      DBUG_PRINT("info",("Aborting write-wait lock"));
1058
1128
      data->type= TL_UNLOCK;
1059
 
      found= true;
 
1129
      found= TRUE;
1060
1130
      pthread_cond_signal(data->cond);
1061
1131
      data->cond= 0;
1062
1132
 
1068
1138
  }
1069
1139
  wake_up_waiters(lock);
1070
1140
  pthread_mutex_unlock(&lock->mutex);
1071
 
  return(found);
 
1141
  DBUG_RETURN(found);
1072
1142
}
1073
1143
 
1074
1144
 
1106
1176
                              enum thr_lock_type new_lock_type)
1107
1177
{
1108
1178
  THR_LOCK *lock=in_data->lock;
 
1179
#ifndef DBUG_OFF
 
1180
  enum thr_lock_type old_lock_type= in_data->type;
 
1181
#endif
 
1182
  DBUG_ENTER("thr_downgrade_write_only_lock");
1109
1183
 
1110
1184
  pthread_mutex_lock(&lock->mutex);
 
1185
  DBUG_ASSERT(old_lock_type == TL_WRITE_ONLY);
 
1186
  DBUG_ASSERT(old_lock_type > new_lock_type);
1111
1187
  in_data->type= new_lock_type;
1112
1188
  check_locks(lock,"after downgrading lock",0);
1113
1189
 
1114
1190
  pthread_mutex_unlock(&lock->mutex);
1115
 
  return;
 
1191
  DBUG_VOID_RETURN;
1116
1192
}
1117
1193
 
1118
1194
/* Upgrade a WRITE_DELAY lock to a WRITE_LOCK */
1119
1195
 
1120
 
bool thr_upgrade_write_delay_lock(THR_LOCK_DATA *data)
 
1196
my_bool thr_upgrade_write_delay_lock(THR_LOCK_DATA *data)
1121
1197
{
1122
1198
  THR_LOCK *lock=data->lock;
 
1199
  DBUG_ENTER("thr_upgrade_write_delay_lock");
1123
1200
 
1124
1201
  pthread_mutex_lock(&lock->mutex);
1125
1202
  if (data->type == TL_UNLOCK || data->type >= TL_WRITE_LOW_PRIORITY)
1126
1203
  {
1127
1204
    pthread_mutex_unlock(&lock->mutex);
1128
 
    return(data->type == TL_UNLOCK);    /* Test if Aborted */
 
1205
    DBUG_RETURN(data->type == TL_UNLOCK);       /* Test if Aborted */
1129
1206
  }
1130
1207
  check_locks(lock,"before upgrading lock",0);
1131
1208
  /* TODO:  Upgrade to TL_WRITE_CONCURRENT_INSERT in some cases */
1139
1216
      if (data->lock->get_status)
1140
1217
        (*data->lock->get_status)(data->status_param, 0);
1141
1218
      pthread_mutex_unlock(&lock->mutex);
1142
 
      return(0);
 
1219
      DBUG_RETURN(0);
1143
1220
    }
1144
1221
 
1145
1222
    if (((*data->prev)=data->next))             /* remove from lock-list */
1159
1236
  {
1160
1237
    check_locks(lock,"waiting for lock",0);
1161
1238
  }
1162
 
  return(wait_for_lock(&lock->write_wait,data,1));
 
1239
  DBUG_RETURN(wait_for_lock(&lock->write_wait,data,1));
1163
1240
}
1164
1241
 
1165
1242
 
1166
1243
/* downgrade a WRITE lock to a WRITE_DELAY lock if there is pending locks */
1167
1244
 
1168
 
bool thr_reschedule_write_lock(THR_LOCK_DATA *data)
 
1245
my_bool thr_reschedule_write_lock(THR_LOCK_DATA *data)
1169
1246
{
1170
1247
  THR_LOCK *lock=data->lock;
 
1248
  DBUG_ENTER("thr_reschedule_write_lock");
1171
1249
 
1172
1250
  pthread_mutex_lock(&lock->mutex);
1173
1251
  if (!lock->read_wait.data)                    /* No waiting read locks */
1174
1252
  {
1175
1253
    pthread_mutex_unlock(&lock->mutex);
1176
 
    return(0);
 
1254
    DBUG_RETURN(0);
1177
1255
  }
1178
1256
 
1179
1257
  data->type=TL_WRITE_DELAYED;
1194
1272
  free_all_read_locks(lock,0);
1195
1273
 
1196
1274
  pthread_mutex_unlock(&lock->mutex);
1197
 
  return(thr_upgrade_write_delay_lock(data));
 
1275
  DBUG_RETURN(thr_upgrade_write_delay_lock(data));
1198
1276
}
1199
1277
 
1200
1278
 
1201
1279
#include <my_sys.h>
1202
1280
 
 
1281
static void thr_print_lock(const char* name,struct st_lock_list *list)
 
1282
{
 
1283
  THR_LOCK_DATA *data,**prev;
 
1284
  uint count=0;
 
1285
 
 
1286
  if (list->data)
 
1287
  {
 
1288
    printf("%-10s: ",name);
 
1289
    prev= &list->data;
 
1290
    for (data=list->data; data && count++ < MAX_LOCKS ; data=data->next)
 
1291
    {
 
1292
      printf("0x%lx (%lu:%d); ", (ulong) data, data->owner->info->thread_id,
 
1293
             (int) data->type);
 
1294
      if (data->prev != prev)
 
1295
        printf("\nWarning: prev didn't point at previous lock\n");
 
1296
      prev= &data->next;
 
1297
    }
 
1298
    puts("");
 
1299
    if (prev != list->last)
 
1300
      printf("Warning: last didn't point at last lock\n");
 
1301
  }
 
1302
}
 
1303
 
 
1304
void thr_print_locks(void)
 
1305
{
 
1306
  LIST *list;
 
1307
  uint count=0;
 
1308
 
 
1309
  pthread_mutex_lock(&THR_LOCK_lock);
 
1310
  puts("Current locks:");
 
1311
  for (list= thr_lock_thread_list; list && count++ < MAX_THREADS;
 
1312
       list= list_rest(list))
 
1313
  {
 
1314
    THR_LOCK *lock=(THR_LOCK*) list->data;
 
1315
    VOID(pthread_mutex_lock(&lock->mutex));
 
1316
    printf("lock: 0x%lx:",(ulong) lock);
 
1317
    if ((lock->write_wait.data || lock->read_wait.data) &&
 
1318
        (! lock->read.data && ! lock->write.data))
 
1319
      printf(" WARNING: ");
 
1320
    if (lock->write.data)
 
1321
      printf(" write");
 
1322
    if (lock->write_wait.data)
 
1323
      printf(" write_wait");
 
1324
    if (lock->read.data)
 
1325
      printf(" read");
 
1326
    if (lock->read_wait.data)
 
1327
      printf(" read_wait");
 
1328
    puts("");
 
1329
    thr_print_lock("write",&lock->write);
 
1330
    thr_print_lock("write_wait",&lock->write_wait);
 
1331
    thr_print_lock("read",&lock->read);
 
1332
    thr_print_lock("read_wait",&lock->read_wait);
 
1333
    VOID(pthread_mutex_unlock(&lock->mutex));
 
1334
    puts("");
 
1335
  }
 
1336
  fflush(stdout);
 
1337
  pthread_mutex_unlock(&THR_LOCK_lock);
 
1338
}
 
1339
 
1203
1340
/*****************************************************************************
1204
1341
** Test of thread locks
1205
1342
****************************************************************************/
1207
1344
#ifdef MAIN
1208
1345
 
1209
1346
struct st_test {
1210
 
  uint32_t lock_nr;
 
1347
  uint lock_nr;
1211
1348
  enum thr_lock_type lock_type;
1212
1349
};
1213
1350
 
1256
1393
 
1257
1394
static pthread_cond_t COND_thread_count;
1258
1395
static pthread_mutex_t LOCK_thread_count;
1259
 
static uint32_t thread_count;
1260
 
static uint32_t sum=0;
 
1396
static uint thread_count;
 
1397
static ulong sum=0;
1261
1398
 
1262
1399
#define MAX_LOCK_COUNT 8
1263
1400
 
1277
1414
{
1278
1415
}
1279
1416
 
1280
 
static bool test_check_status(void* param __attribute__((unused)))
 
1417
static my_bool test_check_status(void* param __attribute__((unused)))
1281
1418
{
1282
1419
  return 0;
1283
1420
}
1316
1453
        sleep(2);
1317
1454
      else
1318
1455
      {
1319
 
        uint32_t k;
1320
 
        for (k=0 ; k < (uint32_t) (tmp-2)*100000L ; k++)
 
1456
        ulong k;
 
1457
        for (k=0 ; k < (ulong) (tmp-2)*100000L ; k++)
1321
1458
          sum+=k;
1322
1459
      }
1323
1460
    }
1329
1466
  thr_print_locks();
1330
1467
  pthread_mutex_lock(&LOCK_thread_count);
1331
1468
  thread_count--;
1332
 
  pthread_cond_signal(&COND_thread_count); /* Tell main we are ready */
 
1469
  VOID(pthread_cond_signal(&COND_thread_count)); /* Tell main we are ready */
1333
1470
  pthread_mutex_unlock(&LOCK_thread_count);
1334
 
  free((unsigned char*) arg);
 
1471
  free((uchar*) arg);
1335
1472
  return 0;
1336
1473
}
1337
1474
 
1342
1479
  pthread_attr_t thr_attr;
1343
1480
  int i,*param,error;
1344
1481
  MY_INIT(argv[0]);
 
1482
  if (argc > 1 && argv[1][0] == '-' && argv[1][1] == '#')
 
1483
    DBUG_PUSH(argv[1]+2);
1345
1484
 
1346
1485
  printf("Main thread: %s\n",my_thread_name());
1347
1486
 
1388
1527
  }
1389
1528
#endif
1390
1529
#ifdef HAVE_THR_SETCONCURRENCY
1391
 
  thr_setconcurrency(2);
 
1530
  VOID(thr_setconcurrency(2));
1392
1531
#endif
1393
1532
  for (i=0 ; i < (int) array_elements(lock_counts) ; i++)
1394
1533
  {