public void SendReleaseMessage(DistributedMonitor monitor)
        {
            var message = MessageFactory.CreateMessageWithValuesPropagation(
                LamportTimeProvider.Instance.IncrementAndReturn(), MessageTypes.MonitorRelease, monitor);

            MessageSender.Instance.BrodcastMessage(message);
        }
        public void SendAcquireMessage(DistributedMonitor monitor)
        {
            bool succeded = false;
            var  message  = MessageFactory.CreateMessage(
                LamportTimeProvider.Instance.IncrementAndReturn(), monitor.ID, -1, -1, MessageTypes.MonitorAcquire);

            MessageHandler.MyCurrentMessage = message;
            while (!succeded)
            {
                monitor.IsPassClearOrWait();
                succeded = MessageSender.Instance.BrodcastMessageWithResult(message, MessageTypes.Acknowledgement);
            }
        }
示例#3
0
        public DistributedMonitor CreateMonitorIfNotExists(int id)
        {
            System.Threading.Monitor.Enter(_monitorsLock);

            var monitor = GetMonitor(id);

            if (monitor != null)
            {
                return(monitor);
            }

            monitor = new DistributedMonitor(id);

            Monitors.Add(monitor);

            System.Threading.Monitor.Exit(_monitorsLock);

            return(monitor);
        }
