summaryrefslogtreecommitdiff
path: root/src/tasks/tasks.hpp
blob: 6a3a0d0998795abfc674d8fdc37f33f9638419b6 (plain)
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
/*
 * Copyright 2023 jacqueline <me@jacqueline.id.au>
 *
 * SPDX-License-Identifier: GPL-3.0-only
 */

#pragma once

#include <atomic>
#include <functional>
#include <future>
#include <memory>
#include <string>

#include "esp_heap_caps.h"
#include "freertos/FreeRTOS.h"
#include "freertos/portmacro.h"
#include "freertos/projdefs.h"
#include "freertos/queue.h"
#include "freertos/task.h"
#include "span.hpp"

namespace tasks {

/*
 * Enumeration of every task (basically a thread) started within the firmware.
 * These are centralised so that it is easier to reason about the relative
 * priorities of tasks, as well as the amount and location of memory allocated
 * to each one.
 */
enum class Type {
  // The main UI task. This runs the LVGL main loop.
  kUi,
  // The main audio pipeline task. Decodes files into PCM stream.
  kAudioDecoder,
  // Second audio task. Converts the PCM stream into one suitable for the
  // current output (e.g. downsampling for bluetooth).
  kAudioConverter,
  // Task for running database queries.
  kDatabase,
  // Task for async background work
  kBackgroundWorker,
};

template <Type t>
auto Name() -> std::pmr::string;
template <Type t>
auto AllocateStack() -> cpp::span<StackType_t>;
template <Type t>
auto Priority() -> UBaseType_t;
template <Type t>
auto WorkerQueueSize() -> std::size_t;

auto PersistentMain(void* fn) -> void;

template <Type t>
auto StartPersistent(const std::function<void(void)>& fn) -> void {
  StaticTask_t* task_buffer = static_cast<StaticTask_t*>(heap_caps_malloc(
      sizeof(StaticTask_t), MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT));
  cpp::span<StackType_t> stack = AllocateStack<t>();
  xTaskCreateStatic(&PersistentMain, Name<t>().c_str(), stack.size(),
                    new std::function<void(void)>(fn), Priority<t>(),
                    stack.data(), task_buffer);
}

template <Type t>
auto StartPersistent(BaseType_t core, const std::function<void(void)>& fn)
    -> void {
  StaticTask_t* task_buffer = static_cast<StaticTask_t*>(heap_caps_malloc(
      sizeof(StaticTask_t), MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT));
  cpp::span<StackType_t> stack = AllocateStack<t>();
  xTaskCreateStaticPinnedToCore(&PersistentMain, Name<t>().c_str(),
                                stack.size(), new std::function<void(void)>(fn),
                                Priority<t>(), stack.data(), task_buffer, core);
}

class Worker {
 private:
  Worker(const std::pmr::string& name,
         cpp::span<StackType_t> stack,
         std::size_t queue_size,
         UBaseType_t priority);

  StackType_t* stack_;
  QueueHandle_t queue_;
  std::atomic<bool> is_task_running_;
  StaticTask_t *task_buffer_;
  TaskHandle_t task_;

  struct WorkItem {
    std::function<void(void)>* fn;
    bool quit;
  };

 public:
  template <Type t>
  static auto Start() -> Worker* {
    return new Worker(Name<t>(), AllocateStack<t>(), WorkerQueueSize<t>(),
                      Priority<t>());
  }

  static auto Main(void* instance);

  /*
   * Schedules the given function to be executed on the worker task, and
   * asynchronously returns the result as a future.
   */
  template <typename T>
  auto Dispatch(const std::function<T(void)> fn) -> std::future<T> {
    std::shared_ptr<std::promise<T>> promise =
        std::make_shared<std::promise<T>>();
    WorkItem item{
        .fn = new std::function([=]() { promise->set_value(std::invoke(fn)); }),
        .quit = false,
    };
    xQueueSend(queue_, &item, portMAX_DELAY);
    return promise->get_future();
  }

  ~Worker();

  Worker(const Worker&) = delete;
  Worker& operator=(const Worker&) = delete;
};

/* Specialisation of Evaluate for functions that return nothing. */
template <>
auto Worker::Dispatch(const std::function<void(void)> fn) -> std::future<void>;

}  // namespace tasks