static void Foo()
        {
            nekara.StartTask(1);

            Console.WriteLine("Foo - writing to Bar Inbox");
            object message = new object();

            nekara.Assert(barInbox != null, "Bar socket not ready");
            barInbox.Write(message);

            Console.WriteLine("Foo - ContextSwitch");
            nekara.ContextSwitch();

            Console.WriteLine("Foo - reading from Foo Inbox");
            object response = fooInbox.Read();

            nekara.Assert(response == message, "Bug found!");

            Console.WriteLine("Foo - writing to Bar Inbox 2");
            message = new object();
            nekara.Assert(barInbox != null, "Bar socket not ready");
            barInbox.Write(message);

            Console.WriteLine("Foo - ContextSwitch 2");
            nekara.ContextSwitch();

            Console.WriteLine("Foo - reading from Foo Inbox 2");
            response = fooInbox.Read();

            nekara.Assert(response == message, "Bug found!");

            Console.WriteLine("Foo EndTask");
            nekara.EndTask(1);
        }
예제 #2
0
        static void RunVanilla()
        {
            balance = 0;

            var t1 = Task.Run(() => Transact(100));

            var t2 = Task.Run(() => Transact(200));

            Models.Task.Run(() => Task.WaitAll(t1, t2));

            nekara.Assert(balance == 300, $"Bug Found! Balance does not equal 300 - it is {balance}");
        }
예제 #3
0
파일: Lock.cs 프로젝트: p-org/nekara-csharp
        public void Release()
        {
            Api.Assert(this.locked == true, "Release called on non-acquired lock");

            this.locked = false;
            Api.SignalUpdatedResource(this.id);
        }
        static void Foo(int taskId)
        {
            nekara.StartTask(taskId);

            Console.WriteLine("Foo({0})/Acquire()", taskId);
            lck.Acquire();

            Console.WriteLine("Foo({0})/ContextSwitch():0", taskId);
            nekara.ContextSwitch();
            x = taskId;

            Console.WriteLine("Foo({0})/ContextSwitch():1", taskId);
            nekara.ContextSwitch();
            int lx1 = x;

            Console.WriteLine("Foo({0})/ContextSwitch():2", taskId);
            nekara.ContextSwitch();
            int lx2 = x;

            Console.WriteLine("Foo({0})/Release()", taskId);
            if (taskId != 1)
            {
                lck.Release();
            }

            nekara.Assert(lx1 == lx2, "Race!");

            Console.WriteLine("Foo({0})/EndTask()", taskId);
            nekara.EndTask(taskId);
        }
예제 #5
0
파일: LockGrain.cs 프로젝트: naman/Nekara
        public Task Release()
        {
            Api.Assert(this.locked == true, "Release called on non-acquired lock");

            this.locked = false;
            Api.SignalUpdatedResource(this.id);

            return(Task.CompletedTask);
        }
예제 #6
0
        static void Foo()
        {
            Console.WriteLine("Foo/Acquire()");
            Acquire();

            Console.WriteLine("Foo/ContextSwitch()");
            nekara.ContextSwitch();
            int lx1 = x;

            Console.WriteLine("Foo/ContextSwitch()");
            nekara.ContextSwitch();
            int lx2 = x;

            Console.WriteLine("Foo/Release()");
            Release();

            nekara.Assert(lx1 == lx2, "Race!");

            Console.WriteLine("Foo EndTask");
        }
예제 #7
0
        static void Release(int taskId)
        {
            Console.WriteLine("Task {0}: Release()", taskId);
            nekara.Assert(lck == true, "Release called on non-acquired lock");

            //nekara.ContextSwitch();
            lck = false;
            //nekara.ContextSwitch();
            nekara.SignalUpdatedResource(0);
            //nekara.ContextSwitch();
        }
예제 #8
0
        private void BCSP_PnpAdd(DeviceExtension e)
        {
            int status = BCSP_IoIncrement(e);

            if (status == 0)
            {
                // Do work here.
                nekara.ContextSwitch();
                nekara.Assert(!this.Stopped, "Bug found!");
            }

            BCSP_IoDecrement(e);
        }
예제 #9
0
        public Task Run()
        {
            this.Buffer     = new char[10];
            this.BufferSize = 10;
            this.First      = 0;
            this.Next       = 0;
            this.Send       = true;
            this.Receive    = false;
            int n = 7;

            var l = new Lock(1);

            Task t1 = Task.Run(() =>
            {
                for (int i = 0; i < n; i++)
                {
                    nekara.ContextSwitch();
                    using (l.Acquire())
                    {
                        if (this.Send)
                        {
                            InsertLogElement(i);
                            this.Send    = false;
                            this.Receive = true;
                        }
                    }
                }
            });

            Task t2 = Task.Run(() =>
            {
                for (int i = 0; i < n; i++)
                {
                    nekara.ContextSwitch();
                    using (l.Acquire())
                    {
                        if (this.Receive)
                        {
                            nekara.Assert(RemoveLogElement() == i, "Bug found!");
                            this.Receive = false;
                            this.Send    = true;
                        }
                    }
                }
            });

            return(Task.WhenAll(t1, t2));
        }
