drizzle
C++ MySQL-protocol-compatible database server
Loading...
Searching...
No Matches
thr_lock.cc
1/* Copyright (C) 2000 MySQL 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; version 2 of the License.
6
7 This program is distributed in the hope that it will be useful,
8 but WITHOUT ANY WARRANTY; without even the implied warranty of
9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 GNU General Public License for more details.
11
12 You should have received a copy of the GNU General Public License
13 along with this program; if not, write to the Free Software
14 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
15
16/*
17Read and write locks for Posix threads. All tread must acquire
18all locks it needs through thr_multi_lock() to avoid dead-locks.
19A lock consists of a master lock (THR_LOCK), and lock instances
20(THR_LOCK_DATA).
21Any thread can have any number of lock instances (read and write:s) on
22any lock. All lock instances must be freed.
23Locks are prioritized according to:
24
25The current lock types are:
26
27TL_READ # Low priority read
28TL_READ_WITH_SHARED_LOCKS
29TL_READ_NO_INSERT # Read without concurrent inserts
30TL_WRITE_ALLOW_WRITE # Write lock that allows other writers
31TL_WRITE_ALLOW_READ # Write lock, but allow reading
32TL_WRITE_CONCURRENT_INSERT
33 # Insert that can be mixed when selects
34TL_WRITE # High priority write
35TL_WRITE_ONLY # High priority write
36 # Abort all new lock request with an error
37
38Locks are prioritized according to:
39
40WRITE_ALLOW_WRITE, WRITE_ALLOW_READ, WRITE_CONCURRENT_INSERT, WRITE_DELAYED,
41WRITE_LOW_PRIORITY, READ, WRITE, READ_HIGH_PRIORITY and WRITE_ONLY
42
43Locks in the same privilege level are scheduled in first-in-first-out order.
44
45To allow concurrent read/writes locks, with 'WRITE_CONCURRENT_INSERT' one
46should put a pointer to the following functions in the lock structure:
47(If the pointer is zero (default), the function is not called)
48
49
50The lock algorithm allows one to have one TL_WRITE_ALLOW_READ,
51TL_WRITE_CONCURRENT_INSERT lock at the same time as multiple read locks.
52
53*/
54
55#include <config.h>
56#include <drizzled/internal/my_sys.h>
57#include <drizzled/internal/thread_var.h>
58#include <drizzled/statistics_variables.h>
59#include <drizzled/pthread_globals.h>
60
61#include <drizzled/session.h>
62
63#include "thr_lock.h"
64#include <drizzled/internal/m_string.h>
65#include <errno.h>
66#include <list>
67#include <sys/time.h>
68#include <time.h>
69
70#include <drizzled/util/test.h>
71
72#include <boost/interprocess/sync/sharable_lock.hpp>
73#include <boost/version.hpp>
74
75using namespace std;
76
77namespace drizzled {
78
79uint64_t table_lock_wait_timeout;
80static enum thr_lock_type thr_upgraded_concurrent_insert_lock = TL_WRITE;
81
82
83uint64_t max_write_lock_count= UINT64_MAX;
84
85/*
86** For the future (now the thread specific cond is alloced by my_pthread.c)
87*/
88
89static inline bool
90thr_lock_owner_equal(THR_LOCK_OWNER *rhs, THR_LOCK_OWNER *lhs)
91{
92 return rhs == lhs;
93}
94
95
96 /* Initialize a lock */
97
98void thr_lock_init(THR_LOCK *lock)
99{
100 lock->read.last= &lock->read.data;
101 lock->read_wait.last= &lock->read_wait.data;
102 lock->write_wait.last= &lock->write_wait.data;
103 lock->write.last= &lock->write.data;
104}
105
106
107void THR_LOCK_INFO::init()
108{
109 thread_id= internal::my_thread_var2()->id;
110 n_cursors= 0;
111}
112
113 /* Initialize a lock instance */
114
115void THR_LOCK_DATA::init(THR_LOCK *lock_arg, void *param_arg)
116{
117 lock= lock_arg;
118 type= TL_UNLOCK;
119 owner= NULL; /* no owner yet */
120 status_param= param_arg;
121 cond= NULL;
122}
123
124
125static inline bool
126have_old_read_lock(THR_LOCK_DATA *data, THR_LOCK_OWNER *owner)
127{
128 for ( ; data ; data=data->next)
129 {
130 if (thr_lock_owner_equal(data->owner, owner))
131 return true; /* Already locked by thread */
132 }
133 return false;
134}
135
136static void wake_up_waiters(THR_LOCK *lock);
137
138
139static enum enum_thr_lock_result wait_for_lock(Session &session, struct st_lock_list *wait, THR_LOCK_DATA *data)
140{
141 boost::condition_variable_any *cond= &session.getThreadVar()->suspend;
142 enum enum_thr_lock_result result= THR_LOCK_ABORTED;
143 bool can_deadlock= test(data->owner->info->n_cursors);
144
145 {
146 (*wait->last)=data; /* Wait for lock */
147 data->prev= wait->last;
148 wait->last= &data->next;
149 }
150
151 current_global_counters.locks_waited++;
152
153 /* Set up control struct to allow others to abort locks */
154 session.getThreadVar()->current_mutex= data->lock->native_handle();
155 session.getThreadVar()->current_cond= &session.getThreadVar()->suspend;
156 data->cond= &session.getThreadVar()->suspend;;
157
158 while (not session.getThreadVar()->abort)
159 {
160 boost::mutex::scoped_lock scoped(*data->lock->native_handle(), boost::adopt_lock_t());
161
162 if (can_deadlock)
163 {
164 boost::xtime xt;
165#if BOOST_VERSION >= 105000
166 xtime_get(&xt, boost::TIME_UTC_);
167#else
168 xtime_get(&xt, boost::TIME_UTC);
169#endif
170 xt.sec += table_lock_wait_timeout;
171 if (not cond->timed_wait(scoped, xt))
172 {
173 result= THR_LOCK_WAIT_TIMEOUT;
174 scoped.release();
175 break;
176 }
177 }
178 else
179 {
180 cond->wait(scoped);
181 }
182 /*
183 We must break the wait if one of the following occurs:
184 - the connection has been aborted (!session.getThreadVar()->abort), but
185 this is not a delayed insert thread (in_wait_list). For a delayed
186 insert thread the proper action at shutdown is, apparently, to
187 acquire the lock and complete the insert.
188 - the lock has been granted (data->cond is set to NULL by the granter),
189 or the waiting has been aborted (additionally data->type is set to
190 TL_UNLOCK).
191 - the wait has timed out (rc == ETIMEDOUT)
192 Order of checks below is important to not report about timeout
193 if the predicate is true.
194 */
195 if (data->cond == NULL)
196 {
197 scoped.release();
198 break;
199 }
200 scoped.release();
201 }
202 if (data->cond || data->type == TL_UNLOCK)
203 {
204 if (data->cond) /* aborted or timed out */
205 {
206 if (((*data->prev)=data->next)) /* remove from wait-list */
207 {
208 data->next->prev= data->prev;
209 }
210 else
211 {
212 wait->last=data->prev;
213 }
214 data->type= TL_UNLOCK; /* No lock */
215 wake_up_waiters(data->lock);
216 }
217 }
218 else
219 {
220 result= THR_LOCK_SUCCESS;
221 }
222 data->lock->unlock();
223
224 /* The following must be done after unlock of lock->mutex */
225 boost::mutex::scoped_lock scopedLock(session.getThreadVar()->mutex);
226 session.getThreadVar()->current_mutex= NULL;
227 session.getThreadVar()->current_cond= NULL;
228
229 return(result);
230}
231
232
233static enum enum_thr_lock_result thr_lock(Session &session, THR_LOCK_DATA *data, THR_LOCK_OWNER *owner, enum thr_lock_type lock_type)
234{
235 THR_LOCK *lock= data->lock;
236 enum enum_thr_lock_result result= THR_LOCK_SUCCESS;
237 struct st_lock_list *wait_queue;
238 THR_LOCK_DATA *lock_owner;
239
240 data->next=0;
241 data->cond=0; /* safety */
242 data->type=lock_type;
243 data->owner= owner; /* Must be reset ! */
244 lock->lock();
245 if ((int) lock_type <= (int) TL_READ_NO_INSERT)
246 {
247 /* Request for READ lock */
248 if (lock->write.data)
249 {
250 /* We can allow a read lock even if there is already a write lock
251 on the table in one the following cases:
252 - This thread alread have a write lock on the table
253 - The write lock is TL_WRITE_ALLOW_READ or TL_WRITE_DELAYED
254 and the read lock is TL_READ_HIGH_PRIORITY or TL_READ
255 - The write lock is TL_WRITE_CONCURRENT_INSERT or TL_WRITE_ALLOW_WRITE
256 and the read lock is not TL_READ_NO_INSERT
257 */
258
259 if (thr_lock_owner_equal(data->owner, lock->write.data->owner) ||
260 (lock->write.data->type <= TL_WRITE_CONCURRENT_INSERT &&
261 (((int) lock_type <= (int) TL_READ_WITH_SHARED_LOCKS) ||
262 (lock->write.data->type != TL_WRITE_CONCURRENT_INSERT &&
263 lock->write.data->type != TL_WRITE_ALLOW_READ))))
264 { /* Already got a write lock */
265 (*lock->read.last)=data; /* Add to running FIFO */
266 data->prev=lock->read.last;
267 lock->read.last= &data->next;
268 if (lock_type == TL_READ_NO_INSERT)
269 lock->read_no_write_count++;
270 current_global_counters.locks_immediate++;
271 goto end;
272 }
273 if (lock->write.data->type == TL_WRITE_ONLY)
274 {
275 /* We are not allowed to get a READ lock in this case */
276 data->type=TL_UNLOCK;
277 result= THR_LOCK_ABORTED; /* Can't wait for this one */
278 goto end;
279 }
280 }
281 else if (!lock->write_wait.data ||
282 lock->write_wait.data->type <= TL_WRITE_DEFAULT ||
283 have_old_read_lock(lock->read.data, data->owner))
284 { /* No important write-locks */
285 (*lock->read.last)=data; /* Add to running FIFO */
286 data->prev=lock->read.last;
287 lock->read.last= &data->next;
288 if (lock_type == TL_READ_NO_INSERT)
289 lock->read_no_write_count++;
290 current_global_counters.locks_immediate++;
291 goto end;
292 }
293 /*
294 We're here if there is an active write lock or no write
295 lock but a high priority write waiting in the write_wait queue.
296 In the latter case we should yield the lock to the writer.
297 */
298 wait_queue= &lock->read_wait;
299 }
300 else /* Request for WRITE lock */
301 {
302 if (lock_type == TL_WRITE_CONCURRENT_INSERT)
303 data->type=lock_type= thr_upgraded_concurrent_insert_lock;
304
305 if (lock->write.data) /* If there is a write lock */
306 {
307 if (lock->write.data->type == TL_WRITE_ONLY)
308 {
309 /* Allow lock owner to bypass TL_WRITE_ONLY. */
310 if (!thr_lock_owner_equal(data->owner, lock->write.data->owner))
311 {
312 /* We are not allowed to get a lock in this case */
313 data->type=TL_UNLOCK;
314 result= THR_LOCK_ABORTED; /* Can't wait for this one */
315 goto end;
316 }
317 }
318
319 /*
320 The following test will not work if the old lock was a
321 TL_WRITE_ALLOW_WRITE, TL_WRITE_ALLOW_READ or TL_WRITE_DELAYED in
322 the same thread, but this will never happen within MySQL.
323 */
324 if (thr_lock_owner_equal(data->owner, lock->write.data->owner) ||
325 (lock_type == TL_WRITE_ALLOW_WRITE &&
326 !lock->write_wait.data &&
327 lock->write.data->type == TL_WRITE_ALLOW_WRITE))
328 {
329 /*
330 We have already got a write lock or all locks are
331 TL_WRITE_ALLOW_WRITE
332 */
333
334 (*lock->write.last)=data; /* Add to running fifo */
335 data->prev=lock->write.last;
336 lock->write.last= &data->next;
337 current_global_counters.locks_immediate++;
338 goto end;
339 }
340 }
341 else
342 {
343 if (!lock->write_wait.data)
344 { /* no scheduled write locks */
345 if (!lock->read.data ||
346 (lock_type <= TL_WRITE_CONCURRENT_INSERT &&
347 ((lock_type != TL_WRITE_CONCURRENT_INSERT &&
348 lock_type != TL_WRITE_ALLOW_WRITE) ||
349 !lock->read_no_write_count)))
350 {
351 (*lock->write.last)=data; /* Add as current write lock */
352 data->prev=lock->write.last;
353 lock->write.last= &data->next;
354 current_global_counters.locks_immediate++;
355 goto end;
356 }
357 }
358 }
359 wait_queue= &lock->write_wait;
360 }
361 /*
362 Try to detect a trivial deadlock when using cursors: attempt to
363 lock a table that is already locked by an open cursor within the
364 same connection. lock_owner can be zero if we succumbed to a high
365 priority writer in the write_wait queue.
366 */
367 lock_owner= lock->read.data ? lock->read.data : lock->write.data;
368 if (lock_owner && lock_owner->owner->info == owner->info)
369 {
370 result= THR_LOCK_DEADLOCK;
371 goto end;
372 }
373
374 /* Can't get lock yet; Wait for it */
375 return(wait_for_lock(session, wait_queue, data));
376
377end:
378 lock->unlock();
379
380 return(result);
381}
382
383
384static void free_all_read_locks(THR_LOCK *lock, bool using_concurrent_insert)
385{
386 THR_LOCK_DATA *data= lock->read_wait.data;
387
388 /* move all locks from read_wait list to read list */
389 (*lock->read.last)=data;
390 data->prev=lock->read.last;
391 lock->read.last=lock->read_wait.last;
392
393 /* Clear read_wait list */
394 lock->read_wait.last= &lock->read_wait.data;
395
396 do
397 {
398 boost::condition_variable_any *cond= data->cond;
399 if ((int) data->type == (int) TL_READ_NO_INSERT)
400 {
401 if (using_concurrent_insert)
402 {
403 /*
404 We can't free this lock;
405 Link lock away from read chain back into read_wait chain
406 */
407 if (((*data->prev)=data->next))
408 data->next->prev=data->prev;
409 else
410 lock->read.last=data->prev;
411 *lock->read_wait.last= data;
412 data->prev= lock->read_wait.last;
413 lock->read_wait.last= &data->next;
414 continue;
415 }
416 lock->read_no_write_count++;
417 }
418 data->cond= NULL; /* Mark thread free */
419 cond->notify_one();
420 } while ((data=data->next));
421 *lock->read_wait.last=0;
422 if (!lock->read_wait.data)
423 lock->write_lock_count=0;
424}
425
426/* Unlock lock and free next thread on same lock */
427
428static void thr_unlock(THR_LOCK_DATA *data)
429{
430 THR_LOCK *lock=data->lock;
431 enum thr_lock_type lock_type=data->type;
432 lock->lock();
433
434 if (((*data->prev)=data->next)) /* remove from lock-list */
435 data->next->prev= data->prev;
436 else if (lock_type <= TL_READ_NO_INSERT)
437 lock->read.last=data->prev;
438 else
439 lock->write.last=data->prev;
440 if (lock_type >= TL_WRITE_CONCURRENT_INSERT)
441 { }
442 else
443 { }
444 if (lock_type == TL_READ_NO_INSERT)
445 lock->read_no_write_count--;
446 data->type=TL_UNLOCK; /* Mark unlocked */
447 wake_up_waiters(lock);
448 lock->unlock();
449}
450
451
459
460static void wake_up_waiters(THR_LOCK *lock)
461{
462 THR_LOCK_DATA *data;
463 enum thr_lock_type lock_type;
464
465 if (!lock->write.data) /* If no active write locks */
466 {
467 data=lock->write_wait.data;
468 if (!lock->read.data) /* If no more locks in use */
469 {
470 /* Release write-locks with TL_WRITE or TL_WRITE_ONLY priority first */
471 if (data &&
472 (!lock->read_wait.data || lock->read_wait.data->type <= TL_READ_WITH_SHARED_LOCKS))
473 {
474 if (lock->write_lock_count++ > max_write_lock_count)
475 {
476 /* Too many write locks in a row; Release all waiting read locks */
477 lock->write_lock_count=0;
478 if (lock->read_wait.data)
479 {
480 free_all_read_locks(lock,0);
481 goto end;
482 }
483 }
484 for (;;)
485 {
486 if (((*data->prev)=data->next)) /* remove from wait-list */
487 data->next->prev= data->prev;
488 else
489 lock->write_wait.last=data->prev;
490 (*lock->write.last)=data; /* Put in execute list */
491 data->prev=lock->write.last;
492 data->next=0;
493 lock->write.last= &data->next;
494
495 {
496 boost::condition_variable_any *cond= data->cond;
497 data->cond= NULL; /* Mark thread free */
498 cond->notify_one(); /* Start waiting thred */
499 }
500 if (data->type != TL_WRITE_ALLOW_WRITE ||
501 !lock->write_wait.data ||
502 lock->write_wait.data->type != TL_WRITE_ALLOW_WRITE)
503 break;
504 data=lock->write_wait.data; /* Free this too */
505 }
506 if (data->type >= TL_WRITE)
507 goto end;
508 /* Release possible read locks together with the write lock */
509 }
510 if (lock->read_wait.data)
511 free_all_read_locks(lock,
512 data &&
513 (data->type == TL_WRITE_CONCURRENT_INSERT ||
514 data->type == TL_WRITE_ALLOW_WRITE));
515 }
516 else if (data &&
517 (lock_type=data->type) <= TL_WRITE_CONCURRENT_INSERT &&
518 ((lock_type != TL_WRITE_CONCURRENT_INSERT &&
519 lock_type != TL_WRITE_ALLOW_WRITE) ||
520 !lock->read_no_write_count))
521 {
522 do {
523 boost::condition_variable_any *cond= data->cond;
524 if (((*data->prev)=data->next)) /* remove from wait-list */
525 data->next->prev= data->prev;
526 else
527 lock->write_wait.last=data->prev;
528 (*lock->write.last)=data; /* Put in execute list */
529 data->prev=lock->write.last;
530 lock->write.last= &data->next;
531 data->next=0; /* Only one write lock */
532 data->cond= NULL; /* Mark thread free */
533 cond->notify_one(); /* Start waiting thread */
534 } while (lock_type == TL_WRITE_ALLOW_WRITE &&
535 (data=lock->write_wait.data) &&
536 data->type == TL_WRITE_ALLOW_WRITE);
537 if (lock->read_wait.data)
538 free_all_read_locks(lock,
539 (lock_type == TL_WRITE_CONCURRENT_INSERT ||
540 lock_type == TL_WRITE_ALLOW_WRITE));
541 }
542 else if (!data && lock->read_wait.data)
543 {
544 free_all_read_locks(lock,0);
545 }
546 }
547end:
548 return;
549}
550
551
552/*
553** Get all locks in a specific order to avoid dead-locks
554** Sort acording to lock position and put write_locks before read_locks if
555** lock on same lock.
556*/
557
558
559#define LOCK_CMP(A,B) ((unsigned char*) (A->lock) - (uint32_t) ((A)->type) < (unsigned char*) (B->lock)- (uint32_t) ((B)->type))
560
561static void sort_locks(THR_LOCK_DATA **data,uint32_t count)
562{
563 THR_LOCK_DATA **pos,**end,**prev,*tmp;
564
565 /* Sort locks with insertion sort (fast because almost always few locks) */
566
567 for (pos=data+1,end=data+count; pos < end ; pos++)
568 {
569 tmp= *pos;
570 if (LOCK_CMP(tmp,pos[-1]))
571 {
572 prev=pos;
573 do {
574 prev[0]=prev[-1];
575 } while (--prev != data && LOCK_CMP(tmp,prev[-1]));
576 prev[0]=tmp;
577 }
578 }
579}
580
581
582enum enum_thr_lock_result
583thr_multi_lock(Session &session, THR_LOCK_DATA **data, uint32_t count, THR_LOCK_OWNER *owner)
584{
585 THR_LOCK_DATA **pos,**end;
586 if (count > 1)
587 sort_locks(data,count);
588 /* lock everything */
589 for (pos=data,end=data+count; pos < end ; pos++)
590 {
591 enum enum_thr_lock_result result= thr_lock(session, *pos, owner, (*pos)->type);
592 if (result != THR_LOCK_SUCCESS)
593 { /* Aborted */
594 thr_multi_unlock(data,(uint32_t) (pos-data));
595 return(result);
596 }
597 }
598 return(THR_LOCK_SUCCESS);
599}
600
601 /* free all locks */
602
603void thr_multi_unlock(THR_LOCK_DATA **data,uint32_t count)
604{
605 THR_LOCK_DATA **pos,**end;
606
607 for (pos=data,end=data+count; pos < end ; pos++)
608 {
609 if ((*pos)->type != TL_UNLOCK)
610 thr_unlock(*pos);
611 }
612}
613
614void DrizzleLock::unlock(uint32_t count)
615{
616 THR_LOCK_DATA **pos,**end;
617
618 for (pos= getLocks(),end= getLocks()+count; pos < end ; pos++)
619 {
620 if ((*pos)->type != TL_UNLOCK)
621 thr_unlock(*pos);
622 }
623}
624
625/*
626 Abort all threads waiting for a lock. The lock will be upgraded to
627 TL_WRITE_ONLY to abort any new accesses to the lock
628*/
629
630void THR_LOCK::abort_locks()
631{
632 boost::mutex::scoped_lock scopedLock(mutex);
633
634 for (THR_LOCK_DATA *local_data= read_wait.data; local_data ; local_data= local_data->next)
635 {
636 local_data->type= TL_UNLOCK; /* Mark killed */
637 /* It's safe to signal the cond first: we're still holding the mutex. */
638 local_data->cond->notify_one();
639 local_data->cond= NULL; /* Removed from list */
640 }
641 for (THR_LOCK_DATA *local_data= write_wait.data; local_data ; local_data= local_data->next)
642 {
643 local_data->type= TL_UNLOCK;
644 local_data->cond->notify_one();
645 local_data->cond= NULL;
646 }
647 read_wait.last= &read_wait.data;
648 write_wait.last= &write_wait.data;
649 read_wait.data= write_wait.data=0;
650 if (write.data)
651 write.data->type=TL_WRITE_ONLY;
652}
653
654
655/*
656 Abort all locks for specific table/thread combination
657
658 This is used to abort all locks for a specific thread
659*/
660
661bool THR_LOCK::abort_locks_for_thread(uint64_t thread_id_arg)
662{
663 bool found= false;
664
665 boost::mutex::scoped_lock scopedLock(mutex);
666 for (THR_LOCK_DATA *local_data= read_wait.data; local_data ; local_data= local_data->next)
667 {
668 if (local_data->owner->info->thread_id == thread_id_arg)
669 {
670 local_data->type= TL_UNLOCK; /* Mark killed */
671 /* It's safe to signal the cond first: we're still holding the mutex. */
672 found= true;
673 local_data->cond->notify_one();
674 local_data->cond= 0; /* Removed from list */
675
676 if (((*local_data->prev)= local_data->next))
677 local_data->next->prev= local_data->prev;
678 else
679 read_wait.last= local_data->prev;
680 }
681 }
682 for (THR_LOCK_DATA *local_data= write_wait.data; local_data ; local_data= local_data->next)
683 {
684 if (local_data->owner->info->thread_id == thread_id_arg)
685 {
686 local_data->type= TL_UNLOCK;
687 found= true;
688 local_data->cond->notify_one();
689 local_data->cond= NULL;
690
691 if (((*local_data->prev)= local_data->next))
692 local_data->next->prev= local_data->prev;
693 else
694 write_wait.last= local_data->prev;
695 }
696 }
697 wake_up_waiters(this);
698
699 return found;
700}
701
702} /* namespace drizzled */
TODO: Rename this file - func.h is stupid.
static void wake_up_waiters(THR_LOCK *lock)
Wake up all threads which pending requests for the lock can be satisfied.
Definition thr_lock.cc:460
STL namespace.