76
76
#include <mystrings/m_string.h>
79
#if TIME_WITH_SYS_TIME
80
# include <sys/time.h>
84
# include <sys/time.h>
90
#include <drizzled/util/test.h>
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;
84
/* The following constants are only for debug output */
85
#define MAX_THREADS 100
98
89
LIST *thr_lock_thread_list; /* List of threads in use */
99
90
ulong max_write_lock_count= ~(ulong) 0L;
122
113
#ifdef EXTRA_DEBUG
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;
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)
132
120
THR_LOCK_DATA *data,**prev;
134
122
THR_LOCK_OWNER *first_owner;
136
124
prev= &list->data;
189
177
static void check_locks(THR_LOCK *lock, const char *where,
190
178
bool allow_no_locks)
192
uint32_t old_found_errors=found_errors;
180
uint old_found_errors=found_errors;
194
182
if (found_errors < MAX_FOUND_ERRORS)
312
300
void thr_lock_init(THR_LOCK *lock)
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;
329
317
void thr_lock_delete(THR_LOCK *lock)
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++;
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)
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 */
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);
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))
895
static void sort_locks(THR_LOCK_DATA **data,uint32_t count)
883
static void sort_locks(THR_LOCK_DATA **data,uint count)
897
885
THR_LOCK_DATA **pos,**end,**prev,*tmp;
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)
919
907
THR_LOCK_DATA **pos,**end;
986
974
/* free all locks */
988
void thr_multi_unlock(THR_LOCK_DATA **data,uint32_t count)
976
void thr_multi_unlock(THR_LOCK_DATA **data,uint count)
990
978
THR_LOCK_DATA **pos,**end;
1213
1201
#include <my_sys.h>
1203
static void thr_print_lock(const char* name,struct st_lock_list *list)
1205
THR_LOCK_DATA *data,**prev;
1210
printf("%-10s: ",name);
1212
for (data=list->data; data && count++ < MAX_LOCKS ; data=data->next)
1214
printf("0x%lx (%lu:%d); ", (ulong) data, data->owner->info->thread_id,
1216
if (data->prev != prev)
1217
printf("\nWarning: prev didn't point at previous lock\n");
1221
if (prev != list->last)
1222
printf("Warning: last didn't point at last lock\n");
1226
void thr_print_locks(void)
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))
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)
1244
if (lock->write_wait.data)
1245
printf(" write_wait");
1246
if (lock->read.data)
1248
if (lock->read_wait.data)
1249
printf(" read_wait");
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));
1259
pthread_mutex_unlock(&THR_LOCK_lock);
1215
1262
/*****************************************************************************
1216
1263
** Test of thread locks
1217
1264
****************************************************************************/
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);