push yarrow locking down into the yarrow code to reduce contention
authorKen Raeburn <raeburn@mit.edu>
Sat, 30 Oct 2004 00:56:30 +0000 (00:56 +0000)
committerKen Raeburn <raeburn@mit.edu>
Sat, 30 Oct 2004 00:56:30 +0000 (00:56 +0000)
This seems to speed up creating a krb5_context a little bit, when it happens
a lot in multiple threads.

* prng.c (yarrow_lock): Rename to krb5int_yarrow_lock via macro, and change to
be non-static.
(krb5int_prng_init): Call do_yarrow_init here.
(krb5_c_random_add_entropy): Don't call it here.  Don't lock the mutex, either.
(krb5_c_random_make_octets): Don't lock the mutex.
* yarrow/ylock.h: Include k5-thread.h.
(krb5int_yarrow_lock): Declare.
(LOCK, UNLOCK): Define as macros using the k5_mutex_ macros.

ticket: new

git-svn-id: svn://anonsvn.mit.edu/krb5/trunk@16846 dc483132-0cff-0310-8789-dd5450dbe970

src/lib/crypto/ChangeLog
src/lib/crypto/prng.c
src/lib/crypto/yarrow/ChangeLog
src/lib/crypto/yarrow/ylock.h

index c283504610110d377d24bc0cafea41f412858c75..b929e89d7577ea53359df376c97e4c0d0002f22c 100644 (file)
@@ -1,3 +1,12 @@
+2004-10-29  Ken Raeburn  <raeburn@mit.edu>
+
+       * prng.c (yarrow_lock): Rename to krb5int_yarrow_lock via macro,
+       and change to be non-static.
+       (krb5int_prng_init): Call do_yarrow_init here.
+       (krb5_c_random_add_entropy): Don't call it here.  Don't lock the
+       mutex, either.
+       (krb5_c_random_make_octets): Don't lock the mutex.
+
 2004-06-16  Ken Raeburn  <raeburn@mit.edu>
 
        * Makefile.in (MAC_SUBDIRS): Don't set.
index f9ea8696dcb1c0d9ad71792c78c52f68c1b92b0f..338eaf0dd47f9243cecfd2e8e2614f29f04e47ba 100644 (file)
@@ -31,7 +31,8 @@
 #include "yarrow.h"
 static Yarrow_CTX y_ctx;
 static int inited, init_error;
-static k5_mutex_t yarrow_lock = K5_MUTEX_PARTIAL_INITIALIZER;
+#define yarrow_lock krb5int_yarrow_lock
+k5_mutex_t yarrow_lock = K5_MUTEX_PARTIAL_INITIALIZER;
 
 /* Helper function to estimate entropy based on sample length
  * and where it comes from.
@@ -56,8 +57,12 @@ entropy_estimate (unsigned int randsource, size_t length)
 return (0);
 }
 
+static void do_yarrow_init(void);
 int krb5int_prng_init(void)
 {
+    do_yarrow_init();
+    if (init_error)
+       return KRB5_CRYPTO_INTERNAL;
     return k5_mutex_finish_init(&yarrow_lock);
 }
 
@@ -95,21 +100,11 @@ krb5_c_random_add_entropy (krb5_context context, unsigned int randsource,
   if (yerr)
       return yerr;
   /* Now, finally, feed in the data.  */
-  yerr = k5_mutex_lock(&yarrow_lock);
-  if (yerr)
-      return yerr;
-  if (!inited)
-      do_yarrow_init();
-  if (init_error) {
-      k5_mutex_unlock(&yarrow_lock);
-      return KRB5_CRYPTO_INTERNAL;
-  }
   yerr = krb5int_yarrow_input (&y_ctx, randsource,
                               data->data, data->length,
                               entropy_estimate (randsource, data->length));
-  k5_mutex_unlock(&yarrow_lock);
   if (yerr != YARROW_OK)
-    return (KRB5_CRYPTO_INTERNAL);
+      return (KRB5_CRYPTO_INTERNAL);
   return (0);
 }
 
@@ -124,16 +119,12 @@ krb5_c_random_make_octets(krb5_context context, krb5_data *data)
 {
     int yerr;
     assert (inited);
-    yerr = k5_mutex_lock(&yarrow_lock);
-    if (yerr)
-       return yerr;
     yerr = krb5int_yarrow_output (&y_ctx, data->data, data->length);
     if (yerr == YARROW_NOT_SEEDED) {
       yerr = krb5int_yarrow_reseed (&y_ctx, YARROW_SLOW_POOL);
       if (yerr == YARROW_OK)
        yerr = krb5int_yarrow_output (&y_ctx, data->data, data->length);
     }
-    k5_mutex_unlock(&yarrow_lock);
     if ( yerr != YARROW_OK)
       return (KRB5_CRYPTO_INTERNAL);
     return(0);
index 38d6fe7c635d1b1b9dc13c6279dfa5feae58afac..bab1a67b4b778a699d2a7433d3164109464bd34e 100644 (file)
@@ -1,3 +1,9 @@
+2004-10-29  Ken Raeburn  <raeburn@mit.edu>
+
+       * ylock.h: Include k5-thread.h.
+       (krb5int_yarrow_lock): Declare.
+       (LOCK, UNLOCK): Define as macros using the k5_mutex_ macros.
+
 2004-06-04  Ken Raeburn  <raeburn@mit.edu>
 
        * yarrow.c (yarrow_str_error): Now const.
index 21d3911b7766f73f10208990ee1aa87bd7e309f4..9c032dc61da3e19777315cf8e59ca1500ad48a01 100644 (file)
  *    and YARROW_LOCKING on failure
  */
 
-
+#if 0
 static int LOCK( void ) {  return (YARROW_OK); }
 static int UNLOCK( void ) {  return (YARROW_OK); }
+#else
+#include "k5-thread.h"
+extern k5_mutex_t krb5int_yarrow_lock;
+#define LOCK() (k5_mutex_lock(&krb5int_yarrow_lock) ? YARROW_LOCKING : YARROW_OK)
+#define UNLOCK() (k5_mutex_unlock(&krb5int_yarrow_lock) ? YARROW_LOCKING : YARROW_OK)
+#endif
 
 #endif /* YLOCK_H */