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)))
337 mutex->readers_count = 0;
338 mutex->writers_count = 0;
345 eina_condition_free(&(mutex->cond_read));
347 eina_lock_free(&(mutex->mutex));
353 eina_rwlock_free(Eina_RWLock *mutex)
355 #if _WIN32_WINNT >= 0x0600
358 eina_condition_free(&(mutex->cond_read));
359 eina_condition_free(&(mutex->cond_write));
360 eina_lock_free(&(mutex->mutex));
364 static inline Eina_Lock_Result
365 eina_rwlock_take_read(Eina_RWLock *mutex)
367 #if _WIN32_WINNT >= 0x0600
368 AcquireSRWLockShared(&mutex->mutex);
369 mutex->is_read_mode = EINA_TRUE;
373 if (eina_lock_take(&(mutex->mutex)) == EINA_LOCK_FAIL)
374 return EINA_LOCK_FAIL;
378 mutex->readers_count++;
379 while (mutex->writers)
381 EnterCriticalSection(&mutex->cond_write.waiters_count_lock);
382 mutex->cond_read.waiters_count++;
383 LeaveCriticalSection(&mutex->cond_write.waiters_count_lock);
384 res = WaitForSingleObject(mutex->cond_write.semaphore, INFINITE);
385 if (res != WAIT_OBJECT_0) break;
387 mutex->readers_count--;
391 eina_lock_release(&(mutex->mutex));
394 return EINA_LOCK_SUCCEED;
397 static inline Eina_Lock_Result
398 eina_rwlock_take_write(Eina_RWLock *mutex)
400 #if _WIN32_WINNT >= 0x0600
401 AcquireSRWLockExclusive(&mutex->mutex);
402 mutex->is_read_mode = EINA_FALSE;
406 if (eina_lock_take(&(mutex->mutex)) == EINA_LOCK_FAIL)
407 return EINA_LOCK_FAIL;
409 if (mutex->writers || mutex->readers > 0)
411 mutex->writers_count++;
412 while (mutex->writers || mutex->readers > 0)
414 EnterCriticalSection(&mutex->cond_write.waiters_count_lock);
415 mutex->cond_read.waiters_count++;
416 LeaveCriticalSection(&mutex->cond_write.waiters_count_lock);
417 res = WaitForSingleObject(mutex->cond_write.semaphore, INFINITE);
418 if (res != WAIT_OBJECT_0) break;
420 mutex->writers_count--;
422 if (res == 0) mutex->writers = 1;
423 eina_lock_release(&(mutex->mutex));
426 return EINA_LOCK_SUCCEED;
429 static inline Eina_Lock_Result
430 eina_rwlock_release(Eina_RWLock *mutex)
432 #if _WIN32_WINNT >= 0x0600
433 if (mutex->is_read_mode)
434 ReleaseSRWLockShared(&mutex->mutex);
436 ReleaseSRWLockExclusive(&mutex->mutex);
438 if (eina_lock_take(&(mutex->mutex)) == EINA_LOCK_FAIL)
439 return EINA_LOCK_FAIL;
444 if (mutex->readers_count == 1)
446 EnterCriticalSection(&mutex->cond_read.waiters_count_lock);
447 if (mutex->cond_read.waiters_count > 0)
448 ReleaseSemaphore(mutex->cond_read.semaphore, 1, 0);
449 LeaveCriticalSection(&mutex->cond_read.waiters_count_lock);
451 else if (mutex->readers_count > 0)
452 eina_condition_broadcast(&(mutex->cond_read));
453 else if (mutex->writers_count > 0)
455 EnterCriticalSection (&mutex->cond_write.waiters_count_lock);
456 if (mutex->cond_write.waiters_count > 0)
457 ReleaseSemaphore(mutex->cond_write.semaphore, 1, 0);
458 LeaveCriticalSection (&mutex->cond_write.waiters_count_lock);
461 else if (mutex->readers > 0)
464 if (mutex->readers == 0 && mutex->writers_count > 0)
466 EnterCriticalSection (&mutex->cond_write.waiters_count_lock);
467 if (mutex->cond_write.waiters_count > 0)
468 ReleaseSemaphore(mutex->cond_write.semaphore, 1, 0);
469 LeaveCriticalSection (&mutex->cond_write.waiters_count_lock);
472 eina_lock_release(&(mutex->mutex));
475 return EINA_LOCK_SUCCEED;
478 static inline Eina_Bool
479 eina_tls_new(Eina_TLS *key)
481 if ((*key = TlsAlloc()) == TLS_OUT_OF_INDEXES)
487 eina_tls_free(Eina_TLS key)
493 eina_tls_get(Eina_TLS key)
495 return (void*)TlsGetValue(key);
498 static inline Eina_Bool
499 eina_tls_set(Eina_TLS key, const void *data)
501 if (TlsSetValue(key, (LPVOID)data) == 0)
506 static inline Eina_Bool
507 eina_semaphore_new(Eina_Semaphore *sem, int count_init)
509 if (!sem || (count_init <= 0))
512 *sem = CreateSemaphore(NULL, count_init, 32767, NULL);
517 static inline Eina_Bool
518 eina_semaphore_free(Eina_Semaphore *sem)
526 static inline Eina_Bool
527 eina_semaphore_lock(Eina_Semaphore *sem)
534 res = WaitForSingleObject(*sem, 0L);
535 if (res == WAIT_OBJECT_0)
541 static inline Eina_Bool
542 eina_semaphore_release(Eina_Semaphore *sem, int count_release)
547 return ReleaseSemaphore(*sem, count_release, NULL) ? EINA_TRUE : EINA_FALSE;