sync with tizen_2.0
[platform/framework/native/appfw.git] / src / base / runtime / FBaseRtSemaphore.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 /**
19  * @file        FBaseRtThreadSemaphore.cpp
20  * @brief       This is the implementation file for the Semaphore class.
21  *
22  */
23
24 #include <new>
25
26 #include <FBaseRtSemaphore.h>
27
28 #include "FBase_NativeError.h"
29 #include <FBaseSysLog.h>
30 #include "FBaseRt_SemaphoreImpl.h"
31
32
33 namespace Tizen { namespace Base { namespace Runtime
34 {
35
36 Semaphore::Semaphore(void)
37         : __pSemaphoreImpl(null)
38 {
39
40 }
41
42 Semaphore::~Semaphore(void)
43 {
44         delete __pSemaphoreImpl;
45         __pSemaphoreImpl = null;
46 }
47
48 result
49 Semaphore::Create(int count)
50 {
51         return Create(L"", count);
52 }
53
54 result
55 Semaphore::Create(const String& name, int count)
56 {
57         // Object is not allowed to construct twice
58         SysAssertf(__pSemaphoreImpl == null,
59                 "Already constructed! Calling Construct() twice or more on a same instance is not allowed for this class");
60
61         SysTryReturnResult(NID_BASE_RT, count >= 0, E_INVALID_ARG, "The count(%d) MUST be greater than or equal to 0.", count);
62
63         __pSemaphoreImpl = new (std::nothrow) _SemaphoreImpl;
64         SysTryReturnResult(NID_BASE_RT, __pSemaphoreImpl != null, E_OUT_OF_MEMORY, "Not enough memory.");
65
66         result r = __pSemaphoreImpl->Create(name, count);
67         if (IsFailed(r))
68         {
69                 delete __pSemaphoreImpl;
70                 __pSemaphoreImpl = null;
71         }
72
73         return r;
74 }
75
76 result
77 Semaphore::Acquire(long timeout)
78 {
79         SysAssertf(__pSemaphoreImpl != null, "Not yet constructed! Construct() should be called before use.");
80         return __pSemaphoreImpl->Acquire(timeout);
81 }
82
83 result
84 Semaphore::TryToAcquire(void)
85 {
86         SysAssertf(__pSemaphoreImpl != null, "Not yet constructed! Construct() should be called before use.");
87         return __pSemaphoreImpl->TryToAcquire();
88 }
89
90 result
91 Semaphore::Release(void)
92 {
93         SysAssertf(__pSemaphoreImpl != null, "Not yet constructed! Construct() should be called before use.");
94         return __pSemaphoreImpl->Release();
95 }
96
97 } } } // Tizen::Runtime