~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to storage/innobase/include/os0sync.h

  • Committer: Padraig O'Sullivan
  • Date: 2009-03-21 01:02:23 UTC
  • mto: (960.2.5 mordred)
  • mto: This revision was merged to the branch mainline in revision 961.
  • Revision ID: osullivan.padraig@gmail.com-20090321010223-j8cph7eeyt1u3xol
Fixed function object to ensure it correctly returns a boolean type since
memcmp returns an integer. Added some more comments.

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
19
19
 
20
20
You should have received a copy of the GNU General Public License along with
21
 
this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
22
 
St, Fifth Floor, Boston, MA 02110-1301 USA
 
21
this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 
22
Place, Suite 330, Boston, MA 02111-1307 USA
23
23
 
24
24
*****************************************************************************/
25
25
 
26
 
/**************************************************//**
27
 
@file include/os0sync.h
 
26
/******************************************************
28
27
The interface to the operating system
29
28
synchronization primitives.
30
29
 
38
37
#include "ut0lst.h"
39
38
 
40
39
#ifdef __WIN__
41
 
/** Native event (slow)*/
42
 
typedef HANDLE                  os_native_event_t;
43
 
/** Native mutex */
44
 
typedef CRITICAL_SECTION        os_fast_mutex_t;
45
 
/** Native condition variable. */
46
 
typedef CONDITION_VARIABLE      os_cond_t;
 
40
 
 
41
#define os_fast_mutex_t CRITICAL_SECTION
 
42
 
 
43
typedef HANDLE          os_native_event_t;
 
44
 
 
45
typedef struct os_event_struct  os_event_struct_t;
 
46
typedef os_event_struct_t*      os_event_t;
 
47
 
 
48
struct os_event_struct {
 
49
        os_native_event_t                 handle;
 
50
                                        /* Windows event */
 
51
        UT_LIST_NODE_T(os_event_struct_t) os_event_list;
 
52
                                        /* list of all created events */
 
53
};
47
54
#else
48
 
/** Native mutex */
49
 
typedef pthread_mutex_t         os_fast_mutex_t;
50
 
/** Native condition variable */
51
 
typedef pthread_cond_t          os_cond_t;
52
 
#endif
 
55
typedef pthread_mutex_t os_fast_mutex_t;
53
56
 
54
 
/** Operating system event */
55
57
typedef struct os_event_struct  os_event_struct_t;
56
 
/** Operating system event handle */
57
58
typedef os_event_struct_t*      os_event_t;
58
59
 
59
 
/** An asynchronous signal sent between threads */
60
60
struct os_event_struct {
61
 
#ifdef __WIN__
62
 
        HANDLE          handle;         /*!< kernel event object, slow,
63
 
                                        used on older Windows */
64
 
#endif
65
 
        os_fast_mutex_t os_mutex;       /*!< this mutex protects the next
 
61
        os_fast_mutex_t os_mutex;       /* this mutex protects the next
66
62
                                        fields */
67
 
        ibool           is_set;         /*!< this is TRUE when the event is
 
63
        ibool           is_set;         /* this is TRUE when the event is
68
64
                                        in the signaled state, i.e., a thread
69
65
                                        does not stop if it tries to wait for
70
66
                                        this event */
71
 
        ib_int64_t      signal_count;   /*!< this is incremented each time
 
67
        ib_int64_t      signal_count;   /* this is incremented each time
72
68
                                        the event becomes signaled */
73
 
        os_cond_t       cond_var;       /*!< condition variable is used in
 
69
        pthread_cond_t  cond_var;       /* condition variable is used in
74
70
                                        waiting for the event */
75
71
        UT_LIST_NODE_T(os_event_struct_t) os_event_list;
76
 
                                        /*!< list of all created events */
 
72
                                        /* list of all created events */
77
73
};
 
74
#endif
78
75
 
79
 
