diff mbox

[11/22] qapi: add signal support to core QMP server

Message ID 1299460984-15849-12-git-send-email-aliguori@us.ibm.com
State New
Headers show

Commit Message

Anthony Liguori March 7, 2011, 1:22 a.m. UTC
Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>

Comments

Stefan Hajnoczi March 7, 2011, 1:21 p.m. UTC | #1
On Mon, Mar 7, 2011 at 1:22 AM, Anthony Liguori <aliguori@us.ibm.com> wrote:
> diff --git a/qmp-core.h b/qmp-core.h
> index e3235ec..5ce02f7 100644
> --- a/qmp-core.h
> +++ b/qmp-core.h
> @@ -21,10 +21,65 @@ typedef struct QmpState QmpState;
>  typedef void (QmpCommandFunc)(const QDict *, QObject **, Error **);
>  typedef void (QmpStatefulCommandFunc)(QmpState *qmp__sess, const QDict *, QObject **, Error **);
>
> +typedef struct QmpSlot
> +{
> +    int handle;
> +    void *func;

This should be a void (*func)() pointer for architectures where
function pointers don't fit into void * (e.g. ppc and itanium).

> +QmpSignal *qmp_signal_init(void);
> +void qmp_signal_ref(QmpSignal *obj);
> +void qmp_signal_unref(QmpSignal *obj);
> +int qmp_signal_connect(QmpSignal *obj, void *func, void *opaque);

Same function pointer issue here.

Stefan
Anthony Liguori March 7, 2011, 1:53 p.m. UTC | #2
On 03/07/2011 07:21 AM, Stefan Hajnoczi wrote:
> On Mon, Mar 7, 2011 at 1:22 AM, Anthony Liguori<aliguori@us.ibm.com>  wrote:
>    
>> diff --git a/qmp-core.h b/qmp-core.h
>> index e3235ec..5ce02f7 100644
>> --- a/qmp-core.h
>> +++ b/qmp-core.h
>> @@ -21,10 +21,65 @@ typedef struct QmpState QmpState;
>>   typedef void (QmpCommandFunc)(const QDict *, QObject **, Error **);
>>   typedef void (QmpStatefulCommandFunc)(QmpState *qmp__sess, const QDict *, QObject **, Error **);
>>
>> +typedef struct QmpSlot
>> +{
>> +    int handle;
>> +    void *func;
>>      
> This should be a void (*func)()

Technically void (*)() is an obsolete type in standard C.

I can switch to void (*)(void) but it requires casting and requires a 
typeof() :-/

Regards,

Anthony Liguori

>   pointer for architectures where
> function pointers don't fit into void * (e.g. ppc and itanium).
>
>    
>> +QmpSignal *qmp_signal_init(void);
>> +void qmp_signal_ref(QmpSignal *obj);
>> +void qmp_signal_unref(QmpSignal *obj);
>> +int qmp_signal_connect(QmpSignal *obj, void *func, void *opaque);
>>      
> Same function pointer issue here.
>
> Stefan
>
>
Stefan Hajnoczi March 7, 2011, 2:36 p.m. UTC | #3
On Mon, Mar 7, 2011 at 1:53 PM, Anthony Liguori <anthony@codemonkey.ws> wrote:
> On 03/07/2011 07:21 AM, Stefan Hajnoczi wrote:
>>
>> On Mon, Mar 7, 2011 at 1:22 AM, Anthony Liguori<aliguori@us.ibm.com>
>>  wrote:
>>
>>>
>>> diff --git a/qmp-core.h b/qmp-core.h
>>> index e3235ec..5ce02f7 100644
>>> --- a/qmp-core.h
>>> +++ b/qmp-core.h
>>> @@ -21,10 +21,65 @@ typedef struct QmpState QmpState;
>>>  typedef void (QmpCommandFunc)(const QDict *, QObject **, Error **);
>>>  typedef void (QmpStatefulCommandFunc)(QmpState *qmp__sess, const QDict
>>> *, QObject **, Error **);
>>>
>>> +typedef struct QmpSlot
>>> +{
>>> +    int handle;
>>> +    void *func;
>>>
>>
>> This should be a void (*func)()
>
> Technically void (*)() is an obsolete type in standard C.
>
> I can switch to void (*)(void) but it requires casting and requires a
> typeof() :-/

You're right, void (*)(void) is the proper form for a general function
pointer.  I think doing this in a portable way is worthwhile.

Stefan
Anthony Liguori March 7, 2011, 2:41 p.m. UTC | #4
On 03/07/2011 08:36 AM, Stefan Hajnoczi wrote:
>
>> Technically void (*)() is an obsolete type in standard C.
>>
>> I can switch to void (*)(void) but it requires casting and requires a
>> typeof() :-/
>>      
> You're right, void (*)(void) is the proper form for a general function
> pointer.  I think doing this in a portable way is worthwhile.
>    

Yeah, using a function pointer is definitely better than using a void 
*.  We'll just have to live with the use of typeof() though which 
fortunately is supported in some form by basically every compiler out there.

Regards,

Anthony Liguori

> Stefan
>
>
diff mbox

Patch

diff --git a/qmp-core.c b/qmp-core.c
index 78aef3a..3a6242c 100644
--- a/qmp-core.c
+++ b/qmp-core.c
@@ -28,6 +28,22 @@  typedef struct QmpCommand
     QTAILQ_ENTRY(QmpCommand) node;
 } QmpCommand;
 
+typedef struct DefaultQmpConnection
+{
+    QmpSignal *obj;
+    int handle;
+    QTAILQ_ENTRY(DefaultQmpConnection) node;
+} DefaultQmpConnection;
+
+struct QmpState
+{
+    int (*add_connection)(QmpState *s, QmpConnection *conn);
+    void (*del_connection)(QmpState *s, int global_handle, Error **errp);
+    void (*event)(QmpState *s, QObject *data);
+
+    QTAILQ_HEAD(, DefaultQmpConnection) default_connections;
+};
+
 static QTAILQ_HEAD(, QmpCommand) qmp_commands =
     QTAILQ_HEAD_INITIALIZER(qmp_commands);
 
@@ -75,3 +91,90 @@  char *qobject_as_string(QObject *obj)
         return NULL;
     }
 }
