~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to mysys/thr_lock.c

  • Committer: Monty Taylor
  • Date: 2008-08-04 19:37:18 UTC
  • mto: (261.2.2 codestyle)
  • mto: This revision was merged to the branch mainline in revision 262.
  • Revision ID: monty@inaugust.com-20080804193718-f0rz13uli4429ozb
Changed gettext_noop() to N_()

Show diffs side-by-side

added added

removed removed

Lines of Context:
76
76
#include <mystrings/m_string.h>
77
77
#include <errno.h>
78
78
 
79
 
#if TIME_WITH_SYS_TIME
80
 
# include <sys/time.h>
81
 
# include <time.h>
82
 
#else
83
 
# if HAVE_SYS_TIME_H
84
 
#  include <sys/time.h>
85
 
# else
86
 
#  include <time.h>
87
 
# endif
88
 
#endif  
89
 
 
90
 
#include <drizzled/util/test.h>
91
 
 
92
79
bool thr_lock_inited=0;
93
 
uint32_t locks_immediate = 0L, locks_waited = 0L;
 
80
ulong locks_immediate = 0L, locks_waited = 0L;
94
81
ulong table_lock_wait_timeout;
95
82
enum thr_lock_type thr_upgraded_concurrent_insert_lock = TL_WRITE;
96
83
 
 
84
/* The following constants are only for debug output */
 
85
#define MAX_THREADS 100
 
86
#define MAX_LOCKS   100
 
87
 
97
88
 
98
89
LIST *thr_lock_thread_list;                     /* List of threads in use */
99
90
ulong max_write_lock_count= ~(ulong) 0L;
120
111
}
121
112
 
122
113
#ifdef EXTRA_DEBUG
123
 
 
124
 
#define MAX_THREADS 100
125
 
#define MAX_LOCKS   100
126
114
#define MAX_FOUND_ERRORS        10              /* Report 10 first errors */
127
 
static uint32_t found_errors=0;
 
115
static uint found_errors=0;
128
116
 
129
117
static int check_lock(struct st_lock_list *list, const char* lock_type,
130
118
                      const char *where, bool same_owner, bool no_cond)
