aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/libraries/eina/src/include/eina_inline_lock_win32.x
blob: 072095c57c96dd895322e6e55054ad51418d8c95 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
/* EINA - EFL data type library
 * Copyright (C) 2011 Vincent Torri
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library;
 * if not, see <http://www.gnu.org/licenses/>.
 */

#ifndef EINA_INLINE_LOCK_WIN32_X_
#define EINA_INLINE_LOCK_WIN32_X_

#include <windows.h>

typedef CRITICAL_SECTION       Eina_Lock;
typedef struct _Eina_Condition Eina_Condition;

#if _WIN32_WINNT >= 0x0600
struct _Eina_Condition
{
   CRITICAL_SECTION  *mutex;
   CONDITION_VARIABLE condition;
};
#else
struct _Eina_Condition
{
   int               waiters_count;
   CRITICAL_SECTION  waiters_count_lock;
   CRITICAL_SECTION *mutex;
   HANDLE            semaphore;
   HANDLE            waiters_done;
   Eina_Bool         was_broadcast;
};
#endif

typedef struct _Eina_Win32_RWLock Eina_RWLock;

struct _Eina_Win32_RWLock
{
   LONG readers_count;
   LONG writers_count;
   int readers;
   int writers;

   Eina_Lock      mutex;
   Eina_Condition cond_read;
   Eina_Condition cond_write;
};

typedef DWORD     Eina_TLS;

EAPI extern Eina_Bool _eina_threads_activated;

static inline Eina_Bool
eina_lock_new(Eina_Lock *mutex)
{
   InitializeCriticalSection(mutex);

   return EINA_TRUE;
}

static inline void
eina_lock_free(Eina_Lock *mutex)
{
   DeleteCriticalSection(mutex);
}

static inline Eina_Lock_Result
eina_lock_take(Eina_Lock *mutex)
{
#ifdef EINA_HAVE_ON_OFF_THREADS
  if (!_eina_threads_activated) return EINA_LOCK_SUCCEED;
#endif

   EnterCriticalSection(mutex);

   return EINA_LOCK_SUCCEED;
}

static inline Eina_Lock_Result
eina_lock_take_try(Eina_Lock *mutex)
{
#ifdef EINA_HAVE_ON_OFF_THREADS
   if (!_eina_threads_activated) return EINA_LOCK_SUCCEED;
#endif

   return TryEnterCriticalSection(mutex) == 0 ? EINA_LOCK_FAIL : EINA_LOCK_SUCCEED;
}

static inline Eina_Lock_Result
eina_lock_release(Eina_Lock *mutex)
{
#ifdef EINA_HAVE_ON_OFF_THREADS
   if (!_eina_threads_activated) return EINA_LOCK_SUCCEED;
#endif

   LeaveCriticalSection(mutex);

   return EINA_LOCK_SUCCEED;
}

static inline void
eina_lock_debug(const Eina_Lock *mutex)
{
   (void)mutex;
}

static inline Eina_Bool
eina_condition_new(Eina_Condition *cond, Eina_Lock *mutex)
{
   cond->mutex = mutex;
#if _WIN32_WINNT >= 0x0600
   InitializeConditionVariable(&cond->condition);
#else
   cond->waiters_count = 0;
   cond->was_broadcast = EINA_FALSE;
   cond->semaphore = CreateSemaphore(NULL,       // no security
                                     0,          // initially 0
                                     0x7fffffff, // max count
                                     NULL);      // unnamed
   if (!cond->semaphore)
     return EINA_FALSE;

   InitializeCriticalSection(&cond->waiters_count_lock);

   cond->waiters_done = CreateEvent(NULL,  // no security
                                    FALSE, // auto-reset
                                    FALSE, // non-signaled initially
                                    NULL); // unnamed
   if (!cond->waiters_done)
     {
        CloseHandle(cond->semaphore);
        return EINA_FALSE;
     }

   return EINA_TRUE;
#endif
}

static inline void
eina_condition_free(Eina_Condition *cond)
{
#if _WIN32_WINNT >= 0x0600
   /* Nothing to do */
#else
   CloseHandle(cond->waiters_done);
   DeleteCriticalSection(&cond->waiters_count_lock);
   CloseHandle(cond->semaphore);
#endif
}

