-
Notifications
You must be signed in to change notification settings - Fork 7.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'bugfix/atomic_gptimer_fsm' into 'master'
gptimer: fix race condition between start and stop See merge request espressif/esp-idf!22620
- Loading branch information
Showing
11 changed files
with
153 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# GPTimer Driver Design | ||
|
||
## State Transition | ||
|
||
> State transition is achieved by using the primitives provided by `<stdatomic.h>`. | ||
```mermaid | ||
stateDiagram-v2 | ||
[*] --> init: gptimer_new_timer | ||
init --> enable: gptimer_enable | ||
enable --> init: gptimer_disable | ||
enable --> run: gptimer_start* | ||
run --> enable: gptimer_stop* | ||
init --> [*]: gptimer_del_timer | ||
``` | ||
|
||
Other functions won't change the driver state. The functions above labeled with `*` are allowed to be used in the interrupt context. | ||
|
||
## Concurrency | ||
|
||
There might be race conditions when the user calls the APIs from a thread and interrupt at the same time. e.g. a Task is just running the `gptimer_start`, and suddenly an interrupt occurs, where the user calls `gptimer_stop` for the same timer handle. Which is possible to make a "stopped" timer continue to run if the interrupt is returned before the Task. | ||
|
||
```mermaid | ||
stateDiagram-v2 | ||
state Race-Condition { | ||
Thread --> gptimer_start | ||
state gptimer_start { | ||
state is_enabled <<choice>> | ||
[*] --> is_enabled: Enabled? | ||
is_enabled --> run_wait: yes | ||
is_enabled --> [*] : no | ||
run_wait --> run: call HAL/LL functions to start timer | ||
} | ||
-- | ||
Interrupt --> gptimer_stop | ||
state gptimer_stop { | ||
state is_running <<choice>> | ||
[*] --> is_running: Running? | ||
is_running --> enable_wait: yes | ||
is_running --> [*] : no | ||
enable_wait --> enable: call HAL/LL functions to stop timer | ||
} | ||
} | ||
``` | ||
|
||
By introducing a "middle" state like `run_wait` and `enable_wait`, we make sure that the timer is in a safe state before we start/stop it. And if the state is invalid, it can return an error code to the user. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* | ||
* SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#include "esp_check.h" | ||
#include "esp_private/gptimer.h" | ||
#include "gptimer_priv.h" | ||
|
||
static const char *TAG = "gptimer"; | ||
|
||
esp_err_t gptimer_get_intr_handle(gptimer_handle_t timer, intr_handle_t *ret_intr_handle) | ||
{ | ||
ESP_RETURN_ON_FALSE(timer && ret_intr_handle, ESP_ERR_INVALID_ARG, TAG, "invalid argument"); | ||
*ret_intr_handle = timer->intr; | ||
return ESP_OK; | ||
} | ||
|
||
esp_err_t gptimer_get_pm_lock(gptimer_handle_t timer, esp_pm_lock_handle_t *ret_pm_lock) | ||
{ | ||
ESP_RETURN_ON_FALSE(timer && ret_pm_lock, ESP_ERR_INVALID_ARG, TAG, "invalid argument"); | ||
*ret_pm_lock = timer->pm_lock; | ||
return ESP_OK; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.