1 | /* |
---|
2 | * Copyright (c) 2012 Zhongwei Yao. |
---|
3 | * COPYRIGHT (c) 1989-2014. |
---|
4 | * On-Line Applications Research Corporation (OAR). |
---|
5 | * |
---|
6 | * The license and distribution terms for this file may be |
---|
7 | * found in the file LICENSE in this distribution or at |
---|
8 | * http://www.rtems.org/license/LICENSE. |
---|
9 | */ |
---|
10 | |
---|
11 | #ifdef HAVE_CONFIG_H |
---|
12 | #include "config.h" |
---|
13 | #endif |
---|
14 | |
---|
15 | #include <pthread.h> |
---|
16 | #include <errno.h> |
---|
17 | #include "tmacros.h" |
---|
18 | #include "pmacros.h" |
---|
19 | |
---|
20 | const char rtems_test_name[] = "PSXKEY 4"; |
---|
21 | |
---|
22 | /* forward declarations to avoid warnings */ |
---|
23 | rtems_task Init(rtems_task_argument argument); |
---|
24 | rtems_task Test_Thread1(rtems_task_argument argument); |
---|
25 | rtems_task Test_Thread2(rtems_task_argument argument); |
---|
26 | |
---|
27 | int Data_array[2] = {1, 2}; |
---|
28 | rtems_id thread1, thread2; |
---|
29 | |
---|
30 | pthread_key_t Key; |
---|
31 | |
---|
32 | rtems_task Test_Thread1( rtems_task_argument argument ) |
---|
33 | { |
---|
34 | int sc; |
---|
35 | int *value; |
---|
36 | struct timespec delay_request; |
---|
37 | |
---|
38 | puts( "Test_Thread 1 - pthread_setspecific - OK" ); |
---|
39 | sc = pthread_setspecific( Key, &Data_array[0] ); |
---|
40 | rtems_test_assert( !sc ); |
---|
41 | |
---|
42 | puts( "Test_Thread 1 - sleep - let thread 2 run - OK" ); |
---|
43 | delay_request.tv_sec = 0; |
---|
44 | delay_request.tv_nsec = 4 * 100000000; |
---|
45 | sc = nanosleep( &delay_request, NULL ); |
---|
46 | rtems_test_assert( !sc ); |
---|
47 | |
---|
48 | puts( "Test_Thread 1 - pthread_getspecific - OK" ); |
---|
49 | value = pthread_getspecific( Key ); |
---|
50 | rtems_test_assert( *value == Data_array[0] ); |
---|
51 | |
---|
52 | rtems_task_delete( RTEMS_SELF ); |
---|
53 | } |
---|
54 | |
---|
55 | rtems_task Test_Thread2( rtems_task_argument argument ) |
---|
56 | { |
---|
57 | int sc; |
---|
58 | int *value; |
---|
59 | |
---|
60 | puts( "Test_Thread 2 - pthread_setspecific - OK" ); |
---|
61 | sc = pthread_setspecific( Key, &Data_array[1] ); |
---|
62 | rtems_test_assert( !sc ); |
---|
63 | |
---|
64 | puts( "Test_Thread 2 - pthread_getspecific - OK" ); |
---|
65 | value = pthread_getspecific( Key ); |
---|
66 | rtems_test_assert( *value == Data_array[1] ); |
---|
67 | |
---|
68 | rtems_task_delete( RTEMS_SELF ); |
---|
69 | } |
---|
70 | |
---|
71 | rtems_task Init( rtems_task_argument ignored ) |
---|
72 | { |
---|
73 | int sc; |
---|
74 | rtems_status_code rc; |
---|
75 | struct timespec delay_request; |
---|
76 | |
---|
77 | TEST_BEGIN(); |
---|
78 | |
---|
79 | puts( "Init - pthread_key_create - OK" ); |
---|
80 | sc = pthread_key_create( &Key, NULL ); |
---|
81 | rtems_test_assert( !sc ); |
---|
82 | |
---|
83 | puts( "Init - create - 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 | &thread1 |
---|
91 | ); |
---|
92 | rtems_test_assert( rc == RTEMS_SUCCESSFUL ); |
---|
93 | |
---|
94 | rc = rtems_task_start( thread1, Test_Thread1, 0 ); |
---|
95 | rtems_test_assert( rc == RTEMS_SUCCESSFUL ); |
---|
96 | |
---|
97 | rc = rtems_task_create( |
---|
98 | rtems_build_name( 'T', 'E', 'S', 'T' ), |
---|
99 | 1, |
---|
100 | RTEMS_MINIMUM_STACK_SIZE, |
---|
101 | RTEMS_DEFAULT_MODES, |
---|
102 | RTEMS_DEFAULT_ATTRIBUTES, |
---|
103 | &thread2 |
---|
104 | ); |
---|
105 | rtems_test_assert( rc == RTEMS_SUCCESSFUL ); |
---|
106 | |
---|
107 | rc = rtems_task_start( thread2, Test_Thread2, 0 ); |
---|
108 | rtems_test_assert( rc == RTEMS_SUCCESSFUL ); |
---|
109 | |
---|
110 | puts( "Init - sleep - let thread run - OK" ); |
---|
111 | delay_request.tv_sec = 0; |
---|
112 | delay_request.tv_nsec = 8 * 100000000; |
---|
113 | sc = nanosleep( &delay_request, NULL ); |
---|
114 | rtems_test_assert( !sc ); |
---|
115 | |
---|
116 | puts( "Init - pthread_key_delete - OK" ); |
---|
117 | sc = pthread_key_delete( Key ); |
---|
118 | rtems_test_assert( sc == 0 ); |
---|
119 | |
---|
120 | TEST_END(); |
---|
121 | rtems_test_exit(0); |
---|
122 | } |
---|
123 | |
---|
124 | /* configuration information */ |
---|
125 | |
---|
126 | #define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER |
---|
127 | #define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER |
---|
128 | |
---|
129 | #define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION |
---|
130 | |
---|
131 | #define CONFIGURE_MAXIMUM_TASKS 3 |
---|
132 | #define CONFIGURE_MAXIMUM_POSIX_KEYS 1 |
---|
133 | |
---|
134 | #define CONFIGURE_RTEMS_INIT_TASKS_TABLE |
---|
135 | |
---|
136 | #define CONFIGURE_INIT |
---|
137 | #include <rtems/confdefs.h> |
---|
138 | |
---|
139 | /* global variables */ |
---|