18 #include "mysys_priv.h"
19 #if defined(NEED_MY_RW_LOCK)
24 static BOOL have_srwlock= FALSE;
26 typedef VOID (WINAPI* srw_func) (PSRWLOCK SRWLock);
27 typedef BOOLEAN (WINAPI* srw_bool_func) (PSRWLOCK SRWLock);
29 static srw_func my_InitializeSRWLock;
30 static srw_func my_AcquireSRWLockExclusive;
31 static srw_func my_ReleaseSRWLockExclusive;
32 static srw_func my_AcquireSRWLockShared;
33 static srw_func my_ReleaseSRWLockShared;
35 static srw_bool_func my_TryAcquireSRWLockExclusive;
36 static srw_bool_func my_TryAcquireSRWLockShared;
43 static void check_srwlock_availability(
void)
45 HMODULE module= GetModuleHandle(
"kernel32");
47 my_InitializeSRWLock= (srw_func) GetProcAddress(module,
49 my_AcquireSRWLockExclusive= (srw_func) GetProcAddress(module,
50 "AcquireSRWLockExclusive");
51 my_AcquireSRWLockShared= (srw_func) GetProcAddress(module,
52 "AcquireSRWLockShared");
53 my_ReleaseSRWLockExclusive= (srw_func) GetProcAddress(module,
54 "ReleaseSRWLockExclusive");
55 my_ReleaseSRWLockShared= (srw_func) GetProcAddress(module,
56 "ReleaseSRWLockShared");
57 my_TryAcquireSRWLockExclusive= (srw_bool_func) GetProcAddress(module,
58 "TryAcquireSRWLockExclusive");
59 my_TryAcquireSRWLockShared= (srw_bool_func) GetProcAddress(module,
60 "TryAcquireSRWLockShared");
72 if (my_TryAcquireSRWLockExclusive)
80 my_InitializeSRWLock(&rwp->srwlock);
81 rwp->have_exclusive_srwlock = FALSE;
88 my_AcquireSRWLockShared(&rwp->srwlock);
96 if (!my_TryAcquireSRWLockShared(&rwp->srwlock))
104 my_AcquireSRWLockExclusive(&rwp->srwlock);
105 rwp->have_exclusive_srwlock= TRUE;
112 if (!my_TryAcquireSRWLockExclusive(&rwp->srwlock))
114 rwp->have_exclusive_srwlock= TRUE;
121 if (rwp->have_exclusive_srwlock)
123 rwp->have_exclusive_srwlock= FALSE;
124 my_ReleaseSRWLockExclusive(&rwp->srwlock);
128 my_ReleaseSRWLockShared(&rwp->srwlock);
176 pthread_condattr_t cond_attr;
187 static my_pthread_once_t once_control= MY_PTHREAD_ONCE_INIT;
188 my_pthread_once(&once_control, check_srwlock_availability);
191 return srw_init(rwp);
194 pthread_mutex_init( &rwp->lock, MY_MUTEX_INIT_FAST);
195 pthread_condattr_init( &cond_attr );
196 pthread_cond_init( &rwp->readers, &cond_attr );
197 pthread_cond_init( &rwp->writers, &cond_attr );
198 pthread_condattr_destroy(&cond_attr);
203 rwp->write_thread = 0;
216 DBUG_ASSERT(rwp->state == 0);
217 pthread_mutex_destroy( &rwp->lock );
218 pthread_cond_destroy( &rwp->readers );
219 pthread_cond_destroy( &rwp->writers );
228 return srw_rdlock(rwp);
231 pthread_mutex_lock(&rwp->lock);
234 while (( rwp->state < 0 ) || rwp->waiters)
235 pthread_cond_wait( &rwp->readers, &rwp->lock);
238 pthread_mutex_unlock(&rwp->lock);
248 return srw_tryrdlock(rwp);
251 pthread_mutex_lock(&rwp->lock);
252 if ((rwp->state < 0 ) || rwp->waiters)
259 pthread_mutex_unlock(&rwp->lock);
268 return srw_wrlock(rwp);
271 pthread_mutex_lock(&rwp->lock);
274 my_rw_lock_assert_not_write_owner(rwp);
277 pthread_cond_wait(&rwp->writers, &rwp->lock);
281 rwp->write_thread= pthread_self();
283 pthread_mutex_unlock(&rwp->lock);
294 return srw_trywrlock(rwp);
297 pthread_mutex_lock(&rwp->lock);
305 rwp->write_thread= pthread_self();
308 pthread_mutex_unlock(&rwp->lock);
317 return srw_unlock(rwp);
327 pthread_mutex_lock(&rwp->lock);
329 DBUG_ASSERT(rwp->state != 0);
331 if (rwp->state == -1)
333 my_rw_lock_assert_write_owner(rwp);
336 rwp->write_thread= 0;
340 pthread_cond_signal( &rwp->writers );
342 pthread_cond_broadcast( &rwp->readers );
346 if ( --rwp->state == 0 &&
348 pthread_cond_signal( &rwp->writers );
351 pthread_mutex_unlock( &rwp->lock );
360 pthread_mutex_init(&rwlock->
lock, NULL);
366 rwlock->writer_thread= 0;
375 pthread_mutex_destroy(&rwlock->
lock);
382 pthread_mutex_lock(&rwlock->
lock);
390 pthread_mutex_unlock(&rwlock->
lock);
397 pthread_mutex_lock(&rwlock->
lock);
425 rwlock->writer_thread= pthread_self();
437 rwlock->writer_thread= 0;
456 pthread_mutex_unlock(&rwlock->
lock);
461 pthread_mutex_lock(&rwlock->
lock);
472 pthread_mutex_unlock(&rwlock->
lock);