Skip to content

Commit c6bc7dc

Browse files
committed
Clean up usage of AutoResetEvent (PORTABLE only)
- see #102
1 parent 8e01dc8 commit c6bc7dc

File tree

1 file changed

+34
-19
lines changed

1 file changed

+34
-19
lines changed

crypto/src/crypto/prng/ThreadedSeedGenerator.cs

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
using System.Threading.Tasks;
66
#endif
77

8+
using Org.BouncyCastle.Utilities;
9+
810
namespace Org.BouncyCastle.Crypto.Prng
911
{
1012
/**
@@ -71,36 +73,49 @@ private byte[] DoGenerateSeed(
7173
ThreadPool.QueueUserWorkItem(new WaitCallback(Run));
7274
#endif
7375

74-
for (int i = 0; i < end; i++)
76+
#if PORTABLE
77+
AutoResetEvent autoResetEvent = new AutoResetEvent(false);
78+
#endif
79+
80+
try
7581
{
76-
while (this.counter == last)
82+
for (int i = 0; i < end; i++)
7783
{
78-
try
84+
while (this.counter == last)
7985
{
86+
try
87+
{
8088
#if PORTABLE
81-
new AutoResetEvent(false).WaitOne(1);
89+
autoResetEvent.WaitOne(1);
8290
#else
83-
Thread.Sleep(1);
91+
Thread.Sleep(1);
8492
#endif
93+
}
94+
catch (Exception)
95+
{
96+
// ignore
97+
}
8598
}
86-
catch (Exception)
87-
{
88-
// ignore
89-
}
90-
}
9199

92-
last = this.counter;
100+
last = this.counter;
93101

94-
if (fast)
95-
{
96-
result[i] = (byte)last;
97-
}
98-
else
99-
{
100-
int bytepos = i / 8;
101-
result[bytepos] = (byte)((result[bytepos] << 1) | (last & 1));
102+
if (fast)
103+
{
104+
result[i] = (byte)last;
105+
}
106+
else
107+
{
108+
int bytepos = i / 8;
109+
result[bytepos] = (byte)((result[bytepos] << 1) | (last & 1));
110+
}
102111
}
103112
}
113+
finally
114+
{
115+
#if PORTABLE
116+
autoResetEvent.Close();
117+
#endif
118+
}
104119

105120
this.stop = true;
106121

0 commit comments

Comments
 (0)