/** Operating system mutex */
80
76
typedef struct os_mutex_struct  os_mutex_str_t;
81
 
/** Operating system mutex handle */
82
77
typedef os_mutex_str_t*         os_mutex_t;
83
78
 
84
 
/** Mutex protecting counts and the event and OS 'slow' mutex lists */
 
79
#define OS_SYNC_INFINITE_TIME   ((ulint)(-1))
 
80
 
 
81
#define OS_SYNC_TIME_EXCEEDED   1
 
82
 
 
83
/* Mutex protecting counts and the event and OS 'slow' mutex lists */
85
84
extern os_mutex_t       os_sync_mutex;
86
85
 
87
 
/** This is incremented by 1 in os_thread_create and decremented by 1 in
 
86
/* This is incremented by 1 in os_thread_create and decremented by 1 in
88
87
os_thread_exit */
89
88
extern ulint            os_thread_count;
90
89
 
92
91
extern ulint            os_mutex_count;
93
92
extern ulint            os_fast_mutex_count;
94
93
 
95
 
/*********************************************************//**
 
94
/*************************************************************
96
95
Initializes global event and OS 'slow' mutex lists. */
97
96
UNIV_INTERN
98
97
void
99
98
os_sync_init(void);
100
99
/*==============*/
101
 
/*********************************************************//**
 
100
/*************************************************************
102
101
Frees created events and OS 'slow' mutexes. */
103
102
UNIV_INTERN
104
103
void
105
104
os_sync_free(void);
106
105
/*==============*/
107
 
/*********************************************************//**
 
106
/*************************************************************
108
107
Creates an event semaphore, i.e., a semaphore which may just have two states:
109
108
signaled and nonsignaled. The created event is manual reset: it must be reset
110
 
explicitly by calling sync_os_reset_event.
111
 
@return the event handle */
 
109
explicitly by calling sync_os_reset_event. */
112
110
UNIV_INTERN
113
111
os_event_t
114
112
os_event_create(
115
113
/*============*/
116
 
        const char*     name);  /*!< in: the name of the event, if NULL
117
 
                                the event is created without a name */
118
 
/**********************************************************//**
 
114
                                /* out: the event handle */
 
115
        const char*     name);  /* in: the name of the event, if NULL
 
116
                                the event is created without a name */
 
117
#ifdef __WIN__
 
118
/*************************************************************
 
119
Creates an auto-reset event semaphore, i.e., an event which is automatically
 
120
reset when a single thread is released. Works only in Windows. */
 
121
UNIV_INTERN
 
122
os_event_t
 
123
os_event_create_auto(
 
124
/*=================*/
 
125
                                /* out: the event handle */
 
126
        const char*     name);  /* in: the name of the event, if NULL
 
127
                                the event is created without a name */
 
128
#endif
 
129
/**************************************************************
119
130
Sets an event semaphore to the signaled state: lets waiting threads
120
131
proceed. */
121
132
UNIV_INTERN
122
133
void
123
134
os_event_set(
124
135
/*=========*/
125
 
        os_event_t      event); /*!< in: event to set */
126
 
/**********************************************************//**
 
136
        os_event_t      event); /* in: event to set */
 
137
/**************************************************************
127
138
Resets an event semaphore to the nonsignaled state. Waiting threads will
128
139
stop to wait for the event.
129
140
The return value should be passed to os_even_wait_low() if it is desired
134
145
ib_int64_t
135
146
os_event_reset(
136
147
/*===========*/
137
 
        os_event_t      event); /*!< in: event to reset */
138
 
/**********************************************************//**
 
148
        os_event_t      event); /* in: event to reset */
 
149
/**************************************************************
139
150
Frees an event object. */
140
151
UNIV_INTERN
141
152
void
142
153
os_event_free(
143
154
/*==========*/
144
 
        os_event_t      event); /*!< in: event to free */
 
155
        os_event_t      event); /* in: event to free */
145
156
 
146
 