static inline Eina_Bool
_eina_condition_internal_timedwait(Eina_Condition *cond, DWORD t)
{
#if _WIN32_WINNT >= 0x0600
   SleepConditionVariableCS(&cond->condition, cond->mutex, t);
#else
   DWORD ret;
   Eina_Bool last_waiter;

   /* Avoid race conditions. */
   EnterCriticalSection(&cond->waiters_count_lock);
   cond->waiters_count++;
   LeaveCriticalSection(&cond->waiters_count_lock);

   /*
    * This call atomically releases the mutex and waits on the
    * semaphore until <pthread_cond_signal> or <pthread_cond_broadcast>
    * are called by another thread.
    */
   ret = SignalObjectAndWait(cond->mutex, cond->semaphore, t, FALSE);
   if (ret == WAIT_FAILED)
     return EINA_FALSE;

   /* Reacquire lock to avoid race conditions. */
   EnterCriticalSection(&cond->waiters_count_lock);

   /* We're no longer waiting... */
   cond->waiters_count--;

   /* Check to see if we're the last waiter after <pthread_cond_broadcast>. */
   last_waiter = (cond->was_broadcast) && (cond->waiters_count == 0);

   LeaveCriticalSection(&cond->waiters_count_lock);

   /*
    * If we're the last waiter thread during this particular broadcast
    * then let all the other threads proceed.
    */
  if (last_waiter)
    {
       /*
        * This call atomically signals the <waiters_done_> event and waits until
        * it can acquire the <external_mutex>.  This is required to ensure fairness.
        */
       ret = SignalObjectAndWait(cond->waiters_done, cond->mutex, t, FALSE);
       if (ret == WAIT_FAILED)
         return EINA_FALSE;
    }
  else
    {
       /*
        * Always regain the external mutex since that's the guarantee we
        * give to our callers.
        */
       ret = WaitForSingleObject(cond->mutex, t);
       if (ret == WAIT_FAILED)
         return EINA_FALSE;
    }
#endif

   return EINA_TRUE;
}

static inline Eina_Bool
eina_condition_timedwait(Eina_Condition *cond, double val)
{
   return _eina_condition_internal_timedwait(cond, (DWORD)(val * 1000));
}

static inline Eina_Bool
eina_condition_wait(Eina_Condition *cond)
{
   return _eina_condition_internal_timedwait(cond, INFINITE);
}

static inline Eina_Bool
eina_condition_broadcast(Eina_Condition *cond)
{
#if _WIN32_WINNT >= 0x0600
   WakeAllConditionVariable(&cond->condition);
   return EINA_TRUE;
#else
   Eina_Bool have_waiters;

   /*
    * This is needed to ensure that <waiters_count_> and <was_broadcast_> are
    * consistent relative to each other.
    */
   EnterCriticalSection(&cond->waiters_count_lock);
   have_waiters = EINA_FALSE;

   if (cond->waiters_count > 0)
     {
        /*
         * We are broadcasting, even if there is just one waiter...
         * Record that we are broadcasting, which helps optimize
         * <pthread_cond_wait> for the non-broadcast case.
         */
        cond->was_broadcast = EINA_TRUE;
        have_waiters = EINA_TRUE;
     }

   if (have_waiters)
     {
        DWORD ret;

        /* Wake up all the waiters atomically. */
        ret = ReleaseSemaphore(cond->semaphore, cond->waiters_count, 0);
        LeaveCriticalSection(&cond->waiters_count_lock);
        if (!ret) return EINA_FALSE;

        /*
         * Wait for all the awakened threads to acquire the counting
         * semaphore.
         */
        ret = WaitForSingleObject(cond->waiters_done, INFINITE);
        if (ret == WAIT_FAILED)
          return EINA_FALSE;
        /*
         * This assignment is okay, even without the <waiters_count_lock_> held
         * because no other waiter threads can wake up to access it.
         */
        cond->was_broadcast = EINA_FALSE;
     }
   else
     LeaveCriticalSection(&cond->waiters_count_lock);

   return EINA_TRUE;
#endif
}

static inline Eina_Bool
eina_condition_signal(Eina_Condition *cond)
{
#if _WIN32_WINNT >= 0x0600
   WakeConditionVariable(&cond->condition);
#else
   Eina_Bool have_waiters;

   EnterCriticalSection(&cond->waiters_count_lock);
   have_waiters = (cond->waiters_count > 0);
   LeaveCriticalSection(&cond->waiters_count_lock);

   /* If there aren't any waiters, then this is a no-op. */
  if (have_waiters)
    {
       if (!ReleaseSemaphore(cond->semaphore, 1, 0))
         return EINA_FALSE;
    }

   return EINA_TRUE;
#endif
}

static inline Eina_Bool
eina_rwlock_new(Eina_RWLock *mutex)
{
   if (!eina_lock_new(&(mutex->mutex))) return EINA_FALSE;
   if (!eina_condition_new(&(mutex->cond_read), &(mutex->mutex)))
     goto on_error1;
   if (!eina_condition_new(&(mutex->cond_write), &(mutex->mutex)))
     goto on_error2;

   return EINA_TRUE;

 on_error2:
   eina_condition_free(&(mutex->cond_read));
 on_error1:
   eina_lock_free(&(mutex->mutex));
   return EINA_FALSE;
}

