Skip to content

Commit 0e2d950

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

14 files changed

Lines changed: 290 additions & 49 deletions

File tree

src/os/inc/osapi-binsem.h

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ 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
181182
/*-------------------------------------------------------------------------------------*/
182183
/**
183184
* @brief Fill a property object buffer with details regarding the resource
@@ -197,6 +198,57 @@ int32 OS_BinSemGetIdByName(osal_id_t *sem_id, const char *sem_name);
197198
*/
198199
int32 OS_BinSemGetInfo(osal_id_t sem_id, OS_bin_sem_prop_t *bin_prop);
199200

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

202254
#endif /* OSAPI_BINSEM_H */

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
475476
int32 OS_BinSemGetInfo_Impl(const OS_object_token_t *token, OS_bin_sem_prop_t *sem_prop)
477+
#else
478+
int32 OS_BinSemGetValue_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
275276
int32 OS_BinSemGetInfo_Impl(const OS_object_token_t *token, OS_bin_sem_prop_t *bin_prop)
277+
#else
278+
int32 OS_BinSemGetValue_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
114115
int32 OS_BinSemGetInfo_Impl(const OS_object_token_t *token, OS_bin_sem_prop_t *bin_prop);
116+
#else
117+
int32 OS_BinSemGetValue_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: 84 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,31 @@ 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+
#ifdef OSAL_OMIT_DEPRECATED
252253
int32 OS_BinSemGetInfo(osal_id_t sem_id, OS_bin_sem_prop_t *bin_prop)
254+
{
255+
OS_common_record_t *record;
256+
OS_object_token_t token;
257+
int32 return_code;
258+
/* Check parameters */
259+
OS_CHECK_POINTER(bin_prop);
260+
memset(bin_prop, 0, sizeof(OS_bin_sem_prop_t));
261+
/* Check Parameters */
262+
return_code = OS_ObjectIdGetById(OS_LOCK_MODE_GLOBAL, LOCAL_OBJID_TYPE, sem_id, &token);
263+
if (return_code == OS_SUCCESS)
264+
{
265+
record = OS_OBJECT_TABLE_GET(OS_global_bin_sem_table, token);
266+
267+
strncpy(bin_prop->name, record->name_entry, sizeof(bin_prop->name) - 1);
268+
bin_prop->creator = record->creator;
269+
return_code = OS_BinSemGetInfo_Impl(&token, bin_prop);
270+
271+
OS_ObjectIdRelease(&token);
272+
}
273+
return return_code;
274+
}
275+
#else
276+
int32 OS_BinSemGetName(osal_id_t sem_id, OS_bin_sem_prop_t *bin_prop)
253277
{
254278
OS_common_record_t *record;
255279
OS_object_token_t token;
@@ -267,11 +291,70 @@ int32 OS_BinSemGetInfo(osal_id_t sem_id, OS_bin_sem_prop_t *bin_prop)
267291
record = OS_OBJECT_TABLE_GET(OS_global_bin_sem_table, token);
268292

269293
strncpy(bin_prop->name, record->name_entry, sizeof(bin_prop->name) - 1);
294+
bin_prop->name[sizeof(bin_prop->name) - 1] = '\0'; /* Ensure null termination */
295+
296+
OS_ObjectIdRelease(&token);
297+
}
298+
299+
return return_code;
300+
}
301+
302+
/*----------------------------------------------------------------
303+
*
304+
* Purpose: Implemented per public OSAL API
305+
* See description in API and header file for detail
306+
*
307+
*-----------------------------------------------------------------*/
308+
int32 OS_BinSemGetCreator(osal_id_t sem_id, OS_bin_sem_prop_t *bin_prop)
309+
{
310+
OS_common_record_t *record;
311+
OS_object_token_t token;
312+
int32 return_code;
313+
314+
/* Check parameters */
315+
OS_CHECK_POINTER(bin_prop);
316+
317+
memset(bin_prop, 0, sizeof(OS_bin_sem_prop_t));
318+
319+
/* Check Parameters */
320+
return_code = OS_ObjectIdGetById(OS_LOCK_MODE_GLOBAL, LOCAL_OBJID_TYPE, sem_id, &token);
321+
if (return_code == OS_SUCCESS)
322+
{
323+
record = OS_OBJECT_TABLE_GET(OS_global_bin_sem_table, token);
324+
270325
bin_prop->creator = record->creator;
271-
return_code = OS_BinSemGetInfo_Impl(&token, bin_prop);
272326

273327
OS_ObjectIdRelease(&token);
274328
}
275329

276330
return return_code;
277331
}
332+
333+
/*----------------------------------------------------------------
334+
*
335+
* Purpose: Implemented per public OSAL API
336+
* See description in API and header file for detail
337+
*
338+
*-----------------------------------------------------------------*/
339+
int32 OS_BinSemGetValue(osal_id_t sem_id, OS_bin_sem_prop_t *bin_prop)
340+
{
341+
OS_object_token_t token;
342+
int32 return_code;
343+
344+
/* Check parameters */
345+
OS_CHECK_POINTER(bin_prop);
346+
347+
memset(bin_prop, 0, sizeof(OS_bin_sem_prop_t));
348+
349+
/* Check Parameters */
350+
return_code = OS_ObjectIdGetById(OS_LOCK_MODE_GLOBAL, LOCAL_OBJID_TYPE, sem_id, &token);
351+
if (return_code == OS_SUCCESS)
352+
{
353+
return_code = OS_BinSemGetValue_Impl(&token, bin_prop);
354+
355+
OS_ObjectIdRelease(&token);
356+
}
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
193194
int32 OS_BinSemGetInfo_Impl(const OS_object_token_t *token, OS_bin_sem_prop_t *bin_prop)
195+
#else
196+
int32 OS_BinSemGetValue_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)