lwpa  0.1.0
LightWeight Platform Abstraction (lwpa)
View other versions:
lwpa_lock.h
1 /******************************************************************************
2  * Copyright 2018 ETC Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *******************************************************************************
16  * This file is a part of lwpa. For more information, go to:
17  * https://github.com/ETCLabs/lwpa
18  ******************************************************************************/
19 
20 #ifndef _LWPA_LOCK_H_
21 #define _LWPA_LOCK_H_
22 
23 #include <winsock2.h> /* To fix winsock include order issues */
24 #include <windows.h>
25 #include "lwpa_common.h"
26 #include "lwpa_bool.h"
27 
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31 
32 /* Critical sections have been shown by empirical testing to provide a large
33  * efficiency boost over Windows mutexes. */
34 typedef struct
35 {
36  bool valid;
37  CRITICAL_SECTION cs;
38 } lwpa_mutex_t;
39 
41 #define lwpa_mutex_take(idptr, wait_ms) \
42  (((idptr) && (idptr)->valid) \
43  ? (((wait_ms) == 0) ? TryEnterCriticalSection(&(idptr)->cs) : (EnterCriticalSection(&(idptr)->cs), true)) \
44  : false)
45 #define lwpa_mutex_give(idptr) \
46  do \
47  { \
48  if ((idptr) && (idptr)->valid) \
49  LeaveCriticalSection(&(idptr)->cs); \
50  } while (0)
51 #define lwpa_mutex_destroy(idptr) \
52  do \
53  { \
54  if ((idptr) && (idptr)->valid) \
55  { \
56  DeleteCriticalSection(&(idptr)->cs); \
57  (idptr)->valid = false; \
58  } \
59  } while (0)
60 
61 typedef HANDLE lwpa_signal_t;
62 
64 #define lwpa_signal_wait(idptr, wait_ms) \
65  ((idptr) ? (WAIT_OBJECT_0 == \
66  WaitForSingleObject(*(idptr), ((wait_ms) == LWPA_WAIT_FOREVER ? INFINITE : (DWORD)wait_ms))) \
67  : false)
68 #define lwpa_signal_post(idptr) \
69  do \
70  { \
71  if (idptr) \
72  SetEvent((HANDLE) * (idptr)); \
73  } while (0)
74 #define lwpa_signal_destroy(idptr) \
75  do \
76  { \
77  if (idptr) \
78  CloseHandle((HANDLE) * (idptr)); \
79  } while (0)
80 
81 typedef struct
82 {
83  bool valid;
84  CRITICAL_SECTION cs;
85  LONG reader_count;
87 
89 bool lwpa_rwlock_readlock(lwpa_rwlock_t *id, int wait_ms);
90 #define lwpa_rwlock_readunlock(idptr) \
91  do \
92  { \
93  if (idptr) \
94  InterlockedDecrement(&(idptr)->reader_count); \
95  } while (0)
96 bool lwpa_rwlock_writelock(lwpa_rwlock_t *id, int wait_ms);
97 #define lwpa_rwlock_writeunlock(idptr) \
98  do \
99  { \
100  if (idptr) \
101  LeaveCriticalSection(&(idptr)->cs); \
102  } while (0)
103 #define lwpa_rwlock_destroy(idptr) \
104  do \
105  { \
106  if ((idptr) && (idptr)->valid) \
107  DeleteCriticalSection(&(idptr)->cs); \
108  (idptr)->valid = false; \
109  } while (0)
110 
111 #ifdef __cplusplus
112 }
113 #endif
114 
115 #endif /* _LWPA_LOCK_H_ */
bool lwpa_mutex_create(lwpa_mutex_t *id)
Create a new mutex.
Definition: lwpa_lock.c:26
UNDEFINED lwpa_mutex_t
The mutex identifier.
Definition: lwpa_lock.dox:38
bool lwpa_rwlock_writelock(lwpa_rwlock_t *id, int wait_ms)
Access a read-write lock for writing.
Definition: lwpa_lock.c:127
UNDEFINED lwpa_rwlock_t
The read-write lock identifier.
Definition: lwpa_lock.dox:147
bool lwpa_rwlock_create(lwpa_rwlock_t *id)
Create a new read-write lock.
Definition: lwpa_lock.c:95
bool lwpa_rwlock_readlock(lwpa_rwlock_t *id, int wait_ms)
Access a read-write lock for reading.
Definition: lwpa_lock.c:105
bool lwpa_signal_create(lwpa_signal_t *id)
Create a new signal.
Definition: lwpa_lock.c:56
UNDEFINED lwpa_signal_t
The signal identifier.
Definition: lwpa_lock.dox:83
Definition: lwpa_lock.h:35
Definition: lwpa_lock.h:47