예제 #10
0
        public static Task Run()
        {
            var emitter0 = client.GetGrain <IEmitterGrain>(0);
            var emitter1 = client.GetGrain <IEmitterGrain>(1);

            var t0 = emitter0.Emit();
            var t1 = emitter1.Emit();

            var r1 = t0.Result;
            var r2 = t1.Result;

            Console.WriteLine("r1 = {0}, r2 = {1}", r1, r2);

            nekara.Assert(r1 < r2, "Emitter2 Went First");

            return(Task.CompletedTask);
        }
예제 #11
0
        public Task Run()
        {
            int size = 10;

            int[] stack = new int[size];
            bool  flag  = false;

            var l = new Lock(0);

            Task t1 = Task.Run(() =>
            {
                for (int i = 0; i < size; i++)
                {
                    nekara.ContextSwitch();
                    using (l.Acquire())
                    {
                        this.Push(stack, i);
                        flag = true;
                    }
                    nekara.ContextSwitch();
                }
            });

            Task t2 = Task.Run(() =>
            {
                for (int i = 0; i < size; i++)
                {
                    nekara.ContextSwitch();
                    using (l.Acquire())
                    {
                        nekara.ContextSwitch();
                        if (flag)
                        {
                            nekara.Assert(this.Pop(stack) != -2, "Bug found!");
                        }
                    }
                }
            });

            return(Task.WhenAll(t1, t2));
        }
예제 #12
0
        public async NativeTasks.Task Run()
        {
            // runtime.RegisterMonitor(typeof(StateMonitor));
            // Runtime = runtime;

            int numTasks = 5;
            var stack    = new SafeStack(numTasks);

            Task[] tasks = new Task[numTasks];
            for (int i = 0; i < numTasks; i++)
            {
                tasks[i] = Task.Run(async() =>
                {
                    int id = i;
                    //Runtime.Logger.WriteLine($"Starting task {id}.");
                    for (int j = 0; j != 2; j += 1)
                    {
                        int elem = await stack.PopAsync(id);
                        if (elem <= 0)
                        {
                            nekara.ContextSwitch();
                            continue;
                        }

                        stack.Array[elem].Value = id;
                        // Runtime.Logger.WriteLine($"Task {id} popped item '{elem}' and writes value '{id}'.");
                        // Runtime.InvokeMonitor<StateMonitor>(new StateMonitor.UpdateStateEvent(stack.Array));
                        nekara.ContextSwitch();
                        nekara.Assert(stack.Array[elem].Value == id,
                                      $"Task {id} found bug: [{elem}].{stack.Array[elem].Value} is not '{id}'!");

                        await stack.PushAsync(id, elem);
                    }
                });
            }

            await Task.WhenAll(tasks);
        }
예제 #13
0
        public Task Run()
        {
            this.Size = 20;
            int[] storedElements = new int[this.Size];

            QType queue = new QType
            {
                Element = new int[this.Size],
                Head    = 0,
                Tail    = 0,
                Amount  = 0
            };

            bool enqueue = true;
            bool dequeue = false;

            var l = new Lock(1);

            Task t1 = Task.Run(() =>
            {
                int value;

                nekara.ContextSwitch();
                using (l.Acquire())
                {
                    nekara.ContextSwitch();
                    value = 0;

                    nekara.ContextSwitch();
                    storedElements[0] = value;
                }

                for (int i = 0; i < (this.Size - 1); i++)
                {
                    nekara.ContextSwitch();
                    using (l.Acquire())
                    {
                        nekara.ContextSwitch();
                        if (enqueue)
                        {
                            nekara.ContextSwitch();
                            value++;

                            nekara.ContextSwitch();
                            this.Enqueue(queue, value);

                            nekara.ContextSwitch();
                            storedElements[i + 1] = value;

                            nekara.ContextSwitch();
                            enqueue = false;

                            nekara.ContextSwitch();
                            dequeue = true;
                        }
                    }
                }
            });

            Task t2 = Task.Run(() =>
            {
                for (int i = 0; i < this.Size; i++)
                {
                    nekara.ContextSwitch();
                    using (l.Acquire())
                    {
                        nekara.ContextSwitch();
                        if (dequeue)
                        {
                            nekara.Assert(this.Dequeue(queue) == storedElements[i], "<Queue> Bug found!");
                            nekara.ContextSwitch();
                            dequeue = false;

                            nekara.ContextSwitch();
                            enqueue = true;
                        }
                    }
                }
            });

            return(Task.WhenAll(t1, t2));
        }
예제 #14
0
        public static void Run()
        {
            var tasks = Dine(N);
            var all   = Task.WhenAll(tasks);

            all.ContinueWith(prev => {
                nekara.Assert(phil == N, $"Bug found! Only {phil} philosophers ate");
            });
            return;
        }