Skip to content

Commit 573c457

Browse files
committed
service: Introduce software service instanciation macros
SERVICE_INSTANCE_INIT_ONCE is a direct replacement for SYS_INIT_NAMED, removing the priority parameter as it is now provided by gen_init_priorities.py script. No alias will be created for SYS_INIT as we seek to limit the usage of SERVICE_INSTANCE_INIT_ONCE, on the contrary to SYS_INIT which got widely abused in the past. Signed-off-by: Tomasz Bursztyka <[email protected]>
1 parent 2129def commit 573c457

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

include/zephyr/service.h

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Copyright (c) 2024 Tomasz Bursztyka
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#ifndef ZEPHYR_INCLUDE_SERVICE_H_
8+
#define ZEPHYR_INCLUDE_SERVICE_H_
9+
10+
#include <zephyr/init.h>
11+
12+
#include <zephyr/sys/util_init.h>
13+
14+
#ifdef __cplusplus
15+
extern "C" {
16+
#endif
17+
18+
/**
19+
* @defgroup service Service instanciation
20+
* @ingroup os_services
21+
*
22+
* Zephyr offers an infrastructure to instanciate software services, that
23+
* can be initialized at boot time, following the levels described in init.h
24+
*
25+
* @{
26+
*/
27+
28+
/**
29+
* @brief Register a software service to be initialized only once
30+
*
31+
* @param name A unique name identifying the service (can reuse init_fn)
32+
* @param init_func Service initialization function.
33+
* @param level Default service initialization level. Allowed tokens: `EARLY`,
34+
*`PRE_KERNEL_1`, `PRE_KERNEL_2`, `POST_KERNEL`, `APPLICATION` and `SMP` if
35+
* @kconfig{CONFIG_SMP} is enabled. Which default level can be superceded by a computed
36+
* one (see scripts/gen_init_priorities.py)
37+
*/
38+
#define SERVICE_INSTANCE_INIT_ONCE(name, init_func, level) \
39+
Z_SERVICE_INSTANCE_INIT_ONCE(name, init_func, \
40+
ZINIT_GET_LEVEL(name, level), \
41+
ZINIT_GET_PRIORITY(name, name))
42+
43+
/** @cond INTERNAL_HIDDEN */
44+
45+
#define Z_SERVICE_INSTANCE_INIT_ONCE(name, init_func, level, prio) \
46+
static const Z_DECL_ALIGN(struct init_entry) __used __noasan \
47+
Z_INIT_ENTRY_SECTION(level, prio) \
48+
Z_INIT_ENTRY_NAME(name) = {.init_fn = {.sys = (init_func)}, \
49+
Z_INIT_SYS_INIT_DEV_NULL}
50+
51+
/** @endcond */
52+
53+
/** @} */
54+
55+
#ifdef __cplusplus
56+
}
57+
#endif
58+
59+
#endif /* ZEPHYR_INCLUDE_SERVICE_H_ */

0 commit comments

Comments
 (0)