private static bool EnterMutex(Thread currentThread) { bool iflag = false; if (currentThread != null #if !SINGULARITY || SEMISPACE_COLLECTOR || SLIDING_COLLECTOR || ADAPTIVE_COPYING_COLLECTOR || MARK_SWEEP_COLLECTOR && StopTheWorldGCData.CurrentPhase != StopTheWorldPhase.SingleThreaded #endif ) { #if SINGULARITY_KERNEL iflag = Processor.DisableInterrupts(); #endif // SINGULARITY_KERNEL #if USE_MUTEX if (mutex != null) { mutex.AcquireMutex(); } #elif USE_SPINLOCK PageManager.Lock.Acquire(currentThread); #endif } return(iflag); }
/// <summary> /// Thread entry point for contention test. Run a tight loop which acquires the mutex, /// does a little work, then releases it /// </summary> private static void ContentionThreadMain() { evtGate.WaitOne(); for (int i = 0; i < numRoundsInContention; i++) { mutex.AcquireMutex(); Thread.Yield(); for (int j = 0; j < 1000; j++) { // sum is volatile so this won't be optimized away sum = unchecked (sum + i * j * j / 23); } if (i % 10000 == 0) { Console.Write('.'); } Thread.Yield(); mutex.ReleaseMutex(); Thread.Yield(); } }
public override bool AddBuffer(ulong Size) { unsafe { UnmanagedMemoryBuffer NewBuffer = new UnmanagedMemoryBuffer(); if (NewBuffer != null) { if (!NewBuffer.AllocateBuffer(Size)) { return(false); } } mutex.AcquireMutex(); NewBuffer.Link = Buffers; Buffers = NewBuffer; mutex.ReleaseMutex(); MemoryStorage.MemoryStorageRegisterBufferImpl(StorageHandle, NewBuffer.GetBuffer(), (uint)NewBuffer.GetSize()); } return(true); }
// Caller should not modify this buffer! public byte[] GetBodyData() { if (m_Req.IsEntireEntityBodyIsPreloaded()) { // Return a copy of the preloaded body and trust the // caller not to monkey with it. return(m_Req.GetPreloadedEntityBody()); } else { int totalBodyLength = m_Req.GetTotalEntityBodyLength(); if (totalBodyLength == 0) { return(null); // No body expected at all } // Else there is body data that wasn't received as part of // the initial request. if (cachedBodyData == null) { try { bodyDataMutex.AcquireMutex(); if (cachedBodyData != null) { // Someone loaded this concurrently to us return(cachedBodyData); } byte[] preloadedBody = m_Req.GetPreloadedEntityBody(); int preloadedLength = m_Req.GetPreloadedEntityBodyLength(); byte[] body = new byte[totalBodyLength]; if ((preloadedLength > 0) && (preloadedBody != null)) { // First copy the cached portion Buffer.BlockCopy(preloadedBody, 0, body, 0, preloadedLength); } else { assert((preloadedBody == null) && (preloadedLength == 0)); } // Now read in any remainder int remainder = totalBodyLength - preloadedLength; assert remainder > 0; // Tested earlier while (remainder > 0) { int readBytes = m_Req.ReadEntityBody(body, totalBodyLength - remainder, remainder); if (readBytes == 0) { // Body wasn't as long as we were expecting for some // reason! This could be because the remote client // lied to us in the headers. Truncate the cached // data to the actual body size. byte[] shortBody = new byte[totalBodyLength - remainder]; Array.Copy(body, 0, shortBody, 0, totalBodyLength - remainder); body = shortBody; remainder = 0; // No more data will be forthcoming } else { remainder -= readBytes; } } // Stash this for other callers cachedBodyData = body; } finally { bodyDataMutex.ReleaseMutex(); } } // else we previously cached the body data. return(cachedBodyData); } }
/// <summary> /// Mutex supports recursion: the same thread can Acquire the same mutex /// multiple times successfully /// </summary> private static void TestRecursion() { Console.Write(" test recursion "); // // same number of acquire and release // Cleanup(); Console.Write('.'); mutex = new Mutex(); mutex.AcquireMutex(); mutex.AcquireMutex(); mutex.AcquireMutex(); mutex.ReleaseMutex(); mutex.ReleaseMutex(); mutex.ReleaseMutex(); Thread thread = new Thread(new ThreadStart(TimeoutWaitMain)); thread.Start(); thread.Join(); if (Thread.VolatileRead(ref counter) != 1) { Expect.Fail("expecting wait to succeed"); return; } // // same number of acquire and release, with the explicit ctor count as 1 // Cleanup(); Console.Write('.'); mutex = new Mutex(true); mutex.AcquireMutex(); mutex.AcquireMutex(); mutex.ReleaseMutex(); mutex.ReleaseMutex(); mutex.ReleaseMutex(); thread = new Thread(new ThreadStart(TimeoutWaitMain)); thread.Start(); thread.Join(); if (Thread.VolatileRead(ref counter) != 1) { Expect.Fail("expecting wait to succeed"); return; } // // acquire without release, the other thread will wait and timeout // Cleanup(); Console.Write('.'); mutex = new Mutex(); mutex.AcquireMutex(); mutex.AcquireMutex(); mutex.AcquireMutex(); thread = new Thread(new ThreadStart(TimeoutWaitMain)); thread.Start(); thread.Join(); if (Thread.VolatileRead(ref counter) != 0) { Expect.Fail("expecting wait to timeout"); return; } // // less release than acquire, the other thread will wait and timeout // Cleanup(); Console.Write('.'); mutex = new Mutex(); mutex.AcquireMutex(); mutex.AcquireMutex(); mutex.AcquireMutex(); mutex.ReleaseMutex(); mutex.ReleaseMutex(); thread = new Thread(new ThreadStart(TimeoutWaitMain)); thread.Start(); thread.Join(); if (Thread.VolatileRead(ref counter) != 0) { Expect.Fail("expecting wait to timeout"); return; } Cleanup(); Console.WriteLine("OK"); }
public static void FlushCaches() { Lock.AcquireMutex(); DescriptorTable.Clear(); Lock.ReleaseMutex(); }