Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Read Barrier for CompareAndSwapObject #1573

Merged
merged 1 commit into from
Apr 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 30 additions & 29 deletions runtime/gc_base/ObjectAccessBarrier.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 1991, 2017 IBM Corp. and others
* Copyright (c) 1991, 2018 IBM Corp. and others
*
* This program and the accompanying materials are made available under
* the terms of the Eclipse Public License 2.0 which accompanies this
Expand Down Expand Up @@ -1501,6 +1501,7 @@ MM_ObjectAccessBarrier::compareAndSwapObject(J9VMThread *vmThread, J9Object *des
fj9object_t *actualDestAddress;
fj9object_t compareValue = convertTokenFromPointer(compareObject);
fj9object_t swapValue = convertTokenFromPointer(swapObject);
bool result = false;

/* TODO: To make this API more consistent, it should probably be split into separate
* indexable and non-indexable versions. Currently, when called on an indexable object,
Expand All @@ -1513,25 +1514,22 @@ MM_ObjectAccessBarrier::compareAndSwapObject(J9VMThread *vmThread, J9Object *des
actualDestAddress = J9OAB_MIXEDOBJECT_EA(destObject, ((UDATA)destAddress - (UDATA)destObject), fj9object_t);
}

/* Note: This is a bit of a special case -- we call preObjectStore even though the store
* may not actually occur. This is safe and correct for Metronome.
*/
preObjectStore(vmThread, destObject, actualDestAddress, swapObject, true);
protectIfVolatileBefore(vmThread, true, false, false);

/* One of these conditions will evaluate to true at compile time based on whether fields are 32 or 64 bits */
bool result = false;
if (sizeof(fj9object_t) == sizeof(UDATA)) {
result = ((UDATA)compareValue == MM_AtomicOperations::lockCompareExchange((UDATA *)actualDestAddress, (UDATA)compareValue, (UDATA)swapValue));
} else if (sizeof(fj9object_t) == sizeof(U_32)) {
if (preObjectRead(vmThread, destObject, actualDestAddress)) {
/* Note: This is a bit of a special case -- we call preObjectStore even though the store
* may not actually occur. This is safe and correct for Metronome.
*/
preObjectStore(vmThread, destObject, actualDestAddress, swapObject, true);
protectIfVolatileBefore(vmThread, true, false, false);

#if defined(J9VM_GC_COMPRESSED_POINTERS)
result = ((U_32)(UDATA)compareValue == MM_AtomicOperations::lockCompareExchangeU32((U_32 *)actualDestAddress, (U_32)(UDATA)compareValue, (U_32)(UDATA)swapValue));
} else {
assume(false, "fj9object_t must be either U_32 (compressed pointers / 32-bit VM) or UDATA");
}

protectIfVolatileAfter(vmThread, true, false, false);
if (result) {
postObjectStore(vmThread, destObject, actualDestAddress, swapObject, true);
#else
result = ((UDATA)compareValue == MM_AtomicOperations::lockCompareExchange((UDATA *)actualDestAddress, (UDATA)compareValue, (UDATA)swapValue));
#endif
protectIfVolatileAfter(vmThread, true, false, false);
if (result) {
postObjectStore(vmThread, destObject, actualDestAddress, swapObject, true);
}
}

return result;
Expand Down Expand Up @@ -1647,6 +1645,7 @@ MM_ObjectAccessBarrier::compareAndExchangeObject(J9VMThread *vmThread, J9Object
fj9object_t *actualDestAddress;
fj9object_t compareValue = convertTokenFromPointer(compareObject);
fj9object_t swapValue = convertTokenFromPointer(swapObject);
J9Object *result = NULL;

/* TODO: To make this API more consistent, it should probably be split into separate
* indexable and non-indexable versions. Currently, when called on an indexable object,
Expand All @@ -1659,21 +1658,23 @@ MM_ObjectAccessBarrier::compareAndExchangeObject(J9VMThread *vmThread, J9Object
actualDestAddress = J9OAB_MIXEDOBJECT_EA(destObject, ((UDATA)destAddress - (UDATA)destObject), fj9object_t);
}

/* Note: This is a bit of a special case -- we call preObjectStore even though the store
* may not actually occur. This is safe and correct for Metronome.
*/
preObjectStore(vmThread, destObject, actualDestAddress, swapObject, true);
protectIfVolatileBefore(vmThread, true, false, false);
if (preObjectRead(vmThread, destObject, actualDestAddress)) {
/* Note: This is a bit of a special case -- we call preObjectStore even though the store
* may not actually occur. This is safe and correct for Metronome.
*/
preObjectStore(vmThread, destObject, actualDestAddress, swapObject, true);
protectIfVolatileBefore(vmThread, true, false, false);

#if defined(J9VM_GC_COMPRESSED_POINTERS)
J9Object *result = (J9Object *)(UDATA)MM_AtomicOperations::lockCompareExchangeU32((U_32 *)actualDestAddress, (U_32)(UDATA)compareValue, (U_32)(UDATA)swapValue);
J9Object *result = (J9Object *)(UDATA)MM_AtomicOperations::lockCompareExchangeU32((U_32 *)actualDestAddress, (U_32)(UDATA)compareValue, (U_32)(UDATA)swapValue);
#else
J9Object *result = (J9Object *)MM_AtomicOperations::lockCompareExchange((UDATA *)actualDestAddress, (UDATA)compareValue, (UDATA)swapValue);
J9Object *result = (J9Object *)MM_AtomicOperations::lockCompareExchange((UDATA *)actualDestAddress, (UDATA)compareValue, (UDATA)swapValue);
#endif

protectIfVolatileAfter(vmThread, true, false, false);
if (result) {
postObjectStore(vmThread, destObject, actualDestAddress, swapObject, true);
protectIfVolatileAfter(vmThread, true, false, false);
if (result) {
postObjectStore(vmThread, destObject, actualDestAddress, swapObject, true);
}
}

return result;
Expand Down
4 changes: 4 additions & 0 deletions runtime/gc_include/ObjectAccessBarrierAPI.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,7 @@ class MM_ObjectAccessBarrierAPI
if (j9gc_modron_wrtbar_always == _writeBarrierType) {
return (1 == vmThread->javaVM->memoryManagerFunctions->j9gc_objaccess_compareAndSwapObject(vmThread, destObject, (J9Object **)destAddress, compareObject, swapObject));
} else {
preMixedObjectReadObject(vmThread, destObject, destAddress);
preMixedObjectStoreObject(vmThread, destObject, destAddress, swapObject);

protectIfVolatileBefore(isVolatile, false);
Expand Down Expand Up @@ -526,6 +527,7 @@ class MM_ObjectAccessBarrierAPI
if (j9gc_modron_wrtbar_always == _writeBarrierType) {
return vmThread->javaVM->memoryManagerFunctions->j9gc_objaccess_compareAndExchangeObject(vmThread, destObject, (J9Object **)destAddress, compareObject, swapObject);
} else {
preMixedObjectReadObject(vmThread, destObject, destAddress);
preMixedObjectStoreObject(vmThread, destObject, destAddress, swapObject);

protectIfVolatileBefore(isVolatile, false);
Expand Down Expand Up @@ -1887,6 +1889,7 @@ class MM_ObjectAccessBarrierAPI
if (j9gc_modron_wrtbar_always == _writeBarrierType) {
return (1 == vmThread->javaVM->memoryManagerFunctions->j9gc_objaccess_compareAndSwapObject(vmThread, destArray, (J9Object **)actualAddress, compareObject, swapObject));
} else {
preIndexableObjectReadObject(vmThread, destArray, actualAddress);
preIndexableObjectStoreObject(vmThread, destArray, actualAddress, swapObject);

protectIfVolatileBefore(isVolatile, false);
Expand Down Expand Up @@ -1928,6 +1931,7 @@ class MM_ObjectAccessBarrierAPI
if (j9gc_modron_wrtbar_always == _writeBarrierType) {
return vmThread->javaVM->memoryManagerFunctions->j9gc_objaccess_compareAndExchangeObject(vmThread, destArray, (J9Object **)actualAddress, compareObject, swapObject);
} else {
preIndexableObjectReadObject(vmThread, destArray, actualAddress);
preIndexableObjectStoreObject(vmThread, destArray, actualAddress, swapObject);

protectIfVolatileBefore(isVolatile, false);
Expand Down