Skip to content

Commit 7e44469

Browse files
rodrigopexcarlescufi
authored andcommitted
zbus: improve the way of storing observers
ZBus stores observers in two ways: statically using a list and dynamically using a memory slab. Both present limitations. Static observers work only for channel definition. The dynamic observers rely on a memory slab that forces the user to manage its size to avoid issues with adding observers. This commit fixes the static allocation problem by using the iterable sections for allocating observation data and replacing the VDED execution sequence since now it is possible to prioritize static observer execution. All the runtime observers are dynamically allocated on the heap instead of a specific memory pool. BREAK changes (only internal, not APIs): * ZBus channel metadata changed. Remove the observers' static array pointer. Rename the `runtime_observers` pointer to `observers`. Add `observer_start_idx` and `observer_end_idx`; * Change the VDED execution sequence. The position (on definition time), the priority in conjunction with the lexical order, is considered for static post-definition time observers. At last, the runtime observer follows the adding sequence; * Replace the `CONFIG_ZBUS_RUNTIME_OBSERVERS_POOL_SIZE` with `CONFIG_ZBUS_RUNTIME_OBSERVERS`. New APIs: * New iterable section iterators (for channels and observers) can now receive a user_data pointer to keep context between the function calls; * New `ZBUS_LISTENER_DEFINE_WITH_ENABLE(_name, _cb, _enable)` and `ZBUS_SUBSCRIBER_DEFINE_WITH_ENABLE(_name, _queue_size, enable)` that enable developers define disabled observers. They need to be enabled during runtime to receive notifications from the bus; * `ZBUS_CHAN_ADD_OBS` macro for adding post-definition static observers of a channel. Important changes: * Move the ZBus LD file content to the `common-ram.ld` LD file. That was necessary to make ZBus compatible with some Xtensa and RISCV boards. Signed-off-by: Rodrigo Peixoto <[email protected]>
1 parent 6a0de9b commit 7e44469

File tree

11 files changed

+492
-200
lines changed

11 files changed

+492
-200
lines changed

cmake/linker_script/common/common-ram.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,8 @@ if(CONFIG_ZTEST_NEW_API)
103103
endif()
104104

105105
if(CONFIG_ZBUS)
106-
zephyr_iterable_section(NAME zbus_channel GROUP DATA_REGION ${XIP_ALIGN_WITH_INPUT} SUBALIGN 4)
107106
zephyr_iterable_section(NAME zbus_observer GROUP DATA_REGION ${XIP_ALIGN_WITH_INPUT} SUBALIGN 4)
107+
zephyr_iterable_section(NAME zbus_channel_observation_mask GROUP DATA_REGION ${XIP_ALIGN_WITH_INPUT} SUBALIGN 4)
108108
endif()
109109

110110
if(CONFIG_UVB)

cmake/linker_script/common/common-rom.cmake

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,3 +213,8 @@ endif()
213213
if(CONFIG_USBD_MSC_CLASS)
214214
zephyr_iterable_section(NAME usbd_msc_lun KVMA RAM_REGION GROUP RODATA_REGION SUBALIGN 4)
215215
endif()
216+
217+
if(CONFIG_ZBUS)
218+
zephyr_iterable_section(NAME zbus_channel KVMA RAM_REGION GROUP RODATA_REGION SUBALIGN 4)
219+
zephyr_iterable_section(NAME zbus_channel_observation KVMA RAM_REGION GROUP RODATA_REGION SUBALIGN 4)
220+
endif()

include/zephyr/linker/common-ram.ld

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,11 @@
124124
ITERABLE_SECTION_RAM(sensing_sensor, 4)
125125
#endif /* CONFIG_SENSING */
126126

127+
#if defined(CONFIG_ZBUS)
128+
ITERABLE_SECTION_RAM(zbus_observer, 4)
129+
ITERABLE_SECTION_RAM(zbus_channel_observation_mask, 1)
130+
#endif /* CONFIG_ZBUS */
131+
127132
#ifdef CONFIG_USERSPACE
128133
_static_kernel_objects_end = .;
129134
#endif

include/zephyr/linker/common-rom/common-rom-misc.ld

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@
3434
ITERABLE_SECTION_ROM(emul, 4)
3535
#endif /* CONFIG_EMUL */
3636

37+
#if defined(CONFIG_ZBUS)
38+
ITERABLE_SECTION_ROM(zbus_channel, 4)
39+
ITERABLE_SECTION_ROM(zbus_channel_observation, 4)
40+
#endif /* CONFIG_ZBUS */
41+
3742
SECTION_DATA_PROLOGUE(symbol_to_keep,,)
3843
{
3944
__symbol_to_keep_start = .;

include/zephyr/zbus/zbus.h

Lines changed: 270 additions & 73 deletions
Large diffs are not rendered by default.

subsys/zbus/CMakeLists.txt

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,8 @@ zephyr_library()
44

55
zephyr_library_sources(zbus.c)
66

7-
if(CONFIG_ZBUS_RUNTIME_OBSERVERS_POOL_SIZE GREATER 0)
7+
if(CONFIG_ZBUS_RUNTIME_OBSERVERS)
88
zephyr_library_sources(zbus_runtime_observers.c)
99
endif()
1010

11-
if(CONFIG_ZBUS_STRUCTS_ITERABLE_ACCESS)
12-
zephyr_library_sources(zbus_iterable_sections.c)
13-
zephyr_linker_sources(DATA_SECTIONS zbus.ld)
14-
endif()
11+
zephyr_library_sources(zbus_iterable_sections.c)

subsys/zbus/Kconfig

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,19 @@ menuconfig ZBUS
88

99
if ZBUS
1010

11-
config ZBUS_STRUCTS_ITERABLE_ACCESS
12-
bool "Zbus iterable sections support."
13-
depends on !XTENSA
14-
default y
11+
config ZBUS_CHANNELS_SYS_INIT_PRIORITY
12+
default 5
13+
int "The priority used during the SYS_INIT procedure."
1514

1615
config ZBUS_CHANNEL_NAME
1716
bool "Channel name field"
1817

1918
config ZBUS_OBSERVER_NAME
2019
bool "Observer name field"
2120

22-
config ZBUS_RUNTIME_OBSERVERS_POOL_SIZE
23-
int "The size of the runtime observers pool."
24-
default 0
25-
help
26-
When the size is bigger than zero this feature will be enabled. It applies the Object Pool Pattern,
27-
where the objects in the pool are pre-allocated and can be used and recycled after use. The
28-
technique avoids dynamic allocation and allows the code to increase the number of observers by
29-
only changing a configuration.
21+
config ZBUS_RUNTIME_OBSERVERS
22+
bool "Runtime observers support."
23+
default n
3024

3125
config ZBUS_ASSERT_MOCK
3226
bool "Zbus assert mock for test purposes."

0 commit comments

Comments
 (0)