static inline void
eina_rwlock_free(Eina_RWLock *mutex)
{
   eina_condition_free(&(mutex->cond_read));
   eina_condition_free(&(mutex->cond_write));
   eina_lock_free(&(mutex->mutex));
}

static inline Eina_Lock_Result
eina_rwlock_take_read(Eina_RWLock *mutex)
{
   DWORD res;

   if (eina_lock_take(&(mutex->mutex)) == EINA_LOCK_FAIL)
     return EINA_LOCK_FAIL;

   if (mutex->writers)
     {
        mutex->readers_count++;
        while (mutex->writers)
          {
             EnterCriticalSection(&mutex->cond_write.waiters_count_lock);
             mutex->cond_read.waiters_count++;
             LeaveCriticalSection(&mutex->cond_write.waiters_count_lock);
             res = WaitForSingleObject(mutex->cond_write.semaphore, INFINITE);
             if (res != WAIT_OBJECT_0) break;
          }
        mutex->readers_count--;
     }
   if (res == 0)
     mutex->readers++;
   eina_lock_release(&(mutex->mutex));

   return EINA_LOCK_SUCCEED;
}

static inline Eina_Lock_Result
eina_rwlock_take_write(Eina_RWLock *mutex)
{
   DWORD res;

   if (eina_lock_take(&(mutex->mutex)) == EINA_LOCK_FAIL)
     return EINA_LOCK_FAIL;

   if (mutex->writers || mutex->readers > 0)
     {
        mutex->writers_count++;
        while (mutex->writers || mutex->readers > 0)
          {
             EnterCriticalSection(&mutex->cond_write.waiters_count_lock);
             mutex->cond_read.waiters_count++;
             LeaveCriticalSection(&mutex->cond_write.waiters_count_lock);
             res = WaitForSingleObject(mutex->cond_write.semaphore, INFINITE);
             if (res != WAIT_OBJECT_0) break;
          }
        mutex->writers_count--;
     }
   if (res == 0) mutex->writers_count = 1;
   eina_lock_release(&(mutex->mutex));

   return EINA_LOCK_SUCCEED;
}

static inline Eina_Lock_Result
eina_rwlock_release(Eina_RWLock *mutex)
{
   if (eina_lock_take(&(mutex->mutex)) == EINA_LOCK_FAIL)
     return EINA_LOCK_FAIL;

   if (mutex->writers)
     {
        mutex->writers = 0;
        if (mutex->readers_count == 1)
          {
             EnterCriticalSection(&mutex->cond_read.waiters_count_lock);
             if (mutex->cond_read.waiters_count > 0)
               ReleaseSemaphore(mutex->cond_read.semaphore, 1, 0);
             LeaveCriticalSection(&mutex->cond_read.waiters_count_lock);
          }
        else if (mutex->readers_count > 0)
          eina_condition_broadcast(&(mutex->cond_read));
        else if (mutex->writers_count > 0)
          {
             EnterCriticalSection (&mutex->cond_write.waiters_count_lock);
             if (mutex->cond_write.waiters_count > 0)
               ReleaseSemaphore(mutex->cond_write.semaphore, 1, 0);
             LeaveCriticalSection (&mutex->cond_write.waiters_count_lock);
          }
     }
   else if (mutex->readers > 0)
     {
        mutex->readers--;
        if (mutex->readers == 0 && mutex->writers_count > 0)
          {
             EnterCriticalSection (&mutex->cond_write.waiters_count_lock);
             if (mutex->cond_write.waiters_count > 0)
               ReleaseSemaphore(mutex->cond_write.semaphore, 1, 0);
             LeaveCriticalSection (&mutex->cond_write.waiters_count_lock);
          }
     }
   eina_lock_release(&(mutex->mutex));

   return EINA_LOCK_SUCCEED;
}

static inline Eina_Bool
eina_tls_new(Eina_TLS *key)
{
   if ((*key = TlsAlloc()) == TLS_OUT_OF_INDEXES)
      return EINA_FALSE;
   return EINA_TRUE;
}

static inline void
eina_tls_free(Eina_TLS key)
{
   TlsFree(key);
}

static inline void *
eina_tls_get(Eina_TLS key)
{
   return (void*)TlsGetValue(key);
}

static inline Eina_Bool
eina_tls_set(Eina_TLS key, const void *data)
{
   if (TlsSetValue(key, (LPVOID)data) == 0)
      return EINA_FALSE;
   return EINA_TRUE;
}

#endif