+
+void qmp_state_add_connection(QmpState *sess, const char *event_name, QmpSignal *obj, int handle, QmpConnection *conn)
+{
+    conn->state = sess;
+    conn->event_name = event_name;
+    conn->signal = obj;
+    conn->handle = handle;
+    conn->global_handle = sess->add_connection(sess, conn);
+}
+
+void qmp_state_del_connection(QmpState *sess, int global_handle, Error **errp)
+{
+    sess->del_connection(sess, global_handle, errp);
+}
+
+void qmp_state_event(QmpConnection *conn, QObject *data)
+{
+    QDict *event = qdict_new();
+    qemu_timeval tv;
+    QObject *ts;
+
+    qemu_gettimeofday(&tv);
+
+    ts = qobject_from_jsonf("{ 'seconds': %" PRId64 ", "
+                            "'microseconds': %" PRId64 " }",
+                            (int64_t)tv.tv_sec, (int64_t)tv.tv_usec);
+    qdict_put_obj(event, "timestamp", ts);
+
+    qdict_put(event, "event", qstring_from_str(conn->event_name));
+    if (data) {
+        qobject_incref(data);
+        qdict_put_obj(event, "data", data);
+    }
+
+    qdict_put(event, "tag", qint_from_int(conn->global_handle));
+
+    conn->state->event(conn->state, QOBJECT(event));
+    QDECREF(event);
+}
+
+QmpSignal *qmp_signal_init(void)
+{
+    QmpSignal *obj = qemu_mallocz(sizeof(*obj));
+    obj->max_handle = 0;
+    obj->ref = 1;
+    QTAILQ_INIT(&obj->slots);
+    return obj;
+}
+
+void qmp_signal_ref(QmpSignal *obj)
+{
+    obj->ref++;
+}
+
+void qmp_signal_unref(QmpSignal *obj)
+{
+    if (--obj->ref) {
+        qemu_free(obj);
+    }
+}
+
+int qmp_signal_connect(QmpSignal *obj, void *func, void *opaque)
+{
+    int handle = ++obj->max_handle;
+    QmpSlot *slot = qemu_mallocz(sizeof(*slot));
+
+    slot->handle = handle;
+    slot->func = func;
+    slot->opaque = opaque;
+
+    QTAILQ_INSERT_TAIL(&obj->slots, slot, node);
+
+    return handle;
+}
+
+void qmp_signal_disconnect(QmpSignal *obj, int handle)
+{
+    QmpSlot *slot;
+
+    QTAILQ_FOREACH(slot, &obj->slots, node) {
+        if (slot->handle == handle) {
+            QTAILQ_REMOVE(&obj->slots, slot, node);
+            qemu_free(slot);
+            break;
+        }
+    }
+}
diff --git a/qmp-core.h b/qmp-core.h
index e3235ec..5ce02f7 100644
--- a/qmp-core.h
+++ b/qmp-core.h
@@ -21,10 +21,65 @@  typedef struct QmpState QmpState;
 typedef void (QmpCommandFunc)(const QDict *, QObject **, Error **);
 typedef void (QmpStatefulCommandFunc)(QmpState *qmp__sess, const QDict *, QObject **, Error **);
 
