summary refs log tree commit diff
path: root/crypto
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2018-10-09 15:48:50 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2018-10-09 15:48:50 +0700
commitc6bc7dc1fc2fc0c932b09a6f20d64cbf51e45375 (patch)
treeffb4e3efc0342661212cffe623ee29db861b1c67 /crypto
parentRestore previous constructors (as Obsolete) for backward compatibility (diff)
downloadBouncyCastle.NET-ed25519-c6bc7dc1fc2fc0c932b09a6f20d64cbf51e45375.tar.xz
Clean up usage of AutoResetEvent (PORTABLE only)
- see https://github.com/bcgit/bc-csharp/issues/102
Diffstat (limited to 'crypto')
-rw-r--r--crypto/src/crypto/prng/ThreadedSeedGenerator.cs53
1 files changed, 34 insertions, 19 deletions
diff --git a/crypto/src/crypto/prng/ThreadedSeedGenerator.cs b/crypto/src/crypto/prng/ThreadedSeedGenerator.cs
index 0a38e5f5a..7412e4919 100644
--- a/crypto/src/crypto/prng/ThreadedSeedGenerator.cs
+++ b/crypto/src/crypto/prng/ThreadedSeedGenerator.cs
@@ -5,6 +5,8 @@ using System.Threading;
 using System.Threading.Tasks;
 #endif
 
+using Org.BouncyCastle.Utilities;
+
 namespace Org.BouncyCastle.Crypto.Prng
 {
     /**
@@ -71,36 +73,49 @@ namespace Org.BouncyCastle.Crypto.Prng
                 ThreadPool.QueueUserWorkItem(new WaitCallback(Run));
 #endif
 
-                for (int i = 0; i < end; i++)
+#if PORTABLE
+                AutoResetEvent autoResetEvent = new AutoResetEvent(false);
+#endif
+
+                try
                 {
-                    while (this.counter == last)
+                    for (int i = 0; i < end; i++)
                     {
-                        try
+                        while (this.counter == last)
                         {
+                            try
+                            {
 #if PORTABLE
-                            new AutoResetEvent(false).WaitOne(1);
+                                autoResetEvent.WaitOne(1);
 #else
-                            Thread.Sleep(1);
+                                Thread.Sleep(1);
 #endif
+                            }
+                            catch (Exception)
+                            {
+                                // ignore
+                            }
                         }
-                        catch (Exception)
-                        {
-                            // ignore
-                        }
-                    }
 
-                    last = this.counter;
+                        last = this.counter;
 
-                    if (fast)
-                    {
-                        result[i] = (byte)last;
-                    }
-                    else
-                    {
-                        int bytepos = i / 8;
-                        result[bytepos] = (byte)((result[bytepos] << 1) | (last & 1));
+                        if (fast)
+                        {
+                            result[i] = (byte)last;
+                        }
+                        else
+                        {
+                            int bytepos = i / 8;
+                            result[bytepos] = (byte)((result[bytepos] << 1) | (last & 1));
+                        }
                     }
                 }
+                finally
+                {
+#if PORTABLE
+                    autoResetEvent.Close();
+#endif
+                }
 
                 this.stop = true;