Commit 7d452be4 authored by Dylan Yudaken's avatar Dylan Yudaken Committed by Marge Bot

Fix hs_try_putmvar losing track of running cap

If hs_try_putmvar was called through an unsafe import, it would lose track of the running cap causing a deadlock
parent 7755ffc2
Pipeline #15550 passed with stages
in 453 minutes and 43 seconds
......@@ -682,6 +682,7 @@ void hs_try_putmvar (/* in */ int capability,
{
Task *task = getTask();
Capability *cap;
Capability *task_old_cap USED_IF_THREADS;
if (capability < 0) {
capability = task->preferred_capability;
......@@ -702,6 +703,7 @@ void hs_try_putmvar (/* in */ int capability,
// If the capability is free, we can perform the tryPutMVar immediately
if (cap->running_task == NULL) {
cap->running_task = task;
task_old_cap = task->cap;
task->cap = cap;
RELEASE_LOCK(&cap->lock);
......@@ -712,6 +714,7 @@ void hs_try_putmvar (/* in */ int capability,
// Wake up the capability, which will start running the thread that we
// just awoke (if there was one).
releaseCapability(cap);
task->cap = task_old_cap;
} else {
PutMVar *p = stgMallocBytes(sizeof(PutMVar),"hs_try_putmvar");
// We cannot deref the StablePtr if we don't have a capability,
......
import Control.Concurrent.MVar
import Control.Monad
import Foreign.C.Types (CInt(..))
import Foreign.StablePtr (StablePtr)
import GHC.Conc
foreign import ccall unsafe hs_try_putmvar :: CInt -> StablePtr PrimMVar -> IO ()
main = do
mvs <- forM [0..numCapabilities] (\idx -> do
a <- newEmptyMVar
b <- newEmptyMVar
return $ (idx, a, b))
forM_ [mvs, reverse mvs] $ \mvars -> do
forM_ mvars $ (\(cap,a,b) -> forkOn cap $ do
takeMVar a
putMVar b ())
forM_ mvars $ \(cap, a, _) -> do
sp <- newStablePtrPrimMVar a
hs_try_putmvar (fromIntegral cap) sp
forM_ mvars $ \(_,_,b) -> takeMVar b
......@@ -417,3 +417,5 @@ test('InitEventLogging',
test('T17088',
[only_ways(['normal']), extra_run_opts('+RTS -c -A256k -RTS')],
compile_and_run, ['-rtsopts -O2'])
test('T15427', normal, compile_and_run, [''])
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment