Skip to content

Commit

Permalink
[datapipe] Allow data caching at output stage. JB#37393
Browse files Browse the repository at this point in the history
Reading cached value from a datapipe gives the last input fed to the
datapipe. If the datapipe uses input filters, the cached input value
can differ from the latest output value that was reported via output
triggers.

Make it possible to update datapipe cache in the output stage too via
using caching policy CACHE_OUTDATA.

Signed-off-by: Simo Piiroinen <simo.piiroinen@jollamobile.com>
  • Loading branch information
spiiroin committed Jan 18, 2017
1 parent b5a1f76 commit d0f3b87
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 28 deletions.
63 changes: 39 additions & 24 deletions datapipe.c
Expand Up @@ -232,8 +232,7 @@ datapipe_struct proximity_blank_pipe;
*/
void execute_datapipe_input_triggers(datapipe_struct *const datapipe,
gpointer const indata,
const data_source_t use_cache,
const caching_policy_t cache_indata)
const data_source_t use_cache)
{
void (*trigger)(gconstpointer const input);
gpointer data;
Expand All @@ -249,15 +248,6 @@ void execute_datapipe_input_triggers(datapipe_struct *const datapipe,

data = (use_cache == USE_CACHE) ? datapipe->cached_data : indata;

if (cache_indata == CACHE_INDATA) {
if (use_cache == USE_INDATA) {
if (datapipe->free_cache == FREE_CACHE)
g_free(datapipe->cached_data);

datapipe->cached_data = data;
}
}

for (i = 0; (trigger = g_slist_nth_data(datapipe->input_triggers,
i)) != NULL; i++) {
trigger(data);
Expand Down Expand Up @@ -298,12 +288,14 @@ gconstpointer execute_datapipe_filters(datapipe_struct *const datapipe,
i)) != NULL; i++) {
gpointer tmp = filter(data);

/* If the data needs to be freed, and this isn't the indata,
* or if we're not using the cache, then free the data
*/
if ((datapipe->free_cache == FREE_CACHE) &&
((i > 0) || (use_cache == USE_INDATA)))
g_free(data);
if( datapipe->free_cache == FREE_CACHE ) {
/* When dealing with dynamic data, the transitional
* values need to be released - except for the value
* that is cached at the datapipe
*/
if( tmp != data && data != datapipe->cached_data )
g_free(data);
}

data = tmp;
}
Expand Down Expand Up @@ -364,7 +356,7 @@ gconstpointer execute_datapipe(datapipe_struct *const datapipe,
const data_source_t use_cache,
const caching_policy_t cache_indata)
{
gconstpointer data = NULL;
gconstpointer outdata = NULL;

if (datapipe == NULL) {
mce_log(LL_ERR,
Expand All @@ -373,19 +365,42 @@ gconstpointer execute_datapipe(datapipe_struct *const datapipe,
goto EXIT;
}

execute_datapipe_input_triggers(datapipe, indata, use_cache,
cache_indata);
/* Determine input value */
if( use_cache == USE_CACHE )
indata = datapipe->cached_data;

/* Optionally cache the value at the input stage */
if( cache_indata & (CACHE_INDATA|CACHE_OUTDATA) ) {
if( datapipe->free_cache == FREE_CACHE &&
datapipe->cached_data != indata )
g_free(datapipe->cached_data);
datapipe->cached_data = indata;
}

/* Execute input value callbacks */
execute_datapipe_input_triggers(datapipe, indata, USE_INDATA);

/* Determine output value */
if (datapipe->read_only == READ_ONLY) {
data = indata;
outdata = indata;
} else {
data = execute_datapipe_filters(datapipe, indata, use_cache);
outdata = execute_datapipe_filters(datapipe, indata, USE_INDATA);
}

/* Optionally cache the value at the output stage */
if( cache_indata & CACHE_OUTDATA ) {
if( datapipe->free_cache == FREE_CACHE &&
datapipe->cached_data != outdata )
g_free(datapipe->cached_data);

datapipe->cached_data = (gpointer)outdata;
}

execute_datapipe_output_triggers(datapipe, data, USE_INDATA);
/* Execute output value callbacks */
execute_datapipe_output_triggers(datapipe, outdata, USE_INDATA);

EXIT:
return data;
return outdata;
}

/**
Expand Down
8 changes: 4 additions & 4 deletions datapipe.h
Expand Up @@ -82,8 +82,9 @@ typedef enum {
* Policy used for caching indata
*/
typedef enum {
DONT_CACHE_INDATA = FALSE, /**< Do not cache the indata */
CACHE_INDATA = TRUE /**< Cache the indata */
DONT_CACHE_INDATA = 0, /**< Do not cache the indata */
CACHE_INDATA = 1<<0, /**< Cache the unfiltered indata */
CACHE_OUTDATA = 1<<1, /**< Cache the filtered outdata */
} caching_policy_t;

/* Available datapipes */
Expand Down Expand Up @@ -170,8 +171,7 @@ extern datapipe_struct proximity_blank_pipe;
/* Datapipe execution */
void execute_datapipe_input_triggers(datapipe_struct *const datapipe,
gpointer const indata,
const data_source_t use_cache,
const caching_policy_t cache_indata);
const data_source_t use_cache);
gconstpointer execute_datapipe_filters(datapipe_struct *const datapipe,
gpointer indata,
const data_source_t use_cache);
Expand Down

0 comments on commit d0f3b87

Please sign in to comment.