public void TestNoInit()
        {
            int x    = 0;
            var init = DeferredInitializer.Create(() => x = 3);

            Assert.AreEqual(x, 0);
            Assert.IsFalse(init.HasInitialized);
        }
예제 #2
0
        /// <summary>
        /// Creates a type that is the decoded version of a node.
        /// </summary>
        /// <param name="node">The node to decode.</param>
        /// <param name="decoder">The decoder to use.</param>
        internal IrType(LNode node, DecoderState decoder)
            : base(node, decoder)
        {
            var typeParamDecoder = decoder.WithScope(new TypeParent(this));

            this.genericParameterCache = new Lazy <IReadOnlyList <IGenericParameter> >(() =>
                                                                                       node.Args[1].Args.EagerSelect(typeParamDecoder.DecodeGenericParameterDefinition));
            this.baseTypeCache = new Lazy <IReadOnlyList <IType> >(() =>
                                                                   node.Args[2].Args.EagerSelect(decoder.DecodeType));
            this.initializer = DeferredInitializer.Create(DecodeMembers);
        }
        public void TestSimpleInit()
        {
            int x    = 0;
            var init = DeferredInitializer.Create(() => x = 3);

            Assert.IsFalse(init.HasInitialized);
            init.Initialize();
            Assert.AreEqual(x, 3);
            Assert.IsTrue(init.HasInitialized);
            x = 0;
            init.Initialize();
            Assert.AreEqual(x, 0);
            Assert.IsTrue(init.HasInitialized);
        }
예제 #4
0
        /// <summary>
        /// Creates a Flame IR assembly from an appropriately-encoded
        /// LNode.
        /// </summary>
        /// <param name="node">The node to decode.</param>
        /// <param name="decoder">The decoder to use.</param>
        public IrAssembly(LNode node, DecoderState decoder)
            : base(node, decoder)
        {
            this.typeInitializer = DeferredInitializer.Create(() => {
                var newResolver = decoder.TypeResolver.CreateMutableCopy();
                var newDecoder  = new DecoderState(
                    decoder.Log,
                    newResolver.ReadOnlyView,
                    decoder.Codec)
                                  .WithScope(new TypeParent(this));

                this.typeCache = this.Node.Args[1].Args.EagerSelect(newDecoder.DecodeTypeDefinition);
                newResolver.AddAssembly(this);
            });
        }
        public void RecursiveInit()
        {
            var integer = new Box <int>(0);
            DeferredInitializer init;

            // This test makes sure that Initialize() does not recurse:
            // calling Initialize() from Initialize() does nothing at all.
            // This is by design, because it allows initialization to touch
            // other things that require initialization.
            init = DeferredInitializer.Create <Box <int> >(
                integer,
                x => { x.Value++; init.Initialize(); });
            Assert.IsFalse(init.HasInitialized);
            init.Initialize();
            Assert.IsTrue(init.HasInitialized);
            Assert.AreEqual(integer.Value, 1);
        }
 public void TestMultithreadedInit()
 {
     for (int j = 0; j < 10; j++)
     {
         var integer = new Box <int>(0);
         var init    = DeferredInitializer.Create <Box <int> >(integer, x => x.Value++);
         Assert.IsFalse(init.HasInitialized);
         var tasks = new List <Task>();
         for (int i = 0; i < 20; i++)
         {
             tasks.Add(Task.Run(() => init.Initialize()));
         }
         Task.WaitAll(tasks.ToArray());
         Assert.IsTrue(init.HasInitialized);
         Assert.AreEqual(integer.Value, 1);
     }
 }
예제 #7
0
 /// <summary>
 /// Creates a deferred initializer object from an initializer
 /// function that is run in a single-threaded fashion with
 /// respect to other functions operating on this assembly.
 /// </summary>
 /// <param name="func">
 /// The initialization function to run synchronously.
 /// </param>
 /// <returns>
 /// A deferred initializer object.
 /// </returns>
 public DeferredInitializer CreateSynchronizedInitializer(Action func)
 {
     return(DeferredInitializer.Create(() => RunSynchronized(func)));
 }