1 // 2 // Copyright (c) 2010-2018 Antmicro 3 // Copyright (c) 2011-2015 Realtime Embedded 4 // 5 // This file is licensed under the MIT License. 6 // Full license text is available in 'licenses/MIT.txt'. 7 // 8 using System; 9 using System.Threading; 10 using Antmicro.Renode.Core; 11 12 namespace Antmicro.Renode.UnitTests.Mocks 13 { 14 public class ActivelyAskedPeripheral : EmptyPeripheral 15 { ActivelyAskedPeripheral()16 public ActivelyAskedPeripheral() 17 { 18 random = EmulationManager.Instance.CurrentEmulation.RandomGenerator; 19 } 20 21 public bool Failed 22 { 23 get 24 { 25 return failed; 26 } 27 } 28 ReadDoubleWord(long offset)29 public override uint ReadDoubleWord (long offset) 30 { 31 var value = Interlocked.Read (ref counter); 32 var toWait = random.Next (spinWaitIterations); 33 Thread.SpinWait (toWait); 34 var exchanged = Interlocked.Exchange(ref counter, ++value); 35 if(exchanged != value - 1) 36 { 37 failed = true; 38 } 39 return (uint)toWait; 40 } 41 42 private long counter; 43 private bool failed; 44 private readonly PseudorandomNumberGenerator random; 45 private const int spinWaitIterations = 10000; 46 } 47 } 48 49