Implement closure-related methods for gio GSource types

Also, fix up the argument ordering on GFDSourceFunc

https://bugzilla.gnome.org/show_bug.cgi?id=634239
This commit is contained in:
Dan Winship 2010-11-07 11:05:26 -05:00
parent ece936e84d
commit 73d823ac1e
6 changed files with 97 additions and 9 deletions

View File

@ -78,7 +78,7 @@ fd_source_dispatch (GSource *source,
g_warn_if_fail (func != NULL); g_warn_if_fail (func != NULL);
return (*func) (user_data, fd_source->pollfd.revents, fd_source->pollfd.fd); return (*func) (fd_source->pollfd.fd, fd_source->pollfd.revents, user_data);
} }
static void static void
@ -98,11 +98,66 @@ fd_source_finalize (GSource *source)
g_object_unref (fd_source->cancellable); g_object_unref (fd_source->cancellable);
} }
static gboolean
fd_source_closure_callback (int fd,
GIOCondition condition,
gpointer data)
{
GClosure *closure = data;
GValue params[2] = { { 0, }, { 0, } };
GValue result_value = { 0, };
gboolean result;
g_value_init (&result_value, G_TYPE_BOOLEAN);
g_value_init (&params[0], G_TYPE_INT);
g_value_set_int (&params[0], fd);
g_value_init (&params[1], G_TYPE_IO_CONDITION);
g_value_set_flags (&params[1], condition);
g_closure_invoke (closure, &result_value, 2, params, NULL);
result = g_value_get_boolean (&result_value);
g_value_unset (&result_value);
g_value_unset (&params[0]);
g_value_unset (&params[1]);
return result;
}
static void
fd_source_closure_marshal (GClosure *closure,
GValue *return_value,
guint n_param_values,
const GValue *param_values,
gpointer invocation_hint,
gpointer marshal_data)
{
GFDSourceFunc callback;
GCClosure *cc = (GCClosure*) closure;
gboolean v_return;
g_return_if_fail (return_value != NULL);
g_return_if_fail (n_param_values == 0);
callback = (GFDSourceFunc) (marshal_data ? marshal_data : cc->callback);
v_return = callback (g_value_get_int (param_values),
g_value_get_flags (param_values + 1),
closure->data);
g_value_set_boolean (return_value, v_return);
}
static GSourceFuncs fd_source_funcs = { static GSourceFuncs fd_source_funcs = {
fd_source_prepare, fd_source_prepare,
fd_source_check, fd_source_check,
fd_source_dispatch, fd_source_dispatch,
fd_source_finalize fd_source_finalize,
(GSourceFunc)fd_source_closure_callback,
(GSourceDummyMarshal)fd_source_closure_marshal,
}; };
/* Might be called on another thread */ /* Might be called on another thread */

View File

@ -27,9 +27,9 @@
G_BEGIN_DECLS G_BEGIN_DECLS
typedef gboolean (*GFDSourceFunc) (gpointer user_data, typedef gboolean (*GFDSourceFunc) (int fd,
GIOCondition condition, GIOCondition condition,
int fd); gpointer user_data);
GSource *_g_fd_source_new (int fd, GSource *_g_fd_source_new (int fd,
gushort events, gushort events,

View File

@ -25,3 +25,5 @@ VOID:POINTER,INT,STRING
BOOLEAN:OBJECT BOOLEAN:OBJECT
INT:OBJECT INT:OBJECT
VOID:INT64 VOID:INT64
VOID:UINT64
BOOLEAN:FLAGS

View File

@ -50,6 +50,7 @@
#include "gioerror.h" #include "gioerror.h"
#include "gioenums.h" #include "gioenums.h"
#include "gioerror.h" #include "gioerror.h"
#include "gio-marshal.h"
#include "gnetworkingprivate.h" #include "gnetworkingprivate.h"
#include "gsocketaddress.h" #include "gsocketaddress.h"
#include "gsocketcontrolmessage.h" #include "gsocketcontrolmessage.h"
@ -2493,12 +2494,42 @@ socket_source_finalize (GSource *source)
} }
} }
static gboolean
socket_source_closure_callback (GSocket *socket,
GIOCondition condition,
gpointer data)
{
GClosure *closure = data;
GValue params[2] = { { 0, }, { 0, } };
GValue result_value = { 0, };
gboolean result;
g_value_init (&result_value, G_TYPE_BOOLEAN);
g_value_init (&params[0], G_TYPE_SOCKET);
g_value_set_object (&params[0], socket);
g_value_init (&params[1], G_TYPE_IO_CONDITION);
g_value_set_flags (&params[1], condition);
g_closure_invoke (closure, &result_value, 2, params, NULL);
result = g_value_get_boolean (&result_value);
g_value_unset (&result_value);
g_value_unset (&params[0]);
g_value_unset (&params[1]);
return result;
}
static GSourceFuncs socket_source_funcs = static GSourceFuncs socket_source_funcs =
{ {
socket_source_prepare, socket_source_prepare,
socket_source_check, socket_source_check,
socket_source_dispatch, socket_source_dispatch,
socket_source_finalize socket_source_finalize,
(GSourceFunc)socket_source_closure_callback,
(GSourceDummyMarshal)_gio_marshal_BOOLEAN__FLAGS,
}; };
static GSource * static GSource *

View File

@ -422,9 +422,9 @@ typedef struct {
} ReadAsyncData; } ReadAsyncData;
static gboolean static gboolean
read_async_cb (ReadAsyncData *data, read_async_cb (int fd,
GIOCondition condition, GIOCondition condition,
int fd) ReadAsyncData *data)
{ {
GSimpleAsyncResult *simple; GSimpleAsyncResult *simple;
GError *error = NULL; GError *error = NULL;

View File

@ -409,9 +409,9 @@ typedef struct {
} WriteAsyncData; } WriteAsyncData;
static gboolean static gboolean
write_async_cb (WriteAsyncData *data, write_async_cb (int fd,
GIOCondition condition, GIOCondition condition,
int fd) WriteAsyncData *data)
{ {
GSimpleAsyncResult *simple; GSimpleAsyncResult *simple;
GError *error = NULL; GError *error = NULL;