1 /* Copyright (C) 2006 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 Street, Fifth Floor, Boston, MA 02110-1335 USA */
15 
16 #ifndef _tablockman_h
17 #define _tablockman_h
18 
19 /*
20   Lock levels:
21   ^^^^^^^^^^^
22 
23   N    - "no lock", not a lock, used sometimes internally to simplify the code
24   S    - Shared
25   X    - eXclusive
26   IS   - Intention Shared
27   IX   - Intention eXclusive
28   SIX  - Shared + Intention eXclusive
29   LS   - Loose Shared
30   LX   - Loose eXclusive
31   SLX  - Shared + Loose eXclusive
32   LSIX - Loose Shared + Intention eXclusive
33 */
34 #ifndef _lockman_h
35 /* QQ: TODO remove N-locks */
36 enum lockman_lock_type { N, S, X, IS, IX, SIX, LS, LX, SLX, LSIX, LOCK_TYPE_LAST };
37 enum lockman_getlock_result {
38   NO_MEMORY_FOR_LOCK=1, DEADLOCK, LOCK_TIMEOUT,
39   GOT_THE_LOCK,
40   GOT_THE_LOCK_NEED_TO_LOCK_A_SUBRESOURCE,
41   GOT_THE_LOCK_NEED_TO_INSTANT_LOCK_A_SUBRESOURCE
42 };
43 #endif
44 
45 #define LOCK_TYPES (LOCK_TYPE_LAST-1)
46 
47 typedef struct st_table_lock TABLE_LOCK;
48 
49 typedef struct st_table_lock_owner {
50   TABLE_LOCK *active_locks;                          /* list of active locks */
51   TABLE_LOCK *waiting_lock;                  /* waiting lock (one lock only) */
52   struct st_table_lock_owner *waiting_for; /* transaction we're waiting for  */
53   pthread_cond_t  *cond;      /* transactions waiting for us, wait on 'cond' */
54   mysql_mutex_t *mutex;                 /* mutex is required to use 'cond' */
55   uint16    loid, waiting_for_loid;                 /* Lock Owner IDentifier */
56 } TABLE_LOCK_OWNER;
57 
58 typedef struct st_locked_table {
59   mysql_mutex_t mutex;                        /* mutex for everything below */
60   HASH latest_locks;                                /* latest locks in a hash */
61   TABLE_LOCK *active_locks[LOCK_TYPES];          /* dl-list of locks per type */
62   TABLE_LOCK *wait_queue_in, *wait_queue_out; /* wait deque (double-end queue)*/
63 } LOCKED_TABLE;
64 
65 typedef TABLE_LOCK_OWNER *loid_to_tlo_func(uint16);
66 
67 typedef struct {
68   mysql_mutex_t pool_mutex;
69   TABLE_LOCK *pool;                                /* lifo pool of free locks */
70   uint lock_timeout;                          /* lock timeout in milliseconds */
71   loid_to_tlo_func *loid_to_tlo;      /* for mapping loid to TABLE_LOCK_OWNER */
72 } TABLOCKMAN;
73 
74 void tablockman_init(TABLOCKMAN *, loid_to_tlo_func *, uint);
75 void tablockman_destroy(TABLOCKMAN *);
76 enum lockman_getlock_result tablockman_getlock(TABLOCKMAN *, TABLE_LOCK_OWNER *,
77                                                LOCKED_TABLE *, enum lockman_lock_type);
78 void tablockman_release_locks(TABLOCKMAN *, TABLE_LOCK_OWNER *);
79 void tablockman_init_locked_table(LOCKED_TABLE *, int);
80 void tablockman_destroy_locked_table(LOCKED_TABLE *);
81 
82 #ifdef EXTRA_DEBUG
83 void tablockman_print_tlo(TABLE_LOCK_OWNER *);
84 #endif
85 
86 #endif
87 
88