Fix the boiler plate codes
[platform/framework/native/appfw.git] / src / base / runtime / FBaseRtMutex.cpp
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        FBaseRtThreadMutex.cpp
19  * @brief       This is the implementation file for the Mutex class.
20  *
21  */
22
23 #include <new>
24
25 #include <FBaseRtMutex.h>
26
27 #include "FBase_NativeError.h"
28 #include <FBaseSysLog.h>
29 #include "FBaseRt_MutexImpl.h"
30
31
32 namespace Tizen { namespace Base { namespace Runtime
33 {
34
35 Mutex::Mutex(void)
36         : __pMutexImpl(null)
37 {
38
39 }
40
41 Mutex::~Mutex(void)
42 {
43         delete __pMutexImpl;
44         __pMutexImpl = null;
45 }
46
47 result
48 Mutex::Create(void)
49 {
50         return Create(L"");
51 }
52
53 result
54 Mutex::Create(NonRecursiveMutexTag)
55 {
56         // Object is not allowed to construct twice
57         SysAssertf(__pMutexImpl == null,
58                 "Already constructed! Calling Construct() twice or more on a same instance is not allowed for this class");
59
60         result r = E_SUCCESS;
61
62         __pMutexImpl = new (std::nothrow) _MutexImpl;
63         SysTryReturnResult(NID_BASE_RT, __pMutexImpl != null, E_OUT_OF_MEMORY, "Not enough memory.");
64
65         r = __pMutexImpl->CreateNonRecursiveMutex();
66         SysTryCatch(NID_BASE_RT, !IsFailed(r), , r, "[%s] Mutex initialization failed.", GetErrorMessage(r));
67
68         return E_SUCCESS;
69
70 CATCH:
71         delete __pMutexImpl;
72         __pMutexImpl = null;
73
74         return r;
75
76 }
77
78
79 result
80 Mutex::Create(const String& name)
81 {
82         // Object is not allowed to construct twice
83         SysAssertf(__pMutexImpl == null,
84                 "Already constructed! Calling Construct() twice or more on a same instance is not allowed for this class");
85
86         result r = E_SUCCESS;
87
88         __pMutexImpl = new (std::nothrow) _MutexImpl;
89         SysTryReturnResult(NID_BASE_RT, __pMutexImpl != null, E_OUT_OF_MEMORY, "Not enough memory.");
90
91         r = __pMutexImpl->Create(name);
92         SysTryCatch(NID_BASE_RT, !IsFailed(r), , r, "[%s] Mutex initialization failed.", GetErrorMessage(r));
93
94         return E_SUCCESS;
95
96 CATCH:
97         delete __pMutexImpl;
98         __pMutexImpl = null;
99
100         return r;
101 }
102
103 result
104 Mutex::Acquire(void)
105 {
106         SysAssertf(__pMutexImpl != null, "Not yet constructed! Construct() should be called before use.");
107         return __pMutexImpl->Acquire();
108 }
109
110 result
111 Mutex::TryToAcquire(void)
112 {
113         SysAssertf(__pMutexImpl != null, "Not yet constructed! Construct() should be called before use.");
114         return __pMutexImpl->TryToAcquire();
115 }
116
117 result
118 Mutex::Release(void)
119 {
120         SysAssertf(__pMutexImpl != null, "Not yet constructed! Construct() should be called before use.");
121         return __pMutexImpl->Release();
122 }
123
124 } } } // Tizen::Runtime