Skip to content

Commit 6459099

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

File tree

14 files changed

+290
-50
lines changed

14 files changed

+290
-50
lines changed

src/os/inc/osapi-binsem.h

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,56 @@ 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+
/*-------------------------------------------------------------------------------------*/
183+
/**
184+
* @brief Get the name of the binary semaphore
185+
*
186+
* This function retrieves the name of the specified binary semaphore.
187+
*
188+
* @param[in] sem_id The object ID to operate on
189+
* @param[out] bin_prop The property object buffer to fill @nonnull
190+
*
191+
* @return Execution status, see @ref OSReturnCodes
192+
* @retval #OS_SUCCESS @copybrief OS_SUCCESS
193+
* @retval #OS_ERR_INVALID_ID if the id passed in is not a valid semaphore
194+
* @retval #OS_INVALID_POINTER if the bin_prop pointer is null
195+
*/
196+
int32 OS_BinSemGetName(osal_id_t sem_id, OS_bin_sem_prop_t *bin_prop);
197+
198+
/*-------------------------------------------------------------------------------------*/
199+
/**
200+
* @brief Get the creator of the binary semaphore
201+
*
202+
* This function retrieves the creator of the specified binary semaphore.
203+
*
204+
* @param[in] sem_id The object ID to operate on
205+
* @param[out] bin_prop The property object buffer to fill @nonnull
206+
*
207+
* @return Execution status, see @ref OSReturnCodes
208+
* @retval #OS_SUCCESS @copybrief OS_SUCCESS
209+
* @retval #OS_ERR_INVALID_ID if the id passed in is not a valid semaphore
210+
* @retval #OS_INVALID_POINTER if the bin_prop pointer is null
211+
*/
212+
int32 OS_BinSemGetCreator(osal_id_t sem_id, OS_bin_sem_prop_t *bin_prop);
213+
214+
/*-------------------------------------------------------------------------------------*/
215+
/**
216+
* @brief Get the value of the binary semaphore
217+
*
218+
* This function retrieves the value of the specified binary semaphore.
219+
*
220+
* @param[in] sem_id The object ID to operate on
221+
* @param[out] bin_prop The property object buffer to fill @nonnull
222+
*
223+
* @return Execution status, see @ref OSReturnCodes
224+
* @retval #OS_SUCCESS @copybrief OS_SUCCESS
225+
* @retval #OS_ERR_INVALID_ID if the id passed in is not a valid semaphore
226+
* @retval #OS_INVALID_POINTER if the bin_prop pointer is null
227+
* @retval #OS_ERR_NOT_IMPLEMENTED @copybrief OS_ERR_NOT_IMPLEMENTED
228+
*/
229+
int32 OS_BinSemGetValue(osal_id_t sem_id, OS_bin_sem_prop_t *bin_prop);
230+
#else
181231
/*-------------------------------------------------------------------------------------*/
182232
/**
183233
* @brief Fill a property object buffer with details regarding the resource
@@ -196,6 +246,7 @@ int32 OS_BinSemGetIdByName(osal_id_t *sem_id, const char *sem_name);
196246
* @retval #OS_ERR_NOT_IMPLEMENTED @copybrief OS_ERR_NOT_IMPLEMENTED
197247
*/
198248
int32 OS_BinSemGetInfo(osal_id_t sem_id, OS_bin_sem_prop_t *bin_prop);
249+
#endif
199250

