source: rtems/testsuites/psxtests/psxcleanup01/init.c @ 9a4eca5

5
Last change on this file since 9a4eca5 was 1b1be254, checked in by Sebastian Huber <sebastian.huber@…>, on 03/25/14 at 09:54:49

score: Thread life cycle re-implementation

The thread deletion is now supported on SMP.

This change fixes the following PRs:

PR1814: SMP race condition between stack free and dispatch

PR2035: psxcancel reveals NULL pointer access in _Thread_queue_Extract()

The POSIX cleanup handler are now called in the right context (should be
called in the context of the terminating thread).

http://pubs.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_09.html

Add a user extension the reflects a thread termination event. This is
used to reclaim the Newlib reentrancy structure (may use file
operations), the POSIX cleanup handlers and the POSIX key destructors.

  • Property mode set to 100644
File size: 3.0 KB
Line 
1/*
2 *  COPYRIGHT (c) 1989-2012.
3 *  On-Line Applications Research Corporation (OAR).
4 *
5 *  The license and distribution terms for this file may be
6 *  found in the file LICENSE in this distribution or at
7 *  http://www.rtems.org/license/LICENSE.
8 */
9
10#ifdef HAVE_CONFIG_H
11#include "config.h"
12#endif
13
14#include <rtems.h>
15#include "pmacros.h"
16#include <pthread.h>  /* thread facilities */
17
18const char rtems_test_name[] = "PSXCLEANUP 1";
19
20/* forward declarations to avoid warnings */
21void *POSIX_Init(void *argument);
22
23static rtems_id main_task_id;
24
25static rtems_id restart_task_id;
26
27static volatile rtems_task_argument restart_cleanup_arg;
28
29static void wake_up_main(void)
30{
31  rtems_status_code sc;
32
33  sc = rtems_event_transient_send(main_task_id);
34  rtems_test_assert(sc == RTEMS_SUCCESSFUL);
35}
36
37static void wait_for_restart_task(void)
38{
39  rtems_status_code sc;
40
41  sc = rtems_event_transient_receive(RTEMS_WAIT, RTEMS_NO_TIMEOUT);
42  rtems_test_assert(sc == RTEMS_SUCCESSFUL);
43}
44
45static void restart_cleanup(void *arg)
46{
47  rtems_test_assert(restart_task_id == rtems_task_self());
48
49  restart_cleanup_arg = (rtems_task_argument) arg;
50
51  wake_up_main();
52}
53
54static void restart_task(rtems_task_argument arg)
55{
56  pthread_cleanup_push(restart_cleanup, (void *) arg);
57
58  wake_up_main();
59
60  rtems_test_assert(0);
61
62  pthread_cleanup_pop(0);
63}
64
65static void test_restart_with_cleanup(void)
66{
67  rtems_status_code sc;
68  rtems_id id;
69  rtems_task_priority prio = 1;
70
71  main_task_id = rtems_task_self();
72
73  sc = rtems_task_set_priority(RTEMS_SELF, prio, &prio);
74  rtems_test_assert(sc == RTEMS_SUCCESSFUL);
75
76  sc = rtems_task_create(
77    rtems_build_name('R', 'E', 'S', 'T'),
78    2,
79    RTEMS_MINIMUM_STACK_SIZE,
80    RTEMS_DEFAULT_MODES,
81    RTEMS_DEFAULT_ATTRIBUTES,
82    &id
83  );
84  rtems_test_assert(sc == RTEMS_SUCCESSFUL);
85
86  restart_task_id = id;
87
88  sc = rtems_task_start(id, restart_task, 1);
89  rtems_test_assert(sc == RTEMS_SUCCESSFUL);
90
91  wait_for_restart_task();
92
93  sc = rtems_task_restart(id, 2);
94  rtems_test_assert(sc == RTEMS_SUCCESSFUL);
95
96  wait_for_restart_task();
97
98  rtems_test_assert(restart_cleanup_arg == 1);
99
100  wait_for_restart_task();
101
102  sc = rtems_task_delete(id);
103  rtems_test_assert(sc == RTEMS_SUCCESSFUL);
104
105  wait_for_restart_task();
106
107  rtems_test_assert(restart_cleanup_arg == 2);
108}
109
110static void cleaner(void *arg)
111{
112  puts( "clean was not supposed to run" );
113  rtems_test_assert(0);
114}
115
116void *POSIX_Init(
117  void *argument
118)
119{
120  TEST_BEGIN();
121
122  test_restart_with_cleanup();
123
124  puts( "Init - pthread_cleanup_push - a routine we will not execute" );
125  pthread_cleanup_push(cleaner, NULL);
126
127  puts( "Init - pthread_cleanup_pop - do not execute" );
128  pthread_cleanup_pop(0);
129
130  TEST_END();
131  rtems_test_exit(0);
132}
133
134
135/* configuration information */
136
137#define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER
138#define CONFIGURE_APPLICATION_DOES_NOT_NEED_CLOCK_DRIVER
139
140#define CONFIGURE_POSIX_INIT_THREAD_TABLE
141
142#define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION
143
144#define CONFIGURE_MAXIMUM_TASKS 1
145
146#define CONFIGURE_MAXIMUM_POSIX_THREADS 1
147
148#define CONFIGURE_INIT
149#include <rtems/confdefs.h>
Note: See TracBrowser for help on using the repository browser.