Skip to content

Commit f8855ab

Browse files
committed
Fix #1440, Split up BinSemGetInfo() to avoid partial success returns
1 parent 8270166 commit f8855ab

File tree

14 files changed

+348
-49
lines changed

14 files changed

+348
-49
lines changed

src/os/inc/osapi-binsem.h

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,8 @@ int32 OS_BinSemDelete(osal_id_t sem_id);
178178
*/
179179
int32 OS_BinSemGetIdByName(osal_id_t *sem_id, const char *sem_name);
180180

181+
#ifdef OSAL_OMIT_DEPRECATED
182+
#else
181183
/*-------------------------------------------------------------------------------------*/
182184
/**
183185
* @brief Fill a property object buffer with details regarding the resource
@@ -196,6 +198,56 @@ int32 OS_BinSemGetIdByName(osal_id_t *sem_id, const char *sem_name);
196198
* @retval #OS_ERR_NOT_IMPLEMENTED @copybrief OS_ERR_NOT_IMPLEMENTED
197199
*/
198200
int32 OS_BinSemGetInfo(osal_id_t sem_id, OS_bin_sem_prop_t *bin_prop);
201+
#endif
202+
203+
/*-------------------------------------------------------------------------------------*/
204+
/**
205+
* @brief Get the name of the binary semaphore
206+
*
207+
* This function retrieves the name of the specified binary semaphore.
208+
*
209+
* @param[in] sem_id The object ID to operate on
210+
* @param[out] bin_prop The property object buffer to fill @nonnull
211+
*
212+
* @return Execution status, see @ref OSReturnCodes
213+
* @retval #OS_SUCCESS @copybrief OS_SUCCESS
214+
* @retval #OS_ERR_INVALID_ID if the id passed in is not a valid semaphore
215+
* @retval #OS_INVALID_POINTER if the bin_prop pointer is null
216+
*/
217+
int32 OS_BinSemGetName(osal_id_t sem_id, OS_bin_sem_prop_t *bin_prop);
218+
219+
/*-------------------------------------------------------------------------------------*/
220+
/**
221+
* @brief Get the creator of the binary semaphore
222+
*
223+
* This function retrieves the creator of the specified binary semaphore.
224+
*
225+
* @param[in] sem_id The object ID to operate on
226+
* @param[out] bin_prop The property object buffer to fill @nonnull
227+
*
228+
* @return Execution status, see @ref OSReturnCodes
229+
* @retval #OS_SUCCESS @copybrief OS_SUCCESS
230+
* @retval #OS_ERR_INVALID_ID if the id passed in is not a valid semaphore
231+
* @retval #OS_INVALID_POINTER if the bin_prop pointer is null
232+
*/
233+
int32 OS_BinSemGetCreator(osal_id_t sem_id, OS_bin_sem_prop_t *bin_prop);
234+
235+
/*-------------------------------------------------------------------------------------*/
236+
/**
237+
* @brief Get the value of the binary semaphore
238+
*
239+
* This function retrieves the value of the specified binary semaphore.
240+
*
241+
* @param[in] sem_id The object ID to operate on
242+
* @param[out] bin_prop The property object buffer to fill @nonnull
243+
*
244+
* @return Execution status, see @ref OSReturnCodes
245+
* @retval #OS_SUCCESS @copybrief OS_SUCCESS
246+
* @retval #OS_ERR_INVALID_ID if the id passed in is not a valid semaphore
247+
* @retval #OS_INVALID_POINTER if the bin_prop pointer is null
248+
* @retval #OS_ERR_NOT_IMPLEMENTED @copybrief OS_ERR_NOT_IMPLEMENTED
249+
*/
250+
int32 OS_BinSemGetValue(osal_id_t sem_id, OS_bin_sem_prop_t *bin_prop);
199251

200252
/**@}*/
201253

src/os/posix/src/os-impl-binsem.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,8 @@ int32 OS_BinSemTimedWait_Impl(const OS_object_token_t *token, uint32 msecs)
466466
return (OS_GenericBinSemTake_Impl(token, &ts));
467467
}
468468