示例#4
0
        static void Main(string[] args)
        {
            var random = new Random();
            MonitorConfiguration config  = ConfigurationReader.Read(GetConfigPath());
            MonitorWrapper       wrapper = MonitorWrapper.Instance;

            wrapper.ApplyConfig(config);
            wrapper.Start();
            DistributedMonitor monitor = wrapper.CreateMonitorIfNotExists(1);

            // Przygotowanie zmiennej warunkowej
            monitor.Acquire();
            var cv = monitor.CreateConditionalVariableIfNotExists(1);

            if (cv.Value == null)
            {
                cv.Value = 0;
            }
            monitor.Release();

            if (wrapper.ID == 0 || wrapper.ID == 1 || wrapper.ID == 2)
            {
                while (true)
                {
                    //READERS
                    monitor.Acquire();
                    while (((int)cv.Value) < 0)
                    {
                        Console.WriteLine($"Wait {wrapper.ID} - pisarz w środku");
                        cv.Wait();
                    }
                    cv.Value = (int)cv.Value + 1;
                    monitor.Release();

                    Console.WriteLine($"Czytam {wrapper.ID}");
                    Thread.Sleep(random.Next(1000, 3000));
                    Console.WriteLine($"Skończyłem {wrapper.ID}");

                    monitor.Acquire();
                    cv.Value = (int)cv.Value - 1;
                    if ((int)cv.Value == 0)
                    {
                        cv.SignalAll();
                    }
                    monitor.Release();

                    Console.WriteLine($"Wyszedłem i czekam {wrapper.ID}");
                    Thread.Sleep(random.Next(3000, 5000));
                }
            }
            else
            {
                //WRITERS
                while (true)
                {
                    monitor.Acquire();
                    while (((int)cv.Value) != 0)
                    {
                        Console.WriteLine($"Wait {wrapper.ID} - ktoś w środku");
                        cv.Wait();
                    }
                    cv.Value = -1;
                    monitor.Release();

                    Console.WriteLine($"Piszę {wrapper.ID}");
                    Thread.Sleep(random.Next(1000, 3000));
                    Console.WriteLine($"Skończyłem {wrapper.ID}");

                    monitor.Acquire();
                    cv.Value = 0;
                    cv.SignalAll();
                    monitor.Release();

                    Console.WriteLine($"Wyszedłem i czekam {wrapper.ID}");
                    Thread.Sleep(random.Next(3000, 5000));
                }
            }
        }
        private void UpsertSourceMap(string fileName, string fileStoreKey, string path)
        {
            const string propertyName = Constants.ThemeStylesheetFiles;

            // Test if modified file is a stylesheet
            if (fileName.EndsWith(".css") && fileStoreKey == Constants.ThemeFiles && path.EndsWith(propertyName))
            {
                ICentralizedFileStorageProvider fileStore = CentralizedFileStorage.GetFileStore(fileStoreKey);
                string mapName = string.Concat(fileName, ".map");

                if (fileStore != null)
                {
                    ICentralizedFile mapFile = fileStore.GetFile(path, mapName);

                    // Check if source map exists
                    if (mapFile != null)
                    {
                        string writePath = GetSanitisedPath(_siteThemeTypeId, _themeContextId, _siteThemeName,
                            propertyName, new Uri(Globals.FullPath("~/")));

                        if (CentralizedFileStorage.GetFileStore(Constants.ThemeFiles).GetFile(writePath, mapName) == null)
                        {
                            lock (_lock)
                            {
                                if (CentralizedFileStorage.GetFileStore(Constants.ThemeFiles).GetFile(writePath, mapName) ==
                                    null)
                                {
                                    using (DistributedMonitor distributedMonitor = new DistributedMonitor())
                                    {
                                        const int limit = 5;

                                        for (int i = 0; i < limit; i++)
                                        {
                                            using (
                                                DistributedLock distributedLock =
                                                    distributedMonitor.Enter(Constants.DistributedMonitorKey))
                                            {
                                                if (distributedLock.IsOwner)
                                                {
                                                    if (
                                                        CentralizedFileStorage.GetFileStore(Constants.ThemeFiles)
                                                            .GetFile(writePath, mapName) ==
                                                        null)
                                                    {

                                                        byte[] array;

                                                        using (Stream stream = mapFile.OpenReadStream())
                                                        {
                                                            array = new byte[stream.Length];
                                                            stream.Read(array, 0, array.Length);
                                                            stream.Close();
                                                        }

                                                        string text = Encoding.UTF8.GetString(array);

                                                        // Modify paths
                                                        text = text.Replace("../",
                                                            string.Format("{0}/cfs-file/__key/themefiles/s-fd-{1}-",
                                                                CSContext.Current.ApplicationPath, _siteThemeName));
                                                        array = Encoding.UTF8.GetBytes(text);
                                                        CentralizedFileStorage.GetFileStore(Constants.ThemeFiles)
                                                            .AddUpdateFile(writePath, mapName, new MemoryStream(array));
                                                    }
                                                    break;
                                                }
                                                distributedMonitor.Wait(distributedLock, 5);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
示例#6
0
        private void UpsertSourceMap(string fileName, string fileStoreKey, string path)
        {
            const string propertyName = Constants.ThemeStylesheetFiles;

            // Test if modified file is a stylesheet
            if (fileName.EndsWith(".css") && fileStoreKey == Constants.ThemeFiles && path.EndsWith(propertyName))
            {
                ICentralizedFileStorageProvider fileStore = CentralizedFileStorage.GetFileStore(fileStoreKey);
                string mapName = string.Concat(fileName, ".map");

                if (fileStore != null)
                {
                    ICentralizedFile mapFile = fileStore.GetFile(path, mapName);

                    // Check if source map exists
                    if (mapFile != null)
                    {
                        string writePath = GetSanitisedPath(_siteThemeTypeId, _themeContextId, _siteThemeName,
                                                            propertyName, new Uri(Globals.FullPath("~/")));

                        if (CentralizedFileStorage.GetFileStore(Constants.ThemeFiles).GetFile(writePath, mapName) == null)
                        {
                            lock (_lock)
                            {
                                if (CentralizedFileStorage.GetFileStore(Constants.ThemeFiles).GetFile(writePath, mapName) ==
                                    null)
                                {
                                    using (DistributedMonitor distributedMonitor = new DistributedMonitor())
                                    {
                                        const int limit = 5;

                                        for (int i = 0; i < limit; i++)
                                        {
                                            using (
                                                DistributedLock distributedLock =
                                                    distributedMonitor.Enter(Constants.DistributedMonitorKey))
                                            {
                                                if (distributedLock.IsOwner)
                                                {
                                                    if (
                                                        CentralizedFileStorage.GetFileStore(Constants.ThemeFiles)
                                                        .GetFile(writePath, mapName) ==
                                                        null)
                                                    {
                                                        byte[] array;

                                                        using (Stream stream = mapFile.OpenReadStream())
                                                        {
                                                            array = new byte[stream.Length];
                                                            stream.Read(array, 0, array.Length);
                                                            stream.Close();
                                                        }

                                                        string text = Encoding.UTF8.GetString(array);

                                                        // Modify paths
                                                        text = text.Replace("../",
                                                                            string.Format("{0}/cfs-file/__key/themefiles/s-fd-{1}-",
                                                                                          CSContext.Current.ApplicationPath, _siteThemeName));
                                                        array = Encoding.UTF8.GetBytes(text);
                                                        CentralizedFileStorage.GetFileStore(Constants.ThemeFiles)
                                                        .AddUpdateFile(writePath, mapName, new MemoryStream(array));
                                                    }
                                                    break;
                                                }
                                                distributedMonitor.Wait(distributedLock, 5);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
示例#7
0
 public static ControlMessage CreateMessageWithValuesPropagation(ulong timer, MessageTypes type, DistributedMonitor monitor)
 {
     return(new ControlMessage()
     {
         SenderId = MonitorWrapper.Instance.ID,
         Timer = timer,
         MonitorId = monitor.ID,
         Type = type,
         ConditionalVariableValues = monitor.GetConditionalVariablesValues()
     });
 }