[gnutls-dev] Re: living without global variables

Simon Josefsson jas at extundo.com
Mon Dec 26 18:59:09 CET 2005


Nikos Mavrogiannopoulos <nmav at gnutls.org> writes:

> On Sunday 25 December 2005 15:13, Simon Josefsson wrote:
>
>> Perhaps GnuTLS should deallocate these variables in an `atexit' hook
>> instead.  Then it seem all of this problem would go away.  In GNU
>> libc, atexit hooks are even called if GnuTLS was dlopen+dlclose'd.
>> gnutls_global_deinit() would then only call gc_done().
>
> I don't see how this can be usefull. Adding a sentance in the 
> global_init() function that it is not thread safe I think should be
> sufficient. This is a one time call initialization function, so it is 
> perfectly ok to leave the thread stuff to the called application.

Agreed.  I have installed the patch below.

Thanks,
Simon

--- gnutls_global.c	23 Dec 2005 15:49:27 +0100	2.110
+++ gnutls_global.c	26 Dec 2005 18:58:22 +0100	
@@ -171,6 +171,21 @@
   * you must do it before calling this function. This is useful in cases you 
   * want to disable libgcrypt's internal lockings etc.
   *
+  * This function increment a global counter, so that
+  * gnutls_global_deinit() only releases resources when it has been
+  * called as many times as gnutls_global_init().  This is useful when
+  * GnuTLS is used by more than one library in an application.  This
+  * function can be called many times, but will only do something the
+  * first time.
+  *
+  * Note!  This function is not thread safe.  If two threads call this
+  * function simultaneously, they can cause a race between checking
+  * the global counter and incrementing it, causing both threads to
+  * execute the library initialization code.  That would lead to a
+  * memory leak.  To handle this, your application could invoke this
+  * function after aquiring a thread mutex.  To ignore the potential
+  * memory leak is also an option.
+  *
   **/
 int
 gnutls_global_init (void)
@@ -265,8 +280,10 @@
   * This function deinitializes the global data, that were initialized
   * using gnutls_global_init().
   *
+  * Note!  This function is not thread safe.  See the discussion for
+  * gnutls_global_init() for more information.
+  *
   **/
-
 void
 gnutls_global_deinit (void)
 {




More information about the Gnutls-devel mailing list