469+
#ifdef OSAL_OMIT_DEPRECATED
470+
#else
469471
/*----------------------------------------------------------------
470472
*
471473
* Purpose: Implemented per internal OSAL API
@@ -482,3 +484,21 @@ int32 OS_BinSemGetInfo_Impl(const OS_object_token_t *token, OS_bin_sem_prop_t *s
482484
sem_prop->value = sem->current_value;
483485
return OS_SUCCESS;
484486
}
487+
#endif
488+
489+
/*----------------------------------------------------------------
490+
*
491+
* Purpose: Implemented per internal OSAL API
492+
* See prototype for argument/return detail
493+
*
494+
*-----------------------------------------------------------------*/
495+
int32 OS_BinSemGetValue_Impl(const OS_object_token_t *token, OS_bin_sem_prop_t *sem_prop)
496+
{
497+
OS_impl_binsem_internal_record_t *sem;
498+
499+
sem = OS_OBJECT_TABLE_GET(OS_impl_bin_sem_table, *token);
500+
501+
/* put the info into the structure */
502+
sem_prop->value = sem->current_value;
503+
return OS_SUCCESS;
504+
}

src/os/rtems/src/os-impl-binsem.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,8 @@ int32 OS_BinSemTimedWait_Impl(const OS_object_token_t *token, uint32 msecs)
266266
return OS_SUCCESS;
267267
}
268268

269+
#ifdef OSAL_OMIT_DEPRECATED
270+
#else
269271
/*----------------------------------------------------------------
270272
*
271273
* Purpose: Implemented per internal OSAL API
@@ -277,3 +279,16 @@ int32 OS_BinSemGetInfo_Impl(const OS_object_token_t *token, OS_bin_sem_prop_t *b
277279
/* RTEMS has no API for obtaining the current value of a semaphore */
278280
return OS_ERR_NOT_IMPLEMENTED;
279281
}
282+
#endif
283+
284+
/*----------------------------------------------------------------
285+
*
286+
* Purpose: Implemented per internal OSAL API
287+
* See prototype for argument/return detail
288+
*
289+
*-----------------------------------------------------------------*/
290+
int32 OS_BinSemGetValue_Impl(const OS_object_token_t *token, OS_bin_sem_prop_t *bin_prop)
291+
{
292+
/* RTEMS has no API for obtaining the current value of a semaphore */
293+
return OS_ERR_NOT_IMPLEMENTED;
294+
}

src/os/shared/inc/os-shared-binsem.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,12 +105,24 @@ int32 OS_BinSemTimedWait_Impl(const OS_object_token_t *token, uint32 msecs);
105105
------------------------------------------------------------------*/
106106
int32 OS_BinSemDelete_Impl(const OS_object_token_t *token);
107107

108+
109+
#ifdef OSAL_OMIT_DEPRECATED
110+
#else
108111
/*----------------------------------------------------------------
109112
110113
Purpose: Obtain OS-specific information about the semaphore
111114
112115
Returns: OS_SUCCESS on success, or relevant error code
113116
------------------------------------------------------------------*/
114117
int32 OS_BinSemGetInfo_Impl(const OS_object_token_t *token, OS_bin_sem_prop_t *bin_prop);
118+
#endif
119+
120+
/*----------------------------------------------------------------
121+
122+
Purpose: Obtain the value of a semaphore (if possible/implemented in the relevent OS)
123+
124+
Returns: OS_SUCCESS on success, or relevant error code
125+
------------------------------------------------------------------*/
126+
int32 OS_BinSemGetValue_Impl(const OS_object_token_t *token, OS_bin_sem_prop_t *bin_prop);
115127

116128
#endif /* OS_SHARED_BINSEM_H */

src/os/shared/src/osapi-binsem.c

Lines changed: 91 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,13 +243,45 @@ int32 OS_BinSemGetIdByName(osal_id_t *sem_id, const char *sem_name)
243243
return return_code;
244244
}
245245

