1 /*---------------------------------------------------------------------------*
2   Project:  Horizon
3   File:     os_Mutex.cpp
4 
5   Copyright (C)2009-2012 Nintendo Co., Ltd.  All rights reserved.
6 
7   These coded instructions, statements, and computer programs contain
8   proprietary information of Nintendo of America Inc. and/or Nintendo
9   Company Ltd., and are protected by Federal copyright law.  They may
10   not be disclosed to third parties or copied or duplicated in any form,
11   in whole or in part, without the prior written consent of Nintendo.
12 
13   $Rev: 46347 $
14  *---------------------------------------------------------------------------*/
15 
16 #include <nn/os/os_Mutex.h>
17 #include <nn/assert.h>
18 #include <nn/config.h>
19 #include <nn/os/os_Result.h>
20 #include <nn/os/os_Synchronization.h>
21 #include <nn/svc/svc_Stub.h>
22 
23 #include "os_Limits.h"
24 
25 //---------------------------------------------------------------------------
26 
27 using namespace nn;
28 using namespace nn::svc;
29 
30 // C Function Definitions
31 
32 #include <new>
33 
34 using namespace nn::fnd;
35 using namespace nn::os;
36 
37 namespace nn { namespace os {
38 
39 #if NN_PLATFORM_HAS_MMU
GetCurrentCount()40     s32  Mutex::GetCurrentCount()
41     {
42         return os::detail::GetLimitCurrentCount(LIMITABLE_RESOURCE_MAX_MUTEX);
43     }
44 
GetMaxCount()45     s32  Mutex::GetMaxCount()
46     {
47         return os::detail::GetLimitMaxCount(LIMITABLE_RESOURCE_MAX_MUTEX);
48     }
49 #endif  // if NN_PLATFORM_HAS_MMU
50 
51 }}
52 
53 extern "C" {
54 
NN_UTIL_DETAIL_CLIBIMPL_DEFINE_CONVERSION(nnosMutexToWaitObject,nnosMutex,nnosWaitObject,Mutex,WaitObject)55 NN_UTIL_DETAIL_CLIBIMPL_DEFINE_CONVERSION(nnosMutexToWaitObject, nnosMutex, nnosWaitObject, Mutex, WaitObject)
56 NN_UTIL_DETAIL_CLIBIMPL_DEFINE_CONVERSION(nnosWaitObjectToMutex, nnosWaitObject, nnosMutex, WaitObject, Mutex)
57 
58 void nnosMutexInitialize(nnosMutex* p, bool initialLocked)
59 {
60     new (p) Mutex(initialLocked);
61 }
62 
nnosMutexTryInitialize(nnosMutex * p,bool initialLocked)63 bool nnosMutexTryInitialize(nnosMutex* p, bool initialLocked)
64 {
65     new (p) Mutex();
66     Mutex* pMutex = reinterpret_cast<Mutex*>(p);
67     Result result = pMutex->TryInitialize(initialLocked);
68     return result.IsSuccess();
69 }
70 
nnosMutexLock(nnosMutex * p)71 void nnosMutexLock(nnosMutex* p)
72 {
73     Mutex* pMutex = reinterpret_cast<Mutex*>(p);
74     pMutex->Lock();
75 }
76 
nnosMutexTryLock(nnosMutex * p,s64 timeout)77 bool nnosMutexTryLock(nnosMutex* p, s64 timeout)
78 {
79     Mutex* pMutex = reinterpret_cast<Mutex*>(p);
80     return pMutex->TryLock(TimeSpan::FromNanoSeconds(timeout));
81 }
82 
nnosMutexUnlock(nnosMutex * p)83 void nnosMutexUnlock(nnosMutex* p)
84 {
85     Mutex* pMutex = reinterpret_cast<Mutex*>(p);
86     pMutex->Unlock();
87 }
88 
nnosMutexFinalize(nnosMutex * p)89 void nnosMutexFinalize(nnosMutex* p)
90 {
91     Mutex* pMutex = reinterpret_cast<Mutex*>(p);
92     pMutex->~Mutex();
93 }
94 
95 }
96