Skip to content

Commit

Permalink
Implement z_task_detach and z_task_drop (#705)
Browse files Browse the repository at this point in the history
  • Loading branch information
sashacmc authored Oct 3, 2024
1 parent 2b48da3 commit 548b58f
Show file tree
Hide file tree
Showing 13 changed files with 84 additions and 8 deletions.
10 changes: 2 additions & 8 deletions docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
=======
Expand Down
36 changes: 36 additions & 0 deletions include/zenoh-pico/system/platform_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions src/system/arduino/esp32/system.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions src/system/arduino/opencr/system.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
3 changes: 3 additions & 0 deletions src/system/emscripten/system.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
//

#include <emscripten/html5.h>
#include <pthread.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
Expand Down Expand Up @@ -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) {
Expand Down
5 changes: 5 additions & 0 deletions src/system/espidf/system.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions src/system/flipper/system.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
5 changes: 5 additions & 0 deletions src/system/freertos_plus_tcp/system.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 5 additions & 0 deletions src/system/mbed/system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
9 changes: 9 additions & 0 deletions src/system/platform_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
2 changes: 2 additions & 0 deletions src/system/unix/system.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
6 changes: 6 additions & 0 deletions src/system/windows/system.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions src/system/zephyr/system.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 548b58f

Please sign in to comment.