~drizzle-trunk/drizzle/development

1 by brian
clean slate
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
15
16
/* For use with thr_lock:s */
17
18
#ifndef _thr_lock_h
19
#define _thr_lock_h
20
#ifdef	__cplusplus
21
extern "C" {
22
#endif
23
24
#include <my_pthread.h>
25
#include <my_list.h>
26
27
struct st_thr_lock;
28
extern ulong locks_immediate,locks_waited ;
53.2.23 by Monty Taylor
Fixed the thr_lock double define the right direction.
29
extern pthread_mutex_t THR_LOCK_lock;
1 by brian
clean slate
30
31
enum thr_lock_type { TL_IGNORE=-1,
32
		     TL_UNLOCK,			/* UNLOCK ANY LOCK */
33
		     TL_READ,			/* Read lock */
34
		     TL_READ_WITH_SHARED_LOCKS,
35
		     /* High prior. than TL_WRITE. Allow concurrent insert */
36
		     TL_READ_HIGH_PRIORITY,
37
		     /* READ, Don't allow concurrent insert */
38
		     TL_READ_NO_INSERT,
39
		     /* 
40
			Write lock, but allow other threads to read / write.
41
			Used by BDB tables in MySQL to mark that someone is
42
			reading/writing to the table.
43
		      */
44
		     TL_WRITE_ALLOW_WRITE,
45
		     /*
46
			Write lock, but allow other threads to read.
47
			Used by ALTER TABLE in MySQL to allow readers
48
			to use the table until ALTER TABLE is finished.
49
		     */
50
		     TL_WRITE_ALLOW_READ,
51
		     /*
52
		       WRITE lock used by concurrent insert. Will allow
53
		       READ, if one could use concurrent insert on table.
54
		     */
55
		     TL_WRITE_CONCURRENT_INSERT,
56
		     /* Write used by INSERT DELAYED.  Allows READ locks */
57
		     TL_WRITE_DELAYED,
58
                     /* 
59
                       parser only! Late bound low_priority flag. 
60
                       At open_tables() becomes thd->update_lock_default.
61
                     */
62
                     TL_WRITE_DEFAULT,
63
		     /* WRITE lock that has lower priority than TL_READ */
64
		     TL_WRITE_LOW_PRIORITY,
65
		     /* Normal WRITE lock */
66
		     TL_WRITE,
67
		     /* Abort new lock request with an error */
68
		     TL_WRITE_ONLY};
69
70
enum enum_thr_lock_result { THR_LOCK_SUCCESS= 0, THR_LOCK_ABORTED= 1,
71
                            THR_LOCK_WAIT_TIMEOUT= 2, THR_LOCK_DEADLOCK= 3 };
72
73
74
extern ulong max_write_lock_count;
75
extern ulong table_lock_wait_timeout;
76
extern my_bool thr_lock_inited;
77
extern enum thr_lock_type thr_upgraded_concurrent_insert_lock;
78
79
/*
80
  A description of the thread which owns the lock. The address
81
  of an instance of this structure is used to uniquely identify the thread.
82
*/
83
84
typedef struct st_thr_lock_info
85
{
86
  pthread_t thread;
87
  my_thread_id thread_id;
88
  ulong n_cursors;
89
} THR_LOCK_INFO;
90
91
/*
92
  Lock owner identifier. Globally identifies the lock owner within the
93
  thread and among all the threads. The address of an instance of this
94
  structure is used as id.
95
*/
96
97
typedef struct st_thr_lock_owner
98
{
99
  THR_LOCK_INFO *info;
100
} THR_LOCK_OWNER;
101
102
103
typedef struct st_thr_lock_data {
104
  THR_LOCK_OWNER *owner;
105
  struct st_thr_lock_data *next,**prev;
106
  struct st_thr_lock *lock;
107
  pthread_cond_t *cond;
108
  enum thr_lock_type type;
109
  void *status_param;			/* Param to status functions */
110
  void *debug_print_param;
111
} THR_LOCK_DATA;
112
113
struct st_lock_list {
114
  THR_LOCK_DATA *data,**last;
115
};
116
117
typedef struct st_thr_lock {
118
  LIST list;
119
  pthread_mutex_t mutex;
120
  struct st_lock_list read_wait;
121
  struct st_lock_list read;
122
  struct st_lock_list write_wait;
123
  struct st_lock_list write;
124
  /* write_lock_count is incremented for write locks and reset on read locks */
125
  ulong write_lock_count;
126
  uint read_no_write_count;
127
  void (*get_status)(void*, int);	/* When one gets a lock */
128
  void (*copy_status)(void*,void*);
129
  void (*update_status)(void*);		/* Before release of write */
130
  void (*restore_status)(void*);         /* Before release of read */
131
  my_bool (*check_status)(void *);
132
} THR_LOCK;
133
134
135
extern LIST *thr_lock_thread_list;
136
137
my_bool init_thr_lock(void);		/* Must be called once/thread */
138
#define thr_lock_owner_init(owner, info_arg) (owner)->info= (info_arg)
139
void thr_lock_info_init(THR_LOCK_INFO *info);
140
void thr_lock_init(THR_LOCK *lock);
141
void thr_lock_delete(THR_LOCK *lock);
142
void thr_lock_data_init(THR_LOCK *lock,THR_LOCK_DATA *data,
143
			void *status_param);
144
enum enum_thr_lock_result thr_lock(THR_LOCK_DATA *data,
145
                                   THR_LOCK_OWNER *owner,
146
                                   enum thr_lock_type lock_type);
147
void thr_unlock(THR_LOCK_DATA *data);
148
enum enum_thr_lock_result thr_multi_lock(THR_LOCK_DATA **data,
149
                                         uint count, THR_LOCK_OWNER *owner);
150
void thr_multi_unlock(THR_LOCK_DATA **data,uint count);
151
void thr_abort_locks(THR_LOCK *lock, my_bool upgrade_lock);
152
my_bool thr_abort_locks_for_thread(THR_LOCK *lock, my_thread_id thread);
153
void thr_print_locks(void);		/* For debugging */
154
my_bool thr_upgrade_write_delay_lock(THR_LOCK_DATA *data);
155
void    thr_downgrade_write_lock(THR_LOCK_DATA *data,
156
                                 enum thr_lock_type new_lock_type);
157
my_bool thr_reschedule_write_lock(THR_LOCK_DATA *data);
158
#ifdef	__cplusplus
159
}
160
#endif
161
#endif /* _thr_lock_h */