/**********************************************************//**
 
157
/**************************************************************
147
158
Waits for an event object until it is in the signaled state. If
148
159
srv_shutdown_state == SRV_SHUTDOWN_EXIT_THREADS this also exits the
149
160
waiting thread when the event becomes signaled (or immediately if the
167
178
void
168
179
os_event_wait_low(
169
180
/*==============*/
170
 
        os_event_t      event,          /*!< in: event to wait */
171
 
        ib_int64_t      reset_sig_count);/*!< in: zero or the value
 
181
        os_event_t      event,          /* in: event to wait */
 
182
        ib_int64_t      reset_sig_count);/* in: zero or the value
172
183
                                        returned by previous call of
173
184
                                        os_event_reset(). */
174
185
 
175
186
#define os_event_wait(event) os_event_wait_low(event, 0)
176
187
 
177
 
/*********************************************************//**
 
188
/**************************************************************
 
189
Waits for an event object until it is in the signaled state or
 
190
a timeout is exceeded. In Unix the timeout is always infinite. */
 
191
UNIV_INTERN
 
192
ulint
 
193
os_event_wait_time(
 
194
/*===============*/
 
195
                                /* out: 0 if success,
 
196
                                OS_SYNC_TIME_EXCEEDED if timeout
 
197
                                was exceeded */
 
198
        os_event_t      event,  /* in: event to wait */
 
199
        ulint           time);  /* in: timeout in microseconds, or
 
200
                                OS_SYNC_INFINITE_TIME */
 
201
#ifdef __WIN__
 
202
/**************************************************************
 
203
Waits for any event in an OS native event array. Returns if even a single
 
204
one is signaled or becomes signaled. */
 
205
UNIV_INTERN
 
206
ulint
 
207
os_event_wait_multiple(
 
208
/*===================*/
 
209
                                        /* out: index of the event
 
210
                                        which was signaled */
 
211
        ulint                   n,      /* in: number of events in the
 
212
                                        array */
 
213
        os_native_event_t*      native_event_array);
 
214
                                        /* in: pointer to an array of event
 
215
                                        handles */
 
216
#endif
 
217
/*************************************************************
178
218
Creates an operating system mutex semaphore. Because these are slow, the
179
 
mutex semaphore of InnoDB itself (mutex_t) should be used where possible.
180
 
@return the mutex handle */
 
219
mutex semaphore of InnoDB itself (mutex_t) should be used where possible. */
181
220
UNIV_INTERN
182
221
os_mutex_t
183
 
os_mutex_create(void);
184
 
/*=================*/
185
 
/**********************************************************//**
 
222
os_mutex_create(
 
223
/*============*/
 
224
                                /* out: the mutex handle */
 
225
        const char*     name);  /* in: the name of the mutex, if NULL
 
226
                                the mutex is created without a name */
 
227
/**************************************************************
186
228
Acquires ownership of a mutex semaphore. */
187
229
UNIV_INTERN
188
230
void
189
231
os_mutex_enter(
190
232
/*===========*/
191
 
        os_mutex_t      mutex); /*!< in: mutex to acquire */
192
 
/**********************************************************//**
 
233
        os_mutex_t      mutex); /* in: mutex to acquire */
 
234
/**************************************************************
193
235
Releases ownership of a mutex. */
194
236
UNIV_INTERN
195
237
void
196
238
os_mutex_exit(
197
239
/*==========*/
198
 
        os_mutex_t      mutex); /*!< in: mutex to release */
199
 
/**********************************************************//**
 
240
        os_mutex_t      mutex); /* in: mutex to release */
 
241
/**************************************************************
200
242
Frees an mutex object. */
201
243
UNIV_INTERN
202
244
void
203
245
os_mutex_free(
204
246
/*==========*/
205
 
        os_mutex_t      mutex); /*!< in: mutex to free */
206
 
