1 | /* SPDX-License-Identifier: BSD-2-Clause */ |
---|
2 | |
---|
3 | /* |
---|
4 | * COPYRIGHT (c) 1989-2014. |
---|
5 | * On-Line Applications Research Corporation (OAR). |
---|
6 | * |
---|
7 | * Redistribution and use in source and binary forms, with or without |
---|
8 | * modification, are permitted provided that the following conditions |
---|
9 | * are met: |
---|
10 | * 1. Redistributions of source code must retain the above copyright |
---|
11 | * notice, this list of conditions and the following disclaimer. |
---|
12 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
13 | * notice, this list of conditions and the following disclaimer in the |
---|
14 | * documentation and/or other materials provided with the distribution. |
---|
15 | * |
---|
16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
---|
17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
---|
20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
---|
21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
---|
22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
---|
23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
---|
24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
---|
25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
---|
26 | * POSSIBILITY OF SUCH DAMAGE. |
---|
27 | */ |
---|
28 | |
---|
29 | #ifdef HAVE_CONFIG_H |
---|
30 | #include "config.h" |
---|
31 | #endif |
---|
32 | |
---|
33 | #include <pthread.h> |
---|
34 | #include <errno.h> |
---|
35 | #include "tmacros.h" |
---|
36 | #include "pmacros.h" |
---|
37 | |
---|
38 | const char rtems_test_name[] = "PSXKEY 3"; |
---|
39 | |
---|
40 | /* forward declarations to avoid warnings */ |
---|
41 | rtems_task Init(rtems_task_argument value ); |
---|
42 | rtems_task Test_Thread( rtems_task_argument value ); |
---|
43 | |
---|
44 | void destructor(void *value); |
---|
45 | |
---|
46 | pthread_key_t Key; |
---|
47 | volatile bool destructor_ran; |
---|
48 | |
---|
49 | void destructor(void *value) |
---|
50 | { |
---|
51 | destructor_ran = true; |
---|
52 | } |
---|
53 | |
---|
54 | rtems_task Test_Thread( rtems_task_argument value ) |
---|
55 | { |
---|
56 | int sc; |
---|
57 | void *key_value = (void *) value; |
---|
58 | |
---|
59 | puts( "Test_Thread - pthread_setspecific - OK" ); |
---|
60 | sc = pthread_setspecific( Key, key_value ); |
---|
61 | rtems_test_assert( !sc ); |
---|
62 | |
---|
63 | puts( "Test_Thread - pthread_exit to run key destructors - OK" ); |
---|
64 | rtems_task_exit(); |
---|
65 | } |
---|
66 | |
---|
67 | rtems_task Init(rtems_task_argument ignored) |
---|
68 | { |
---|
69 | rtems_id thread; |
---|
70 | rtems_status_code rc; |
---|
71 | int sc; |
---|
72 | struct timespec delay_request; |
---|
73 | |
---|
74 | TEST_BEGIN(); |
---|
75 | |
---|
76 | /* |
---|
77 | * Key with NULL destructor |
---|
78 | */ |
---|
79 | puts( "Init - pthread_key_create with NULL destructor - OK" ); |
---|
80 | sc = pthread_key_create( &Key, NULL ); |
---|
81 | rtems_test_assert( !sc ); |
---|
82 | |
---|
83 | puts( "Init - create/start - OK" ); |
---|
84 | rc = rtems_task_create( |
---|
85 | rtems_build_name( 'T', 'E', 'S', 'T' ), |
---|
86 | 1, |
---|
87 | RTEMS_MINIMUM_STACK_SIZE, |
---|
88 | RTEMS_DEFAULT_MODES, |
---|
89 | RTEMS_DEFAULT_ATTRIBUTES, |
---|
90 | &thread |
---|
91 | ); |
---|
92 | rtems_test_assert( rc == RTEMS_SUCCESSFUL ); |
---|
93 | |
---|
94 | rc = rtems_task_start( thread, Test_Thread, 0 ); |
---|
95 | rtems_test_assert( rc == RTEMS_SUCCESSFUL ); |
---|
96 | |
---|
97 | puts( "Init - sleep - let thread run - OK" ); |
---|
98 | delay_request.tv_sec = 0; |
---|
99 | delay_request.tv_nsec = 5 * 100000000; |
---|
100 | sc = nanosleep( &delay_request, NULL ); |
---|
101 | rtems_test_assert( !sc ); |
---|
102 | |
---|
103 | puts( "Init - pthread_key_delete - OK" ); |
---|
104 | sc = pthread_key_delete( Key ); |
---|
105 | rtems_test_assert( sc == 0 ); |
---|
106 | |
---|
107 | /* |
---|
108 | * Key with non-NULL destructor |
---|
109 | */ |
---|
110 | destructor_ran = false; |
---|
111 | puts( "Init - pthread_key_create with non-NULL destructor - OK" ); |
---|
112 | sc = pthread_key_create( &Key, destructor ); |
---|
113 | rtems_test_assert( !sc ); |
---|
114 | |
---|
115 | puts( "Init - task create/start - OK" ); |
---|
116 | rc = rtems_task_create( |
---|
117 | rtems_build_name( 'T', 'E', 'S', 'T' ), |
---|
118 | 1, |
---|
119 | RTEMS_MINIMUM_STACK_SIZE, |
---|
120 | RTEMS_DEFAULT_MODES, |
---|
121 | RTEMS_DEFAULT_ATTRIBUTES, |
---|
122 | &thread |
---|
123 | ); |
---|
124 | rtems_test_assert( rc == RTEMS_SUCCESSFUL ); |
---|
125 | |
---|
126 | rc = rtems_task_start( thread, Test_Thread, 0 ); |
---|
127 | rtems_test_assert( rc == RTEMS_SUCCESSFUL ); |
---|
128 | |
---|
129 | puts( "Init - sleep - let thread run - OK" ); |
---|
130 | sc = nanosleep( &delay_request, NULL ); |
---|
131 | rtems_test_assert( !sc ); |
---|
132 | |
---|
133 | puts( "Init - verify destructor did NOT ran" ); |
---|
134 | rtems_test_assert( destructor_ran == false ); |
---|
135 | |
---|
136 | puts( "Init - pthread_key_delete - OK" ); |
---|
137 | sc = pthread_key_delete( Key ); |
---|
138 | rtems_test_assert( sc == 0 ); |
---|
139 | |
---|
140 | TEST_END(); |
---|
141 | rtems_test_exit(0); |
---|
142 | } |
---|
143 | |
---|
144 | /* configuration information */ |
---|
145 | |
---|
146 | #define CONFIGURE_APPLICATION_NEEDS_SIMPLE_CONSOLE_DRIVER |
---|
147 | #define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER |
---|
148 | |
---|
149 | #define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION |
---|
150 | |
---|
151 | #define CONFIGURE_MAXIMUM_TASKS 2 |
---|
152 | #define CONFIGURE_MAXIMUM_POSIX_KEYS 1 |
---|
153 | |
---|
154 | #define CONFIGURE_RTEMS_INIT_TASKS_TABLE |
---|
155 | |
---|
156 | #define CONFIGURE_INIT |
---|
157 | #include <rtems/confdefs.h> |
---|
158 | |
---|
159 | /* global variables */ |
---|