summaryrefslogtreecommitdiffstats
path: root/testsuites/sptests/sp44/init.c
blob: fc9d60b6715807bde2fdbbcfa127e625e8650710 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
 * Original version submitted as part of PR1212
 *
 * This example shows a possible blocking of timeslicing if task mode is
 * changed to timeslicing in the middle of its executing.
 *
 * The expected output is:
 *
 *   Task #0's turn. Now setting turn to 1
 *   Task #1's turn. Now setting turn to 0
 *   Task #0's turn. Now setting turn to 1
 *   Task #1's turn. Now setting turn to 0
 *   Task #0's turn. Now setting turn to 1
 *   Task #1's turn. Now setting turn to 0
 *   Task #0's turn. Now setting turn to 1
 *   ...
 *
 * The actural output is:
 *
 *   Task #0's turn. Now setting turn to 1
 *
 * Task #1 can not be dispatched also both tasks have timeslice enabled.
 *
 * Setting TASK_A/B_INITMODE to RTEMS_DEFAULT_MODES | RTEMS_TIMESLICE
 * will produce the expected output.
 *
 * $Id$
 */

#include <tmacros.h>
#include <rtems.h>
#include <stdio.h>
#include <stdlib.h>

rtems_task Init(rtems_task_argument ignored);
rtems_task TaskAB_entry(rtems_task_argument me);

/*** Task priorities ***/
#define TASK_A_PRIORITY    10
#define TASK_B_PRIORITY   10

/*** Task names ***/
#define TASK_A_NAME     1
#define TASK_B_NAME     2

/*** Task atributes ***/
#define TASK_A_INITMODE    RTEMS_DEFAULT_MODES
#define TASK_B_INITMODE    RTEMS_DEFAULT_MODES

/*** Task generic parameters ***/
#define TASK_A_STACKSIZE   RTEMS_MINIMUM_STACK_SIZE
#define TASK_A_MODEATTR    RTEMS_DEFAULT_ATTRIBUTES
#define TASK_B_STACKSIZE   RTEMS_MINIMUM_STACK_SIZE
#define TASK_B_MODEATTR    RTEMS_DEFAULT_ATTRIBUTES

volatile uint32_t turn;
rtems_id TaskA_id, TaskB_id;

#define TEST_FAILED(_status, _msg) \
  do { \
    printf("Test failed, status code: %d, msg: %s\n", _status, _msg); \
    exit(1); \
  } while(0)

/* TASK A/B */
rtems_task TaskAB_entry(rtems_task_argument me)
{
  static rtems_mode previous_mode_set;
  rtems_status_code status;
  uint32_t iterations = 0;

  status = rtems_task_mode(RTEMS_PREEMPT | RTEMS_TIMESLICE,
                  RTEMS_PREEMPT_MASK | RTEMS_TIMESLICE_MASK,
                  &previous_mode_set);
  if (status != RTEMS_SUCCESSFUL)
    TEST_FAILED(status, "Unable to change task mode.");

  while(1) {
    if (turn == me) {
      printf("Task #%" PRIdrtems_task_argument "'s turn. Now setting turn to %" PRIdrtems_task_argument "\n", me, 1 - me);
      turn = 1 - me;

      if ( ++iterations == 10 ) {
        puts( "*** END OF SP44 TEST ***" );
        exit( 0 );
      }
    }
  }
}

rtems_task Init(rtems_task_argument ignored)
{
  static rtems_status_code status;

  puts( "\n\n*** SP44 TEST ***" );

  /* Create Task A */
  status = rtems_task_create(TASK_A_NAME, TASK_A_PRIORITY, TASK_A_STACKSIZE,
         TASK_A_INITMODE, TASK_A_MODEATTR, &TaskA_id);

  if (status != RTEMS_SUCCESSFUL)
    TEST_FAILED(status,"rtems_task_create failed.\n");

  /* Start Task A */
  status = rtems_task_start(TaskA_id, TaskAB_entry, 0);
  if (status != RTEMS_SUCCESSFUL)
    TEST_FAILED(status,"rtems_task_start failed.\n");

  /* Create Task B */
  status = rtems_task_create(TASK_B_NAME, TASK_B_PRIORITY, TASK_B_STACKSIZE,
         TASK_B_INITMODE, TASK_B_MODEATTR, &TaskB_id);

  if (status != RTEMS_SUCCESSFUL)
    TEST_FAILED(status,"rtems_task_create failed.\n");

  /* Start Task B */
  status = rtems_task_start(TaskB_id, TaskAB_entry, 1);
  if (status != RTEMS_SUCCESSFUL)
    TEST_FAILED(status,"rtems_task_start failed.\n");

  /* Suspend itself */
  status = rtems_task_suspend(RTEMS_SELF);
  if (status != RTEMS_SUCCESSFUL)
    TEST_FAILED(status,"rtems_task_suspend failed.\n");

  /* This task is not suposed to be executed anymore */
  printf("\nNOT SUPOSED TO RETURN HERE...\n");
  exit(1);
}

/* configuration information */

#define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER
#define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER

#define CONFIGURE_MICROSECONDS_PER_TICK 1000
#define CONFIGURE_TICKS_PER_TIMESLICE   10
#define CONFIGURE_MAXIMUM_TASKS         3

#define CONFIGURE_RTEMS_INIT_TASKS_TABLE

#define CONFIGURE_INIT

#include <rtems/confdefs.h>