int __capacity;
int __length;
mutable int __hash;
- mutable int* __pRefCount;
+ mutable volatile int* __pRefCount;
mutable wchar_t* __pValue;
static const float GROWTH_FACTOR;
#include <FBaseResult.h>
#include <FBaseSysLog.h>
#include <unique_ptr.h>
+#include "FBase_String.h"
namespace Tizen { namespace Base
{
SysTryReturnVoidResult(NID_BASE, value.__length >= 0, E_OUT_OF_RANGE, "The length has to be greater than 0.");
- ++(*value.__pRefCount);
+ _String::AtomicInc(value.__pRefCount);
__pRefCount = value.__pRefCount;
}
}
else
{
- (*__pRefCount)--;
+ _String::AtomicDec(__pRefCount);
__pRefCount = null;
}
}
SysTryReturnResult(NID_BASE, pRefCntTemp != null, E_OUT_OF_MEMORY, "Memory allocation failed");
wcsncpy(__pValue, pValue, __length);
- (*__pRefCount)--;
+ _String::AtomicDec(__pRefCount);
__pRefCount = pRefCntTemp.release();
}
{
std::unique_ptr<int> pRefCntTemp(new (std::nothrow) int(1));
SysTryReturnResult(NID_BASE, pRefCntTemp != null, E_OUT_OF_MEMORY, "Memory allocation failed");
- (*__pRefCount)--;
+ _String::AtomicDec(__pRefCount);
__pRefCount = pRefCntTemp.release();
}
}
std::unique_ptr<int> pRefCntTemp(new (std::nothrow) int(1));
SysTryReturn(NID_BASE, pRefCntTemp != null, false, E_OUT_OF_MEMORY, "[%s] Memory allocation failed",
GetErrorMessage(E_OUT_OF_MEMORY));
- (*__pRefCount)--;
+ _String::AtomicDec(__pRefCount);
__pRefCount = pRefCntTemp.release();
}
}
wcsncpy(__pValue, pValue, __length);
__pValue[__length] = '\0';
- --(*__pRefCount);
+ _String::AtomicDec(__pRefCount);
__pRefCount = pRefCntTemp.release();
return E_SUCCESS;
}
--- /dev/null
+//
+// Copyright (c) 2012 Samsung Electronics Co., Ltd.
+//
+// Licensed under the Apache License, Version 2.0 (the License);
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
+/**
+ * @file FBase_String.h
+ * @brief This is the header file for handling atomic operations.
+ */
+
+#ifndef _FBASE_INTERNAL_STRING_H_
+#define _FBASE_INTERNAL_STRING_H_
+
+namespace Tizen { namespace Base
+{
+namespace _String
+{
+
+/**
+ * Atomically add the value to the variable that pRefCount points to.
+ *
+ * @since 2.1
+ */
+inline int AtomicAdd(volatile int* pRefCount, int value)
+{
+#if defined(__arm__)
+ register int result;
+ asm volatile (
+ "1: ldrex %0, [%1] \n\t"
+ "add r1, %0, %2 \n\t"
+ "strex r2, r1, [%1] \n\t"
+ "cmp r2, #0 \n\t"
+ "bne 1b"
+ : "=&r" (result)
+ : "r"(pRefCount), "r"(value)
+ : "r1","r2"
+ );
+ return result;
+#else
+ return __sync_fetch_and_add(pRefCount, value);
+#endif
+}
+
+/**
+ * Atomically increase the variable that pRefCount points to by 1
+ *
+ * @since 2.1
+ */
+inline int AtomicInc(volatile int* pRefCount)
+{
+ return AtomicAdd(pRefCount, 1);
+}
+
+/**
+ * Atomically decrease the variable that pRefCount points to by 1
+ *
+ * @since 2.1
+ */
+inline int AtomicDec(volatile int* pRefCount)
+{
+ return AtomicAdd(pRefCount, -1);
+}
+
+}// _String
+}} // Tizen::Base
+
+#endif // _FBASE_INTERNAL_STRING_H_
\ No newline at end of file