Skip to content

Commit

Permalink
pw_system: Configurable thread stack sizes
Browse files Browse the repository at this point in the history
Change-Id: I684c685526db4d99014e06988dc260f1d8bf4546
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/221394
Pigweed-Auto-Submit: Wyatt Hepler <[email protected]>
Presubmit-Verified: CQ Bot Account <[email protected]>
Reviewed-by: Keir Mierle <[email protected]>
Commit-Queue: Auto-Submit <[email protected]>
Lint: Lint 🤖 <[email protected]>
  • Loading branch information
255 authored and CQ Bot Account committed Jul 10, 2024
1 parent 911590b commit 2058720
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 8 deletions.
39 changes: 39 additions & 0 deletions pw_system/public/pw_system/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// the License.
#pragma once

#include <cstddef>
#include <cstdint>

// PW_SYSTEM_LOG_BUFFER_SIZE is the log buffer size which determines how many
Expand Down Expand Up @@ -119,6 +120,30 @@
#define PW_SYSTEM_SOCKET_IO_PORT 33000
#endif // PW_SYSTEM_SOCKET_IO_PORT

// PW_SYSTEM_ASYNC_LOG_THREAD_STACK_SIZE_BYTES specifies the size of the
// internal pw_log thread stack.
#ifndef PW_SYSTEM_ASYNC_LOG_THREAD_STACK_SIZE_BYTES
#define PW_SYSTEM_ASYNC_LOG_THREAD_STACK_SIZE_BYTES 4096
#endif // PW_SYSTEM_ASYNC_LOG_THREAD_STACK_SIZE_BYTES

// PW_SYSTEM_ASYNC_RPC_THREAD_STACK_SIZE_BYTES specifies the size of the
// internal pw_rpc thread stack.
#ifndef PW_SYSTEM_ASYNC_RPC_THREAD_STACK_SIZE_BYTES
#define PW_SYSTEM_ASYNC_RPC_THREAD_STACK_SIZE_BYTES 2048
#endif // PW_SYSTEM_ASYNC_RPC_THREAD_STACK_SIZE_BYTES

// PW_SYSTEM_ASYNC_TRANSFER_THREAD_STACK_SIZE_BYTES specifies the size of the
// internal pw_transfer thread stack.
#ifndef PW_SYSTEM_ASYNC_TRANSFER_THREAD_STACK_SIZE_BYTES
#define PW_SYSTEM_ASYNC_TRANSFER_THREAD_STACK_SIZE_BYTES 2048
#endif // PW_SYSTEM_ASYNC_TRANSFER_THREAD_STACK_SIZE_BYTES

// PW_SYSTEM_ASYNC_DISPATCHER_THREAD_STACK_SIZE_BYTES specifies the size of the
// internal pw_async2 dispatcher thread stack.
#ifndef PW_SYSTEM_ASYNC_DISPATCHER_THREAD_STACK_SIZE_BYTES
#define PW_SYSTEM_ASYNC_DISPATCHER_THREAD_STACK_SIZE_BYTES 2048
#endif // PW_SYSTEM_ASYNC_DISPATCHER_THREAD_STACK_SIZE_BYTES

namespace pw::system {

// This is the default channel used by the pw_system RPC server. Some other
Expand All @@ -133,4 +158,18 @@ inline constexpr uint32_t kExtraLoggingRpcChannelId =
PW_SYSTEM_EXTRA_LOGGING_CHANNEL_ID;
#endif // PW_SYSTEM_EXTRA_LOGGING_CHANNEL_ID != PW_SYSTEM_LOGGING_CHANNEL_ID

inline constexpr size_t kLogThreadStackSizeBytes =
PW_SYSTEM_ASYNC_LOG_THREAD_STACK_SIZE_BYTES;
inline constexpr size_t kRpcThreadStackSizeBytes =
PW_SYSTEM_ASYNC_RPC_THREAD_STACK_SIZE_BYTES;
inline constexpr size_t kTransferThreadStackSizeBytes =
PW_SYSTEM_ASYNC_TRANSFER_THREAD_STACK_SIZE_BYTES;
inline constexpr size_t kDispatcherThreadStackSizeBytes =
PW_SYSTEM_ASYNC_DISPATCHER_THREAD_STACK_SIZE_BYTES;

#undef PW_SYSTEM_ASYNC_LOG_THREAD_STACK_SIZE_BYTES
#undef PW_SYSTEM_ASYNC_RPC_THREAD_STACK_SIZE_BYTES
#undef PW_SYSTEM_ASYNC_TRANSFER_THREAD_STACK_SIZE_BYTES
#undef PW_SYSTEM_ASYNC_DISPATCHER_THREAD_STACK_SIZE_BYTES

} // namespace pw::system
24 changes: 16 additions & 8 deletions pw_system/threads.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// License for the specific language governing permissions and limitations under
// the License.

#include "pw_system/config.h"
#include "pw_thread/thread.h"

// For now, pw_system:async only supports FreeRTOS or standard library threads.
Expand All @@ -29,6 +30,13 @@
#include "task.h"

namespace pw::system {
namespace {

constexpr size_t ToWords(size_t bytes) {
return (bytes + sizeof(StackType_t) - 1) / sizeof(StackType_t);
}

} // namespace

[[noreturn]] void StartScheduler() {
vTaskStartScheduler();
Expand All @@ -49,8 +57,8 @@ enum class ThreadPriority : UBaseType_t {
static_assert(static_cast<UBaseType_t>(ThreadPriority::kNumPriorities) <=
configMAX_PRIORITIES);

static constexpr size_t kLogThreadStackWords = 1024;
static thread::freertos::StaticContextWithStack<kLogThreadStackWords>
static thread::freertos::StaticContextWithStack<ToWords(
kLogThreadStackSizeBytes)>
log_thread_context;
const thread::Options& LogThreadOptions() {
static constexpr auto options =
Expand All @@ -61,8 +69,8 @@ const thread::Options& LogThreadOptions() {
return options;
}

static constexpr size_t kRpcThreadStackWords = 512;
static thread::freertos::StaticContextWithStack<kRpcThreadStackWords>
static thread::freertos::StaticContextWithStack<ToWords(
kRpcThreadStackSizeBytes)>
rpc_thread_context;
const thread::Options& RpcThreadOptions() {
static constexpr auto options =
Expand All @@ -73,8 +81,8 @@ const thread::Options& RpcThreadOptions() {
return options;
}

static constexpr size_t kTransferThreadStackWords = 512;
static thread::freertos::StaticContextWithStack<kTransferThreadStackWords>
static thread::freertos::StaticContextWithStack<ToWords(
kTransferThreadStackSizeBytes)>
transfer_thread_context;
const thread::Options& TransferThreadOptions() {
static constexpr auto options =
Expand All @@ -85,8 +93,8 @@ const thread::Options& TransferThreadOptions() {
return options;
}

static constexpr size_t kDispatcherThreadStackWords = 512;
static thread::freertos::StaticContextWithStack<kDispatcherThreadStackWords>
static thread::freertos::StaticContextWithStack<ToWords(
kDispatcherThreadStackSizeBytes)>
dispatcher_thread_context;
const thread::Options& DispatcherThreadOptions() {
static constexpr auto options =
Expand Down

0 comments on commit 2058720

Please sign in to comment.