diff --git a/docs/api.rst b/docs/api.rst index 2bd8af88d..5c224cf99 100644 --- a/docs/api.rst +++ b/docs/api.rst @@ -762,14 +762,8 @@ Functions ^^^^^^^^^ .. autocfunction:: platform_common.h::z_task_init .. autocfunction:: platform_common.h::z_task_join -.. TODO: implement .. autocfunction:: platform_common.h::z_task_detach - -Ownership Functions -^^^^^^^^^^^^^^^^^^^ - -See details at :ref:`owned_types_concept` - -.. c:function:: void z_task_drop(z_moved_task_t * task) +.. autocfunction:: platform_common.h::z_task_detach +.. autocfunction:: platform_common.h::z_task_drop Session ======= diff --git a/include/zenoh-pico/system/platform_common.h b/include/zenoh-pico/system/platform_common.h index ae467dc6b..d6956aba9 100644 --- a/include/zenoh-pico/system/platform_common.h +++ b/include/zenoh-pico/system/platform_common.h @@ -113,12 +113,48 @@ _Z_OWNED_FUNCTIONS_SYSTEM_DEF(task) z_result_t _z_task_init(_z_task_t *task, z_task_attr_t *attr, void *(*fun)(void *), void *arg); z_result_t _z_task_join(_z_task_t *task); +z_result_t _z_task_detach(_z_task_t *task); z_result_t _z_task_cancel(_z_task_t *task); void _z_task_free(_z_task_t **task); +/** + * Constructs a new task. + * + * Parameters: + * task: An uninitialized memory location where task will be constructed. + * attr: Attributes of the task. + * fun: Function to be executed by the task. + * arg: Argument that will be passed to the function `fun`. + * + * Returns: + * ``0`` in case of success, negative error code otherwise. + */ z_result_t z_task_init(z_owned_task_t *task, z_task_attr_t *attr, void *(*fun)(void *), void *arg); + +/** + * Joins the task and releases all allocated resources + * + * Returns: + * ``0`` in case of success, negative error code otherwise. + */ z_result_t z_task_join(z_moved_task_t *task); +/** + * Detaches the task and releases all allocated resources. + * + * Returns: + * ``0`` in case of success, negative error code otherwise. + */ +z_result_t z_task_detach(z_moved_task_t *task); + +/** + * Drop the task. Same as :c:func:`z_task_detach`. Use :c:func:`z_task_join` to wait for the task completion. + * + * Returns: + * ``0`` in case of success, negative error code otherwise. + */ +z_result_t z_task_drop(z_moved_task_t *task); + /*------------------ Mutex ------------------*/ _Z_OWNED_TYPE_VALUE(_z_mutex_t, mutex) _Z_OWNED_FUNCTIONS_SYSTEM_DEF(mutex) diff --git a/src/system/arduino/esp32/system.c b/src/system/arduino/esp32/system.c index b7c5135d8..3bd178ee7 100644 --- a/src/system/arduino/esp32/system.c +++ b/src/system/arduino/esp32/system.c @@ -87,6 +87,11 @@ z_result_t _z_task_join(_z_task_t *task) { return _z_task_cancel(task); } +z_result_t _z_task_detach(_z_task_t *task) { + // Note: task/thread detach not supported on FreeRTOS API, so we force its deletion instead. + return _z_task_cancel(task); +} + z_result_t _z_task_cancel(_z_task_t *task) { vTaskDelete(*task); return 0; diff --git a/src/system/arduino/opencr/system.c b/src/system/arduino/opencr/system.c index 2a3521e35..1acd5cef9 100644 --- a/src/system/arduino/opencr/system.c +++ b/src/system/arduino/opencr/system.c @@ -67,6 +67,8 @@ z_result_t _z_task_init(_z_task_t *task, z_task_attr_t *attr, void *(*fun)(void z_result_t _z_task_join(_z_task_t *task) { return -1; } +z_result_t _z_task_detach(_z_task_t *task) { return -1; } + z_result_t _z_task_cancel(_z_task_t *task) { return -1; } void _z_task_free(_z_task_t **task) { diff --git a/src/system/emscripten/system.c b/src/system/emscripten/system.c index 1f1cfafef..6e184939f 100644 --- a/src/system/emscripten/system.c +++ b/src/system/emscripten/system.c @@ -13,6 +13,7 @@ // #include +#include #include #include #include @@ -50,6 +51,8 @@ z_result_t _z_task_init(_z_task_t *task, pthread_attr_t *attr, void *(*fun)(void z_result_t _z_task_join(_z_task_t *task) { _Z_CHECK_SYS_ERR(pthread_join(*task, NULL)); } +z_result_t _z_task_detach(_z_task_t *task) { _Z_CHECK_SYS_ERR(pthread_detach(*task)); } + z_result_t _z_task_cancel(_z_task_t *task) { _Z_CHECK_SYS_ERR(pthread_cancel(*task)); } void _z_task_free(_z_task_t **task) { diff --git a/src/system/espidf/system.c b/src/system/espidf/system.c index 6010c42a0..7c03bcb7a 100644 --- a/src/system/espidf/system.c +++ b/src/system/espidf/system.c @@ -115,6 +115,11 @@ z_result_t _z_task_join(_z_task_t *task) { return 0; } +z_result_t _z_task_detach(_z_task_t *task) { + // Not implemented + return _Z_ERR_GENERIC; +} + z_result_t z_task_cancel(_z_task_t *task) { vTaskDelete(task->handle); return 0; diff --git a/src/system/flipper/system.c b/src/system/flipper/system.c index 8fe2a7921..0c481db07 100644 --- a/src/system/flipper/system.c +++ b/src/system/flipper/system.c @@ -87,6 +87,8 @@ z_result_t _z_task_join(_z_task_t* task) { return furi_thread_join(*task); } +z_result_t _z_task_detach(_z_task_t* task) { return -1; } + z_result_t _z_task_cancel(_z_task_t* task) { return -1; } void _z_task_free(_z_task_t** task) { diff --git a/src/system/freertos_plus_tcp/system.c b/src/system/freertos_plus_tcp/system.c index 0a7dc9115..8d31e5d55 100644 --- a/src/system/freertos_plus_tcp/system.c +++ b/src/system/freertos_plus_tcp/system.c @@ -124,6 +124,11 @@ z_result_t _z_task_join(_z_task_t *task) { return 0; } +z_result_t _z_task_detach(_z_task_t *task) { + // Not implemented + return _Z_ERR_GENERIC; +} + z_result_t _z_task_cancel(_z_task_t *task) { vTaskDelete(task->handle); return 0; diff --git a/src/system/mbed/system.cpp b/src/system/mbed/system.cpp index 211062d72..30c63b98d 100644 --- a/src/system/mbed/system.cpp +++ b/src/system/mbed/system.cpp @@ -53,6 +53,11 @@ z_result_t _z_task_join(_z_task_t *task) { return res; } +z_result_t _z_task_detach(_z_task_t *task) { + // Not implemented + return _Z_ERR_GENERIC; +} + z_result_t _z_task_cancel(_z_task_t *task) { int res = ((Thread *)*task)->terminate(); delete ((Thread *)*task); diff --git a/src/system/platform_common.c b/src/system/platform_common.c index 01362903c..2317d0cc6 100644 --- a/src/system/platform_common.c +++ b/src/system/platform_common.c @@ -34,6 +34,15 @@ z_result_t z_task_join(z_moved_task_t *task) { return ret; } +z_result_t z_task_detach(z_moved_task_t *task) { + _z_task_t *ptr = &task->_this._val; + z_result_t ret = _z_task_detach(ptr); + _z_task_free(&ptr); + return ret; +} + +z_result_t z_task_drop(z_moved_task_t *task) { return z_task_detach(task); } + /*------------------ Mutex ------------------*/ _Z_OWNED_FUNCTIONS_SYSTEM_IMPL(_z_mutex_t, mutex) diff --git a/src/system/unix/system.c b/src/system/unix/system.c index c197fed78..6399b9f31 100644 --- a/src/system/unix/system.c +++ b/src/system/unix/system.c @@ -110,6 +110,8 @@ z_result_t _z_task_init(_z_task_t *task, z_task_attr_t *attr, void *(*fun)(void z_result_t _z_task_join(_z_task_t *task) { _Z_CHECK_SYS_ERR(pthread_join(*task, NULL)); } +z_result_t _z_task_detach(_z_task_t *task) { _Z_CHECK_SYS_ERR(pthread_detach(*task)); } + z_result_t _z_task_cancel(_z_task_t *task) { _Z_CHECK_SYS_ERR(pthread_cancel(*task)); } void _z_task_free(_z_task_t **task) { diff --git a/src/system/windows/system.c b/src/system/windows/system.c index 09c5bdff6..df2100c01 100644 --- a/src/system/windows/system.c +++ b/src/system/windows/system.c @@ -76,6 +76,12 @@ z_result_t _z_task_join(_z_task_t *task) { return ret; } +z_result_t _z_task_detach(_z_task_t *task) { + z_result_t ret = _Z_RES_OK; + CloseHandle(*task); + return ret; +} + z_result_t _z_task_cancel(_z_task_t *task) { z_result_t ret = _Z_RES_OK; TerminateThread(*task, 0); diff --git a/src/system/zephyr/system.c b/src/system/zephyr/system.c index 8ee6bc522..0a4d5db11 100644 --- a/src/system/zephyr/system.c +++ b/src/system/zephyr/system.c @@ -85,6 +85,8 @@ z_result_t _z_task_init(_z_task_t *task, z_task_attr_t *attr, void *(*fun)(void z_result_t _z_task_join(_z_task_t *task) { _Z_CHECK_SYS_ERR(pthread_join(*task, NULL)); } +z_result_t _z_task_detach(_z_task_t *task) { _Z_CHECK_SYS_ERR(pthread_detach(*task)); } + z_result_t _z_task_cancel(_z_task_t *task) { _Z_CHECK_SYS_ERR(pthread_cancel(*task)); } void _z_task_free(_z_task_t **task) {