/**********************************************************//**
 
247
        os_mutex_t      mutex); /* in: mutex to free */
 
248
/**************************************************************
207
249
Acquires ownership of a fast mutex. Currently in Windows this is the same
208
 
as os_fast_mutex_lock!
209
 
@return 0 if success, != 0 if was reserved by another thread */
 
250
as os_fast_mutex_lock! */
210
251
UNIV_INLINE
211
252
ulint
212
253
os_fast_mutex_trylock(
213
254
/*==================*/
214
 
        os_fast_mutex_t*        fast_mutex);    /*!< in: mutex to acquire */
215
 
/**********************************************************//**
 
255
                                                /* out: 0 if success, != 0 if
 
256
                                                was reserved by another
 
257
                                                thread */
 
258
        os_fast_mutex_t*        fast_mutex);    /* in: mutex to acquire */
 
259
/**************************************************************
216
260
Releases ownership of a fast mutex. */
217
261
UNIV_INTERN
218
262
void
219
263
os_fast_mutex_unlock(
220
264
/*=================*/
221
 
        os_fast_mutex_t*        fast_mutex);    /*!< in: mutex to release */
222
 
/*********************************************************//**
 
265
        os_fast_mutex_t*        fast_mutex);    /* in: mutex to release */
 
266
/*************************************************************
223
267
Initializes an operating system fast mutex semaphore. */
224
268
UNIV_INTERN
225
269
void
226
270
os_fast_mutex_init(
227
271
/*===============*/
228
 
        os_fast_mutex_t*        fast_mutex);    /*!< in: fast mutex */
229
 
/**********************************************************//**
 
272
        os_fast_mutex_t*        fast_mutex);    /* in: fast mutex */
 
273
/**************************************************************
230
274
Acquires ownership of a fast mutex. */
231
275
UNIV_INTERN
232
276
void
233
277
os_fast_mutex_lock(
234
278
/*===============*/
235
 
        os_fast_mutex_t*        fast_mutex);    /*!< in: mutex to acquire */
236
 
/**********************************************************//**
 
279
        os_fast_mutex_t*        fast_mutex);    /* in: mutex to acquire */
 
280
/**************************************************************
237
281
Frees an mutex object. */
238
282
UNIV_INTERN
239
283
void
240
284
os_fast_mutex_free(
241
285
/*===============*/
242
 
        os_fast_mutex_t*        fast_mutex);    /*!< in: mutex to free */
243
 
 
244
 
/**********************************************************//**
245
 
Atomic compare-and-swap and increment for InnoDB. */
246
 
 
247
 
#if defined(HAVE_GCC_ATOMIC_BUILTINS)
248
 
 
249
 
#define HAVE_ATOMIC_BUILTINS
250
 
 
251
 
/**********************************************************//**
 
286
        os_fast_mutex_t*        fast_mutex);    /* in: mutex to free */
 
287
 
 
288
#ifdef HAVE_GCC_ATOMIC_BUILTINS
 
289
/**************************************************************
 
290
Atomic compare-and-swap for InnoDB. Currently requires GCC atomic builtins.
252
291
Returns true if swapped, ptr is pointer to target, old_val is value to
253
292
compare to, new_val is the value to swap in. */
254
 
 
255
 
# define os_compare_and_swap(ptr, old_val, new_val) \
 
293
#define os_compare_and_swap(ptr, old_val, new_val) \
256
294
        __sync_bool_compare_and_swap(ptr, old_val, new_val)
257
295
 
258
 
# define os_compare_and_swap_ulint(ptr, old_val, new_val) \
259
 
        os_compare_and_swap(ptr, old_val, new_val)
260
 
 
261
 
# define os_compare_and_swap_lint(ptr, old_val, new_val) \
262
 
        os_compare_and_swap(ptr, old_val, new_val)
263
 
 
264
 
# ifdef HAVE_IB_ATOMIC_PTHREAD_T_GCC
265
 
