gmio_core: limit task_control to interruption request (no progress report feature)

This commit is contained in:
Hugues Delorme 2014-11-20 09:25:43 +01:00
parent 60db7e397a
commit 666f90857a
6 changed files with 20 additions and 65 deletions

View File

@ -1,39 +1,10 @@
#include "task_control.h" #include "task_control.h"
#include <math.h> #include <stddef.h>
#include <string.h>
/*! \var gmio_bool_t (*gmio_task_control::handle_progress_func)(void*, uint8_t) gmio_bool_t gmio_task_control_is_stop_requested(const gmio_task_control_t* task_ctrl)
*
* 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)
{ {
if (ctrl != NULL && ctrl->handle_progress_func != NULL) if (task_ctrl != NULL && task_ctrl->is_stop_requested_func != NULL)
return ctrl->handle_progress_func(ctrl->cookie, progress_pc); return task_ctrl->is_stop_requested_func(task_ctrl->cookie);
return 1; return GMIO_FALSE;
}
/*! \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;
} }

View File

@ -2,31 +2,30 @@
#define GMIO_TASK_CONTROL_H #define GMIO_TASK_CONTROL_H
#include "global.h" #include "global.h"
#include "memory.h"
GMIO_C_LINKAGE_BEGIN GMIO_C_LINKAGE_BEGIN
/*! Provides control over a general task. /*! 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 struct gmio_task_control
{ {
/*! Opaque pointer on a user task object, passed as first argument to hook functions */ /*! Opaque pointer on a user task object, passed as first argument to hook functions */
void* cookie; void* cookie;
/*! Pointer on a function that that does something with progress value \p progress */ /*! \brief Pointer on a function that says if the current task must stop
gmio_bool_t (*handle_progress_func)(void* cookie, uint8_t progress); *
* 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; typedef struct gmio_task_control gmio_task_control_t;
/*! Safe and convenient function for gmio_task_control::handle_progress_func() */ GMIO_LIB_EXPORT
GMIO_LIB_EXPORT gmio_bool_t gmio_task_control_handle_progress(gmio_task_control_t* ctrl, gmio_bool_t gmio_task_control_is_stop_requested(const gmio_task_control_t* task_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_C_LINKAGE_END GMIO_C_LINKAGE_END

View File

@ -100,11 +100,9 @@ static void gmio_stream_fwd_iterator_stla_read_hook(void* cookie,
const gmio_ascii_string_buffer_t* buffer) const gmio_ascii_string_buffer_t* buffer)
{ {
_internal_gmio_fwd_iterator_cookie_t* tcookie = (_internal_gmio_fwd_iterator_cookie_t*)(cookie); _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; const gmio_task_control_t* ctrl = tcookie != NULL ? tcookie->task_control : NULL;
if (ctrl != 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_is_stop_requested(ctrl);
tcookie->is_stop_requested = !gmio_task_control_handle_progress(ctrl, progress_pc);
}
if (tcookie != NULL) if (tcookie != NULL)
tcookie->stream_offset += buffer->len; tcookie->stream_offset += buffer->len;
} }

View File

@ -111,7 +111,6 @@ int gmio_stla_write(const gmio_stl_mesh_t* mesh,
uint8_t real32_prec) uint8_t real32_prec)
{ {
const uint32_t total_facet_count = mesh != NULL ? mesh->triangle_count : 0; 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; const uint32_t buffer_facet_count = trsf != NULL ? trsf->buffer_size / GMIO_STLA_FACET_SIZE_P2 : 0;
uint32_t ifacet = 0; uint32_t ifacet = 0;
char* buffer_iterator = trsf != NULL ? trsf->buffer : NULL; 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; error = GMIO_STREAM_ERROR;
/* Task control */ /* Task control */
if (gmio_no_error(error) && trsf->task_control.handle_progress_func != NULL) { if (gmio_no_error(error) && gmio_task_control_is_stop_requested(&trsf->task_control))
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))
error = GMIO_TASK_STOPPED_ERROR; error = GMIO_TASK_STOPPED_ERROR;
}
} /* end for (ifacet) */ } /* end for (ifacet) */
/* Write end of solid */ /* Write end of solid */

View File

@ -97,12 +97,9 @@ int gmio_stlb_read(gmio_stl_mesh_creator_t *creator,
break; /* Exit if no facet to read */ break; /* Exit if no facet to read */
if (gmio_no_error(error)) { if (gmio_no_error(error)) {
uint8_t progress_pc;
gmio_stlb_read_facets(creator, trsf->buffer, &rparams); gmio_stlb_read_facets(creator, trsf->buffer, &rparams);
rparams.i_facet_offset += rparams.facet_count; rparams.i_facet_offset += rparams.facet_count;
progress_pc = gmio_percentage(0, total_facet_count, rparams.i_facet_offset); if (gmio_task_control_is_stop_requested(&trsf->task_control))
if (!gmio_task_control_handle_progress(&trsf->task_control, progress_pc))
error = GMIO_TASK_STOPPED_ERROR; error = GMIO_TASK_STOPPED_ERROR;
} }
} /* end while */ } /* end while */

View File

@ -100,12 +100,8 @@ int gmio_stlb_write(const gmio_stl_mesh_t *mesh,
} }
/* Task control */ /* Task control */
if (gmio_no_error(error) if (gmio_no_error(error) && gmio_task_control_is_stop_requested(&trsf->task_control))
&& !gmio_task_control_handle_progress(&trsf->task_control,
gmio_percentage(0, facet_count, i_facet + 1)))
{
error = GMIO_TASK_STOPPED_ERROR; error = GMIO_TASK_STOPPED_ERROR;
}
} /* end for */ } /* end for */
return error; return error;