Example #1
0
        public void RacyAccess()
        {
            for (int k = 0; k < 1000; k++)
            {
                var owners           = new OwnedBuffer <byte> [128];
                var memories         = new Buffer <byte> [owners.Length];
                var reserves         = new DisposableReservation <byte> [owners.Length];
                var disposeSuccesses = new bool[owners.Length];
                var reserveSuccesses = new bool[owners.Length];

                for (int i = 0; i < owners.Length; i++)
                {
                    var array = new byte[1024];
                    owners[i]   = array;
                    memories[i] = owners[i].Buffer;
                }

                var dispose_task = Task.Run(() => {
                    for (int i = 0; i < owners.Length; i++)
                    {
                        try {
                            owners[i].Dispose();
                            disposeSuccesses[i] = true;
                        } catch (InvalidOperationException) {
                            disposeSuccesses[i] = false;
                        }
                    }
                });

                var reserve_task = Task.Run(() => {
                    for (int i = owners.Length - 1; i >= 0; i--)
                    {
                        try {
                            reserves[i]         = memories[i].Reserve();
                            reserveSuccesses[i] = true;
                        } catch (ObjectDisposedException) {
                            reserveSuccesses[i] = false;
                        }
                    }
                });

                Task.WaitAll(reserve_task, dispose_task);

                for (int i = 0; i < owners.Length; i++)
                {
                    Assert.False(disposeSuccesses[i] && reserveSuccesses[i]);
                }
            }
        }
Example #2
0
 /// <summary>
 /// Create a new PreservedBuffer structure.
 /// </summary>
 /// <param name="buffer"></param>
 public PreservedBuffer(Buffer <T> buffer)
 {
     Buffer       = buffer;
     _reservation = buffer.Reserve();
 }