summaryrefslogtreecommitdiff
path: root/storage/maria/tablockman.h
blob: 58c852b5a215e73defcf77d4fa285ccee177dc8c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/* Copyright (C) 2006 MySQL AB

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; version 2 of the License.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */

#ifndef _tablockman_h
#define _tablockman_h

/*
  Lock levels:
  ^^^^^^^^^^^

  N    - "no lock", not a lock, used sometimes internally to simplify the code
  S    - Shared
  X    - eXclusive
  IS   - Intention Shared
  IX   - Intention eXclusive
  SIX  - Shared + Intention eXclusive
  LS   - Loose Shared
  LX   - Loose eXclusive
  SLX  - Shared + Loose eXclusive
  LSIX - Loose Shared + Intention eXclusive
*/
#ifndef _lockman_h
/* QQ: TODO remove N-locks */
enum lock_type { N, S, X, IS, IX, SIX, LS, LX, SLX, LSIX, LOCK_TYPE_LAST };
enum lockman_getlock_result {
  NO_MEMORY_FOR_LOCK=1, DEADLOCK, LOCK_TIMEOUT,
  GOT_THE_LOCK,
  GOT_THE_LOCK_NEED_TO_LOCK_A_SUBRESOURCE,
  GOT_THE_LOCK_NEED_TO_INSTANT_LOCK_A_SUBRESOURCE
};
#endif

#define LOCK_TYPES (LOCK_TYPE_LAST-1)

typedef struct st_table_lock TABLE_LOCK;

typedef struct st_table_lock_owner {
  TABLE_LOCK *active_locks;                          /* list of active locks */
  TABLE_LOCK *waiting_lock;                  /* waiting lock (one lock only) */
  struct st_table_lock_owner *waiting_for; /* transaction we're waiting for  */
  pthread_cond_t  *cond;      /* transactions waiting for us, wait on 'cond' */
  pthread_mutex_t *mutex;                 /* mutex is required to use 'cond' */
  uint16    loid, waiting_for_loid;                 /* Lock Owner IDentifier */
} TABLE_LOCK_OWNER;

typedef struct st_locked_table {
  pthread_mutex_t mutex;                        /* mutex for everything below */
  HASH latest_locks;                                /* latest locks in a hash */
  TABLE_LOCK *active_locks[LOCK_TYPES];          /* dl-list of locks per type */
  TABLE_LOCK *wait_queue_in, *wait_queue_out; /* wait deque (double-end queue)*/
} LOCKED_TABLE;

typedef TABLE_LOCK_OWNER *loid_to_tlo_func(uint16);

typedef struct {
  pthread_mutex_t pool_mutex;
  TABLE_LOCK *pool;                                /* lifo pool of free locks */
  uint lock_timeout;                          /* lock timeout in milliseconds */
  loid_to_tlo_func *loid_to_tlo;      /* for mapping loid to TABLE_LOCK_OWNER */
} TABLOCKMAN;

void tablockman_init(TABLOCKMAN *, loid_to_tlo_func *, uint);
void tablockman_destroy(TABLOCKMAN *);
enum lockman_getlock_result tablockman_getlock(TABLOCKMAN *, TABLE_LOCK_OWNER *,
                                               LOCKED_TABLE *, enum lock_type);
void tablockman_release_locks(TABLOCKMAN *, TABLE_LOCK_OWNER *);
void tablockman_init_locked_table(LOCKED_TABLE *, int);
void tablockman_destroy_locked_table(LOCKED_TABLE *);

#ifdef EXTRA_DEBUG
void tablockman_print_tlo(TABLE_LOCK_OWNER *);
#endif

#endif