summaryrefslogtreecommitdiffstats
path: root/testsuites/smptests/smpscheduler01/init.c
blob: 9f0b5c1ed2ec9cabc94b939a62b9c4ba66257200 (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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/*
 * Copyright (c) 2013 embedded brains GmbH.  All rights reserved.
 *
 *  embedded brains GmbH
 *  Dornierstr. 4
 *  82178 Puchheim
 *  Germany
 *  <rtems@embedded-brains.de>
 *
 * The license and distribution terms for this file may be
 * found in the file LICENSE in this distribution or at
 * http://www.rtems.org/license/LICENSE.
 */

#ifdef HAVE_CONFIG_H
  #include "config.h"
#endif

#define TEST_INIT

#include <rtems.h>
#include <rtems/score/threadimpl.h>

#include "tmacros.h"

const char rtems_test_name[] = "SMPSCHEDULER 1";

#define CPU_COUNT 2

#define TASK_COUNT 4

#define FIRST_TASK_PRIORITY 1

#define SECOND_TASK_READY RTEMS_EVENT_0

static rtems_id task_ids[TASK_COUNT];

static void suspend(size_t i)
{
  rtems_status_code sc = rtems_task_suspend(task_ids[i]);
  rtems_test_assert(sc == RTEMS_SUCCESSFUL);
}

static void resume(size_t i)
{
  rtems_status_code sc = rtems_task_resume(task_ids[i]);
  rtems_test_assert(sc == RTEMS_SUCCESSFUL);
}

static void task(rtems_task_argument arg)
{
  rtems_task_priority task_priority;
  rtems_status_code sc;

  sc = rtems_task_set_priority(
    RTEMS_SELF,
    RTEMS_CURRENT_PRIORITY,
    &task_priority
  );
  rtems_test_assert(sc == RTEMS_SUCCESSFUL);

  if (arg == 1) {
    sc = rtems_event_send(task_ids[0], SECOND_TASK_READY);
    rtems_test_assert(sc == RTEMS_SUCCESSFUL);
  }

  while (true) {
    /* Do nothing */
  }
}

static bool is_per_cpu_state_ok(void)
{
  bool ok = true;
  uint32_t n = rtems_get_processor_count();
  uint32_t i;

  for (i = 0; i < n; ++i) {
    const Thread_Control *thread = _Per_CPU_Get_by_index(i)->executing;
    uint32_t count = 0;
    uint32_t j;

    for (j = 0; j < n; ++j) {
      const Per_CPU_Control *cpu = _Per_CPU_Get_by_index(j);
      const Thread_Control *executing = cpu->executing;
      const Thread_Control *heir = cpu->heir;

      if (i != j) {
        count += executing == thread;
        count += heir == thread;
      } else {
        ++count;
      }

      ok = ok && _Thread_Get_CPU( executing ) == cpu;
      ok = ok && _Thread_Get_CPU( heir ) == cpu;
    }

    ok = ok && (count == 1);
  }

  return ok;
}

static void test_scheduler_cross(void)
{
  bool per_cpu_state_ok;
  Per_CPU_Control *cpu_self;

  cpu_self = _Thread_Dispatch_disable();

  suspend(0);
  suspend(1);
  resume(0);
  resume(1);

  per_cpu_state_ok = is_per_cpu_state_ok();

  _Thread_Dispatch_enable( cpu_self );

  rtems_test_assert(per_cpu_state_ok);
}

static void test_scheduler_move_heir(void)
{
  bool per_cpu_state_ok;
  Per_CPU_Control *cpu_self;

  cpu_self = _Thread_Dispatch_disable();

  suspend(2);
  suspend(3);
  suspend(0);
  resume(2);
  suspend(1);
  resume(3);
  resume(0);

  per_cpu_state_ok = is_per_cpu_state_ok();

  resume(1);

  _Thread_Dispatch_enable( cpu_self );

  rtems_test_assert(per_cpu_state_ok);
}

static void test(void)
{
  rtems_event_set events;
  rtems_status_code sc;
  rtems_task_argument task_index;

  task_ids[0] = rtems_task_self();

  for (task_index = 1; task_index < TASK_COUNT; ++task_index) {
      rtems_id task_id;

      sc = rtems_task_create(
        rtems_build_name('T', 'A', 'S', 'K'),
        FIRST_TASK_PRIORITY + task_index,
        RTEMS_MINIMUM_STACK_SIZE,
        RTEMS_DEFAULT_MODES,
        RTEMS_DEFAULT_ATTRIBUTES,
        &task_id
      );
      rtems_test_assert(sc == RTEMS_SUCCESSFUL);

      sc = rtems_task_start(task_id, task, task_index);
      rtems_test_assert(sc == RTEMS_SUCCESSFUL);

      task_ids[task_index] = task_id;
  }

  sc = rtems_event_receive(
    SECOND_TASK_READY,
    RTEMS_EVENT_ALL | RTEMS_WAIT,
    RTEMS_NO_TIMEOUT,
    &events
  );
  rtems_test_assert(sc == RTEMS_SUCCESSFUL);
  rtems_test_assert(events == SECOND_TASK_READY);

  test_scheduler_cross();
  test_scheduler_move_heir();
}

static void Init(rtems_task_argument arg)
{
  TEST_BEGIN();

  test();

  TEST_END();
  rtems_test_exit(0);
}

#define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER
#define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER

#define CONFIGURE_MAXIMUM_PROCESSORS CPU_COUNT

#define CONFIGURE_MAXIMUM_TASKS TASK_COUNT

/* We need a scheduler with lazy processor allocation for this test */
#define CONFIGURE_SCHEDULER_SIMPLE_SMP

#define CONFIGURE_INIT_TASK_PRIORITY FIRST_TASK_PRIORITY
#define CONFIGURE_INIT_TASK_INITIAL_MODES RTEMS_DEFAULT_MODES
#define CONFIGURE_INIT_TASK_ATTRIBUTES RTEMS_DEFAULT_ATTRIBUTES

#define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION

#define CONFIGURE_RTEMS_INIT_TASKS_TABLE

#define CONFIGURE_INIT

#include <rtems/confdefs.h>