Yet another locking question



	Hi all,
here is my question related to locking issues. What I have in mind is to 
use gtk_idle_add to insure that UI stuff will take place within the main 
thread. That is : I think that the gtk+ loop (which is in the main thread) 
will always call the idle handlers, so idle handlers are always run inside 
the main thread.

For the cert asking code, here is what I'd propose :

int
libmutt_ask_for_cert_acceptance(X509 *cert)
{
     static pthread_mutex_t ask_cert_lock = PTHREAD_MUTEX_INITIALIZER;
     AskCertData acd;

     if (pthread_self() == libbalsa_get_main_thread()) {
         int res;
	gdk_thread_enter();
	res = ask_cert_real(cert);
	gdk_threads_leave();
	return res;
     }


     pthread_mutex_lock(&ask_cert_lock);
     pthread_cond_init(&acd.cond, NULL);
     acd.cert = cert;
     gtk_idle_add(ask_cert_idle, &acd);
     pthread_cond_wait(&acd.cond, &ask_cert_lock);
         pthread_cond_destroy(&acd.cond);
     pthread_mutex_unlock(&ask_cert_lock);
     pthread_mutex_destroy(&ask_cert_lock);
     return acd.res;
}

If we are yet in the main thread we need to grab the gdk lock before 
calling the real func (we do not have the gdk lock before).
Else we are in another thread and we set the idle handler : we do not need 
to mess with the lock because the gdk lock is already released. So no more 
risky libmutt unlocking (we have it yet).

Bye
Manu



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