13
13
#include <zephyr/sys/util.h>
14
14
#include <zephyr/ztest.h>
15
15
16
- static sem_t sema ;
17
- static void * dummy_sem ;
18
-
19
16
static void * child_func (void * p1 )
20
17
{
21
- zassert_equal (sem_post (& sema ), 0 , "sem_post failed" );
18
+ sem_t * sem = (sem_t * )p1 ;
19
+
20
+ zassert_equal (sem_post (sem ), 0 , "sem_post failed" );
22
21
return NULL ;
23
22
}
24
23
25
- ZTEST ( posix_apis , test_semaphore )
24
+ static void semaphore_test ( sem_t * sem )
26
25
{
27
26
pthread_t thread1 , thread2 ;
28
27
int val , ret ;
@@ -31,28 +30,23 @@ ZTEST(posix_apis, test_semaphore)
31
30
/* TESTPOINT: Check if sema value is less than
32
31
* CONFIG_SEM_VALUE_MAX
33
32
*/
34
- zassert_equal (sem_init (& sema , 0 , (CONFIG_SEM_VALUE_MAX + 1 )), -1 ,
33
+ zassert_equal (sem_init (sem , 0 , (CONFIG_SEM_VALUE_MAX + 1 )), -1 ,
35
34
"value larger than %d\n" , CONFIG_SEM_VALUE_MAX );
36
35
zassert_equal (errno , EINVAL );
37
36
38
- zassert_equal (sem_init (& sema , 0 , 0 ), 0 , "sem_init failed" );
39
-
40
- /* TESTPOINT: Call sem_post with invalid kobject */
41
- zassert_equal (sem_post (dummy_sem ), -1 , "sem_post of"
42
- " invalid semaphore object didn't fail" );
43
- zassert_equal (errno , EINVAL );
37
+ zassert_equal (sem_init (sem , 0 , 0 ), 0 , "sem_init failed" );
44
38
45
39
/* TESTPOINT: Check if semaphore value is as set */
46
- zassert_equal (sem_getvalue (& sema , & val ), 0 );
40
+ zassert_equal (sem_getvalue (sem , & val ), 0 );
47
41
zassert_equal (val , 0 );
48
42
49
43
/* TESTPOINT: Check if sema is acquired when it
50
44
* is not available
51
45
*/
52
- zassert_equal (sem_trywait (& sema ), -1 );
46
+ zassert_equal (sem_trywait (sem ), -1 );
53
47
zassert_equal (errno , EAGAIN );
54
48
55
- ret = pthread_create (& thread1 , NULL , child_func , NULL );
49
+ ret = pthread_create (& thread1 , NULL , child_func , sem );
56
50
zassert_equal (ret , 0 , "Thread creation failed" );
57
51
58
52
zassert_equal (clock_gettime (CLOCK_REALTIME , & abstime ), 0 ,
@@ -63,38 +57,54 @@ ZTEST(posix_apis, test_semaphore)
63
57
/* TESPOINT: Wait for 5 seconds and acquire sema given
64
58
* by thread1
65
59
*/
66
- zassert_equal (sem_timedwait (& sema , & abstime ), 0 );
60
+ zassert_equal (sem_timedwait (sem , & abstime ), 0 );
67
61
68
62
/* TESTPOINT: Semaphore is already acquired, check if
69
63
* no semaphore is available
70
64
*/
71
- zassert_equal (sem_timedwait (& sema , & abstime ), -1 );
65
+ zassert_equal (sem_timedwait (sem , & abstime ), -1 );
72
66
zassert_equal (errno , ETIMEDOUT );
73
67
74
- /* TESTPOINT: sem_destroy with invalid kobject */
75
- zassert_equal (sem_destroy (dummy_sem ), -1 , "invalid"
76
- " semaphore is destroyed" );
77
- zassert_equal (errno , EINVAL );
78
-
79
- zassert_equal (sem_destroy (& sema ), 0 , "semaphore is not destroyed" );
68
+ zassert_equal (sem_destroy (sem ), 0 , "semaphore is not destroyed" );
80
69
81
70
/* TESTPOINT: Initialize sema with 1 */
82
- zassert_equal (sem_init (& sema , 0 , 1 ), 0 , "sem_init failed" );
83
- zassert_equal (sem_getvalue (& sema , & val ), 0 );
71
+ zassert_equal (sem_init (sem , 0 , 1 ), 0 , "sem_init failed" );
72
+ zassert_equal (sem_getvalue (sem , & val ), 0 );
84
73
zassert_equal (val , 1 );
85
74
86
- zassert_equal (sem_destroy (& sema ), -1 , "acquired semaphore"
75
+ zassert_equal (sem_destroy (sem ), -1 , "acquired semaphore"
87
76
" is destroyed" );
88
77
zassert_equal (errno , EBUSY );
89
78
90
79
/* TESTPOINT: take semaphore which is initialized with 1 */
91
- zassert_equal (sem_trywait (& sema ), 0 );
80
+ zassert_equal (sem_trywait (sem ), 0 );
92
81
93
- zassert_equal (pthread_create (& thread2 , NULL , child_func , NULL ), 0 ,
82
+ zassert_equal (pthread_create (& thread2 , NULL , child_func , sem ), 0 ,
94
83
"Thread creation failed" );
95
84
96
85
/* TESTPOINT: Wait and acquire semaphore till thread2 gives */
97
- zassert_equal (sem_wait (& sema ), 0 , "sem_wait failed" );
86
+ zassert_equal (sem_wait (sem ), 0 , "sem_wait failed" );
87
+
88
+ /* Make sure the threads are terminated */
89
+ zassert_ok (pthread_join (thread1 , NULL ));
90
+ zassert_ok (pthread_join (thread2 , NULL ));
91
+ }
92
+
93
+ ZTEST (posix_apis , test_semaphore )
94
+ {
95
+ sem_t sema ;
96
+
97
+ /* TESTPOINT: Call sem_post with invalid kobject */
98
+ zassert_equal (sem_post (NULL ), -1 , "sem_post of"
99
+ " invalid semaphore object didn't fail" );
100
+ zassert_equal (errno , EINVAL );
101
+
102
+ /* TESTPOINT: sem_destroy with invalid kobject */
103
+ zassert_equal (sem_destroy (NULL ), -1 , "invalid"
104
+ " semaphore is destroyed" );
105
+ zassert_equal (errno , EINVAL );
106
+
107
+ semaphore_test (& sema );
98
108
}
99
109
100
110
unsigned int nsem_get_ref_count (sem_t * sem );
0 commit comments