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