diff mbox

libcacard: replace qemu thread primitives with glib ones

Message ID 1398615995-14574-1-git-send-email-mjt@msgid.tls.msk.ru
State New
Headers show

Commit Message

Michael Tokarev April 27, 2014, 4:26 p.m. UTC
Replace QemuMutex with GMutex and QemuCond with GCond
(with corresponding function changes), to make libcacard
independent of qemu internal functions.

After this step, none of libcacard internals use any
qemu-provided symbols.  Maybe it's a good idea to
stop including qemu-common.h internally too.

Signed-off-by: Michael Tokarev <mjt@tls.msk.ru>
---
 libcacard/Makefile  |    8 +-------
 libcacard/event.c   |   24 ++++++++++++------------
 libcacard/vreader.c |   16 ++++++++--------
 3 files changed, 21 insertions(+), 27 deletions(-)

Note: compile-tested only, and only on linux.

Comments

Peter Maydell April 28, 2014, 10:19 a.m. UTC | #1
On 27 April 2014 17:26, Michael Tokarev <mjt@tls.msk.ru> wrote:
> Replace QemuMutex with GMutex and QemuCond with GCond
> (with corresponding function changes), to make libcacard
> independent of qemu internal functions.

> --- a/libcacard/event.c
> +++ b/libcacard/event.c
> @@ -43,13 +43,13 @@ vevent_delete(VEvent *vevent)
>
>  static VEvent *vevent_queue_head;
>  static VEvent *vevent_queue_tail;
> -static QemuMutex vevent_queue_lock;
> -static QemuCond vevent_queue_condition;
> +static GMutex vevent_queue_lock;
> +static GCond vevent_queue_condition;
>
>  void vevent_queue_init(void)
>  {
> -    qemu_mutex_init(&vevent_queue_lock);
> -    qemu_cond_init(&vevent_queue_condition);
> +    g_mutex_init(&vevent_queue_lock);
> +    g_cond_init(&vevent_queue_condition);
>      vevent_queue_head = vevent_queue_tail = NULL;
>  }

Just to record a conversation from IRC: this won't
compile on older versions of glib, because those don't
support g_cond_init(). Conversely, if you use g_cond_new()
then newer glib will complain that it's deprecated.
trace/simple.c has an example of how you have to work
around this API churn...

thanks
-- PMM
Michael Tokarev April 28, 2014, 11:48 a.m. UTC | #2
28.04.2014 14:19, Peter Maydell wrote:
> On 27 April 2014 17:26, Michael Tokarev <mjt@tls.msk.ru> wrote:

>> -static QemuMutex vevent_queue_lock;
>> -static QemuCond vevent_queue_condition;
>> +static GMutex vevent_queue_lock;
>> +static GCond vevent_queue_condition;
>>
>>  void vevent_queue_init(void)
>>  {
>> -    qemu_mutex_init(&vevent_queue_lock);
>> -    qemu_cond_init(&vevent_queue_condition);
>> +    g_mutex_init(&vevent_queue_lock);
>> +    g_cond_init(&vevent_queue_condition);
>>      vevent_queue_head = vevent_queue_tail = NULL;
>>  }
> 
> Just to record a conversation from IRC: this won't
> compile on older versions of glib, because those don't
> support g_cond_init(). Conversely, if you use g_cond_new()
> then newer glib will complain that it's deprecated.
> trace/simple.c has an example of how you have to work
> around this API churn...

I've added a tiny (but hackish and fun) wrapper header for all
this, and pushed whole thing into a branch on my site -- see

 http://git.corpit.ru/?p=qemu.git;a=shortlog;h=refs/heads/libcacard-standalone

The wrapper is here:

 http://git.corpit.ru/?p=qemu.git;a=blob;f=libcacard/glib-compat.h;h=47fd06a43572fc5cc639fb7d705e85f3965d6b91;hb=1aeefee2a4bb2694b2314cddd04e08675360e255

It allows to use the new primitives/interface with either new (unchanged)
or old (wrapped) glib cond and mutex API.

I think this is better than a way used in trace/simple.c, and can be
used there too, making stuff much more readable.