#  define os_compare_and_swap_thread_id(ptr, old_val, new_val) \
266
 
        os_compare_and_swap(ptr, old_val, new_val)
267
 
#  define INNODB_RW_LOCKS_USE_ATOMICS
268
 
#  define IB_ATOMICS_STARTUP_MSG \
269
 
        "Mutexes and rw_locks use GCC atomic builtins"
270
 
# else /* HAVE_IB_ATOMIC_PTHREAD_T_GCC */
271
 
#  define IB_ATOMICS_STARTUP_MSG \
272
 
        "Mutexes use GCC atomic builtins, rw_locks do not"
273
 
# endif /* HAVE_IB_ATOMIC_PTHREAD_T_GCC */
274
 
 
275
 
/**********************************************************//**
 
296
/**************************************************************
 
297
Atomic increment for InnoDB. Currently requires GCC atomic builtins.
276
298
Returns the resulting value, ptr is pointer to target, amount is the
277
299
amount of increment. */
278
 
 
279
 
# define os_atomic_increment(ptr, amount) \
 
300
#define os_atomic_increment(ptr, amount) \
280
301
        __sync_add_and_fetch(ptr, amount)
281
302
 
282
 
# define os_atomic_increment_lint(ptr, amount) \
283
 
        os_atomic_increment(ptr, amount)
284
 
 
285
 
# define os_atomic_increment_ulint(ptr, amount) \
286
 
        os_atomic_increment(ptr, amount)
287
 
 
288
 
/**********************************************************//**
289
 
Returns the old value of *ptr, atomically sets *ptr to new_val */
290
 
 
291
 
# define os_atomic_test_and_set_byte(ptr, new_val) \
292
 
        __sync_lock_test_and_set(ptr, (byte) new_val)
293
 
 
294
 
#elif defined(HAVE_SOLARIS_ATOMICS)
295
 
 
296
 
#define HAVE_ATOMIC_BUILTINS
297
 
 
298
 
/* If not compiling with GCC or GCC doesn't support the atomic
299
 
intrinsics and running on Solaris >= 10 use Solaris atomics */
300
 
 
301
 
#include <atomic.h>
302
 
 
303
 
/**********************************************************//**
304
 
Returns true if swapped, ptr is pointer to target, old_val is value to
305
 
compare to, new_val is the value to swap in. */
306
 
 
307
 
# define os_compare_and_swap_ulint(ptr, old_val, new_val) \
308
 
        (atomic_cas_ulong(ptr, old_val, new_val) == old_val)
309
 
 
310
 
# define os_compare_and_swap_lint(ptr, old_val, new_val) \
311
 
        ((lint)atomic_cas_ulong((ulong_t*) ptr, old_val, new_val) == old_val)
312
 
 
313
 
# ifdef HAVE_IB_ATOMIC_PTHREAD_T_SOLARIS
314
 
#  if SIZEOF_PTHREAD_T == 4
315
 
#   define os_compare_and_swap_thread_id(ptr, old_val, new_val) \
316
 
        ((pthread_t)atomic_cas_32(ptr, old_val, new_val) == old_val)
317
 
#  elif SIZEOF_PTHREAD_T == 8
318
 
#   define os_compare_and_swap_thread_id(ptr, old_val, new_val) \
319
 
        ((pthread_t)atomic_cas_64(ptr, old_val, new_val) == old_val)
320
 
#  else
321
 
#   error "SIZEOF_PTHREAD_T != 4 or 8"
322
 
#  endif /* SIZEOF_PTHREAD_T CHECK */
323
 
#  define INNODB_RW_LOCKS_USE_ATOMICS
324
 
#  define IB_ATOMICS_STARTUP_MSG \
325
 
        "Mutexes and rw_locks use Solaris atomic functions"
326
 
# else /* HAVE_IB_ATOMIC_PTHREAD_T_SOLARIS */
327
 