246+
#ifdef OSAL_OMIT_DEPRECATED
247+
#else
246248
/*----------------------------------------------------------------
247249
*
248250
* Purpose: Implemented per public OSAL API
249251
* See description in API and header file for detail
250252
*
251253
*-----------------------------------------------------------------*/
252254
int32 OS_BinSemGetInfo(osal_id_t sem_id, OS_bin_sem_prop_t *bin_prop)
255+
{
256+
OS_common_record_t *record;
257+
OS_object_token_t token;
258+
int32 return_code;
259+
/* Check parameters */
260+
OS_CHECK_POINTER(bin_prop);
261+
memset(bin_prop, 0, sizeof(OS_bin_sem_prop_t));
262+
/* Check Parameters */
263+
return_code = OS_ObjectIdGetById(OS_LOCK_MODE_GLOBAL, LOCAL_OBJID_TYPE, sem_id, &token);
264+
if (return_code == OS_SUCCESS)
265+
{
266+
record = OS_OBJECT_TABLE_GET(OS_global_bin_sem_table, token);
267+
268+
strncpy(bin_prop->name, record->name_entry, sizeof(bin_prop->name) - 1);
269+
bin_prop->creator = record->creator;
270+
return_code = OS_BinSemGetInfo_Impl(&token, bin_prop);
271+
272+
OS_ObjectIdRelease(&token);
273+
}
274+
return return_code;
275+
}
276+
#endif
277+
278+
/*----------------------------------------------------------------
279+
*
280+
* Purpose: Implemented per public OSAL API
281+
* See description in API and header file for detail
282+
*
283+
*-----------------------------------------------------------------*/
284+
int32 OS_BinSemGetName(osal_id_t sem_id, OS_bin_sem_prop_t *bin_prop)
253285
{
254286
OS_common_record_t *record;
255287
OS_object_token_t token;
@@ -267,8 +299,66 @@ int32 OS_BinSemGetInfo(osal_id_t sem_id, OS_bin_sem_prop_t *bin_prop)
267299
record = OS_OBJECT_TABLE_GET(OS_global_bin_sem_table, token);
268300

269301
strncpy(bin_prop->name, record->name_entry, sizeof(bin_prop->name) - 1);
302+
bin_prop->name[sizeof(bin_prop->name) - 1] = '\0'; /* Ensure null termination */
303+
304+
OS_ObjectIdRelease(&token);
305+
}
306+
307+
return return_code;
308+
}
309+
310+
/*----------------------------------------------------------------
311+
*
312+
* Purpose: Implemented per public OSAL API
313+
* See description in API and header file for detail
314+
*
315+
*-----------------------------------------------------------------*/
316+
int32 OS_BinSemGetCreator(osal_id_t sem_id, OS_bin_sem_prop_t *bin_prop)
317+
{
318+
OS_common_record_t *record;
319+
OS_object_token_t token;
320+
int32 return_code;
321+
322+
/* Check parameters */
323+
OS_CHECK_POINTER(bin_prop);
324+
325+
memset(bin_prop, 0, sizeof(OS_bin_sem_prop_t));
326+
327+
/* Check Parameters */
328+
return_code = OS_ObjectIdGetById(OS_LOCK_MODE_GLOBAL, LOCAL_OBJID_TYPE, sem_id, &token);
329+
if (return_code == OS_SUCCESS)
330+
{
331+
record = OS_OBJECT_TABLE_GET(OS_global_bin_sem_table, token);
332+
270333
bin_prop->creator = record->creator;
271-
return_code = OS_BinSemGetInfo_Impl(&token, bin_prop);
334+
335+
OS_ObjectIdRelease(&token);
336+
}
337+
338+
return return_code;
339+
}
340+
341+
/*----------------------------------------------------------------
342+
*
343+
* Purpose: Implemented per public OSAL API
344+
* See description in API and header file for detail
345+
*
346+
*-----------------------------------------------------------------*/
347+
int32 OS_BinSemGetValue(osal_id_t sem_id, OS_bin_sem_prop_t *bin_prop)
348+
{
349+
OS_object_token_t token;
350+
int32 return_code;
351+
352+
/* Check parameters */
353+
OS_CHECK_POINTER(bin_prop);
354+
355+
memset(bin_prop, 0, sizeof(OS_bin_sem_prop_t));
356+
357+
/* Check Parameters */
358+
return_code = OS_ObjectIdGetById(OS_LOCK_MODE_GLOBAL, LOCAL_OBJID_TYPE, sem_id, &token);
359+
if (return_code == OS_SUCCESS)
360+
{
361+
return_code = OS_BinSemGetValue_Impl(&token, bin_prop);
272362

273363
OS_ObjectIdRelease(&token);
274364
}

src/os/vxworks/src/os-impl-binsem.c

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,8 @@ int32 OS_BinSemTimedWait_Impl(const OS_object_token_t *token, uint32 msecs)
184184
return status;
185185
}
186186