200251
/**@}*/
201252

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,11 @@ int32 OS_BinSemTimedWait_Impl(const OS_object_token_t *token, uint32 msecs)
472472
* See prototype for argument/return detail
473473
*
474474
*-----------------------------------------------------------------*/
475+
#ifdef OSAL_OMIT_DEPRECATED
476+
int32 OS_BinSemGetValue_Impl(const OS_object_token_t *token, OS_bin_sem_prop_t *sem_prop)
477+
#else
475478
int32 OS_BinSemGetInfo_Impl(const OS_object_token_t *token, OS_bin_sem_prop_t *sem_prop)
479+
#endif
476480
{
477481
OS_impl_binsem_internal_record_t *sem;
478482

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,11 @@ int32 OS_BinSemTimedWait_Impl(const OS_object_token_t *token, uint32 msecs)
272272
* See prototype for argument/return detail
273273
*
274274
*-----------------------------------------------------------------*/
275+
#ifdef OSAL_OMIT_DEPRECATED
276+
int32 OS_BinSemGetValue_Impl(const OS_object_token_t *token, OS_bin_sem_prop_t *bin_prop)
277+
#else
275278
int32 OS_BinSemGetInfo_Impl(const OS_object_token_t *token, OS_bin_sem_prop_t *bin_prop)
279+
#endif
276280
{
277281
/* RTEMS has no API for obtaining the current value of a semaphore */
278282
return OS_ERR_NOT_IMPLEMENTED;

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,14 @@ int32 OS_BinSemDelete_Impl(const OS_object_token_t *token);
107107

108108
/*----------------------------------------------------------------
109109
110-
Purpose: Obtain OS-specific information about the semaphore
110+
Purpose: Obtain the value of a semaphore (if possible/implemented in the relevent OS)
111111
112112
Returns: OS_SUCCESS on success, or relevant error code
113113
------------------------------------------------------------------*/
114+
#ifdef OSAL_OMIT_DEPRECATED
115+
int32 OS_BinSemGetValue_Impl(const OS_object_token_t *token, OS_bin_sem_prop_t *bin_prop);
116+
#else
114117
int32 OS_BinSemGetInfo_Impl(const OS_object_token_t *token, OS_bin_sem_prop_t *bin_prop);
118+
#endif
115119

116120
#endif /* OS_SHARED_BINSEM_H */

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

Lines changed: 85 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,8 @@ int32 OS_BinSemGetIdByName(osal_id_t *sem_id, const char *sem_name)
249249
* See description in API and header file for detail
250250
*
251251
*-----------------------------------------------------------------*/
252-
int32 OS_BinSemGetInfo(osal_id_t sem_id, OS_bin_sem_prop_t *bin_prop)
252+
#ifdef OSAL_OMIT_DEPRECATED
253+
int32 OS_BinSemGetName(osal_id_t sem_id, OS_bin_sem_prop_t *bin_prop)
253254
{
254255
OS_common_record_t *record;
255256
OS_object_token_t token;
@@ -267,11 +268,93 @@ int32 OS_BinSemGetInfo(osal_id_t sem_id, OS_bin_sem_prop_t *bin_prop)
267268
record = OS_OBJECT_TABLE_GET(OS_global_bin_sem_table, token);
268269

269270
strncpy(bin_prop->name, record->name_entry, sizeof(bin_prop->name) - 1);
271+
bin_prop->name[sizeof(bin_prop->name) - 1] = '\0'; /* Ensure null termination */
272+
273+
OS_ObjectIdRelease(&token);
274+
}
275+
276+
return return_code;
277+
}
278+
279+
/*----------------------------------------------------------------
280+
*
281+
* Purpose: Implemented per public OSAL API
282+
* See description in API and header file for detail
283+
*
284+
*-----------------------------------------------------------------*/
285+
int32 OS_BinSemGetCreator(osal_id_t sem_id, OS_bin_sem_prop_t *bin_prop)
286+
{
287+
OS_common_record_t *record;
288+
OS_object_token_t token;
289+
int32 return_code;
290+
291+
/* Check parameters */
292+
OS_CHECK_POINTER(bin_prop);
293+
294+
memset(bin_prop, 0, sizeof(OS_bin_sem_prop_t));
295+
296+
/* Check Parameters */
297+
return_code = OS_ObjectIdGetById(OS_LOCK_MODE_GLOBAL, LOCAL_OBJID_TYPE, sem_id, &token);
298+
if (return_code == OS_SUCCESS)
299+
{
300+
record = OS_OBJECT_TABLE_GET(OS_global_bin_sem_table, token);
301+
270302
bin_prop->creator = record->creator;
271-
return_code = OS_BinSemGetInfo_Impl(&token, bin_prop);
272303

273304
OS_ObjectIdRelease(&token);
274305
}
275306

276307
return return_code;
277308
}
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_BinSemGetValue(osal_id_t sem_id, OS_bin_sem_prop_t *bin_prop)
317+
{
318+
OS_object_token_t token;
319+
int32 return_code;
320+
321+
/* Check parameters */
322+
OS_CHECK_POINTER(bin_prop);
323+
324+
memset(bin_prop, 0, sizeof(OS_bin_sem_prop_t));
325+
326+
/* Check Parameters */
327+
return_code = OS_ObjectIdGetById(OS_LOCK_MODE_GLOBAL, LOCAL_OBJID_TYPE, sem_id, &token);
328+
if (return_code == OS_SUCCESS)
329+
{
330+
return_code = OS_BinSemGetValue_Impl(&token, bin_prop);
331+
332+
OS_ObjectIdRelease(&token);
333+
}
334+
335+
return return_code;
336+
}
337+
#else
338+
int32 OS_BinSemGetInfo(osal_id_t sem_id, OS_bin_sem_prop_t *bin_prop)
339+
{
340+
OS_common_record_t *record;
341+
OS_object_token_t token;
342+
int32 return_code;
343+
/* Check parameters */
344+
OS_CHECK_POINTER(bin_prop);
345+
memset(bin_prop, 0, sizeof(OS_bin_sem_prop_t));
346+
/* Check Parameters */
347+
return_code = OS_ObjectIdGetById(OS_LOCK_MODE_GLOBAL, LOCAL_OBJID_TYPE, sem_id, &token);
348+
if (return_code == OS_SUCCESS)
349+
{
350+
record = OS_OBJECT_TABLE_GET(OS_global_bin_sem_table, token);
351+
352+
strncpy(bin_prop->name, record->name_entry, sizeof(bin_prop->name) - 1);
353+
bin_prop->creator = record->creator;
354+
return_code = OS_BinSemGetInfo_Impl(&token, bin_prop);
355+
356+
OS_ObjectIdRelease(&token);
357+
}
358+
return return_code;
359+
}
360+
#endif

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,12 @@ int32 OS_BinSemTimedWait_Impl(const OS_object_token_t *token, uint32 msecs)
190190
* See prototype for argument/return detail
191191
*
192192
*-----------------------------------------------------------------*/
193+
#ifdef OSAL_OMIT_DEPRECATED
194+
int32 OS_BinSemGetValue_Impl(const OS_object_token_t *token, OS_bin_sem_prop_t *bin_prop)
195+
#else
193196
int32 OS_BinSemGetInfo_Impl(const OS_object_token_t *token, OS_bin_sem_prop_t *bin_prop)
197+
#endif
194198
{
195199
/* VxWorks has no API for obtaining the current value of a semaphore */
196-
return OS_SUCCESS;
200+
return OS_ERR_NOT_IMPLEMENTED;
197201
}

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

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,15 @@ 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);
@@ -93,7 +95,9 @@ void Test_BinSem(void)
9395
/* 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+
UtAssert_INT32_EQ(OS_BinSemGetName(sem_id[0], NULL), OS_INVALID_POINTER);
99+
UtAssert_INT32_EQ(OS_BinSemGetCreator(sem_id[0], NULL), OS_INVALID_POINTER);
100+
UtAssert_INT32_EQ(OS_BinSemGetValue(sem_id[0], NULL), OS_INVALID_POINTER);
97101
UtAssert_INT32_EQ(OS_BinSemGetIdByName(NULL, "Test_Sem"), OS_INVALID_POINTER);
98102
UtAssert_INT32_EQ(OS_BinSemGetIdByName(&sem_id[0], NULL), OS_INVALID_POINTER);
99103

@@ -109,15 +113,15 @@ void Test_BinSem(void)
109113
/* Nonzero create */
110114
UtAssert_INT32_EQ(OS_BinSemCreate(&sem_id[1], "Test_Sem_Nonzero", 1, 0), OS_SUCCESS);
111115

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

