1 /* EINA - EFL data type library
2 * Copyright (C) 2011 Vincent Torri
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library;
16 * if not, see <http://www.gnu.org/licenses/>.
19 #ifndef EINA_INLINE_LOCK_WIN32_X_
20 #define EINA_INLINE_LOCK_WIN32_X_
24 typedef CRITICAL_SECTION Eina_Lock;
25 typedef struct _Eina_Condition Eina_Condition;
26 typedef struct _Eina_RWLock Eina_RWLock;
27 typedef DWORD Eina_TLS;
28 typedef HANDLE Eina_Semaphore;
30 #if _WIN32_WINNT >= 0x0600
31 struct _Eina_Condition
33 CRITICAL_SECTION *mutex;
34 CONDITION_VARIABLE condition;
41 Eina_Bool is_read_mode : 1;
44 struct _Eina_Condition
47 CRITICAL_SECTION waiters_count_lock;
48 CRITICAL_SECTION *mutex;
51 Eina_Bool was_broadcast;
62 Eina_Condition cond_read;
63 Eina_Condition cond_write;
68 EAPI extern Eina_Bool _eina_threads_activated;
71 static inline Eina_Bool
72 eina_lock_new(Eina_Lock *mutex)
74 InitializeCriticalSection(mutex);
80 eina_lock_free(Eina_Lock *mutex)
82 DeleteCriticalSection(mutex);
85 static inline Eina_Lock_Result
86 eina_lock_take(Eina_Lock *mutex)
88 #ifdef EINA_HAVE_ON_OFF_THREADS
89 if (!_eina_threads_activated) return EINA_LOCK_SUCCEED;
92 EnterCriticalSection(mutex);
94 return EINA_LOCK_SUCCEED;
97 static inline Eina_Lock_Result
98 eina_lock_take_try(Eina_Lock *mutex)
100 #ifdef EINA_HAVE_ON_OFF_THREADS
101 if (!_eina_threads_activated) return EINA_LOCK_SUCCEED;
104 return TryEnterCriticalSection(mutex) == 0 ? EINA_LOCK_FAIL : EINA_LOCK_SUCCEED;
107 static inline Eina_Lock_Result
108 eina_lock_release(Eina_Lock *mutex)
110 #ifdef EINA_HAVE_ON_OFF_THREADS
111 if (!_eina_threads_activated) return EINA_LOCK_SUCCEED;
114 LeaveCriticalSection(mutex);
116 return EINA_LOCK_SUCCEED;
120 eina_lock_debug(const Eina_Lock *mutex)
125 static inline Eina_Bool
126 eina_condition_new(Eina_Condition *cond, Eina_Lock *mutex)
129 #if _WIN32_WINNT >= 0x0600
130 InitializeConditionVariable(&cond->condition);
132 cond->waiters_count = 0;
133 cond->was_broadcast = EINA_FALSE;
134 cond->semaphore = CreateSemaphore(NULL, // no security
136 0x7fffffff, // max count
138 if (!cond->semaphore)
141 InitializeCriticalSection(&cond->waiters_count_lock);
143 cond->waiters_done = CreateEvent(NULL, // no security
145 FALSE, // non-signaled initially
147 if (!cond->waiters_done)
149 CloseHandle(cond->semaphore);
158 eina_condition_free(Eina_Condition *cond)
160 #if _WIN32_WINNT >= 0x0600
164 CloseHandle(cond->waiters_done);
165 DeleteCriticalSection(&cond->waiters_count_lock);
166 CloseHandle(cond->semaphore);
170 static inline Eina_Bool
171 _eina_condition_internal_timedwait(Eina_Condition *cond, DWORD t)
173 #if _WIN32_WINNT >= 0x0600
174 SleepConditionVariableCS(&cond->condition, cond->mutex, t);
177 Eina_Bool last_waiter;
179 /* Avoid race conditions. */
180 EnterCriticalSection(&cond->waiters_count_lock);
181 cond->waiters_count++;
182 LeaveCriticalSection(&cond->waiters_count_lock);
185 * This call atomically releases the mutex and waits on the
186 * semaphore until <pthread_cond_signal> or <pthread_cond_broadcast>
187 * are called by another thread.
189 ret = SignalObjectAndWait(cond->mutex, cond->semaphore, t, FALSE);
190 if (ret == WAIT_FAILED)
193 /* Reacquire lock to avoid race conditions. */
194 EnterCriticalSection(&cond->waiters_count_lock);
196 /* We're no longer waiting... */
197 cond->waiters_count--;
199 /* Check to see if we're the last waiter after <pthread_cond_broadcast>. */
200 last_waiter = (cond->was_broadcast) && (cond->waiters_count == 0);
202 LeaveCriticalSection(&cond->waiters_count_lock);
205 * If we're the last waiter thread during this particular broadcast
206 * then let all the other threads proceed.
211 * This call atomically signals the <waiters_done_> event and waits until
212 * it can acquire the <external_mutex>. This is required to ensure fairness.
214 ret = SignalObjectAndWait(cond->waiters_done, cond->mutex, t, FALSE);
215 if (ret == WAIT_FAILED)
221 * Always regain the external mutex since that's the guarantee we
222 * give to our callers.
224 ret = WaitForSingleObject(cond->mutex, t);
225 if (ret == WAIT_FAILED)
233 static inline Eina_Bool
234 eina_condition_timedwait(Eina_Condition *cond, double val)
236 return _eina_condition_internal_timedwait(cond, (DWORD)(val * 1000));
239 static inline Eina_Bool
240 eina_condition_wait(Eina_Condition *cond)
242 return _eina_condition_internal_timedwait(cond, INFINITE);
245 static inline Eina_Bool
246 eina_condition_broadcast(Eina_Condition *cond)
248 #if _WIN32_WINNT >= 0x0600
249 WakeAllConditionVariable(&cond->condition);
252 Eina_Bool have_waiters;
255 * This is needed to ensure that <waiters_count_> and <was_broadcast_> are
256 * consistent relative to each other.
258 EnterCriticalSection(&cond->waiters_count_lock);
259 have_waiters = EINA_FALSE;
261 if (cond->waiters_count > 0)
264 * We are broadcasting, even if there is just one waiter...
265 * Record that we are broadcasting, which helps optimize
266 * <pthread_cond_wait> for the non-broadcast case.
268 cond->was_broadcast = EINA_TRUE;
269 have_waiters = EINA_TRUE;
276 /* Wake up all the waiters atomically. */
277 ret = ReleaseSemaphore(cond->semaphore, cond->waiters_count, 0);
278 LeaveCriticalSection(&cond->waiters_count_lock);
279 if (!ret) return EINA_FALSE;
282 * Wait for all the awakened threads to acquire the counting
285 ret = WaitForSingleObject(cond->waiters_done, INFINITE);
286 if (ret == WAIT_FAILED)
289 * This assignment is okay, even without the <waiters_count_lock_> held
290 * because no other waiter threads can wake up to access it.
292 cond->was_broadcast = EINA_FALSE;
295 LeaveCriticalSection(&cond->waiters_count_lock);
301 static inline Eina_Bool
302 eina_condition_signal(Eina_Condition *cond)
304 #if _WIN32_WINNT >= 0x0600
305 WakeConditionVariable(&cond->condition);
307 Eina_Bool have_waiters;
309 EnterCriticalSection(&cond->waiters_count_lock);
310 have_waiters = (cond->waiters_count > 0);
311 LeaveCriticalSection(&cond->waiters_count_lock);
313 /* If there aren't any waiters, then this is a no-op. */
316 if (!ReleaseSemaphore(cond->semaphore, 1, 0))
324 static inline Eina_Bool
325 eina_rwlock_new(Eina_RWLock *mutex)
327 #if _WIN32_WINNT >= 0x0600
328 InitializeSRWLock(mutex->mutex);
331 if (!eina_lock_new(&(mutex->mutex))) return EINA_FALSE;
332 if (!eina_condition_new(&(mutex->cond_read), &(mutex->mutex)))
334 if (!eina_condition_new(&(mutex->cond_write), &(mutex->mutex)))
340 eina_condition_free(&(mutex->cond_read));
342 eina_lock_free(&(mutex->mutex));
348 eina_rwlock_free(Eina_RWLock *mutex)
350 #if _WIN32_WINNT >= 0x0600
353 eina_condition_free(&(mutex->cond_read));
354 eina_condition_free(&(mutex->cond_write));
355 eina_lock_free(&(mutex->mutex));
359 static inline Eina_Lock_Result
360 eina_rwlock_take_read(Eina_RWLock *mutex)
362 #if _WIN32_WINNT >= 0x0600
363 AcquireSRWLockShared(mutex->mutex);
364 mutex->is_read_mode = EINA_TRUE;
368 if (eina_lock_take(&(mutex->mutex)) == EINA_LOCK_FAIL)
369 return EINA_LOCK_FAIL;
373 mutex->readers_count++;
374 while (mutex->writers)
376 EnterCriticalSection(&mutex->cond_write.waiters_count_lock);
377 mutex->cond_read.waiters_count++;
378 LeaveCriticalSection(&mutex->cond_write.waiters_count_lock);
379 res = WaitForSingleObject(mutex->cond_write.semaphore, INFINITE);
380 if (res != WAIT_OBJECT_0) break;
382 mutex->readers_count--;
386 eina_lock_release(&(mutex->mutex));
389 return EINA_LOCK_SUCCEED;
392 static inline Eina_Lock_Result
393 eina_rwlock_take_write(Eina_RWLock *mutex)
395 #if _WIN32_WINNT >= 0x0600
396 AcquireSRWLockExclusive(mutex->mutex);
397 mutex->is_read_mode = EINA_FALSE;
401 if (eina_lock_take(&(mutex->mutex)) == EINA_LOCK_FAIL)
402 return EINA_LOCK_FAIL;
404 if (mutex->writers || mutex->readers > 0)
406 mutex->writers_count++;
407 while (mutex->writers || mutex->readers > 0)
409 EnterCriticalSection(&mutex->cond_write.waiters_count_lock);
410 mutex->cond_read.waiters_count++;
411 LeaveCriticalSection(&mutex->cond_write.waiters_count_lock);
412 res = WaitForSingleObject(mutex->cond_write.semaphore, INFINITE);
413 if (res != WAIT_OBJECT_0) break;
415 mutex->writers_count--;
417 if (res == 0) mutex->writers_count = 1;
418 eina_lock_release(&(mutex->mutex));
421 return EINA_LOCK_SUCCEED;
424 static inline Eina_Lock_Result
425 eina_rwlock_release(Eina_RWLock *mutex)
427 #if _WIN32_WINNT >= 0x0600
428 if (mutex->is_read_mode)
429 ReleaseSRWLockShared(mutex->mutex);
431 ReleaseSRWLockExclusive(mutex->mutex);
433 if (eina_lock_take(&(mutex->mutex)) == EINA_LOCK_FAIL)
434 return EINA_LOCK_FAIL;
439 if (mutex->readers_count == 1)
441 EnterCriticalSection(&mutex->cond_read.waiters_count_lock);
442 if (mutex->cond_read.waiters_count > 0)
443 ReleaseSemaphore(mutex->cond_read.semaphore, 1, 0);
444 LeaveCriticalSection(&mutex->cond_read.waiters_count_lock);
446 else if (mutex->readers_count > 0)
447 eina_condition_broadcast(&(mutex->cond_read));
448 else if (mutex->writers_count > 0)
450 EnterCriticalSection (&mutex->cond_write.waiters_count_lock);
451 if (mutex->cond_write.waiters_count > 0)
452 ReleaseSemaphore(mutex->cond_write.semaphore, 1, 0);
453 LeaveCriticalSection (&mutex->cond_write.waiters_count_lock);
456 else if (mutex->readers > 0)
459 if (mutex->readers == 0 && mutex->writers_count > 0)
461 EnterCriticalSection (&mutex->cond_write.waiters_count_lock);
462 if (mutex->cond_write.waiters_count > 0)
463 ReleaseSemaphore(mutex->cond_write.semaphore, 1, 0);
464 LeaveCriticalSection (&mutex->cond_write.waiters_count_lock);
467 eina_lock_release(&(mutex->mutex));
470 return EINA_LOCK_SUCCEED;
473 static inline Eina_Bool
474 eina_tls_new(Eina_TLS *key)
476 if ((*key = TlsAlloc()) == TLS_OUT_OF_INDEXES)
482 eina_tls_free(Eina_TLS key)
488 eina_tls_get(Eina_TLS key)
490 return (void*)TlsGetValue(key);
493 static inline Eina_Bool
494 eina_tls_set(Eina_TLS key, const void *data)
496 if (TlsSetValue(key, (LPVOID)data) == 0)
501 static inline Eina_Bool
502 eina_semaphore_new(Eina_Semaphore *sem, int count_init)
504 if (!sem || (count_init <= 0))
507 *sem = CreateSemaphore(NULL, count_init, 32767, NULL);
512 static inline Eina_Bool
513 eina_semaphore_free(Eina_Semaphore *sem)
521 static inline Eina_Bool
522 eina_semaphore_lock(Eina_Semaphore *sem)
529 res = WaitForSingleObject(*sem, 0L);
530 if (res == WAIT_OBJECT_0)
536 static inline Eina_Bool
537 eina_semaphore_release(Eina_Semaphore *sem, int count_release)
542 return ReleaseSemaphore(*sem, count_release, NULL) ? EINA_TRUE : EINA_FALSE;