(メッセージはありません)
@@ -1,5 +1,5 @@ | ||
1 | 1 | /* |
2 | - ChibiOS - Copyright (C) 2006..2021 Giovanni Di Sirio | |
2 | + ChibiOS - Copyright (C) 2006..2023 Giovanni Di Sirio | |
3 | 3 | |
4 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); |
5 | 5 | you may not use this file except in compliance with the License. |
@@ -1,5 +1,5 @@ | ||
1 | 1 | /* |
2 | - ChibiOS - Copyright (C) 2006..2021 Giovanni Di Sirio | |
2 | + ChibiOS - Copyright (C) 2006..2023 Giovanni Di Sirio | |
3 | 3 | |
4 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); |
5 | 5 | you may not use this file except in compliance with the License. |
@@ -33,6 +33,11 @@ | ||
33 | 33 | #include "oop_base_object.h" |
34 | 34 | |
35 | 35 | /** |
36 | + * @brief Type of a references counter. | |
37 | + */ | |
38 | +typedef unsigned int oop_object_references_t; | |
39 | + | |
40 | +/** | |
36 | 41 | * @brief Type of a referenced object class. |
37 | 42 | */ |
38 | 43 | typedef struct referenced_object referenced_object_c; |
@@ -50,7 +55,7 @@ | ||
50 | 55 | */ |
51 | 56 | #define __referenced_object_data \ |
52 | 57 | __base_object_data \ |
53 | - unsigned references; | |
58 | + oop_object_references_t references; | |
54 | 59 | |
55 | 60 | |
56 | 61 | /** |
@@ -88,7 +93,7 @@ | ||
88 | 93 | referenced_object_c *objp = (referenced_object_c *)ip; |
89 | 94 | |
90 | 95 | __base_object_objinit_impl(ip, vmt); |
91 | - objp->references = 1U; | |
96 | + objp->references = (oop_object_references_t)1; | |
92 | 97 | |
93 | 98 | return ip; |
94 | 99 | } |
@@ -101,9 +106,11 @@ | ||
101 | 106 | */ |
102 | 107 | CC_FORCE_INLINE |
103 | 108 | static inline void __referenced_object_dispose_impl(void *ip) { |
109 | + referenced_object_c *objp = (referenced_object_c *)ip; | |
104 | 110 | |
105 | - /* TODO assertion */ | |
106 | - __base_object_dispose_impl(ip); | |
111 | + osalDbgAssert(objp->references == (oop_object_references_t)0, "not zero"); | |
112 | + | |
113 | + __base_object_dispose_impl(objp); | |
107 | 114 | } |
108 | 115 | |
109 | 116 | /** |
@@ -118,6 +125,8 @@ | ||
118 | 125 | |
119 | 126 | objp->references++; |
120 | 127 | |
128 | + osalDbgAssert(objp->references != (oop_object_references_t)0, "overflow"); | |
129 | + | |
121 | 130 | return ip; |
122 | 131 | } |
123 | 132 |
@@ -128,7 +137,7 @@ | ||
128 | 137 | * @return Remaining references. |
129 | 138 | */ |
130 | 139 | CC_FORCE_INLINE |
131 | -static inline unsigned __referenced_object_getref_impl(void *ip) { | |
140 | +static inline oop_object_references_t __referenced_object_getref_impl(void *ip) { | |
132 | 141 | referenced_object_c *objp = (referenced_object_c *)ip; |
133 | 142 | |
134 | 143 | return objp->references; |
@@ -141,7 +150,7 @@ | ||
141 | 150 | * @return The number of references left. |
142 | 151 | */ |
143 | 152 | CC_FORCE_INLINE |
144 | -static inline unsigned __referenced_object_release_impl(void *ip) { | |
153 | +static inline oop_object_references_t __referenced_object_release_impl(void *ip) { | |
145 | 154 | referenced_object_c *objp = (referenced_object_c *)ip; |
146 | 155 | |
147 | 156 | osalDbgAssert(objp->references > 0U, "zero references"); |
@@ -174,7 +183,7 @@ | ||
174 | 183 | * @return The number of references left. |
175 | 184 | */ |
176 | 185 | CC_FORCE_INLINE |
177 | -static inline unsigned roRelease(void *ip) { | |
186 | +static inline oop_object_references_t roRelease(void *ip) { | |
178 | 187 | referenced_object_c *objp = (referenced_object_c *)ip; |
179 | 188 | |
180 | 189 | return objp->vmt->release(ip); |
@@ -1,5 +1,5 @@ | ||
1 | 1 | /* |
2 | - ChibiOS - Copyright (C) 2006..2021 Giovanni Di Sirio | |
2 | + ChibiOS - Copyright (C) 2006..2023 Giovanni Di Sirio | |
3 | 3 | |
4 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); |
5 | 5 | you may not use this file except in compliance with the License. |
@@ -29,7 +29,7 @@ | ||
29 | 29 | #ifndef OOP_SYNCHRONIZED_OBJECT_H |
30 | 30 | #define OOP_SYNCHRONIZED_OBJECT_H |
31 | 31 | |
32 | -#include "ch.h" | |
32 | +#include "osal.h" | |
33 | 33 | #include "oop_referenced_object.h" |
34 | 34 | |
35 | 35 | /** |
@@ -86,7 +86,7 @@ | ||
86 | 86 | synchronized_object_c *objp = (synchronized_object_c *)ip; |
87 | 87 | |
88 | 88 | __referenced_object_objinit_impl(ip, vmt); |
89 | - chMtxObjectInit(&objp->mutex); | |
89 | + osalMutexObjectInit(&objp->mutex); | |
90 | 90 | |
91 | 91 | return ip; |
92 | 92 | } |
@@ -114,7 +114,7 @@ | ||
114 | 114 | static inline void soLock(void *ip) { |
115 | 115 | synchronized_object_c *objp = (synchronized_object_c *)ip; |
116 | 116 | |
117 | - chMtxLock(&objp->mutex); | |
117 | + osalMutexLock(&objp->mutex); | |
118 | 118 | } |
119 | 119 | |
120 | 120 | /** |
@@ -126,7 +126,7 @@ | ||
126 | 126 | static inline void soUnlock(void *ip) { |
127 | 127 | synchronized_object_c *objp = (synchronized_object_c *)ip; |
128 | 128 | |
129 | - chMtxUnlock(&objp->mutex); | |
129 | + osalMutexUnlock(&objp->mutex); | |
130 | 130 | } |
131 | 131 | |
132 | 132 | #endif /* OOP_SYNCHRONIZED_OBJECT_H */ |
@@ -1,5 +1,5 @@ | ||
1 | 1 | /* |
2 | - ChibiOS - Copyright (C) 2006..2018 Giovanni Di Sirio | |
2 | + ChibiOS - Copyright (C) 2006..2023 Giovanni Di Sirio | |
3 | 3 | |
4 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); |
5 | 5 | you may not use this file except in compliance with the License. |
@@ -37,14 +37,54 @@ | ||
37 | 37 | /* Driver constants. */ |
38 | 38 | /*===========================================================================*/ |
39 | 39 | |
40 | +/** | |
41 | + * @name Common driver states | |
42 | + * @{ | |
43 | + */ | |
44 | +#define HAL_DRV_STATE_UNINIT 0U | |
45 | +#define HAL_DRV_STATE_STOPPED 1U | |
46 | +#define HAL_DRV_STATE_READY 2U | |
47 | +#define HAL_DRV_STATE_ACTIVE 3U | |
48 | +#define HAL_DRV_STATE_ERROR 4U | |
49 | +/** @} */ | |
50 | + | |
40 | 51 | /*===========================================================================*/ |
41 | 52 | /* Driver pre-compile time settings. */ |
42 | 53 | /*===========================================================================*/ |
43 | 54 | |
55 | +/** | |
56 | + * @name Common driver configuration options | |
57 | + * @{ | |
58 | + */ | |
59 | +/** | |
60 | + * @brief Enables the mutual exclusion APIs on driver instances. | |
61 | + * @note Disabling this option saves both code and data space. | |
62 | + */ | |
63 | +#if !defined(HAL_USE_MUTUAL_EXCLUSION) || defined(__DOXYGEN__) | |
64 | +#define HAL_USE_MUTUAL_EXCLUSION TRUE | |
65 | +#endif | |
66 | + | |
67 | +/** | |
68 | + * @brief Enables the HAL registry for drivers. | |
69 | + * @note Disabling this option saves both code and data space. | |
70 | + */ | |
71 | +#if !defined(HAL_USE_REGISTRY) || defined(__DOXYGEN__) | |
72 | +#define HAL_USE_REGISTRY TRUE | |
73 | +#endif | |
74 | +/** @} */ | |
75 | + | |
44 | 76 | /*===========================================================================*/ |
45 | 77 | /* Derived constants and error checks. */ |
46 | 78 | /*===========================================================================*/ |
47 | 79 | |
80 | +#if (HAL_USE_MUTUAL_EXCLUSION != TRUE) && (HAL_USE_MUTUAL_EXCLUSION != FALSE) | |
81 | +#error "invalid HAL_USE_MUTUAL_EXCLUSION value" | |
82 | +#endif | |
83 | + | |
84 | +#if (HAL_USE_REGISTRY != TRUE) && (HAL_USE_REGISTRY != FALSE) | |
85 | +#error "invalid HAL_USE_REGISTRY value" | |
86 | +#endif | |
87 | + | |
48 | 88 | /*===========================================================================*/ |
49 | 89 | /* Driver data structures and types. */ |
50 | 90 | /*===========================================================================*/ |
@@ -63,19 +103,27 @@ | ||
63 | 103 | * @brief @p base_driver_c specific methods. |
64 | 104 | */ |
65 | 105 | #define __base_driver_methods \ |
66 | - __referenced_object_methods \ | |
67 | - void (*stop)(void *ip); \ | |
68 | - void (*lock)(void *ip); \ | |
69 | - void (*unlock)(void *ip); | |
106 | + __referenced_object_methods | |
70 | 107 | |
108 | +#if (HAL_USE_MUTUAL_EXCLUSION == TRUE) || defined(__DOXYGEN__) | |
71 | 109 | /** |
72 | 110 | * @brief @p base_driver_c specific data. |
73 | 111 | */ |
74 | 112 | #define __base_driver_data \ |
75 | 113 | __referenced_object_data \ |
114 | + /* Driver state.*/ \ | |
76 | 115 | hal_driver_state_t state; \ |
116 | + /* Driver owner or NULL.*/ \ | |
117 | + void *owner; \ | |
118 | + /* Mutual exclusion object.*/ \ | |
77 | 119 | mutex_t mutex; |
78 | 120 | |
121 | +#else /* HAL_USE_MUTUAL_EXCLUSION != TRUE */ | |
122 | +#define __base_driver_data \ | |
123 | + __referenced_object_data \ | |
124 | + hal_driver_state_t state; \ | |
125 | + void *owner; | |
126 | +#endif /* HAL_USE_MUTUAL_EXCLUSION != TRUE */ | |
79 | 127 | |
80 | 128 | /** |
81 | 129 | * @brief @p base_driver_c virtual methods table. |
@@ -132,6 +180,7 @@ | ||
132 | 180 | base_driver_c *objp = (base_driver_c *)ip; |
133 | 181 | |
134 | 182 | __referenced_object_objinit_impl(objp, vmt); |
183 | + objp->owner = NULL; | |
135 | 184 | osalMutexObjectInit(&objp->mutex); |
136 | 185 | |
137 | 186 | return ip; |
@@ -153,9 +202,76 @@ | ||
153 | 202 | /** @} */ |
154 | 203 | |
155 | 204 | /** |
156 | - * @brief Object lock. | |
205 | + * @brief Driver close. | |
206 | + * @details Releases a reference to the driver, when the count reaches zero | |
207 | + * then the driver is physically uninitialized. | |
157 | 208 | * |
158 | - * @param[in] ip Pointer to a @p synchronized_object_c structure to be | |
209 | + * @param[in] ip Pointer to a @p base_driver_c structure. | |
210 | + * @return The number of references left. | |
211 | + */ | |
212 | +CC_FORCE_INLINE | |
213 | +static inline oop_object_references_t drvClose(void *ip) { | |
214 | + base_driver_c *objp = (base_driver_c *)ip; | |
215 | + | |
216 | + return roRelease(objp); | |
217 | +} | |
218 | + | |
219 | +/** | |
220 | + * @brief Driver state get. | |
221 | + * | |
222 | + * @param[in] ip Pointer to a @p base_driver_c structure. | |
223 | + * @return The driver state. | |
224 | + */ | |
225 | +CC_FORCE_INLINE | |
226 | +static inline hal_driver_state_t drvGetStateX(void *ip) { | |
227 | + base_driver_c *objp = (base_driver_c *)ip; | |
228 | + | |
229 | + return objp->state; | |
230 | +} | |
231 | + | |
232 | +/** | |
233 | + * @brief Driver state set. | |
234 | + * | |
235 | + * @param[in] ip Pointer to a @p base_driver_c structure. | |
236 | + * @param[in] state New driver state. | |
237 | + */ | |
238 | +CC_FORCE_INLINE | |
239 | +static inline void drvSetStateX(void *ip, hal_driver_state_t state) { | |
240 | + | |
241 | + ((base_driver_c *)ip)->state = state; | |
242 | +} | |
243 | + | |
244 | +/** | |
245 | + * @brief Driver owner get. | |
246 | + * | |
247 | + * @param[in] ip Pointer to a @p base_driver_c structure. | |
248 | + * @return The driver owner. | |
249 | + */ | |
250 | +CC_FORCE_INLINE | |
251 | +static inline void *drvGetOwnerX(void *ip) { | |
252 | + base_driver_c *objp = (base_driver_c *)ip; | |
253 | + | |
254 | + return objp->owner; | |
255 | +} | |
256 | + | |
257 | +/** | |
258 | + * @brief Driver owner set. | |
259 | + * | |
260 | + * @param[in] ip Pointer to a @p base_driver_c structure. | |
261 | + * @param[in] owner New driver owner. | |
262 | + */ | |
263 | +CC_FORCE_INLINE | |
264 | +static inline void drvSetOwnerX(void *ip, void *owner) { | |
265 | + base_driver_c *objp = (base_driver_c *)ip; | |
266 | + | |
267 | + objp->owner = owner; | |
268 | +} | |
269 | + | |
270 | +#if (HAL_USE_MUTUAL_EXCLUSION == TRUE) || defined(__DOXYGEN__) | |
271 | +/** | |
272 | + * @brief Driver lock. | |
273 | + * | |
274 | + * @param[in] ip Pointer to a @p base_driver_c structure to be | |
159 | 275 | * locked. |
160 | 276 | */ |
161 | 277 | CC_FORCE_INLINE |
@@ -166,9 +282,9 @@ | ||
166 | 282 | } |
167 | 283 | |
168 | 284 | /** |
169 | - * @brief Object unlock. | |
285 | + * @brief Driver unlock. | |
170 | 286 | * |
171 | - * @param[in] ip Pointer to a @p synchronized_object_c structure to be | |
287 | + * @param[in] ip Pointer to a @p base_driver_c structure to be | |
172 | 288 | * unlocked. |
173 | 289 | */ |
174 | 290 | CC_FORCE_INLINE |
@@ -177,6 +293,7 @@ | ||
177 | 293 | |
178 | 294 | osalMutexUnlock(&objp->mutex); |
179 | 295 | } |
296 | +#endif /* HAL_USE_MUTUAL_EXCLUSION == TRUE */ | |
180 | 297 | |
181 | 298 | #endif /* HAL_BASE_DRIVER */ |
182 | 299 |