@@ -181,14 +181,14 @@ ZTEST(posix_apis, test_named_semaphore)
181
181
182
182
/* Open named sem */
183
183
sem1 = sem_open ("sem1" , O_CREAT , 0 , 0 );
184
- zassert_equal (nsem_get_ref_count (sem1 ), 1 );
184
+ zassert_equal (nsem_get_ref_count (sem1 ), 2 );
185
185
zassert_equal (nsem_get_list_len (), 1 );
186
186
sem2 = sem_open ("sem2" , O_CREAT , 0 , 0 );
187
- zassert_equal (nsem_get_ref_count (sem2 ), 1 );
187
+ zassert_equal (nsem_get_ref_count (sem2 ), 2 );
188
188
zassert_equal (nsem_get_list_len (), 2 );
189
189
190
190
/* Open created named sem repeatedly */
191
- for (size_t i = 1 ; i < N_LOOPS ; i ++ ) {
191
+ for (size_t i = 1 ; i <= N_LOOPS ; i ++ ) {
192
192
sem_t * new_sem1 , * new_sem2 ;
193
193
194
194
/* oflags are ignored (except when both O_CREAT & O_EXCL are set) */
@@ -200,8 +200,8 @@ ZTEST(posix_apis, test_named_semaphore)
200
200
zassert_equal_ptr (new_sem2 , sem2 );
201
201
202
202
/* ref_count should increment */
203
- zassert_equal (nsem_get_ref_count (sem1 ), i + 1 );
204
- zassert_equal (nsem_get_ref_count (sem2 ), i + 1 );
203
+ zassert_equal (nsem_get_ref_count (sem1 ), 2 + i );
204
+ zassert_equal (nsem_get_ref_count (sem2 ), 2 + i );
205
205
206
206
/* Should reuse the same named sem instead of creating another one */
207
207
zassert_equal (nsem_get_list_len (), 2 );
@@ -217,16 +217,14 @@ ZTEST(posix_apis, test_named_semaphore)
217
217
zassert_equal (nsem_get_list_len (), 2 );
218
218
219
219
/* Close sem */
220
- for (size_t i = 0 ;
220
+ for (size_t i = N_LOOPS ;
221
221
/* close until one left, required by the test later */
222
- i < ( N_LOOPS - 1 ) ; i ++ ) {
222
+ i >= 1 ; i -- ) {
223
223
zassert_ok (sem_close (sem1 ));
224
- zassert_not_null (sem1 );
225
- zassert_equal (nsem_get_ref_count (sem1 ), N_LOOPS - (i + 1 ));
224
+ zassert_equal (nsem_get_ref_count (sem1 ), 2 + i - 1 );
226
225
227
226
zassert_ok (sem_close (sem2 ));
228
- zassert_not_null (sem2 );
229
- zassert_equal (nsem_get_ref_count (sem2 ), N_LOOPS - (i + 1 ));
227
+ zassert_equal (nsem_get_ref_count (sem2 ), 2 + i - 1 );
230
228
231
229
zassert_equal (nsem_get_list_len (), 2 );
232
230
}
@@ -254,9 +252,10 @@ ZTEST(posix_apis, test_named_semaphore)
254
252
zassert_equal (nsem_get_list_len (), 2 );
255
253
256
254
/* Unlink sem1 when it is still being used */
257
- zassert_equal (nsem_get_ref_count (sem1 ), 1 );
255
+ zassert_equal (nsem_get_ref_count (sem1 ), 2 );
258
256
zassert_ok (sem_unlink ("sem1" ));
259
257
/* sem won't be destroyed */
258
+ zassert_equal (nsem_get_ref_count (sem1 ), 1 );
260
259
zassert_equal (nsem_get_list_len (), 2 );
261
260
262
261
/* Create another sem with the name of an unlinked sem */
@@ -274,15 +273,15 @@ ZTEST(posix_apis, test_named_semaphore)
274
273
275
274
/* Closing a linked sem won't destroy the sem */
276
275
zassert_ok (sem_close (sem2 ));
277
- zassert_equal (nsem_get_ref_count (sem2 ), 0 );
276
+ zassert_equal (nsem_get_ref_count (sem2 ), 1 );
278
277
zassert_equal (nsem_get_list_len (), 2 );
279
278
280
279
/* Instead the sem will be destroyed upon call to sem_unlink() */
281
280
zassert_ok (sem_unlink ("sem2" ));
282
281
zassert_equal (nsem_get_list_len (), 1 );
283
282
284
- /* What we have left open here is `different_sem` as "sem1", which has 1 ref_count */
285
- zassert_equal (nsem_get_ref_count (different_sem1 ), 1 );
283
+ /* What we have left open here is `different_sem` as "sem1", which has a ref_count of 2 */
284
+ zassert_equal (nsem_get_ref_count (different_sem1 ), 2 );
286
285
287
286
/* Stress test: open & close "sem1" repeatedly */
288
287
zassert_ok (pthread_create (& thread1 , NULL , nsem_open_func , "sem1" ));
@@ -298,14 +297,14 @@ ZTEST(posix_apis, test_named_semaphore)
298
297
/* Create a new named sem to be used in the normal semaphore test */
299
298
sem1 = sem_open ("nsem" , O_CREAT , 0 , 0 );
300
299
zassert_equal (nsem_get_list_len (), 1 );
301
- zassert_equal (nsem_get_ref_count (sem1 ), 1 );
300
+ zassert_equal (nsem_get_ref_count (sem1 ), 2 );
302
301
303
302
/* Run the semaphore test with the created named semaphore */
304
303
semaphore_test (sem1 );
305
304
306
305
/* List length and ref_count shouldn't change after the test */
307
306
zassert_equal (nsem_get_list_len (), 1 );
308
- zassert_equal (nsem_get_ref_count (sem1 ), 1 );
307
+ zassert_equal (nsem_get_ref_count (sem1 ), 2 );
309
308
310
309
/* Unless it is unlinked and closed */
311
310
sem_unlink ("nsem" );
0 commit comments