GNOME Bugzilla – Bug 415891
Use EFlag to simplify logic in evolution-data-server
Last modified: 2013-09-14 16:49:38 UTC
I'd like to propose a small addition to libedataserver called EFlag. EFlag is a very simple thread synchronization mechanism. It implements a thread-safe flag that can be blocked on. Internally it just wraps a GMutex, a GCond, and a gboolean. An EFlag is essentially a binary semaphore with a more intuitive interface. It's based on Python's threading.Event class [1]. I find myself implementing this little pattern quite frequently when doing concurrent programming, so I thought it would be nice to formalize an API around it. I'd like to utilize it in some refactoring work that I have planned for Evolution 2.11/2.12. Here's the API. See the patch for documentation. EFlag * e_flag_new (void); gboolean e_flag_is_set (EFlag *flag); void e_flag_set (EFlag *flag); void e_flag_clear (EFlag *flag); void e_flag_wait (EFlag *flag); gboolean e_flag_timed_wait (EFlag *flag, GTimeVal *abs_time); void e_flag_free (EFlag *flag); One example where this is useful is synchronizing messages between threads: Sender ------ msg = create_new_message (); ... populate message ... send_message (msg); e_flag_wait (msg->done); /* wait for message to be processed */ ... retrieve results ... free_message (msg); Receiver -------- msg = get_incoming_message (); ... process message ... e_flag_set (msg->done); /* notify sender */ [1] http://docs.python.org/lib/event-objects.html
Created attachment 84201 [details] [review] Proposed patch
Created attachment 84293 [details] [review] EFlag usage examples Here's a few examples in existing evolution-data-server code where an EFlag can be used to simplify thread synchronization.
Recent refactoring of e-passwords.c also uses EFlag (see bug #376991).
Also using it in the mailer to replace EMsgPort (see bug #362638).
And in the printing clean up (see bug #426816).
Varadhan, Srini says I need to have you review this. Would you please?
Committed the EFlag implementation to Subversion trunk (revision 7706). Varadhan wanted to give the other patch (containing EFlag usage examples) some more scrutiny, so I'm leaving this bug open until that patch gets a thumbs up or thumbs down.
FYI: I'm not yet patching Tinymail's camel-lite with this because I don't think I would be using it. I'd be okay if I would have to patch this in (or starting using it), though.
Camel shouldn't be using EFlag at all. My plan is to have Evolution and E-D-S use EFlag wherever it currently uses EMsgPort, so that EMsgPort can be moved to Camel (if that makes sense). Then that would be one less thing that Camel needs libedataserver for.
That would make sense, indeed
Changing summary to describe the remaining patch for this bug.
I'm happy for the addressbook patches to be committed.
Created attachment 92934 [details] [review] EFlag usage examples Here's an updated patch for the current Subversion trunk.
Committing based on Ross' approval in comment #12. Revision 7935.