1 /*---------------------------------------------------------------------------*
2   Project:  Horizon
3   File:     os_WaitableCounter.h
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 #ifndef NN_OS_OS_WAITABLE_COUNTER_H_
17 #define NN_OS_OS_WAITABLE_COUNTER_H_
18 
19 #ifdef __cplusplus
20 
21 #include <nn/fnd/fnd_Interlocked.h>
22 #include <nn/fnd/fnd_TimeSpan.h>
23 #include <nn/util/util_NonCopyable.h>
24 #include <nn/Handle.h>
25 #include <nn/Result.h>
26 #include <nn/svc.h>
27 
28 namespace nn { namespace os {
29 
30 class WaitableCounter
31 {
32 private:
33     static nnHandle s_Handle;
34 
35 private:
36     typedef nn::fnd::InterlockedVariable<s32> ValueType;
37     ValueType   m_Value;
38 
39 public:
40     static void Initialize();
41 
42     static void Finalize();
43 
44     ValueType& operator *() { return m_Value; }
45     const ValueType& operator *() const { return m_Value; }
46     ValueType* operator->() { return &m_Value; }
47 
DecrementAndWaitIfLessThan(s32 value,fnd::TimeSpan timeout)48     Result DecrementAndWaitIfLessThan(s32 value, fnd::TimeSpan timeout)
49     {
50         return ArbitrateAddress(nn::os::ARBITRATION_TYPE_DECREMENT_AND_WAIT_IF_LESS_THAN_WITH_TIMEOUT, value, timeout);
51     }
52 
WaitIfLessThan(s32 value,fnd::TimeSpan timeout)53     Result WaitIfLessThan(s32 value, fnd::TimeSpan timeout)
54     {
55         return ArbitrateAddress(nn::os::ARBITRATION_TYPE_WAIT_IF_LESS_THAN_WITH_TIMEOUT, value, timeout);
56     }
57 
DecrementAndWaitIfLessThan(s32 value)58     Result DecrementAndWaitIfLessThan(s32 value)
59     {
60         return ArbitrateAddress(nn::os::ARBITRATION_TYPE_DECREMENT_AND_WAIT_IF_LESS_THAN, value);
61     }
62 
WaitIfLessThan(s32 value)63     Result WaitIfLessThan(s32 value)
64     {
65         return ArbitrateAddress(nn::os::ARBITRATION_TYPE_WAIT_IF_LESS_THAN, value);
66     }
67 
Signal(s32 num)68     Result Signal(s32 num)
69     {
70         return ArbitrateAddress(nn::os::ARBITRATION_TYPE_SIGNAL, num);
71     }
72 
SignalAll()73     Result SignalAll()
74     {
75         return Signal(-1);
76     }
77 
78 private:
ArbitrateAddress(nn::os::ArbitrationType type,s32 value)79     Result ArbitrateAddress(nn::os::ArbitrationType type, s32 value)
80     {
81         return nn::svc::ArbitrateAddress(s_Handle, reinterpret_cast<uptr>(&m_Value), type, value, 0);
82     }
ArbitrateAddress(nn::os::ArbitrationType type,s32 value,fnd::TimeSpan timeout)83     Result ArbitrateAddress(nn::os::ArbitrationType type, s32 value, fnd::TimeSpan timeout)
84     {
85         return nn::svc::ArbitrateAddress(s_Handle, reinterpret_cast<uptr>(&m_Value), type, value, timeout.GetNanoSeconds());
86     }
87 };
88 
89 
90 }} // namespace nn::os
91 
92 #endif // __cplusplus
93 
94 #endif
95