Skip to content

Commit

Permalink
Allow attaching of context specific user data to mce io monitors
Browse files Browse the repository at this point in the history
After io monitor is created, used data can be attached to it via
mce_io_mon_set_user_data().

The existing notification interface is not changed, i.e. the user
data is not automatically passed to notification callbacks and
must be queried via mce_io_mon_get_user_data() function when needed.

If free callback is provided, the user data is released when the
io monitor itself gets deleted.

[mce] Allow attaching of context specific user data to mce io monitors
  • Loading branch information
spiiroin committed Dec 23, 2014
1 parent 3135652 commit 63960dd
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 2 deletions.
53 changes: 52 additions & 1 deletion mce-io.c
Expand Up @@ -68,12 +68,14 @@ struct mce_io_mon_t {
GIOChannel *iochan; /**< I/O channel */
guint iowatch_id; /**< GSource ID for input */

mce_io_mon_notify_cb nofity_cb; /**< Input handling callback */
mce_io_mon_notify_cb nofity_cb; /**< Input handling callback */
mce_io_mon_delete_cb delete_cb; /**< Iomon delete callback */

error_policy_t error_policy; /**< Error policy */
gboolean rewind_policy; /**< Rewind policy */

void *user_data; /**< Attached user data block */
mce_io_mon_free_cb user_free_cb;/**< Callback for freeing user_data */
};

/* ========================================================================= *
Expand Down Expand Up @@ -346,6 +348,9 @@ static mce_io_mon_t *mce_io_mon_create(const char *path, mce_io_mon_delete_cb de
self->error_policy = MCE_IO_ERROR_POLICY_WARN;
self->rewind_policy = FALSE;

self->user_data = 0;
self->user_free_cb = 0;

mce_log(LL_NOTICE, "adding monitor for: %s", self->path);

EXIT:
Expand Down Expand Up @@ -374,6 +379,10 @@ static void mce_io_mon_delete(mce_io_mon_t *self)
self->delete_cb(self);
}

/* Free attached user data */
if( self->user_data && self->user_free_cb )
self->user_free_cb(self->user_data);

/* Unlink from monitor list */
if( !g_slist_find(file_monitors, self) ) {
mce_log(LL_WARN, "Trying to unregister non-registered"
Expand Down Expand Up @@ -1088,6 +1097,48 @@ int mce_io_mon_get_fd(const mce_io_mon_t *iomon)
return fd;
}

/** Attach user data block to io monitor
*
* If non-null free_cb callback is given, the user_data block will
* be released using it when io-monitor itself is deleted.
*
* Note: The delete notification callback is called before the
* user data is released, i.e. user data is still available
* at that point.
*
* @param io_monitor An opaque pointer to the I/O monitor structure
* @param user_data Data block to attach to io monitor, or NULL
* @param free_cb Free function to release data block or NULL
*/
void mce_io_mon_set_user_data(mce_io_mon_t *iomon,
void *user_data,
mce_io_mon_free_cb free_cb)
{
if( !iomon )
goto EXIT;

/* Clear already existing user data */
if( iomon->user_data && iomon->user_free_cb )
iomon->user_free_cb(iomon->user_data);

/* Set user data */
iomon->user_data = user_data;
iomon->user_free_cb = free_cb;
EXIT:
return;
}

/** Get user data block attached to io monitor
*
* @param io_monitor An opaque pointer to the I/O monitor structure
*
* @return user data block, or NULL if not set
*/
void *mce_io_mon_get_user_data(const mce_io_mon_t *iomon)
{
return iomon ? iomon->user_data : 0;
}

/* ========================================================================= *
* MISC_UTILS
* ========================================================================= */
Expand Down
12 changes: 11 additions & 1 deletion mce-io.h
Expand Up @@ -73,11 +73,15 @@ typedef struct {

typedef struct mce_io_mon_t mce_io_mon_t;

/** Function pointer for I/O monitor callback */
/** Callback function type for I/O monitor input notifications */
typedef gboolean (*mce_io_mon_notify_cb)(gpointer data, gsize bytes_read);

/** Callback function type for I/O monitor delete notifications */
typedef void (*mce_io_mon_delete_cb)(mce_io_mon_t *iomon);

/** Callback function type for releasing I/O monitor user data block */
typedef void (*mce_io_mon_free_cb)(void *user_data);

/* iomon functions */

mce_io_mon_t *mce_io_mon_register_string(const gint fd,
Expand Down Expand Up @@ -109,6 +113,12 @@ const gchar *mce_io_mon_get_path(const mce_io_mon_t *iomon);

int mce_io_mon_get_fd(const mce_io_mon_t *iomon);

void mce_io_mon_set_user_data(mce_io_mon_t *iomon,
void *user_data,
mce_io_mon_free_cb free_cb);

void *mce_io_mon_get_user_data(const mce_io_mon_t *iomon);

/* output_state_t funtions */

void mce_close_output(output_state_t *output);
Expand Down

0 comments on commit 63960dd

Please sign in to comment.