Skip to content

Commit

Permalink
Optimise lockClosure when n_capabilities == 1; fixes #693
Browse files Browse the repository at this point in the history
Based on a patch from Yuras Shumovich.
  • Loading branch information
Ian Lynagh committed Jun 15, 2013
1 parent 9a8c20d commit 75947bb
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 11 deletions.
2 changes: 1 addition & 1 deletion includes/Rts.h
Expand Up @@ -203,6 +203,7 @@ INLINE_HEADER Time fsecondsToTime (double t)
#include "rts/SpinLock.h"

#include "rts/Messages.h"
#include "rts/Threads.h"

/* Storage format definitions */
#include "rts/storage/FunTypes.h"
Expand Down Expand Up @@ -230,7 +231,6 @@ INLINE_HEADER Time fsecondsToTime (double t)
#include "rts/Globals.h"
#include "rts/IOManager.h"
#include "rts/Linker.h"
#include "rts/Threads.h"
#include "rts/Ticky.h"
#include "rts/Timer.h"
#include "rts/Stable.h"
Expand Down
28 changes: 22 additions & 6 deletions includes/rts/storage/SMPClosureOps.h
Expand Up @@ -18,6 +18,7 @@
#else

EXTERN_INLINE StgInfoTable *lockClosure(StgClosure *p);
EXTERN_INLINE StgInfoTable *reallyLockClosure(StgClosure *p);
EXTERN_INLINE StgInfoTable *tryLockClosure(StgClosure *p);
EXTERN_INLINE void unlockClosure(StgClosure *p, const StgInfoTable *info);

Expand All @@ -31,7 +32,7 @@ EXTERN_INLINE void unlockClosure(StgClosure *p, const StgInfoTable *info);

// We want a callable copy of lockClosure() so that we can refer to it
// from .cmm files compiled using the native codegen.
EXTERN_INLINE StgInfoTable *lockClosure(StgClosure *p)
EXTERN_INLINE StgInfoTable *reallyLockClosure(StgClosure *p)
{
StgWord info;
do {
Expand All @@ -44,14 +45,29 @@ EXTERN_INLINE StgInfoTable *lockClosure(StgClosure *p)
} while (1);
}

EXTERN_INLINE StgInfoTable *lockClosure(StgClosure *p)
{
if (n_capabilities == 1) {
return (StgInfoTable *)p->header.info;
}
else {
return reallyLockClosure(p);
}
}

EXTERN_INLINE StgInfoTable *tryLockClosure(StgClosure *p)
{
StgWord info;
info = xchg((P_)(void *)&p->header.info, (W_)&stg_WHITEHOLE_info);
if (info != (W_)&stg_WHITEHOLE_info) {
return (StgInfoTable *)info;
} else {
return NULL;
if (n_capabilities == 1) {
return (StgInfoTable *)p->header.info;
}
else {
info = xchg((P_)(void *)&p->header.info, (W_)&stg_WHITEHOLE_info);
if (info != (W_)&stg_WHITEHOLE_info) {
return (StgInfoTable *)info;
} else {
return NULL;
}
}
}

Expand Down
28 changes: 24 additions & 4 deletions rts/PrimOps.cmm
Expand Up @@ -1193,7 +1193,12 @@ stg_takeMVarzh ( P_ mvar /* :: MVar a */ )
W_ val, info, tso, q;

#if defined(THREADED_RTS)
("ptr" info) = ccall lockClosure(mvar "ptr");
if (CInt[n_capabilities] == 1 :: CInt) {
info = GET_INFO(mvar);
}
else {
("ptr" info) = ccall reallyLockClosure(mvar "ptr");
}
#else
info = GET_INFO(mvar);
#endif
Expand Down Expand Up @@ -1290,7 +1295,12 @@ stg_tryTakeMVarzh ( P_ mvar /* :: MVar a */ )
W_ val, info, tso, q;

#if defined(THREADED_RTS)
("ptr" info) = ccall lockClosure(mvar "ptr");
if (CInt[n_capabilities] == 1 :: CInt) {
info = GET_INFO(mvar);
}
else {
("ptr" info) = ccall reallyLockClosure(mvar "ptr");
}
#else
info = GET_INFO(mvar);
#endif
Expand Down Expand Up @@ -1361,7 +1371,12 @@ stg_putMVarzh ( P_ mvar, /* :: MVar a */
W_ info, tso, q;

#if defined(THREADED_RTS)
("ptr" info) = ccall lockClosure(mvar "ptr");
if (CInt[n_capabilities] == 1 :: CInt) {
info = GET_INFO(mvar);
}
else {
("ptr" info) = ccall reallyLockClosure(mvar "ptr");
}
#else
info = GET_INFO(mvar);
#endif
Expand Down Expand Up @@ -1454,7 +1469,12 @@ stg_tryPutMVarzh ( P_ mvar, /* :: MVar a */
W_ info, tso, q;

#if defined(THREADED_RTS)
("ptr" info) = ccall lockClosure(mvar "ptr");
if (CInt[n_capabilities] == 1 :: CInt) {
info = GET_INFO(mvar);
}
else {
("ptr" info) = ccall reallyLockClosure(mvar "ptr");
}
#else
info = GET_INFO(mvar);
#endif
Expand Down

0 comments on commit 75947bb

Please sign in to comment.