Please note: the wrapper redifines some core symbols which are used in
glib so it must come somewhere near the end of #include set.  Most
interesting is that it redefines GCOnd into GCond* (only for old glib).

Thanks,

/mjt
Michael Tokarev April 28, 2014, 12:49 p.m. UTC | #3
28.04.2014 15:48, Michael Tokarev wrote:
[]
> I've added a tiny (but hackish and fun) wrapper header for all
> this, and pushed whole thing into a branch on my site -- see
> 
>  http://git.corpit.ru/?p=qemu.git;a=shortlog;h=refs/heads/libcacard-standalone
> 
> The wrapper is here:
> 
>  http://git.corpit.ru/?p=qemu.git;a=blob;f=libcacard/glib-compat.h;h=47fd06a43572fc5cc639fb7d705e85f3965d6b91;hb=1aeefee2a4bb2694b2314cddd04e08675360e255

And the wrapper which actually works (tested on glib-2.24.2 on debian
squeeze (oldstable) system) is here:

http://git.corpit.ru/?p=qemu.git;a=blob;f=libcacard/glib-compat.h;h=8e92b2032a0b9b81e748f8dceb60a3a7d98d00c0

> It allows to use the new primitives/interface with either new (unchanged)
> or old (wrapped) glib cond and mutex API.
> 
> I think this is better than a way used in trace/simple.c, and can be
> used there too, making stuff much more readable.
> 
> Please note: the wrapper redifines some core symbols which are used in
> glib so it must come somewhere near the end of #include set.  Most
> interesting is that it redefines GCOnd into GCond* (only for old glib).

I had to do the same for GMutex.  Previous attempt to make this work with
GStaticMutex didn't work because g_cond_wait() expects a GMutex*, not
GStaticMutex.

And also had to add g_thread_new() wrapper.

And finally, had to revert one commit, 2e679780ae86c6ca8, which converted
strncpy() to pstrcpy().

Thanks,

/mjt
diff mbox

Patch

diff --git a/libcacard/Makefile b/libcacard/Makefile
index bb00c94..550d88b 100644
--- a/libcacard/Makefile
+++ b/libcacard/Makefile
@@ -3,13 +3,7 @@  libcacard_includedir=$(includedir)/cacard
 TOOLS += vscclient$(EXESUF)
 
 # objects linked into a shared library, built with libtool with -fPIC if required
-libcacard-obj-y = $(stub-obj-y) $(libcacard-y)
-libcacard-obj-y += util/osdep.o util/cutils.o util/qemu-timer-common.o
-libcacard-obj-y += util/error.o util/qemu-error.o
-libcacard-obj-$(CONFIG_WIN32) += util/oslib-win32.o util/qemu-thread-win32.o
-libcacard-obj-$(CONFIG_POSIX) += util/oslib-posix.o util/qemu-thread-posix.o
-libcacard-obj-y += $(filter trace/%, $(util-obj-y))
-
+libcacard-obj-y = $(libcacard-y)
 libcacard-lobj-y=$(patsubst %.o,%.lo,$(libcacard-obj-y))
 
 # libtool will build the .o files, too
diff --git a/libcacard/event.c b/libcacard/event.c
index 2d7500f..c4761f2 100644
--- a/libcacard/event.c
+++ b/libcacard/event.c
@@ -43,13 +43,13 @@  vevent_delete(VEvent *vevent)
 
 static VEvent *vevent_queue_head;
 static VEvent *vevent_queue_tail;
-static QemuMutex vevent_queue_lock;
-static QemuCond vevent_queue_condition;
+static GMutex vevent_queue_lock;
+static GCond vevent_queue_condition;
 
 void vevent_queue_init(void)
 {
-    qemu_mutex_init(&vevent_queue_lock);
-    qemu_cond_init(&vevent_queue_condition);
+    g_mutex_init(&vevent_queue_lock);
+    g_cond_init(&vevent_queue_condition);
     vevent_queue_head = vevent_queue_tail = NULL;
 }
 
