From f7b7b56dea28dd69a44a877f7b7073c4496ced9e Mon Sep 17 00:00:00 2001 From: Patrick Williams Date: Tue, 23 Aug 2011 15:16:35 -0500 Subject: Ensure scratch operations happen on master core. Change-Id: I970d645108de041d410599847edce877cb794015 Reviewed-on: http://gfw160.austin.ibm.com:8080/gerrit/275 Tested-by: Jenkins Server Reviewed-by: Daniel M. Crowell Reviewed-by: A. Patrick Williams III --- src/include/util/lockfree/atomic_construct.H | 62 ++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 src/include/util/lockfree/atomic_construct.H (limited to 'src/include/util') diff --git a/src/include/util/lockfree/atomic_construct.H b/src/include/util/lockfree/atomic_construct.H new file mode 100644 index 000000000..6946a1c60 --- /dev/null +++ b/src/include/util/lockfree/atomic_construct.H @@ -0,0 +1,62 @@ +// IBM_PROLOG_BEGIN_TAG +// This is an automatically generated prolog. +// +// $Source: src/include/util/lockfree/atomic_construct.H $ +// +// IBM CONFIDENTIAL +// +// COPYRIGHT International Business Machines Corp. 2011 +// +// p1 +// +// Object Code Only (OCO) source materials +// Licensed Internal Code Source Materials +// IBM HostBoot Licensed Internal Code +// +// The source code for this program is not published or other- +// wise divested of its trade secrets, irrespective of what has +// been deposited with the U.S. Copyright Office. +// +// Origin: 30 +// +// IBM_PROLOG_END +#ifndef __UTIL_LOCKFREE_ATOMIC_CONSTRUCT_H +#define __UTIL_LOCKFREE_ATOMIC_CONSTRUCT_H + +namespace Util +{ + namespace Lockfree + { + + /** @brief Atomically construct an object and assign it to a pointer. + * + * This function will check if a pointer is still NULL and + * construct an object, atomically, if needed. If the pointer is + * not NULL, the current instance will be left alone. + * + * @param[in] ptr - Pointer-pointer to the object. + * + * Example: + * Foo* iv_foo; //<--- instance variable in class. + * atomic_construct(&iv_foo); + * + * @note It is possible for multiple instances to temporarily exist, + * if this code is called from multiple CPUs, but only the one + * instance will exist outside the lifetime of this function. + */ + template + void atomic_construct(_T** ptr) + { + if (__sync_bool_compare_and_swap(ptr, NULL, NULL)) + { + _T* instance = new _T(); + if (!__sync_bool_compare_and_swap(ptr, NULL, instance)) + { + delete instance; + } + } + } + }; +}; + +#endif -- cgit v1.2.1