Merge "[2.2.1] Merge different codes between 2.2 and 3.0" into tizen_2.2
[platform/framework/native/appfw.git] / src / base / inc / FBase_String.h
1 //
2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
3 //
4 // Licensed under the Apache License, Version 2.0 (the License);
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16
17 /**
18  * @file                FBase_String.h
19  * @brief               This is the header file for handling atomic operations.
20  */
21 #ifndef _FBASE_INTERNAL_STRING_H_
22 #define _FBASE_INTERNAL_STRING_H_
23
24 namespace Tizen { namespace Base
25 {
26 namespace _String
27 {
28
29 /**
30  * Atomically add the value to the variable that pRefCount points to.
31  *
32  * @since 2.1
33  */
34 inline int
35 AtomicAdd(volatile int* pRefCount, int value)
36 {
37 #if defined(__arm__)
38         register int result;
39         asm volatile (
40                 "1: ldrex   %0,  [%1]   \n\t"
41                 "add     r1,   %0,  %2  \n\t"
42                 "strex   r2,   r1, [%1] \n\t"
43                 "cmp     r2,   #0 \n\t"
44                 "bne     1b"
45                 : "=&r" (result)
46                 : "r"(pRefCount), "r"(value)
47                 : "r1","r2"
48         );
49         return result;
50 #else
51         return __sync_fetch_and_add(pRefCount, value);
52 #endif
53 }
54
55 /**
56  * Atomically increase the variable that pRefCount points to by 1
57  *
58  * @since 2.1
59  */
60 inline int
61 AtomicInc(volatile int* pRefCount)
62 {
63         return AtomicAdd(pRefCount, 1);
64 }
65
66 /**
67  * Atomically decrease the variable that pRefCount points to by 1
68  *
69  * @since 2.1
70  */
71 inline int
72 AtomicDec(volatile int* pRefCount)
73 {
74         return AtomicAdd(pRefCount, -1);
75 }
76 } // _String
77 }}  // Tizen::Base
78 #endif  // _FBASE_INTERNAL_STRING_H_