summaryrefslogtreecommitdiffstats
path: root/cpukit/librtemscxx/thread.cpp
blob: bfc2b84925a6063c54542aeeed773de0e85b1e42 (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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
/* SPDX-License-Identifier: BSD-2-Clause */

/*
 * Copyright (C) 2020 Chris Johns (http://contemporary.software)
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#if !defined(_GNU_SOURCE)
#define _GNU_SOURCE
#endif

#include <system_error>

#include <rtems/error.hpp>
#include <rtems/thread.hpp>

#include <pthread.h>

#include <rtems.h>

#if HAVE_GET_SCHEDULER_NAME
extern "C" bool get_scheduler_name(rtems_id sid, char* name);
#endif

#if HAVE_GET_SCHEDULER_NAME
bool get_scheduler_name(rtems_id sid, char* name)
{
  name[0] = 'N';
  name[1] = 'O';
  name[2] = 'P';
  name[3] = '\0';
  return true;
}
#endif

namespace rtems
{
  namespace thread
  {
    void
    system_error_check(int ec, const char* what)
    {
      if (ec != 0) {
        throw std::system_error(ec, std::system_category(), what);
      }
    }

    attributes::attributes()
      : priority(-1),
        stack_size(RTEMS_MINIMUM_STACK_SIZE),
        attr(sched_inherit),
        policy(sched_fifo)
    {
      update();
    }

    attributes::attributes(const attributes& attr)
      : name(attr.name),
        priority(attr.priority),
        stack_size(attr.stack_size),
        scheduler(attr.scheduler),
        attr(attr.attr),
        policy(attr.policy)
    {
    }

    void
    attributes::set_name(const std::string& name_)
    {
      name = name_;
    }

    void
    attributes::set_name(const char* name_)
    {
      name = name_;
    }

    const std::string&
    attributes::get_name() const
    {
      return name;
    }

    void
    attributes::set_priority(int priority_)
    {
      priority = priority_;
    }

    int
    attributes::get_priority() const
    {
      return priority;
    }

    void
    attributes::set_stack_size(size_t size)
    {
      stack_size = size;
    }

    size_t
    attributes::get_stack_size() const
    {
      return stack_size;
    }

    void
    attributes::set_scheduler(const std::string& scheduler_)
    {
      scheduler = scheduler_;
    }

    void
    attributes::set_scheduler(const char* scheduler_)
    {
      scheduler = scheduler_;
    }

    const std::string&
    attributes::get_scheduler()
    {
      return scheduler;
    }

    attributes::sched_attr
    attributes::get_scheduler_attr() const
    {
      return attr;
    }

    void
    attributes::set_scheduler_policy(sched_policy policy_)
    {
      attr = sched_explicit;
      policy = policy_;
    }

    attributes::sched_policy
    attributes::get_scheduler_policy() const
    {
      return policy;
    }

    void
    attributes::commit()
    {
      pthread_t pid = ::pthread_self();

      system_error_check(::pthread_setname_np(pid, name.c_str()),
                         "getting name");

      int                spolicy;
      struct sched_param sched_param;

      system_error_check(::pthread_getschedparam(::pthread_self(),
                                                 &spolicy,
                                                 &sched_param),
                         "getting scheduler parameters");

      switch (policy) {
      case sched_other:
        spolicy = SCHED_OTHER;
        break;
      case sched_fifo:
        spolicy = SCHED_FIFO;
        break;
      case sched_roundrobin:
        spolicy = SCHED_RR;
        break;
      case sched_sporadic:
        spolicy = SCHED_SPORADIC;
        break;
      default:
        system_error_check(EINVAL, "get scheduler policy");
        break;
      }

      sched_param.sched_priority = priority;

      system_error_check(::pthread_setschedparam(::pthread_self(),
                                                 spolicy,
                                                 &sched_param),
                         "getting scheduler parameters");

      if (!scheduler.empty()) {
        char sname[4] = { ' ', ' ', ' ', ' ' };
        for (size_t c = 0; c < sizeof(sname); ++c) {
          if (c >= scheduler.length()) {
            break;
          }
          sname[c] = scheduler[c];
        }
        rtems_name scheduler_name = rtems_build_name(sname[0],
                                                     sname[1],
                                                     sname[2],
                                                     sname[3]);
        rtems_id scheduler_id;
        runtime_error_check(::rtems_scheduler_ident(scheduler_name,
                                                    &scheduler_id),
                            "get scheduler id");
        // runtime_error_check (::rtems_task_set_scheduler (RTEMS_SELF,
        //                                                  scheduler_id,
        //                                                  1),
        //                      "set scheduler id");
      }
    }

    void
    attributes::update()
    {
      char buf[32];
      system_error_check(::pthread_getname_np(::pthread_self(),
                                              buf,
                                              sizeof (buf)),
                         "getting name");
      name = buf;

      int                spolicy;
      struct sched_param sched_param;
      system_error_check(::pthread_getschedparam(::pthread_self(),
                                                 &spolicy,
                                                 &sched_param),
                         "getting scheduler parameters");

      switch (spolicy) {
      case SCHED_OTHER:
        policy = sched_other;
        break;
      case SCHED_FIFO:
        policy = sched_fifo;
        break;
      case SCHED_RR:
        policy = sched_roundrobin;
        break;
      case SCHED_SPORADIC:
        policy = sched_sporadic;
        break;
      default:
        system_error_check(EINVAL, "get scheduler policy");
        break;
      }
      priority = sched_param.sched_priority;

      pthread_attr_t pattr;
      system_error_check(::pthread_getattr_np(::pthread_self(), &pattr),
                         "getting thread attributes");
      system_error_check(::pthread_attr_getstacksize(&pattr, &stack_size),
                         "getting stack size");
      int inheritsched = 0;
      system_error_check(::pthread_attr_getinheritsched(&pattr, &inheritsched),
                         "getting inherited sheduler attribute");
      switch (inheritsched) {
      case PTHREAD_INHERIT_SCHED:
        attr = sched_inherit;
        break;
      case PTHREAD_EXPLICIT_SCHED:
        attr = sched_explicit;
        break;
      default:
        system_error_check(EINVAL, "get scheduler attribute");
        break;
      }

      rtems_id scheduler_id;
      runtime_error_check(::rtems_task_get_scheduler(RTEMS_SELF, &scheduler_id));
#if HAVE_GET_SCHEDULER_NAME
      char name[5];
      if (!get_scheduler_name(scheduler_id, &name[0])) {
        system_error_check(ENOENT, "get scheduler name");
      }
      scheduler = name;
#endif
    }

    attributes&
    attributes::operator=(const attributes& other)
    {
      name = other.name;
      priority = other.priority;
      stack_size = other.stack_size;
      attr = other.attr;
      policy = other.policy;
      return *this;
    }

    bool
    attributes::operator==(const attributes& other) const
    {
      return
        name == other.name &&
        priority == other.priority &&
        stack_size == other.stack_size &&
        attr == other.attr &&
        policy == other.policy;
    }

    void*
    thread_generic_entry(void* arg)
    {
      thread::state_ptr s{ static_cast<thread::state_base*>(arg) };
      try {
        s->run();
      } catch (...) {
        std::terminate();
      }
      return nullptr;
    }

    thread&
    thread::operator=(thread&& thread_)
    {
      if (joinable()) {
        std::terminate();
      }
      swap(thread_);
      return *this;
    }

    void
    thread::swap(thread& thread_) noexcept
    {
      std::swap(id_, thread_.id_);
    }

    bool
    thread::joinable() const noexcept
    {
      return !(id_ == id());
    }

    void
    thread::join()
    {
      if (!joinable()) {
        system_error_check(EINVAL, "join");
      }
      system_error_check(::pthread_join(id_.id_, nullptr), "join");
      id_ = id();
    }

    void
    thread::detach()
    {
      if (!joinable()) {
        system_error_check(EINVAL, "detach");
      }
      system_error_check(::pthread_detach(id_.id_), "detach");
      id_ = id();
    }

    thread::state_base::~state_base() = default;

    void
    thread::start_thread(thread::state_ptr s)
    {
      const attributes attr = s->get_attributes();

      pthread_attr_t pattr;

      system_error_check(::pthread_attr_init(&pattr),
                         "attribute init");

      struct sched_param param;
      param.sched_priority = attr.get_priority();
      system_error_check(::pthread_attr_setschedparam(&pattr, &param),
                         "set sched param");

      int spolicy;
      switch (attr.get_scheduler_policy()) {
      case attributes::sched_other:
        spolicy = SCHED_OTHER;
        break;
      case attributes::sched_roundrobin:
        spolicy = SCHED_RR;
        break;
      case attributes::sched_sporadic:
        spolicy = SCHED_SPORADIC;
        break;
      default:
        spolicy = SCHED_FIFO;
        break;
      }
      system_error_check(::pthread_attr_setschedpolicy(&pattr, spolicy),
                         "set scheduler policy");

      if (attr.get_scheduler_attr() == attributes::sched_inherit) {
        ::pthread_attr_setinheritsched(&pattr, PTHREAD_INHERIT_SCHED);
      }
      else {
        ::pthread_attr_setinheritsched(&pattr, PTHREAD_EXPLICIT_SCHED);
      }

      system_error_check(::pthread_attr_setstacksize(&pattr,
                                                     attr.get_stack_size()),
                         "set stack size");

      /*
       * Hold the new thread in the state's run handler until the rest
       * of the thread is set up after the create call.
       */
      system_error_check(::pthread_create(&id_.id_,
                                          &pattr,
                                          thread_generic_entry,
                                          s.get()),
                         "create thread");

      system_error_check(::pthread_setname_np(id_.id_,
                                              attr.get_name().c_str()),
                         "setting thread name");

      ::pthread_attr_destroy(&pattr);

      s.release();
    };
  };
};