1
0
mirror of https://github.com/p2r3/bareiron.git synced 2025-10-01 23:25:09 +02:00

make esp task yielding more consistent and reduce overhead

This commit is contained in:
p2r3
2025-08-20 15:21:02 +03:00
parent 5f3dcc56f9
commit 8936eb32db
6 changed files with 36 additions and 23 deletions

View File

@@ -4,5 +4,5 @@
FILE(GLOB_RECURSE app_sources ${CMAKE_SOURCE_DIR}/src/*.*)
idf_component_register(SRCS ${app_sources}
PRIV_REQUIRES esp_wifi nvs_flash
PRIV_REQUIRES esp_wifi nvs_flash esp_timer
INCLUDE_DIRS ".")

View File

@@ -5,6 +5,24 @@
#include "globals.h"
#ifdef ESP_PLATFORM
#include "esp_task_wdt.h"
#include "esp_timer.h"
// Time between vTaskDelay calls in microseconds
#define TASK_YIELD_INTERVAL 1000 * 1000
// How many ticks to delay for on each yield
#define TASK_YIELD_TICKS 1
int64_t last_yield = 0;
void task_yield () {
int64_t time_now = esp_timer_get_time();
if (time_now - last_yield < TASK_YIELD_INTERVAL) return;
vTaskDelay(TASK_YIELD_TICKS);
last_yield = time_now;
}
#endif
ssize_t recv_count;
uint8_t recv_buffer[256] = {0};

View File

@@ -16,6 +16,7 @@
#include "esp_wifi.h"
#include "esp_event.h"
#include "esp_task_wdt.h"
#include "esp_timer.h"
#include "lwip/sockets.h"
#include "lwip/netdb.h"
#else
@@ -97,7 +98,7 @@ void handlePacket (int client_fd, int length, int packet_id) {
sc_synchronizePlayerPosition(client_fd, spawn_x, spawn_y, spawn_z, spawn_yaw, spawn_pitch);
wdt_reset();
task_yield();
for (uint8_t i = 0; i < 41; i ++) {
sc_setContainerSlot(client_fd, 0, serverSlotToClientSlot(0, i), player->inventory_count[i], player->inventory_items[i]);
@@ -115,7 +116,7 @@ void handlePacket (int client_fd, int length, int packet_id) {
sc_startWaitingForChunks(client_fd);
sc_setCenterChunk(client_fd, _x, _z);
wdt_reset();
task_yield();
// Send spawn chunk first
sc_chunkDataAndUpdateLight(client_fd, _x, _z);
@@ -128,7 +129,7 @@ void handlePacket (int client_fd, int length, int packet_id) {
// Re-synchronize player position after all chunks have been sent
sc_synchronizePlayerPosition(client_fd, spawn_x, spawn_y, spawn_z, spawn_yaw, spawn_pitch);
wdt_reset();
task_yield();
// Register all existing players and spawn their entities, and broadcast
// information about the joining player to all existing players.
@@ -398,7 +399,8 @@ int main () {
* client connection.
*/
while (true) {
if (client_count == 0) { wdt_reset(); }
// Check if it's time to yield to the idle task
task_yield();
// Attempt to accept a new connection
for (int i = 0; i < MAX_PLAYERS; i ++) {
@@ -473,8 +475,6 @@ int main () {
disconnectClient(&clients[client_index], 4);
continue;
}
// Reset watchdog and yield between packets
wdt_reset();
}
@@ -486,7 +486,6 @@ int main () {
#ifdef ESP_PLATFORM
void bareiron_main (void *pvParameters) {
esp_task_wdt_add(NULL);
main();
vTaskDelete(NULL);
}
@@ -528,6 +527,7 @@ void wifi_init () {
}
void app_main () {
esp_timer_early_init();
wifi_init();
}

View File

@@ -308,8 +308,8 @@ int sc_chunkDataAndUpdateLight (int client_fd, int _x, int _z) {
writeByte(client_fd, 0); // biome bits
writeByte(client_fd, 0); // biome palette
}
// reset watchdog and yield
wdt_reset();
// yield to idle task
task_yield();
// send chunk sections
for (int i = 0; i < 20; i ++) {
@@ -325,8 +325,8 @@ int sc_chunkDataAndUpdateLight (int client_fd, int _x, int _z) {
// biome data
writeByte(client_fd, 0); // bits per entry
writeByte(client_fd, W_plains); // biome palette
// reset watchdog and yield
wdt_reset();
// yield to idle task
task_yield();
}
// send 8 chunk sections (up to Y=192) with no blocks
@@ -337,8 +337,8 @@ int sc_chunkDataAndUpdateLight (int client_fd, int _x, int _z) {
writeByte(client_fd, 0); // biome bits
writeByte(client_fd, 0); // biome palette
}
// reset watchdog and yield
wdt_reset();
// yield to idle task
task_yield();
writeVarInt(client_fd, 0); // omit block entities

View File

@@ -47,8 +47,7 @@ ssize_t recv_all (int client_fd, void *buf, size_t n, uint8_t require_first) {
ssize_t r = recv(client_fd, p + total, n - total, 0);
if (r < 0) {
if (errno == EAGAIN || errno == EWOULDBLOCK) {
// spin until data arrives
wdt_reset();
task_yield();
continue;
} else {
return -1; // real error
@@ -78,7 +77,7 @@ ssize_t send_all (int client_fd, const void *buf, size_t len) {
return -1;
}
if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK) {
wdt_reset();
task_yield();
continue;
}
return -1;