+typedef struct QmpSlot
+{
+    int handle;
+    void *func;
+    void *opaque;
+    QTAILQ_ENTRY(QmpSlot) node;
+} QmpSlot;
+
+struct QmpSignal
+{
+    int max_handle;
+    int ref;
+    QTAILQ_HEAD(, QmpSlot) slots;
+};
+
+typedef struct QmpConnection
+{
+    QmpState *state;
+    const char *event_name;
+    QmpSignal *signal;
+    int handle;
+    int global_handle;
+    QTAILQ_ENTRY(QmpConnection) node;
+} QmpConnection;
+
 void qmp_register_command(const char *name, QmpCommandFunc *fn);
 void qmp_register_stateful_command(const char *name, QmpStatefulCommandFunc *fn);
 void qmp_init_chardev(CharDriverState *chr);
 
 char *qobject_as_string(QObject *obj);
 
+QmpSignal *qmp_signal_init(void);
+void qmp_signal_ref(QmpSignal *obj);
+void qmp_signal_unref(QmpSignal *obj);
+int qmp_signal_connect(QmpSignal *obj, void *func, void *opaque);
+void qmp_signal_disconnect(QmpSignal *obj, int handle);
+
+void qmp_state_add_connection(QmpState *sess, const char *name, QmpSignal *obj, int handle, QmpConnection *conn);
+void qmp_state_del_connection(QmpState *sess, int global_handle, Error **errp);
+void qmp_state_event(QmpConnection *conn, QObject *data);
+
+#define signal_init(obj) do {          \
+    (obj)->signal = qmp_signal_init(); \
+} while (0)
+
+#define signal_unref(obj) qmp_signal_unref((obj)->signal)
+
+#define signal_connect(obj, fn, opaque) \
+    qmp_signal_connect((obj)->signal, (obj)->func = fn, opaque)
+
+#define signal_disconnect(obj, handle) \
+    qmp_signal_disconnect((obj)->signal, handle)
+
+#define signal_notify(obj, ...) do {                         \
+    QmpSlot *qmp__slot;                                      \
+    QTAILQ_FOREACH(qmp__slot, &(obj)->signal->slots, node) { \
+        (obj)->func = qmp__slot->func;                       \
+        (obj)->func(qmp__slot->opaque, ## __VA_ARGS__);      \
+    }                                                        \
+} while(0)
+
 #endif