#  define IB_ATOMICS_STARTUP_MSG \
328
 
        "Mutexes use Solaris atomic functions, rw_locks do not"
329
 
# endif /* HAVE_IB_ATOMIC_PTHREAD_T_SOLARIS */
330
 
 
331
 
/**********************************************************//**
332
 
Returns the resulting value, ptr is pointer to target, amount is the
333
 
amount of increment. */
334
 
 
335
 
# define os_atomic_increment_lint(ptr, amount) \
336
 
        atomic_add_long_nv((ulong_t*) ptr, amount)
337
 
 
338
 
# define os_atomic_increment_ulint(ptr, amount) \
339
 
        atomic_add_long_nv(ptr, amount)
340
 
 
341
 
/**********************************************************//**
342
 
Returns the old value of *ptr, atomically sets *ptr to new_val */
343
 
 
344
 
# define os_atomic_test_and_set_byte(ptr, new_val) \
345
 
        atomic_swap_uchar(ptr, new_val)
346
 
 
347
 
#elif defined(HAVE_WINDOWS_ATOMICS)
348
 
 
349
 
#define HAVE_ATOMIC_BUILTINS
350
 
 
351
 
/* On Windows, use Windows atomics / interlocked */
352
 
# ifdef _WIN64
353
 
#  define win_cmp_and_xchg InterlockedCompareExchange64
354
 
#  define win_xchg_and_add InterlockedExchangeAdd64
355
 
# else /* _WIN64 */
356
 
#  define win_cmp_and_xchg InterlockedCompareExchange
357
 
#  define win_xchg_and_add InterlockedExchangeAdd
358
 
# endif
359
 
 
360
 
/**********************************************************//**
361
 
Returns true if swapped, ptr is pointer to target, old_val is value to
362
 
compare to, new_val is the value to swap in. */
363
 
 
364
 
# define os_compare_and_swap_ulint(ptr, old_val, new_val) \
365
 
        (win_cmp_and_xchg(ptr, new_val, old_val) == old_val)
366
 
 
367
 
# define os_compare_and_swap_lint(ptr, old_val, new_val) \
368
 
        (win_cmp_and_xchg(ptr, new_val, old_val) == old_val)
369
 
 
370
 
/* windows thread objects can always be passed to windows atomic functions */
371
 
# define os_compare_and_swap_thread_id(ptr, old_val, new_val) \
372
 
        (InterlockedCompareExchange(ptr, new_val, old_val) == old_val)
373
 
# define INNODB_RW_LOCKS_USE_ATOMICS
374
 
# define IB_ATOMICS_STARTUP_MSG \
375
 
        "Mutexes and rw_locks use Windows interlocked functions"
376
 
 
377
 
/**********************************************************//**
378
 
Returns the resulting value, ptr is pointer to target, amount is the
379
 
amount of increment. */
380
 
 
381
 
# define os_atomic_increment_lint(ptr, amount) \
382
 
        (win_xchg_and_add(ptr, amount) + amount)
383
 
 
384
 
# define os_atomic_increment_ulint(ptr, amount) \
385
 
        ((ulint) (win_xchg_and_add(ptr, amount) + amount))
386
 
 
387
 
/**********************************************************//**
388
 
Returns the old value of *ptr, atomically sets *ptr to new_val.
389
 
InterlockedExchange() operates on LONG, and the LONG will be
390
 
clobbered */
391
 
 
392
 
# define os_atomic_test_and_set_byte(ptr, new_val) \
393
 
        ((byte) InterlockedExchange(ptr, new_val))
394
 
 
395
 
#else
396
 
# define IB_ATOMICS_STARTUP_MSG \
397
 
        "Mutexes and rw_locks use InnoDB's own implementation"
398
 
#endif
 
303
#endif /* HAVE_GCC_ATOMIC_BUILTINS */
399
304
 
400
305
#ifndef UNIV_NONINL
401
306
#include "os0sync.ic"