source: rtems/testsuites/psxtests/psxkey06/init.c @ b5c9064

4.115
Last change on this file since b5c9064 was b5c9064, checked in by Zhongwei Yao <ashi08104@…>, on 08/05/13 at 13:20:45

Unlimited objects support for POSIX keys

This patch enables unlimited model in POSIX key manger and have a decent
runtime on POSIX key searching, adding and deleting operations. Memory
overhead is lower than current implementation when the size of key and key
value becomes big.

  • Property mode set to 100644
File size: 3.8 KB
Line 
1/*
2 *  Copyright (c) 2012 Zhongwei Yao.
3 *  COPYRIGHT (c) 1989-2012.
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.com/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/* forward declarations to avoid warnings */
21void *POSIX_Init(void *argument);
22void *Test_Thread1(void *argument);
23void *Test_Thread2(void *argument);
24
25int Data_array[4] = {1, 2, 3, 4};
26
27pthread_key_t key1, key2;
28
29void *Test_Thread1(
30  void *argument
31)
32{
33  int sc;
34  int *value;
35  struct timespec  delay_request;
36  /*
37   * Detach ourselves so we don't wait for a join that won't happen.
38   */
39  pthread_detach( pthread_self() );
40
41  puts( "Test_Thread 1 - key1 pthread_setspecific - OK" );
42  sc = pthread_setspecific( key1, &Data_array[0] );
43  rtems_test_assert( !sc );
44
45  puts( "Test_Thread 1 - key2 pthread_setspecific - OK" );
46  sc = pthread_setspecific( key2, &Data_array[1] );
47  rtems_test_assert( !sc );
48
49  puts( "Test_Thread 1 - sleep - let thread2 run - OK" );
50  delay_request.tv_sec = 0;
51  delay_request.tv_nsec = 4 * 100000000;
52  sc = nanosleep( &delay_request, NULL );
53  rtems_test_assert( !sc );
54
55  puts( "Test_Thread 1 - key1 pthread_getspecific - OK" );
56  value = pthread_getspecific( key1 );
57  rtems_test_assert( *value == Data_array[0] );
58
59  puts( "Test_Thread 1 - key2 pthread_getspecific - OK" );
60  value = pthread_getspecific( key2 );
61  rtems_test_assert( *value == Data_array[1] );
62
63  return NULL;
64}
65
66void *Test_Thread2(
67  void *argument
68)
69{
70  int sc;
71  int *value;
72  /*
73   * Detach ourselves so we don't wait for a join that won't happen.
74   */
75  pthread_detach( pthread_self() );
76
77  puts( "Test_Thread 2 - key1 pthread_setspecific - OK" );
78  sc = pthread_setspecific( key1, &Data_array[2] );
79  rtems_test_assert( !sc );
80
81  puts( "Test_Thread 2 - key2 pthread_setspecific - OK" );
82  sc = pthread_setspecific( key2, &Data_array[3] );
83  rtems_test_assert( !sc );
84
85  puts( "Test_Thread 2 - key1 pthread_getspecific - OK" );
86  value = pthread_getspecific( key1 );
87  rtems_test_assert( *value == Data_array[2] );
88
89  puts( "Test_Thread 2 - key2 pthread_getspecific - OK" );
90  value = pthread_getspecific( key2 );
91  rtems_test_assert( *value == Data_array[3] );
92
93  return NULL;
94}
95
96void *POSIX_Init(
97  void *ignored
98)
99{
100  pthread_t        thread1, thread2;
101  int              sc;
102  struct timespec  delay_request;
103
104  puts( "\n\n*** TEST KEY 06 ***" );
105
106  puts( "Init - pthread key1 create - OK" );
107  sc = pthread_key_create( &key1, NULL );
108  rtems_test_assert( !sc );
109
110  puts( "Init - pthread key2 create - OK" );
111  sc = pthread_key_create( &key2, NULL );
112  rtems_test_assert( !sc );
113
114  puts( "Init - pthread1 create - OK" );
115  sc = pthread_create( &thread1, NULL, Test_Thread1, NULL );
116  rtems_test_assert( !sc );
117
118  puts( "Init - pthread2 create - OK" );
119  sc = pthread_create( &thread2, NULL, Test_Thread2, NULL );
120  rtems_test_assert( !sc );
121
122  puts( "Init - sleep - let thread run - OK" );
123  delay_request.tv_sec = 0;
124  delay_request.tv_nsec = 8 * 100000000;
125  sc = nanosleep( &delay_request, NULL );
126  rtems_test_assert( !sc );
127
128  puts( "Init - pthread key1 delete - OK" );
129  sc = pthread_key_delete( key1 );
130  rtems_test_assert( sc == 0 );
131
132  puts( "Init - pthread key2 delete - OK" );
133  sc = pthread_key_delete( key2 );
134  rtems_test_assert( sc == 0 );
135
136  puts( "*** END OF TEST KEY 06 ***" );
137  rtems_test_exit(0);
138}
139
140/* configuration information */
141
142#define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER
143#define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER
144
145#define CONFIGURE_MAXIMUM_POSIX_THREADS  3
146#define CONFIGURE_MAXIMUM_POSIX_KEYS     2
147
148#define CONFIGURE_POSIX_INIT_THREAD_TABLE
149
150#define CONFIGURE_INIT
151#include <rtems/confdefs.h>
152
153/* global variables */
Note: See TracBrowser for help on using the repository browser.