[GLIB] Async calls to Sync calls with g_main_context_iteration



Hi,

I am very new to GLIB so please excuse me if I made any stupid assumptions...

I did a lot of reading and testing my understanding but I feel a bit stuck. I feel like I am missing something obvious, or trying to use GLIB in the wrong way.

I am using GLIB in my applications (consisting of 3 separate processes) originally because I need DBUS access to Ofono and Connman (and between each other to implement shared variables) on our embedded Linux environment. 

(1) The first issue came when I realised that making synchronous DBUS calls between processes can result in a deadlock when two processes make DBUS requests to each other each waiting for the synchronous call to complete, which is preventing the GLIB event loop from servicing the DBUS request. 

   generated_com_mixtelematics_parameters_call_get_parameter_sync(..)

   =>

   generated_com_mixtelematics_parameters_call_get_parameter(..)

I changed all the DBUS calls to use the asynchronous calls. 

(2) The second issue came when I realized our application framework requires synchronous calls mapping to the underlying asynchronous GLIB requests, so I implemented wrapper functions which waits on an async_queue_pop() to wait for the result, before returning to the caller.

   // Async
   generated_com_mixtelematics_parameters_call_get_parameter(..)
   
   g_async_queue_pop(callbackParameters.semaphore);

However, this introduced another issue. If this synchronous wrapper is called from the GLIB main context thread, it would deadlock. The blocking call to pop() would not release the event loop to service the async DBUS call.

(3) This is where I am getting stuck chasing my own tail ... 

The problem is I would like the API call to be possible _from_ the GLIB event thread, but also _outside_ from another thread. The case in (2) only works from outside threads. 

So I added this (please do not shoot me):

   while(g_async_queue_try_pop(context.semaphore) == NULL)
   {
      g_main_context_iteration(NULL,TRUE/FALSE);
   }

(A) It seems like this call can not work from outside the GLIB event thread.

(B) If I call it work TRUE=blocking, it gets stuck in an outside thread

(C) If I call it with FALSE=non blocking, it spins

So it looks like I am going down the wrong path here. In the event thread it would work, and in an outside thread I need to use (2).

Any hints would me very much appreciated.

Kind Regards,
Frederik Lotter

MiX Telematics
South Africa


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]