131
119
{
132
120
  THR_LOCK_DATA *data,**prev;
133
 
  uint32_t count=0;
 
121
  uint count=0;
134
122
  THR_LOCK_OWNER *first_owner;
135
123
 
136
124
  prev= &list->data;
189
177
static void check_locks(THR_LOCK *lock, const char *where,
190
178
                        bool allow_no_locks)
191
179
{
192
 
  uint32_t old_found_errors=found_errors;
 
180
  uint old_found_errors=found_errors;
193
181
 
194
182
  if (found_errors < MAX_FOUND_ERRORS)
195
183
  {
201
189
 
202
190
    if (found_errors < MAX_FOUND_ERRORS)
203
191
    {
204
 
      uint32_t count=0;
 
192
      uint count=0;
205
193
      THR_LOCK_DATA *data;
206
194
      for (data=lock->read.data ; data ; data=data->next)
207
195
      {
311
299
 
312
300
void thr_lock_init(THR_LOCK *lock)
313
301
{
314
 
  memset(lock, 0, sizeof(*lock));
315
 
  pthread_mutex_init(&lock->mutex,MY_MUTEX_INIT_FAST);
 
302
  memset((char*) lock, 0, sizeof(*lock));
 
303
  VOID(pthread_mutex_init(&lock->mutex,MY_MUTEX_INIT_FAST));
316
304
  lock->read.last= &lock->read.data;
317
305
  lock->read_wait.last= &lock->read_wait.data;
318
306
  lock->write_wait.last= &lock->write_wait.data;
328
316
 
329
317
void thr_lock_delete(THR_LOCK *lock)
330
318
{
331
 
  pthread_mutex_destroy(&lock->mutex);
 
319
  VOID(pthread_mutex_destroy(&lock->mutex));
332
320
  pthread_mutex_lock(&THR_LOCK_lock);
333
321
  thr_lock_thread_list=list_delete(thr_lock_thread_list,&lock->list);
334
322
  pthread_mutex_unlock(&THR_LOCK_lock);
487
475
  data->cond=0;                                 /* safety */
488
476
  data->type=lock_type;
489
477
  data->owner= owner;                           /* Must be reset ! */
490
 
  pthread_mutex_lock(&lock->mutex);
 
478
  VOID(pthread_mutex_lock(&lock->mutex));
491
479
  check_locks(lock,(uint) lock_type <= (uint) TL_READ_NO_INSERT ?
492
480
              "enter read_lock" : "enter write_lock",0);
493
481
  if ((int) lock_type <= (int) TL_READ_NO_INSERT)
718
706
      lock->read_no_write_count++;
719
707
    }      
720
708
    data->cond=0;                               /* Mark thread free */
721
 
    pthread_cond_signal(cond);
 
709
    VOID(pthread_cond_signal(cond));
722
710
  } while ((data=data->next));
723
711
  *lock->read_wait.last=0;
724
712
  if (!lock->read_wait.data)
818
806
          {
819
807
            pthread_cond_t *cond=data->cond;
820
808
            data->cond=0;                               /* Mark thread free */
821
 
            pthread_cond_signal(cond);  /* Start waiting thread */
 
809
            VOID(pthread_cond_signal(cond));    /* Start waiting thread */
822
810
          }
823
811
          if (data->type != TL_WRITE_ALLOW_WRITE ||
824
812
              !lock->write_wait.data ||
865
853
        lock->write.last= &data->next;
866
854
        data->next=0;                           /* Only one write lock */
867
855
        data->cond=0;                           /* Mark thread free */
868
 
        pthread_cond_signal(cond);      /* Start waiting thread */
 
856
        VOID(pthread_cond_signal(cond));        /* Start waiting thread */
869
857
      } while (lock_type == TL_WRITE_ALLOW_WRITE &&
870
858
               (data=lock->write_wait.data) &&
871
859
               data->type == TL_WRITE_ALLOW_WRITE);
890
878
*/
891
879
 
892
880
 
893
 
#define LOCK_CMP(A,B) ((unsigned char*) (A->lock) - (uint) ((A)->type) < (unsigned char*) (B->lock)- (uint) ((B)->type))
 
881
#define LOCK_CMP(A,B) ((uchar*) (A->lock) - (uint) ((A)->type) < (uchar*) (B->lock)- (uint) ((B)->type))
894
882
 
895
 
static void sort_locks(THR_LOCK_DATA **data,uint32_t count)
 
883
static void sort_locks(THR_LOCK_DATA **data,uint count)
896
884
{
897
885
  THR_LOCK_DATA **pos,**end,**prev,*tmp;
898
886
 
914
902
 
915
903
 
916
904
enum enum_thr_lock_result
917
 
thr_multi_lock(THR_LOCK_DATA **data, uint32_t count, THR_LOCK_OWNER *owner)
 
905
thr_multi_lock(THR_LOCK_DATA **data, uint count, THR_LOCK_OWNER *owner)
918
906
{
919
907
  THR_LOCK_DATA **pos,**end;
920
908
  if (count > 1)
985
973
 
986
974
  /* free all locks */
987
975
 
988
 
void thr_multi_unlock(THR_LOCK_DATA **data,uint32_t count)
 
976
void thr_multi_unlock(THR_LOCK_DATA **data,uint count)
989
977
{
990
978
  THR_LOCK_DATA **pos,**end;
991
979
 
1212
1200
 
1213
1201
#include <my_sys.h>
1214
1202
 
 
1203
static void thr_print_lock(const char* name,struct st_lock_list *list)
 
1204
{
 
1205
  THR_LOCK_DATA *data,**prev;
 
1206
  uint count=0;
 
1207
 
 
1208
  if (list->data)
 
1209
  {
 
1210
    printf("%-10s: ",name);
 
1211
    prev= &list->data;
 
1212
    for (data=list->data; data && count++ < MAX_LOCKS ; data=data->next)
 
1213
    {
 
1214
      printf("0x%lx (%lu:%d); ", (ulong) data, data->owner->info->thread_id,
 
1215
             (int) data->type);
 
1216
      if (data->prev != prev)
 
1217
        printf("\nWarning: prev didn't point at previous lock\n");
 
1218
      prev= &data->next;
 
1219
    }
 
1220
    puts("");
 
1221
    if (prev != list->last)
 
1222
      printf("Warning: last didn't point at last lock\n");
 
1223
  }
 
1224
}
 
1225
 
 
1226
void thr_print_locks(void)
 
1227
{
 
1228
  LIST *list;
 
1229
  uint count=0;
 
1230
 
 
1231
  pthread_mutex_lock(&THR_LOCK_lock);
 
1232
  puts("Current locks:");
 
1233
  for (list= thr_lock_thread_list; list && count++ < MAX_THREADS;
 
1234
       list= list_rest(list))
 
1235
  {
 
1236
    THR_LOCK *lock=(THR_LOCK*) list->data;
 
1237
    VOID(pthread_mutex_lock(&lock->mutex));
 
1238
    printf("lock: 0x%lx:",(ulong) lock);
 
1239
    if ((lock->write_wait.data || lock->read_wait.data) &&
 
1240
        (! lock->read.data && ! lock->write.data))
 
1241
      printf(" WARNING: ");
 
1242
    if (lock->write.data)
 
1243
      printf(" write");
 
1244
    if (lock->write_wait.data)
 
1245
      printf(" write_wait");
 
1246
    if (lock->read.data)
 
1247
      printf(" read");
 
1248
    if (lock->read_wait.data)
 
1249
      printf(" read_wait");
 
1250
    puts("");
 
1251
    thr_print_lock("write",&lock->write);
 
1252
    thr_print_lock("write_wait",&lock->write_wait);
 
1253
    thr_print_lock("read",&lock->read);
 
1254
    thr_print_lock("read_wait",&lock->read_wait);
 
1255
    VOID(pthread_mutex_unlock(&lock->mutex));
 
1256
    puts("");
 
1257
  }
 
1258
  fflush(stdout);
 
1259
  pthread_mutex_unlock(&THR_LOCK_lock);
 
1260
}
 
1261
 
1215
1262
/*****************************************************************************
1216
1263
** Test of thread locks
1217
1264
****************************************************************************/
1219
1266
#ifdef MAIN
1220
1267
 
1221
1268
struct st_test {
1222
 
  uint32_t lock_nr;
 
1269
  uint lock_nr;
1223
1270
  enum thr_lock_type lock_type;
1224
1271
};
1225
1272
 
1268
1315
 
1269
1316
static pthread_cond_t COND_thread_count;
1270
1317
static pthread_mutex_t LOCK_thread_count;
1271
 
static uint32_t thread_count;
1272
 
static uint32_t sum=0;
 
1318
static uint thread_count;
 
1319
static ulong sum=0;
1273
1320
 
1274
1321
#define MAX_LOCK_COUNT 8
1275
1322
 
1328
1375
        sleep(2);
1329
1376
      else
1330
1377
      {
1331
 
        uint32_t k;
1332
 
        for (k=0 ; k < (uint32_t) (tmp-2)*100000L ; k++)
 
1378
        ulong k;
 
1379
        for (k=0 ; k < (ulong) (tmp-2)*100000L ; k++)
1333
1380
          sum+=k;
1334
1381
      }
1335
1382
    }
1341
1388
  thr_print_locks();
1342
1389
  pthread_mutex_lock(&LOCK_thread_count);
1343
1390
  thread_count--;
1344
 
  pthread_cond_signal(&COND_thread_count); /* Tell main we are ready */
 
1391
  VOID(pthread_cond_signal(&COND_thread_count)); /* Tell main we are ready */
1345
1392
  pthread_mutex_unlock(&LOCK_thread_count);
1346
 
  free((unsigned char*) arg);
 
1393
  free((uchar*) arg);
1347
1394
  return 0;
1348
1395
}
1349
1396
 
1400
1447
  }
1401
1448
#endif
1402
1449
#ifdef HAVE_THR_SETCONCURRENCY
1403
 
  thr_setconcurrency(2);
 
1450
  VOID(thr_setconcurrency(2));
1404
1451
#endif
1405
1452
  for (i=0 ; i < (int) array_elements(lock_counts) ; i++)
1406
1453
  {