File size: 8,196 Bytes
b1b3bae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/// ------------------------------------------------------
/// RandomOps - (Pseudo) Random Number Generator For C#
/// Copyright (C) 2003-2010 Magnus Erik Hvass Pedersen.
/// Please see the file license.txt for license details.
/// RandomOps on the internet: http://www.Hvass-Labs.org/
/// ------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Threading;

namespace RandomOps
{
    /// <summary>
    /// A version of the ByteStream RNG base-class which works
    /// asynchronously by creating its own thread on which the
    /// buffer refilling is executed. This allows for immediate use
    /// of the fallback RNG instead of waiting for the buffer to
    /// be refilled, and is preferred for time-critical uses.
    /// </summary>
    /// <remarks>
    /// Be sure to call Dispose() after you are done using this RNG,
    /// so the worker-thread can be shut down properly. Also note
    /// that this class is not thread-safe with regard to multiple
    /// threads calling e.g. the Uniform() method simultaneously.
    /// To make it thread-safe in this manner, wrap it in a
    /// ThreadSafe-object.
    /// </remarks>
    public abstract class ByteStreamAsync : ByteStream, IDisposable
    {
        #region Constructor.
        /// <summary>
        /// Construct the RNG-object.
        /// </summary>
        /// <param name="size">Buffer-size (in bytes).</param>
        /// <param name="retrieveTrigger">Refill buffer asynchronously when its size falls below this.</param>
        /// <param name="randFallback">Fallback RNG to be used when buffer is empty.</param>
        /// <param name="numFallback">Use fallback RNG for this many bytes before trying to fill buffer again.</param>
        public ByteStreamAsync(int size, int retrieveTrigger, Random randFallback, int numFallback)

            : base(size, randFallback, numFallback)
        {
            RetrieveTrigger = retrieveTrigger;

            _workerThread = new Thread(Work);
            _workerThread.Start();
        }
        #endregion

        #region Availability status.
        /// <summary>
        /// Determine if the requested number of bytes are available, if not
        /// then refill buffer asynchronously. Use Fallback RNG in the meantime.
        /// </summary>
        /// <param name="numBytes">Number of bytes requested.</param>
        /// <returns>Boolean indicating whether the requested number of bytes are available.</returns>
        protected override bool IsAvailable(int numBytes)
        {
            int queueCount = Queue.Count;

            if (FallbackCount > 0)
            {
                // Decrease the fallback-counter, thread-safe.
                lock (FallbackCountLock)
                {
                    FallbackCount = System.Math.Max(0, FallbackCount - numBytes);
                }
            }
            else if (queueCount < numBytes || queueCount < RetrieveTrigger)
            {
                // Resize buffer if more bytes are suddenly requested than it
                // has capacity for.
                if (BufferSize < numBytes)
                {
                    BufferSize = numBytes;
                }

                // Wake-up async. worker to refill the buffer.
                WakeupWorker();
            }

            return (queueCount >= numBytes);
        }
        #endregion

        #region Internal variables.
        /// <summary>
        /// Refill buffer asynchronously when its size falls below this.
        /// </summary>
        int RetrieveTrigger;

        /// <summary>
        /// Lock used for thread-safe updating of FallbackCount.
        /// </summary>
        Object FallbackCountLock = new Object();
        #endregion

        #region Worker Thread.
        /// <summary>
        /// The thread on which the buffer-filling is to be executed.
        /// </summary>
        Thread _workerThread;

        /// <summary>
        /// The signal used for waking up the worker-thread when it
        /// must fill the buffer.
        /// </summary>
        EventWaitHandle _waitHandle = new AutoResetEvent(false);

        /// <summary>
        /// Should worker-thread abort execution?
        /// </summary>
        bool _abortPending = false;

        /// <summary>
        /// Eternal-worker method.
        /// </summary>
        void Work()
        {
            while (!_abortPending)
            {
                // Fill the buffer with random bytes.
                FillBuffer();

                // Wait until buffer needs to be refilled.
                _waitHandle.WaitOne();
            }
        }

        /// <summary>
        /// Wakeup the worker so as to fill buffer.
        /// </summary>
        void WakeupWorker()
        {
            _waitHandle.Set();
        }
        #endregion

        #region IDisposable implementation.
        public void Dispose()
        {
            // Flag as abort pending.
            _abortPending = true;

            // Wakeup worker-thread so it will run until completion.
            WakeupWorker();

            // Join with worker-thread.
            _workerThread.Join();

            // Close the event-signalling.
            _waitHandle.Close();
        }
        #endregion

        #region Fill buffer.
        /// <summary>
        /// Same as for the basic ByteStream-class only the FallbackCount
        /// is updated in a thread-safe lock.
        /// </summary>
        public sealed override void FillBuffer()
        {
            int fillCount = FillCount;

            try
            {
                while (fillCount > 0)
                {
                    DoFillBuffer(System.Math.Min(fillCount, MaxRetrieveLength));

                    fillCount -= MaxRetrieveLength;
                }
            }
            catch
            {
                // Various internet errors may occur. Simply do nothing
                // and let the fallback RNG generate numbers instead
                // when the byte-buffer is discovered to be empty.

                lock (FallbackCountLock)
                {
                    FallbackCount = NumFallback;
                }
            }
        }

        /// <summary>
        /// Thread-safe version of ByteStream.AddBuffer().
        /// </summary>
        /// <param name="buffer">The buffer of bytes to add.</param>
        protected override void AddBuffer(IEnumerable<byte> buffer)
        {
            lock (Queue)
            {
                foreach (byte b in buffer)
                {
                    Queue.Enqueue(b);
                }
            }
        }
        #endregion

        #region Base-class overrides.
        /// <summary>
        /// Draw a random boolean with equal probability of true or false.
        /// </summary>
        public sealed override byte Byte()
        {
            byte b;

            if (IsAvailableByte())
            {
                lock (Queue)
                {
                    b = Queue.Dequeue();
                }
            }
            else
            {
                b = RandFallback.Byte();
            }

            return b;
        }

        /// <summary>
        /// Draw an array of random and uniform bytes.
        /// </summary>
        /// <param name="length">The array length requested.</param>
        public sealed override byte[] Bytes(int length)
        {
            byte[] arr;

            if (IsAvailableBytes(length))
            {
                arr = new byte[length];

                lock (Queue)
                {
                    for (int i = 0; i < length; i++)
                    {
                        arr[i] = Queue.Dequeue();
                    }
                }
            }
            else
            {
                arr = RandFallback.Bytes(length);
            }

            return arr;
        }
        #endregion
    }
}