| 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 02111-1301 USA */ |
| 15 | |
| 16 | #ifndef _lockman_h |
| 17 | #define _lockman_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 | enum lockman_lock_type { N, S, X, IS, IX, SIX, LS, LX, SLX, LSIX, LOCK_TYPE_LAST }; |
| 35 | |
| 36 | struct lockman_lock; |
| 37 | |
| 38 | typedef struct st_lock_owner LOCK_OWNER; |
| 39 | struct st_lock_owner { |
| 40 | LF_PINS *pins; /* must be allocated from lockman's pinbox */ |
| 41 | struct lockman_lock *all_locks; /* a LIFO */ |
| 42 | LOCK_OWNER *waiting_for; |
| 43 | pthread_cond_t *cond; /* transactions waiting for this, wait on 'cond' */ |
| 44 | pthread_mutex_t *mutex; /* mutex is required to use 'cond' */ |
| 45 | uint16 loid; |
| 46 | }; |
| 47 | |
| 48 | typedef LOCK_OWNER *loid_to_lo_func(uint16); |
| 49 | typedef struct { |
| 50 | LF_DYNARRAY array; /* hash itself */ |
| 51 | LF_ALLOCATOR alloc; /* allocator for elements */ |
| 52 | int32 volatile size; /* size of array */ |
| 53 | int32 volatile count; /* number of elements in the hash */ |
| 54 | uint lock_timeout; |
| 55 | loid_to_lo_func *loid_to_lo; |
| 56 | } LOCKMAN; |
| 57 | #define DIDNT_GET_THE_LOCK 0 |
| 58 | enum lockman_getlock_result { |
| 59 | NO_MEMORY_FOR_LOCK=1, DEADLOCK, LOCK_TIMEOUT, |
| 60 | GOT_THE_LOCK, |
| 61 | GOT_THE_LOCK_NEED_TO_LOCK_A_SUBRESOURCE, |
| 62 | GOT_THE_LOCK_NEED_TO_INSTANT_LOCK_A_SUBRESOURCE |
| 63 | }; |
| 64 | |
| 65 | void lockman_init(LOCKMAN *, loid_to_lo_func *, uint); |
| 66 | void lockman_destroy(LOCKMAN *); |
| 67 | enum lockman_getlock_result lockman_getlock(LOCKMAN *lm, LOCK_OWNER *lo, |
| 68 | uint64 resource, |
| 69 | enum lockman_lock_type lock); |
| 70 | int lockman_release_locks(LOCKMAN *, LOCK_OWNER *); |
| 71 | |
| 72 | #ifdef EXTRA_DEBUG |
| 73 | void print_lockhash(LOCKMAN *lm); |
| 74 | #endif |
| 75 | |
| 76 | #endif |
| 77 | |