@@ -57,7 +57,7 @@  void
 vevent_queue_vevent(VEvent *vevent)
 {
     vevent->next = NULL;
-    qemu_mutex_lock(&vevent_queue_lock);
+    g_mutex_lock(&vevent_queue_lock);
     if (vevent_queue_head) {
         assert(vevent_queue_tail);
         vevent_queue_tail->next = vevent;
@@ -65,8 +65,8 @@  vevent_queue_vevent(VEvent *vevent)
         vevent_queue_head = vevent;
     }
     vevent_queue_tail = vevent;
-    qemu_cond_signal(&vevent_queue_condition);
-    qemu_mutex_unlock(&vevent_queue_lock);
+    g_cond_signal(&vevent_queue_condition);
+    g_mutex_unlock(&vevent_queue_lock);
 }
 
 /* must have lock */
@@ -86,11 +86,11 @@  VEvent *vevent_wait_next_vevent(void)
 {
     VEvent *vevent;
 
-    qemu_mutex_lock(&vevent_queue_lock);
+    g_mutex_lock(&vevent_queue_lock);
     while ((vevent = vevent_dequeue_vevent()) == NULL) {
-        qemu_cond_wait(&vevent_queue_condition, &vevent_queue_lock);
+        g_cond_wait(&vevent_queue_condition, &vevent_queue_lock);
     }
-    qemu_mutex_unlock(&vevent_queue_lock);
+    g_mutex_unlock(&vevent_queue_lock);
     return vevent;
 }
 
@@ -98,9 +98,9 @@  VEvent *vevent_get_next_vevent(void)
 {
     VEvent *vevent;
 
-    qemu_mutex_lock(&vevent_queue_lock);
+    g_mutex_lock(&vevent_queue_lock);
     vevent = vevent_dequeue_vevent();
-    qemu_mutex_unlock(&vevent_queue_lock);
+    g_mutex_unlock(&vevent_queue_lock);
     return vevent;
 }
 
diff --git a/libcacard/vreader.c b/libcacard/vreader.c
index 5793d73..70cc529 100644
--- a/libcacard/vreader.c
+++ b/libcacard/vreader.c
@@ -28,7 +28,7 @@  struct VReaderStruct {
     VCard *card;
     char *name;
     vreader_id_t id;
-    QemuMutex lock;
+    GMutex lock;
     VReaderEmul  *reader_private;
     VReaderEmulFree reader_private_free;
 };
@@ -97,13 +97,13 @@  apdu_ins_to_string(int ins)
 static inline void
 vreader_lock(VReader *reader)
 {
-    qemu_mutex_lock(&reader->lock);
+    g_mutex_lock(&reader->lock);
 }
 
 static inline void
 vreader_unlock(VReader *reader)
 {
-    qemu_mutex_unlock(&reader->lock);
+    g_mutex_unlock(&reader->lock);
 }
 
 /*
@@ -116,7 +116,7 @@  vreader_new(const char *name, VReaderEmul *private,
     VReader *reader;
 
     reader = (VReader *)g_malloc(sizeof(VReader));
-    qemu_mutex_init(&reader->lock);
+    g_mutex_init(&reader->lock);
     reader->reference_count = 1;
     reader->name = g_strdup(name);
     reader->card = NULL;
@@ -413,25 +413,25 @@  vreader_dequeue(VReaderList *list, VReaderListEntry *entry)
 }
 
 static VReaderList *vreader_list;
-static QemuMutex vreader_list_mutex;
+static GMutex vreader_list_mutex;
 
 static void
 vreader_list_init(void)
 {
     vreader_list = vreader_list_new();
-    qemu_mutex_init(&vreader_list_mutex);
+    g_mutex_init(&vreader_list_mutex);
 }
 
 static void
 vreader_list_lock(void)
 {
-    qemu_mutex_lock(&vreader_list_mutex);
+    g_mutex_lock(&vreader_list_mutex);
 }
 
 static void
 vreader_list_unlock(void)
 {
-    qemu_mutex_unlock(&vreader_list_mutex);
+    g_mutex_unlock(&vreader_list_mutex);
 }
 
 static VReaderList *