Example #1
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);
        }
Example #2
0
        private static Task CreateTaskWithResourceLockAndUnlockResource(int id, SimpleTaskScheduler.ExecutionToken token, Object resource)
        {
            const int NUM_ITERATIONS         = 10;
            int       twoHundredMilliseconds = 200;
            Task      t = new Task(() =>
            {
                for (int i = 0; i < NUM_ITERATIONS; ++i)
                {
                    Task.Delay(twoHundredMilliseconds).Wait();
                    Console.WriteLine($"id: {id}, iteration: {i}");

                    if (i == 1)
                    {
                        int val = token.LockResource(resource);
                        if (val == 0)
                        {
                            Console.WriteLine("Task with id: " + id + " succeeded getting the 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 == 7)
                    {
                        token.UnlockResource(resource);
                    }

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

            return(t);
        }