Motr  M0
lockers.h
Go to the documentation of this file.
1 /* -*- C -*- */
2 /*
3  * Copyright (c) 2013-2020 Seagate Technology LLC and/or its Affiliates
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * For any questions about this software or licensing,
18  * please email opensource@seagate.com or cortx-questions@seagate.com.
19  *
20  */
21 
22 
23 
24 #pragma once
25 
26 #ifndef __MOTR_LIB_LOCKERS_H__
27 #define __MOTR_LIB_LOCKERS_H__
28 
29 #include "lib/types.h"
30 
77  /* Maximum number of keys that can be alloted */
78  uint32_t lot_max;
79  /* Current number of alloted keys */
80  bool *lot_inuse;
81 };
82 
83 struct m0_lockers {
84  void *loc_slots[0];
85 };
86 
87 #define M0_LOCKERS_DECLARE(scope, name, max) \
88  M0_LOCKERS__DECLARE(scope, name, name, max)
89 
90 #define M0_LOCKERS__DECLARE(scope, name, amb, max) \
91 struct name; \
92  \
93 enum { M0_LOCKERS_ ## name ## _max = (max) }; \
94  \
95 struct name ## _lockers { \
96  struct m0_lockers __base; \
97  void *__slots[(max)]; \
98 }; \
99  \
100 M0_BASSERT(offsetof(struct name ## _lockers, __slots[0]) == \
101  offsetof(struct m0_lockers, loc_slots[0])); \
102  \
103 scope void name ## _lockers_init(struct amb *par); \
104 scope void name ## _lockers_fini(struct amb *par); \
105 scope int name ## _lockers_allot(void); \
106 scope void name ## _lockers_free(int key); \
107 scope void name ## _lockers_set(struct amb *par, int key, void *data); \
108 scope void * name ## _lockers_get(const struct amb *par, int key); \
109 scope void name ## _lockers_clear(struct amb *par, int key); \
110 scope bool name ## _lockers_is_empty(const struct amb *par, int key)
111 
112 M0_INTERNAL void m0_lockers_init(const struct m0_lockers_type *lt,
113  struct m0_lockers *lockers);
114 
115 
116 M0_INTERNAL void m0_lockers_fini(struct m0_lockers_type *lt,
117  struct m0_lockers *lockers);
118 
123 M0_INTERNAL int m0_lockers_allot(struct m0_lockers_type *lt);
124 
129 M0_INTERNAL void m0_lockers_free(struct m0_lockers_type *lt, int key);
130 
137 M0_INTERNAL void m0_lockers_set(const struct m0_lockers_type *lt,
138  struct m0_lockers *lockers,
139  uint32_t key,
140  void *data);
146 M0_INTERNAL void *m0_lockers_get(const struct m0_lockers_type *lt,
147  const struct m0_lockers *lockers,
148  uint32_t key);
149 
155 M0_INTERNAL void m0_lockers_clear(const struct m0_lockers_type *lt,
156  struct m0_lockers *lockers,
157  uint32_t key);
158 
159 M0_INTERNAL bool m0_lockers_is_empty(const struct m0_lockers_type *lt,
160  const struct m0_lockers *lockers,
161  uint32_t key);
162 
163 #define M0_LOCKERS_DEFINE(scope, name, field) \
164  M0_LOCKERS__DEFINE(scope, name, name, field)
165 
166 #define M0_LOCKERS__DEFINE(scope, name, amb, field) \
167 scope bool __ ## name ## _inuse [M0_LOCKERS_ ## name ## _max]; \
168 scope struct m0_lockers_type name ## _lockers_type = { \
169  .lot_max = ARRAY_SIZE(M0_FIELD_VALUE(struct name ## _lockers, \
170  __slots)), \
171  .lot_inuse = __ ## name ## _inuse \
172 }; \
173  \
174 scope void name ## _lockers_init(struct amb *par) \
175 { \
176  m0_lockers_init(&name ## _lockers_type, &par->field.__base); \
177 } \
178  \
179 scope void name ## _lockers_fini(struct amb *par) \
180 { \
181  m0_lockers_fini(&name ## _lockers_type, &par->field.__base); \
182 } \
183  \
184 scope int name ## _lockers_allot(void) \
185 { \
186  return m0_lockers_allot(&name ## _lockers_type); \
187 } \
188  \
189 scope void name ## _lockers_free(int key) \
190 { \
191  m0_lockers_free(&name ## _lockers_type, key); \
192 } \
193  \
194 scope void name ## _lockers_set(struct amb *par, int key, void *data) \
195 { \
196  m0_lockers_set(&name ## _lockers_type, \
197  &par->field.__base, key, data); \
198 } \
199  \
200 scope void * name ## _lockers_get(const struct amb *par, int key) \
201 { \
202  return m0_lockers_get(&name ## _lockers_type, \
203  &par->field.__base, key); \
204 } \
205  \
206 scope void name ## _lockers_clear(struct amb *par, int key) \
207 { \
208  m0_lockers_clear(&name ## _lockers_type, &par->field.__base, key); \
209 } \
210  \
211 scope bool name ## _lockers_is_empty(const struct amb *par, int key) \
212 { \
213  return m0_lockers_is_empty(&name ## _lockers_type, \
214  &par->field.__base, key); \
215 } \
216  \
217 struct __ ## type ## _semicolon_catcher
218 
220 #endif /* __MOTR_LIB_LOCKERS_H__ */
221 
222 /*
223  * Local variables:
224  * c-indentation-style: "K&R"
225  * c-basic-offset: 8
226  * tab-width: 8
227  * fill-column: 80
228  * scroll-step: 1
229  * End:
230  */
231 /*
232  * vim: tabstop=8 shiftwidth=8 noexpandtab textwidth=80 nowrap
233  */
bool * lot_inuse
Definition: lockers.h:80
M0_INTERNAL void m0_lockers_fini(struct m0_lockers_type *lt, struct m0_lockers *lockers)
Definition: lockers.c:96
struct m0_bufvec data
Definition: di.c:40
M0_INTERNAL void * m0_lockers_get(const struct m0_lockers_type *lt, const struct m0_lockers *lockers, uint32_t key)
Definition: lockers.c:72
M0_INTERNAL int m0_lockers_allot(struct m0_lockers_type *lt)
Definition: lockers.c:44
M0_INTERNAL void m0_lockers_clear(const struct m0_lockers_type *lt, struct m0_lockers *lockers, uint32_t key)
Definition: lockers.c:80
M0_INTERNAL bool m0_lockers_is_empty(const struct m0_lockers_type *lt, const struct m0_lockers *lockers, uint32_t key)
Definition: lockers.c:88
M0_INTERNAL void m0_lockers_set(const struct m0_lockers_type *lt, struct m0_lockers *lockers, uint32_t key, void *data)
Definition: lockers.c:63
uint32_t lot_max
Definition: lockers.h:78
M0_INTERNAL void m0_lockers_init(const struct m0_lockers_type *lt, struct m0_lockers *lockers)
Definition: lockers.c:37
void * loc_slots[0]
Definition: lockers.h:84
M0_INTERNAL void m0_lockers_free(struct m0_lockers_type *lt, int key)
Definition: lockers.c:57
Definition: idx_mock.c:47