Merge "Update deprecated libprivilege-control API functions." into tizen
[platform/framework/native/appfw.git] / src / base / runtime / FBaseRtSemaphore.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        FBaseRtThreadSemaphore.cpp
19  * @brief       This is the implementation file for the Semaphore class.
20  *
21  */
22
23 #include <new>
24
25 #include <FBaseRtSemaphore.h>
26
27 #include "FBase_NativeError.h"
28 #include <FBaseSysLog.h>
29 #include "FBaseRt_SemaphoreImpl.h"
30
31
32 namespace Tizen { namespace Base { namespace Runtime
33 {
34
35 Semaphore::Semaphore(void)
36         : __pSemaphoreImpl(null)
37 {
38
39 }
40
41 Semaphore::~Semaphore(void)
42 {
43         delete __pSemaphoreImpl;
44         __pSemaphoreImpl = null;
45 }
46
47 result
48 Semaphore::Create(int count)
49 {
50         return Create(L"", count);
51 }
52
53 result
54 Semaphore::Create(const String& name, int count)
55 {
56         // Object is not allowed to construct twice
57         SysAssertf(__pSemaphoreImpl == null,
58                 "Already constructed! Calling Construct() twice or more on a same instance is not allowed for this class");
59
60         SysTryReturnResult(NID_BASE_RT, count >= 0, E_INVALID_ARG, "The count(%d) MUST be greater than or equal to 0.", count);
61
62         __pSemaphoreImpl = new (std::nothrow) _SemaphoreImpl;
63         SysTryReturnResult(NID_BASE_RT, __pSemaphoreImpl != null, E_OUT_OF_MEMORY, "Not enough memory.");
64
65         result r = __pSemaphoreImpl->Create(name, count);
66         if (IsFailed(r))
67         {
68                 delete __pSemaphoreImpl;
69                 __pSemaphoreImpl = null;
70         }
71
72         return r;
73 }
74
75 result
76 Semaphore::Acquire(long timeout)
77 {
78         SysAssertf(__pSemaphoreImpl != null, "Not yet constructed! Construct() should be called before use.");
79         return __pSemaphoreImpl->Acquire(timeout);
80 }
81
82 result
83 Semaphore::TryToAcquire(void)
84 {
85         SysAssertf(__pSemaphoreImpl != null, "Not yet constructed! Construct() should be called before use.");
86         return __pSemaphoreImpl->TryToAcquire();
87 }
88
89 result
90 Semaphore::Release(void)
91 {
92         SysAssertf(__pSemaphoreImpl != null, "Not yet constructed! Construct() should be called before use.");
93         return __pSemaphoreImpl->Release();
94 }
95
96 } } } // Tizen::Runtime