InitializeAndLock() public method

Initializes and locks this mutex. This is an atomic operation on Windows, but not Linux.
public InitializeAndLock ( ) : IDisposable
return IDisposable
Exemplo n.º 1
0
		public void InitializeAndLock_Reentrancy_DoesNotBlock()
		{
			using (var mutex = new GlobalMutex("test"))
			{
				using (mutex.InitializeAndLock())
				{
					using (mutex.Lock()) {}
				}
				mutex.Unlink();
			}
		}
Exemplo n.º 2
0
		public void InitializeAndLock_CreatedNew_ReturnsTrue()
		{
			using (var mutex = new GlobalMutex("test"))
			{
				mutex.Unlink();
				bool createdNew;
				using (mutex.InitializeAndLock(out createdNew)) {}
				Assert.That(createdNew, Is.True);
				mutex.Unlink();
			}
		}
Exemplo n.º 3
0
 public void InitializeAndLock_Reentrancy_DoesNotBlock()
 {
     using (var mutex = new GlobalMutex("test"))
     {
         using (mutex.InitializeAndLock())
         {
             using (mutex.Lock()) {}
         }
         mutex.Unlink();
     }
 }
Exemplo n.º 4
0
 public void InitializeAndLock_CreatedNew_ReturnsTrue()
 {
     using (var mutex = new GlobalMutex("test"))
     {
         mutex.Unlink();
         bool createdNew;
         using (mutex.InitializeAndLock(out createdNew)) {}
         Assert.That(createdNew, Is.True);
         mutex.Unlink();
     }
 }
Exemplo n.º 5
0
		public void InitializeAndLock_Existing_ReturnsFalse()
		{
			using (var mutex1 = new GlobalMutex("test"))
			{
				mutex1.Initialize();
				using (var mutex2 = new GlobalMutex("test"))
				{
					bool createdNew;
					using (mutex2.InitializeAndLock(out createdNew)) {}
					Assert.That(createdNew, Is.False);
				}
				mutex1.Unlink();
			}
		}
Exemplo n.º 6
0
 public void InitializeAndLock_Existing_ReturnsFalse()
 {
     using (var mutex1 = new GlobalMutex("test"))
     {
         mutex1.Initialize();
         using (var mutex2 = new GlobalMutex("test"))
         {
             bool createdNew;
             using (mutex2.InitializeAndLock(out createdNew)) {}
             Assert.That(createdNew, Is.False);
         }
         mutex1.Unlink();
     }
 }
		protected override int StartupInternal(int currentModelVersion)
		{
			m_commitLogMutex = new GlobalMutex(MutexName);
			bool createdNew;
			using (m_commitLogMutex.InitializeAndLock(out createdNew))
			{
				CreateSharedMemory(createdNew);
				using (MemoryMappedViewStream stream = m_commitLogMetadata.CreateViewStream())
				{
					CommitLogMetadata metadata = null;
					if (!createdNew)
					{
						if (TryGetMetadata(stream, out metadata))
						{
							CheckExitedPeerProcesses(metadata);
							if (m_peerProcesses.Count == 0)
								createdNew = true;
						}
						else
						{
							createdNew = true;
						}
					}

					if (createdNew)
						metadata = new CommitLogMetadata();

					using (Process curProcess = Process.GetCurrentProcess())
						metadata.Peers[m_peerID] = new CommitLogPeer { ProcessID = curProcess.Id, Generation = metadata.FileGeneration };

					if (metadata.Master == Guid.Empty)
					{
						base.LockProject();
						metadata.Master = m_peerID;
					}

					int startupModelVersion = ReadInSurrogates(currentModelVersion);
					// non-master peers cannot migrate the XML file
					if (startupModelVersion < currentModelVersion && metadata.Master != m_peerID)
						throw new FdoDataMigrationForbiddenException();

					SaveMetadata(stream, metadata);

					return startupModelVersion;
				}
			}
		}
		protected override void CreateInternal()
		{
			m_commitLogMutex = new GlobalMutex(MutexName);

			using (m_commitLogMutex.InitializeAndLock())
			{
				CreateSharedMemory(true);
				var metadata = new CommitLogMetadata { Master = m_peerID };
				using (Process curProcess = Process.GetCurrentProcess())
					metadata.Peers[m_peerID] = new CommitLogPeer { ProcessID = curProcess.Id, Generation = metadata.FileGeneration };
				using (MemoryMappedViewStream stream = m_commitLogMetadata.CreateViewStream())
				{
					SaveMetadata(stream, metadata);
				}

				base.CreateInternal();
			}
		}