187+
#ifdef OSAL_OMIT_DEPRECATED
188+
#else
187189
/*----------------------------------------------------------------
188190
*
189191
* Purpose: Implemented per internal OSAL API
@@ -193,5 +195,18 @@ int32 OS_BinSemTimedWait_Impl(const OS_object_token_t *token, uint32 msecs)
193195
int32 OS_BinSemGetInfo_Impl(const OS_object_token_t *token, OS_bin_sem_prop_t *bin_prop)
194196
{
195197
/* VxWorks has no API for obtaining the current value of a semaphore */
196-
return OS_SUCCESS;
198+
return OS_ERR_NOT_IMPLEMENTED;
199+
}
200+
#endif
201+
202+
/*----------------------------------------------------------------
203+
*
204+
* Purpose: Implemented per internal OSAL API
205+
* See prototype for argument/return detail
206+
*
207+
*-----------------------------------------------------------------*/
208+
int32 OS_BinSemGetValue_Impl(const OS_object_token_t *token, OS_bin_sem_prop_t *bin_prop)
209+
{
210+
/* VxWorks has no API for obtaining the current value of a semaphore */
211+
return OS_ERR_NOT_IMPLEMENTED;
197212
}

src/tests/bin-sem-test/bin-sem-test.c

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -77,23 +77,32 @@ void Test_BinSem(void)
7777
char long_name[OS_MAX_API_NAME + 1];
7878
OS_bin_sem_prop_t sem_prop;
7979
uint32 test_val;
80-
bool get_info_implemented;
80+
bool get_value_implemented;
8181

8282
memset(&sem_prop, 0, sizeof(sem_prop));
8383
memset(task_counter, 0, sizeof(task_counter));
8484

8585
/* Invalid id checks */
86-
UtAssert_INT32_EQ(OS_BinSemGetInfo(OS_OBJECT_ID_UNDEFINED, &sem_prop), OS_ERR_INVALID_ID);
86+
UtAssert_INT32_EQ(OS_BinSemGetName(OS_OBJECT_ID_UNDEFINED, &sem_prop), OS_ERR_INVALID_ID);
87+
UtAssert_INT32_EQ(OS_BinSemGetCreator(OS_OBJECT_ID_UNDEFINED, &sem_prop), OS_ERR_INVALID_ID);
88+
UtAssert_INT32_EQ(OS_BinSemGetValue(OS_OBJECT_ID_UNDEFINED, &sem_prop), OS_ERR_INVALID_ID);
8789
UtAssert_INT32_EQ(OS_BinSemFlush(OS_OBJECT_ID_UNDEFINED), OS_ERR_INVALID_ID);
8890
UtAssert_INT32_EQ(OS_BinSemGive(OS_OBJECT_ID_UNDEFINED), OS_ERR_INVALID_ID);
8991
UtAssert_INT32_EQ(OS_BinSemTake(OS_OBJECT_ID_UNDEFINED), OS_ERR_INVALID_ID);
9092
UtAssert_INT32_EQ(OS_BinSemTimedWait(OS_OBJECT_ID_UNDEFINED, 0), OS_ERR_INVALID_ID);
9193
UtAssert_INT32_EQ(OS_BinSemDelete(OS_OBJECT_ID_UNDEFINED), OS_ERR_INVALID_ID);
9294

93-
/* Null checks */
95+
/* OS_BinSemCreate NULL checks */
9496
UtAssert_INT32_EQ(OS_BinSemCreate(NULL, "Test_Sem", 0, 0), OS_INVALID_POINTER);
9597
UtAssert_INT32_EQ(OS_BinSemCreate(&sem_id[0], NULL, 0, 0), OS_INVALID_POINTER);
96-
UtAssert_INT32_EQ(OS_BinSemGetInfo(sem_id[0], NULL), OS_INVALID_POINTER);
98+
99+
// Initialize sem_id[0] to a valid value for the following NULL checks
100+
sem_id[0] = OS_OBJECT_ID_UNDEFINED;
101+
102+
// OS_BinSemGet* NULL checks
103+
UtAssert_INT32_EQ(OS_BinSemGetName(sem_id[0], NULL), OS_INVALID_POINTER);
104+
UtAssert_INT32_EQ(OS_BinSemGetCreator(sem_id[0], NULL), OS_INVALID_POINTER);
105+
UtAssert_INT32_EQ(OS_BinSemGetValue(sem_id[0], NULL), OS_INVALID_POINTER);
97106
UtAssert_INT32_EQ(OS_BinSemGetIdByName(NULL, "Test_Sem"), OS_INVALID_POINTER);
98107
UtAssert_INT32_EQ(OS_BinSemGetIdByName(&sem_id[0], NULL), OS_INVALID_POINTER);
99108

@@ -109,15 +118,15 @@ void Test_BinSem(void)
109118
/* Nonzero create */
110119
UtAssert_INT32_EQ(OS_BinSemCreate(&sem_id[1], "Test_Sem_Nonzero", 1, 0), OS_SUCCESS);
111120

