Ejemplo n.º 1
0
        private static Task CreateTaskWithResourceLock(int id, SimpleTaskScheduler.ExecutionToken token, Object resource1, Object resource2)
        {
            Task t = new Task(() =>
            {
                int twoHundredMilliseconds = 200;
                for (int i = 0; i < 10; ++i)
                {
                    Task.Delay(twoHundredMilliseconds).Wait();
                    Console.WriteLine($"id: {id}, iteration: {i}");

                    if (i == 1)
                    {
                        Console.WriteLine("Task with id: " + id + " trying to get a resource");
                        int val = token.LockResource(resource1);
                        if (val == 0)
                        {
                            Console.WriteLine("Task with id: " + id + " succeeded getting resource ");
                        }
                        else if (val == 1)
                        {
                            Console.WriteLine("Task with id: " + id + " completed while requesting a resource ");
                            return;
                        }
                        else if (val == 2)
                        {
                            Console.WriteLine("Task with id: " + id + " couldn't lock a resource, would've deadlocked ");
                        }
                    }
                    if (i == 3)
                    {
                        Console.WriteLine("Task with id: " + id + " trying to get a resource");
                        int val = token.LockResource(resource2);
                        if (val == 0)
                        {
                            Console.WriteLine("Task with id: " + id + " succeeded getting resource ");
                        }
                        else if (val == 1)
                        {
                            Console.WriteLine("Task with id: " + id + " completed while requesting a resource ");
                            return;
                        }
                        else if (val == 2)
                        {
                            Console.WriteLine("Task with id: " + id + " couldn't lock a resource, would've deadlocked ");
                        }
                    }

                    if (token.IsCanceled)
                    {
                        return;
                    }
                    if (token.IsPaused)
                    {
                        EventWaitHandle.SignalAndWait(token.TaskPaused, token.TaskContinued);
                    }
                }
            });

            return(t);
        }
Ejemplo n.º 2
0
        private static Task CreateTaskWithResourceLock(int id, SimpleTaskScheduler.ExecutionToken token, Object resource1, Object resource2, List <int> listCount)
        {
            Task t = new Task(() =>
            {
                int twoHundredMilliseconds = 200;
                for (int i = 0; i < 10; ++i)
                {
                    Task.Delay(twoHundredMilliseconds).Wait();
                    Console.WriteLine($"id: {id}, iteration: {i}");

                    if (i == 1)
                    {
                        int val = token.LockResource(resource1);
                        if (val == 0)
                        {
                            listCount.Add(0);
                        }
                        else if (val == 1)
                        {
                            return;
                        }
                        else if (val == 2)
                        {
                        }
                    }
                    if (i == 3)
                    {
                        int val = token.LockResource(resource2);
                        if (val == 0)
                        {
                            listCount.Add(0);
                        }
                        else if (val == 1)
                        {
                            return;
                        }
                        else if (val == 2)
                        {
                        }
                    }

                    if (token.IsCanceled)
                    {
                        return;
                    }
                    if (token.IsPaused)
                    {
                        EventWaitHandle.SignalAndWait(token.TaskPaused, token.TaskContinued);
                    }
                }
            });

            return(t);
        }
Ejemplo n.º 3
0
        private static Task CreateTaskWithResourceLock(int id, SimpleTaskScheduler.ExecutionToken token, Object resource)
        {
            Task t = new Task(() =>
            {
                int twoHundredMilliseconds = 200;
                for (int i = 0; i < 10; ++i)
                {
                    Task.Delay(twoHundredMilliseconds).Wait();
                    Console.WriteLine($"id: {id}, iteration: {i}");

                    if (i == 1)
                    {
                        token.LockResource(resource);
                    }
                    if (i == 7)
                    {
                        token.UnlockResource(resource);
                    }

                    if (token.IsCanceled)
                    {
                        return;
                    }
                    if (token.IsPaused)
                    {
                        EventWaitHandle.SignalAndWait(token.TaskPaused, token.TaskContinued);
                    }
                }
            });

            return(t);
        }