summaryrefslogtreecommitdiffstats
path: root/testsuites/sptests/spintrcritical23/init.c
blob: ac9f1a105c9648415be16a606c750e94840b0dff (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
/*
 * Copyright (c) 2015 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

#include <tmacros.h>
#include <intrcritical.h>

#include <string.h>

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

const char rtems_test_name[] = "SPINTRCRITICAL 23";

typedef struct {
  RTEMS_INTERRUPT_LOCK_MEMBER(lock)
  rtems_id task_id;
  Thread_Control *tcb;
  rtems_task_priority priority_task;
  rtems_task_priority priority_interrupt;
  uint32_t priority_generation;
  Scheduler_priority_Node scheduler_node;
  bool done;
} test_context;

static test_context ctx_instance;

static Thread_Control *get_tcb(rtems_id id)
{
  Objects_Locations location;
  Thread_Control *tcb;

  tcb = _Thread_Get(id, &location);
  _Objects_Put(&tcb->Object);

  rtems_test_assert(tcb != NULL && location == OBJECTS_LOCAL);

  return tcb;
}

static bool scheduler_node_unchanged(const test_context *ctx)
{
   return memcmp(
     &ctx->scheduler_node,
     ctx->tcb->Scheduler.node,
     sizeof(ctx->scheduler_node)
   ) == 0;
}

static void change_priority(rtems_id timer, void *arg)
{
  /* The arg is NULL */
  test_context *ctx = &ctx_instance;
  rtems_interrupt_lock_context lock_context;
  rtems_task_priority priority_interrupt;
  rtems_task_priority priority_task;

  rtems_interrupt_lock_acquire(&ctx->lock, &lock_context);
  if (
    ctx->priority_generation != ctx->tcb->priority_generation
      && scheduler_node_unchanged(ctx)
  ) {
    ctx->done = true;
    priority_interrupt = ctx->priority_interrupt;
    priority_task = ctx->priority_task;
  }
  rtems_interrupt_lock_release(&ctx->lock, &lock_context);

  if (ctx->done) {
    rtems_status_code sc;
    rtems_task_priority previous;

    sc = rtems_task_set_priority(
      ctx->task_id,
      priority_interrupt,
      &previous
    );
    rtems_test_assert(sc == RTEMS_SUCCESSFUL);
    rtems_test_assert(previous == priority_task);
  }
}

static bool test_body(void *arg)
{
  test_context *ctx = arg;
  rtems_status_code sc;
  rtems_interrupt_lock_context lock_context;
  rtems_task_priority priority_last;
  rtems_task_priority priority_task;
  rtems_task_priority priority_interrupt;
  rtems_task_priority previous;

  rtems_interrupt_lock_acquire(&ctx->lock, &lock_context);
  priority_last = ctx->priority_task;
  priority_task = 1 + (priority_last + 1) % 3;
  priority_interrupt = 1 + (priority_task + 1) % 3;
  ctx->priority_task = priority_task;
  ctx->priority_interrupt = priority_interrupt;
  ctx->priority_generation = ctx->tcb->priority_generation;
  memcpy(
    &ctx->scheduler_node,
    ctx->tcb->Scheduler.node,
    sizeof(ctx->scheduler_node)
  );
  rtems_interrupt_lock_release(&ctx->lock, &lock_context);

  sc = rtems_task_set_priority(
    ctx->task_id,
    priority_task,
    &previous
  );
  rtems_test_assert(sc == RTEMS_SUCCESSFUL);
  rtems_test_assert(previous == priority_last);

  if (ctx->done) {
    sc = rtems_task_set_priority(
      ctx->task_id,
      RTEMS_CURRENT_PRIORITY,
      &previous
    );
    rtems_test_assert(sc == RTEMS_SUCCESSFUL);
    rtems_test_assert(previous == priority_interrupt);
  }

  return ctx->done;
}

static void Init(rtems_task_argument arg)
{
  test_context *ctx = &ctx_instance;
  rtems_status_code sc;

  TEST_BEGIN();

  rtems_interrupt_lock_initialize(&ctx->lock, "Test");
  ctx->priority_task = 1;

  sc = rtems_task_create(
    rtems_build_name('T', 'E', 'S', 'T'),
    ctx->priority_task,
    RTEMS_MINIMUM_STACK_SIZE,
    RTEMS_DEFAULT_MODES,
    RTEMS_DEFAULT_ATTRIBUTES,
    &ctx->task_id
  );
  rtems_test_assert(sc == RTEMS_SUCCESSFUL);

  ctx->tcb = get_tcb(ctx->task_id);

  interrupt_critical_section_test(test_body, ctx, change_priority);
  rtems_test_assert(ctx->done);

  TEST_END();
  rtems_test_exit(0);
}

#define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER
#define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER

#define CONFIGURE_MICROSECONDS_PER_TICK 1000

#define CONFIGURE_MAXIMUM_TASKS 2
#define CONFIGURE_MAXIMUM_TIMERS 1
#define CONFIGURE_MAXIMUM_USER_EXTENSIONS 1

#define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION

#define CONFIGURE_RTEMS_INIT_TASKS_TABLE

#define CONFIGURE_INIT

#include <rtems/confdefs.h>