112-
/* Check get info implementation */
113-
get_info_implemented = (OS_BinSemGetInfo(sem_id[0], &sem_prop) != OS_ERR_NOT_IMPLEMENTED);
121+
/* Check get value implementation */
122+
get_value_implemented = (OS_BinSemGetValue(sem_id[0], &sem_prop) != OS_ERR_NOT_IMPLEMENTED);
114123

115124
/* Validate values */
116-
if (get_info_implemented)
125+
if (get_value_implemented)
117126
{
118-
UtAssert_INT32_EQ(OS_BinSemGetInfo(sem_id[0], &sem_prop), OS_SUCCESS);
127+
UtAssert_INT32_EQ(OS_BinSemGetValue(sem_id[0], &sem_prop), OS_SUCCESS);
119128
UtAssert_INT32_EQ(sem_prop.value, 0);
120-
UtAssert_INT32_EQ(OS_BinSemGetInfo(sem_id[1], &sem_prop), OS_SUCCESS);
129+
UtAssert_INT32_EQ(OS_BinSemGetValue(sem_id[1], &sem_prop), OS_SUCCESS);
121130
UtAssert_INT32_EQ(sem_prop.value, 1);
122131
}
123132

@@ -141,11 +150,11 @@ void Test_BinSem(void)
141150
UtAssert_INT32_EQ(OS_BinSemTimedWait(sem_id[1], 0), OS_SUCCESS);
142151

143152
/* Validate zeros */
144-
if (get_info_implemented)
153+
if (get_value_implemented)
145154
{
146-
UtAssert_INT32_EQ(OS_BinSemGetInfo(sem_id[0], &sem_prop), OS_SUCCESS);
155+
UtAssert_INT32_EQ(OS_BinSemGetValue(sem_id[0], &sem_prop), OS_SUCCESS);
147156
UtAssert_INT32_EQ(sem_prop.value, 0);
148-
UtAssert_INT32_EQ(OS_BinSemGetInfo(sem_id[1], &sem_prop), OS_SUCCESS);
157+
UtAssert_INT32_EQ(OS_BinSemGetValue(sem_id[1], &sem_prop), OS_SUCCESS);
149158
UtAssert_INT32_EQ(sem_prop.value, 0);
150159
}
151160
else
@@ -162,9 +171,9 @@ void Test_BinSem(void)
162171
UtAssert_INT32_EQ(OS_TaskDelete(task_id[0]), OS_SUCCESS);
163172
UtAssert_UINT32_EQ(task_counter[0], 0);
164173

165-
if (get_info_implemented)
174+
if (get_value_implemented)
166175
{
167-
UtAssert_INT32_EQ(OS_BinSemGetInfo(sem_id[0], &sem_prop), OS_SUCCESS);
176+
UtAssert_INT32_EQ(OS_BinSemGetValue(sem_id[0], &sem_prop), OS_SUCCESS);
168177
UtAssert_INT32_EQ(sem_prop.value, 0);
169178
}
170179
else

src/tests/bin-sem-timeout-test/bin-sem-timeout-test.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ void task_1(void)
9898
if (status == OS_SUCCESS)
9999
{
100100
OS_printf("TASK 1: Doing some work: %d\n", (int)counter++);
101-
status = OS_BinSemGetInfo(bin_sem_id, &bin_sem_prop);
101+
status = OS_BinSemGetValue(bin_sem_id, &bin_sem_prop);
102102
if (status == OS_SUCCESS)
103103
{
104104
if (bin_sem_prop.value > 1)

src/tests/select-test/select-test.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ void BinSemSetup(void)
7373
UtAssert_INT32_EQ(OS_BinSemCreate(&bin_sem_id, "BinSem1", 0, 0), OS_SUCCESS);
7474
UtAssert_True(OS_ObjectIdDefined(bin_sem_id), "bin_sem_id (%lu) != UNDEFINED", OS_ObjectIdToInteger(bin_sem_id));
7575

76-
UtAssert_INT32_EQ(OS_BinSemGetInfo(bin_sem_id, &bin_sem_prop), OS_SUCCESS);
76+
UtAssert_INT32_EQ(OS_BinSemGetValue(bin_sem_id, &bin_sem_prop), OS_SUCCESS);
7777
UtPrintf("BinSem1 value=%d", (int)bin_sem_prop.value);
7878
}
7979

0 commit comments

Comments
 (0)