Skip to content

Commit

Permalink
settings: introduce priority for commit
Browse files Browse the repository at this point in the history
A priority is introduced to allow scheduling of commit routines.

Signed-off-by: Laczen JMS <[email protected]>
  • Loading branch information
Laczen committed Oct 15, 2024
1 parent 66ff30e commit 60c7cb1
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 24 deletions.
42 changes: 39 additions & 3 deletions include/zephyr/settings/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,17 @@ extern "C" {
*/
#define SETTINGS_EXTRA_LEN ((SETTINGS_MAX_DIR_DEPTH - 1) + 2)

enum settings_commit_prio_level {
SETTINGS_COMMIT_PRIO_DEFAULT,
SETTINGS_COMMIT_PRIO_LEVEL1,
SETTINGS_COMMIT_PRIO_LEVEL2,
SETTINGS_COMMIT_PRIO_LEVEL3,
SETTINGS_COMMIT_PRIO_LEVEL4,
SETTINGS_COMMIT_PRIO_LEVEL5,
SETTINGS_COMMIT_PRIO_LEVEL6,
SETTINGS_COMMIT_PRIO_LEVEL7,
};

/**
* Function used to read the data from the settings storage in
* h_set handler implementations.
Expand All @@ -70,6 +81,9 @@ struct settings_handler {
const char *name;
/**< Name of subtree. */

int cprio;
/**< Priority of commit */

int (*h_get)(const char *key, char *val, int val_len_max);
/**< Get values handler of settings items identified by keyword names.
*
Expand Down Expand Up @@ -136,6 +150,9 @@ struct settings_handler_static {
const char *name;
/**< Name of subtree. */

int cprio;
/**< Priority of commit */

int (*h_get)(const char *key, char *val, int val_len_max);
/**< Get values handler of settings items identified by keyword names.
*
Expand Down Expand Up @@ -201,17 +218,23 @@ struct settings_handler_static {
*
*/

#define SETTINGS_STATIC_HANDLER_DEFINE(_hname, _tree, _get, _set, _commit, \
_export) \
#define SETTINGS_STATIC_HANDLER_DEFINE_WITH_CPRIO(_hname, _tree, _get, _set, \
_commit, _export, _cprio) \
const STRUCT_SECTION_ITERABLE(settings_handler_static, \
settings_handler_ ## _hname) = { \
.name = _tree, \
.cprio = _cprio, \
.h_get = _get, \
.h_set = _set, \
.h_commit = _commit, \
.h_export = _export, \
}

#define SETTINGS_STATIC_HANDLER_DEFINE(_hname, _tree, _get, _set, _commit, \
_export) \
SETTINGS_STATIC_HANDLER_DEFINE_WITH_CPRIO(_hname, _tree, _get, _set, \
_commit, _export, CONFIG_SETTINGS_COMMIT_PRIO_MINIMUM)

/**
* Initialization of settings and backend
*
Expand All @@ -224,7 +247,20 @@ struct settings_handler_static {
int settings_subsys_init(void);

/**
* Register a handler for settings items stored in RAM.
* Register a handler for settings items stored in RAM with
* commit priority.
*
* @param cf Structure containing registration info.
* @param cprio Commit priority level
*
* @return 0 on success, non-zero on failure.
*/
int settings_register_with_cprio(struct settings_handler *cf,
int cprio);

/**
* Register a handler for settings items stored in RAM with
* commit priority set to default.
*
* @param cf Structure containing registration info.
*
Expand Down
8 changes: 8 additions & 0 deletions subsys/settings/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ config SETTINGS_DYNAMIC_HANDLERS
help
Enables the use of dynamic settings handlers

config SETTINGS_COMMIT_PRIO_MINIMUM
int "minimum commit priority level"
default 0
help
Minimum commit priority level. All h_commit from
handlers with commit prio below this level are
executed at the this minimum priority level.

# Hidden option to enable encoding length into settings entry
config SETTINGS_ENCODE_LEN
bool
Expand Down
85 changes: 64 additions & 21 deletions subsys/settings/src/settings.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ void settings_init(void)
}

#if defined(CONFIG_SETTINGS_DYNAMIC_HANDLERS)
int settings_register(struct settings_handler *handler)
int settings_register_with_cprio(struct settings_handler *handler, int cprio)
{
int rc = 0;

Expand All @@ -55,12 +55,19 @@ int settings_register(struct settings_handler *handler)
goto end;
}
}

handler->cprio = cprio;
sys_slist_append(&settings_handlers, &handler->node);

end:
k_mutex_unlock(&settings_lock);
return rc;
}

int settings_register(struct settings_handler *handler)
{
return settings_register_with_cprio(handler, CONFIG_SETTINGS_COMMIT_PRIO_MINIMUM);
}
#endif /* CONFIG_SETTINGS_DYNAMIC_HANDLERS */

int settings_name_steq(const char *name, const char *key, const char **next)
Expand Down Expand Up @@ -234,39 +241,75 @@ int settings_commit(void)
return settings_commit_subtree(NULL);
}

static bool skip_commit(int ch_cprio, int cprio, int *nprio)
{
if (ch_cprio > cprio) {
*nprio = (*nprio == cprio) ? ch_cprio : MIN(*nprio, ch_cprio);
return true;
}

if ((ch_cprio < cprio) && (cprio != CONFIG_SETTINGS_COMMIT_PRIO_MINIMUM)) {
return true;
}

return false;
}

int settings_commit_subtree(const char *subtree)
{
int rc;
int rc2;
int cprio = CONFIG_SETTINGS_COMMIT_PRIO_MINIMUM;

rc = 0;

STRUCT_SECTION_FOREACH(settings_handler_static, ch) {
if (subtree && !settings_name_steq(ch->name, subtree, NULL)) {
continue;
}
if (ch->h_commit) {
rc2 = ch->h_commit();
if (!rc) {
rc = rc2;
while (true) {
int nprio = cprio;

STRUCT_SECTION_FOREACH(settings_handler_static, ch) {
if (subtree && !settings_name_steq(ch->name, subtree, NULL)) {
continue;
}
}
}

#if defined(CONFIG_SETTINGS_DYNAMIC_HANDLERS)
struct settings_handler *ch;
SYS_SLIST_FOR_EACH_CONTAINER(&settings_handlers, ch, node) {
if (subtree && !settings_name_steq(ch->name, subtree, NULL)) {
continue;
if (ch->h_commit) {
if (skip_commit(ch->cprio, cprio, &nprio)) {
continue;
}

rc2 = ch->h_commit();
if (!rc) {
rc = rc2;
}
}
}
if (ch->h_commit) {
rc2 = ch->h_commit();
if (!rc) {
rc = rc2;

if (IS_ENABLED(CONFIG_SETTINGS_DYNAMIC_HANDLERS)) {
struct settings_handler *ch;

SYS_SLIST_FOR_EACH_CONTAINER(&settings_handlers, ch, node) {
if (subtree && !settings_name_steq(ch->name, subtree, NULL)) {
continue;
}

if (ch->h_commit) {
if (skip_commit(ch->cprio, cprio, &nprio)) {
continue;
}

rc2 = ch->h_commit();
if (!rc) {
rc = rc2;
}
}
}
}

if (nprio == cprio) {
break;
}

cprio = nprio;
}
#endif /* CONFIG_SETTINGS_DYNAMIC_HANDLERS */

return rc;
}

0 comments on commit 60c7cb1

Please sign in to comment.