115119
/* Validate values */
116-
if (get_info_implemented)
120+
if (get_value_implemented)
117121
{
118-
UtAssert_INT32_EQ(OS_BinSemGetInfo(sem_id[0], &sem_prop), OS_SUCCESS);
122+
UtAssert_INT32_EQ(OS_BinSemGetValue(sem_id[0], &sem_prop), OS_SUCCESS);
119123
UtAssert_INT32_EQ(sem_prop.value, 0);
120-
UtAssert_INT32_EQ(OS_BinSemGetInfo(sem_id[1], &sem_prop), OS_SUCCESS);
124+
UtAssert_INT32_EQ(OS_BinSemGetValue(sem_id[1], &sem_prop), OS_SUCCESS);
121125
UtAssert_INT32_EQ(sem_prop.value, 1);
122126
}
123127

@@ -141,11 +145,11 @@ void Test_BinSem(void)
141145
UtAssert_INT32_EQ(OS_BinSemTimedWait(sem_id[1], 0), OS_SUCCESS);
142146

143147
/* Validate zeros */
144-
if (get_info_implemented)
148+
if (get_value_implemented)
145149
{
146-
UtAssert_INT32_EQ(OS_BinSemGetInfo(sem_id[0], &sem_prop), OS_SUCCESS);
150+
UtAssert_INT32_EQ(OS_BinSemGetValue(sem_id[0], &sem_prop), OS_SUCCESS);
147151
UtAssert_INT32_EQ(sem_prop.value, 0);
148-
UtAssert_INT32_EQ(OS_BinSemGetInfo(sem_id[1], &sem_prop), OS_SUCCESS);
152+
UtAssert_INT32_EQ(OS_BinSemGetValue(sem_id[1], &sem_prop), OS_SUCCESS);
149153
UtAssert_INT32_EQ(sem_prop.value, 0);
150154
}
151155
else
@@ -162,9 +166,9 @@ void Test_BinSem(void)
162166
UtAssert_INT32_EQ(OS_TaskDelete(task_id[0]), OS_SUCCESS);
163167
UtAssert_UINT32_EQ(task_counter[0], 0);
164168

165-
if (get_info_implemented)
169+
if (get_value_implemented)
166170
{
167-
UtAssert_INT32_EQ(OS_BinSemGetInfo(sem_id[0], &sem_prop), OS_SUCCESS);
171+
UtAssert_INT32_EQ(OS_BinSemGetValue(sem_id[0], &sem_prop), OS_SUCCESS);
168172
UtAssert_INT32_EQ(sem_prop.value, 0);
169173
}
170174
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)