gmio_core: limit task_control to interruption request (no progress report feature)
This commit is contained in:
parent
60db7e397a
commit
666f90857a
@ -1,39 +1,10 @@
|
||||
#include "task_control.h"
|
||||
|
||||
#include <math.h>
|
||||
#include <string.h>
|
||||
#include <stddef.h>
|
||||
|
||||
/*! \var gmio_bool_t (*gmio_task_control::handle_progress_func)(void*, uint8_t)
|
||||
*
|
||||
* Frequent example of hook functions is update of a UI progress bar
|
||||
*
|
||||
* The return value is important : it is used as an interruption request status. If GMIO_TRUE then
|
||||
* the current task can continue, otherwise it should abort as soon as possible.
|
||||
*/
|
||||
|
||||
|
||||
/*! \details Basically the same as :
|
||||
* \code
|
||||
* ctrl->handle_progress_func(ctrl->cookie, progress_pc)
|
||||
* \endcode
|
||||
*/
|
||||
gmio_bool_t gmio_task_control_handle_progress(gmio_task_control_t* ctrl, uint8_t progress_pc)
|
||||
gmio_bool_t gmio_task_control_is_stop_requested(const gmio_task_control_t* task_ctrl)
|
||||
{
|
||||
if (ctrl != NULL && ctrl->handle_progress_func != NULL)
|
||||
return ctrl->handle_progress_func(ctrl->cookie, progress_pc);
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*! \details \p value is assumed to be relative to the interval (range) defined by
|
||||
* [ \p range_min , \p range_max ]
|
||||
*/
|
||||
uint8_t gmio_percentage(size_t range_min, size_t range_max, size_t value)
|
||||
{
|
||||
if (value >= range_max)
|
||||
return 100;
|
||||
else if (value <= range_min)
|
||||
return 0;
|
||||
else if (range_min < range_max)
|
||||
return (value * 100) / (range_max - range_min);
|
||||
return 0;
|
||||
if (task_ctrl != NULL && task_ctrl->is_stop_requested_func != NULL)
|
||||
return task_ctrl->is_stop_requested_func(task_ctrl->cookie);
|
||||
return GMIO_FALSE;
|
||||
}
|
||||
|
@ -2,31 +2,30 @@
|
||||
#define GMIO_TASK_CONTROL_H
|
||||
|
||||
#include "global.h"
|
||||
#include "memory.h"
|
||||
|
||||
GMIO_C_LINKAGE_BEGIN
|
||||
|
||||
/*! Provides control over a general task.
|
||||
*
|
||||
* "Control" here means task progress handling and interruption request (abort).
|
||||
* "Control" here means task interruption request (abort).
|
||||
*/
|
||||
struct gmio_task_control
|
||||
{
|
||||
/*! Opaque pointer on a user task object, passed as first argument to hook functions */
|
||||
void* cookie;
|
||||
|
||||
/*! Pointer on a function that that does something with progress value \p progress */
|
||||
gmio_bool_t (*handle_progress_func)(void* cookie, uint8_t progress);
|
||||
/*! \brief Pointer on a function that says if the current task must stop
|
||||
*
|
||||
* If GMIO_TRUE is returned then the current task should abort as soon as possible, otherwise it
|
||||
* can continue execution.
|
||||
*/
|
||||
gmio_bool_t (*is_stop_requested_func)(void* cookie);
|
||||
};
|
||||
|
||||
typedef struct gmio_task_control gmio_task_control_t;
|
||||
|
||||
/*! Safe and convenient function for gmio_task_control::handle_progress_func() */
|
||||
GMIO_LIB_EXPORT gmio_bool_t gmio_task_control_handle_progress(gmio_task_control_t* ctrl,
|
||||
uint8_t progress_pc);
|
||||
|
||||
/*! Utility function that converts \p value as a percentage */
|
||||
GMIO_LIB_EXPORT uint8_t gmio_percentage(size_t range_min, size_t range_max, size_t value);
|
||||
GMIO_LIB_EXPORT
|
||||
gmio_bool_t gmio_task_control_is_stop_requested(const gmio_task_control_t* task_ctrl);
|
||||
|
||||
GMIO_C_LINKAGE_END
|
||||
|
||||
|
@ -100,11 +100,9 @@ static void gmio_stream_fwd_iterator_stla_read_hook(void* cookie,
|
||||
const gmio_ascii_string_buffer_t* buffer)
|
||||
{
|
||||
_internal_gmio_fwd_iterator_cookie_t* tcookie = (_internal_gmio_fwd_iterator_cookie_t*)(cookie);
|
||||
gmio_task_control_t* ctrl = tcookie != NULL ? tcookie->task_control : NULL;
|
||||
if (ctrl != NULL) {
|
||||
const uint8_t progress_pc = gmio_percentage(0, tcookie->stream_data_size, tcookie->stream_offset);
|
||||
tcookie->is_stop_requested = !gmio_task_control_handle_progress(ctrl, progress_pc);
|
||||
}
|
||||
const gmio_task_control_t* ctrl = tcookie != NULL ? tcookie->task_control : NULL;
|
||||
if (ctrl != NULL)
|
||||
tcookie->is_stop_requested = !gmio_task_control_is_stop_requested(ctrl);
|
||||
if (tcookie != NULL)
|
||||
tcookie->stream_offset += buffer->len;
|
||||
}
|
||||
|
@ -111,7 +111,6 @@ int gmio_stla_write(const gmio_stl_mesh_t* mesh,
|
||||
uint8_t real32_prec)
|
||||
{
|
||||
const uint32_t total_facet_count = mesh != NULL ? mesh->triangle_count : 0;
|
||||
uint32_t written_facet_count = 0;
|
||||
const uint32_t buffer_facet_count = trsf != NULL ? trsf->buffer_size / GMIO_STLA_FACET_SIZE_P2 : 0;
|
||||
uint32_t ifacet = 0;
|
||||
char* buffer_iterator = trsf != NULL ? trsf->buffer : NULL;
|
||||
@ -184,13 +183,8 @@ int gmio_stla_write(const gmio_stl_mesh_t* mesh,
|
||||
error = GMIO_STREAM_ERROR;
|
||||
|
||||
/* Task control */
|
||||
if (gmio_no_error(error) && trsf->task_control.handle_progress_func != NULL) {
|
||||
uint32_t percentage = 0;
|
||||
written_facet_count += buffer_facet_count;
|
||||
percentage = gmio_percentage(0, total_facet_count, written_facet_count);
|
||||
if (!gmio_task_control_handle_progress(&trsf->task_control, percentage))
|
||||
if (gmio_no_error(error) && gmio_task_control_is_stop_requested(&trsf->task_control))
|
||||
error = GMIO_TASK_STOPPED_ERROR;
|
||||
}
|
||||
} /* end for (ifacet) */
|
||||
|
||||
/* Write end of solid */
|
||||
|
@ -97,12 +97,9 @@ int gmio_stlb_read(gmio_stl_mesh_creator_t *creator,
|
||||
break; /* Exit if no facet to read */
|
||||
|
||||
if (gmio_no_error(error)) {
|
||||
uint8_t progress_pc;
|
||||
|
||||
gmio_stlb_read_facets(creator, trsf->buffer, &rparams);
|
||||
rparams.i_facet_offset += rparams.facet_count;
|
||||
progress_pc = gmio_percentage(0, total_facet_count, rparams.i_facet_offset);
|
||||
if (!gmio_task_control_handle_progress(&trsf->task_control, progress_pc))
|
||||
if (gmio_task_control_is_stop_requested(&trsf->task_control))
|
||||
error = GMIO_TASK_STOPPED_ERROR;
|
||||
}
|
||||
} /* end while */
|
||||
|
@ -100,12 +100,8 @@ int gmio_stlb_write(const gmio_stl_mesh_t *mesh,
|
||||
}
|
||||
|
||||
/* Task control */
|
||||
if (gmio_no_error(error)
|
||||
&& !gmio_task_control_handle_progress(&trsf->task_control,
|
||||
gmio_percentage(0, facet_count, i_facet + 1)))
|
||||
{
|
||||
if (gmio_no_error(error) && gmio_task_control_is_stop_requested(&trsf->task_control))
|
||||
error = GMIO_TASK_STOPPED_ERROR;
|
||||
}
|
||||
} /* end for */
|
||||
|
||||
return error;
|
||||
|
Loading…
Reference in New Issue
Block a user