1 /*---------------------------------------------------------------------------*
2   Project:  Horizon
3   File:     osl_EventFlag.h
4 
5   Copyright (C)2009 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: 12546 $
14  *---------------------------------------------------------------------------*/
15 
16 #ifndef NN_NET_OSL_OSL_EVENTFLAG_H_
17 #define NN_NET_OSL_OSL_EVENTFLAG_H_
18 
19 //#define NN_NET_OSL_EVENTFLAG_IS_LIGHT
20 
21 #include <nn/fnd/fnd_Interlocked.h>
22 
23 #ifdef NN_NET_OSL_EVENTFLAG_IS_LIGHT
24 #include <nn/os/os_LightEvent.h>
25 #include <nn/os/os_Tick.h>
26 #include <nn/os/os_CriticalSection.h>
27 #else
28 #include <nn/os/os_Event.h>
29 #endif
30 
31 typedef enum
32 {
33     NN_NET_OSL_WAITMODE_AND,
34     NN_NET_OSL_WAITMODE_OR
35 } nnnetOslWaitMode;
36 
37 #ifdef __cplusplus
38 
39 namespace nn{ namespace net{ namespace osl{
40 
41 class EventFlag
42 {
43 #ifdef NN_NET_OSL_EVENTFLAG_IS_LIGHT
44     typedef os::LightEvent  EventImpl;
45 #else
46     typedef os::Event       EventImpl;
47 #endif
48 
49 public:
50     enum WaitMode
51     {
52         ModeAnd = NN_NET_OSL_WAITMODE_AND,
53         ModeOr  = NN_NET_OSL_WAITMODE_OR
54     };
55 
EventFlag()56     EventFlag()
57     : m_flag(0)
58     , m_indexForSleep(0)
59     {
60     }
61 
62     void Initialize(void);
63     Result TryInitialize(void);
64 
65     void Signal(bit32 pattern);
66     bit32 Wait(bit32 pattern, WaitMode mode, nn::fnd::TimeSpan timeout);
Wait(bit32 pattern,WaitMode mode)67     inline bit32 Wait(bit32 pattern, WaitMode mode)
68     {
69         return Wait(pattern, mode, nn::fnd::TimeSpan::FromNanoSeconds(NN_OS_WAIT_INFINITE));
70     }
71 
72     bit32 WaitAndClear(bit32 pattern, WaitMode mode, bit32 clear, nn::fnd::TimeSpan timeout);
WaitAndClear(bit32 pattern,WaitMode mode,bit32 clear)73     inline bit32 WaitAndClear(bit32 pattern, WaitMode mode, bit32 clear)
74     {
75         return WaitAndClear(pattern, mode, clear, nn::fnd::TimeSpan::FromNanoSeconds(NN_OS_WAIT_INFINITE));
76     }
77 
78     void ClearSignal(bit32 pattern);
79 protected:
80     void Notify();
81     bool WaitEventWithTimeout(EventImpl& event, nn::fnd::TimeSpan& timeout);
82     static void TimeoutHandler(EventFlag* pEventFlag, bool cancelled);
83 
InfiniteTimeSpan()84     static inline fnd::TimeSpan InfiniteTimeSpan()
85     {
86         return fnd::TimeSpan::FromNanoSeconds(-1);
87     }
88 
89 private:
AssertValid(void)90     inline void AssertValid(void) const
91     {
92         NN_THIS_TASSERT_();
93 #ifndef NN_NET_OSL_EVENTFLAG_IS_LIGHT
94         NN_TASSERT_(m_event[0].IsValid() && m_event[1].IsValid());
95 #endif
96     }
97 
98     EventImpl                           m_event[2];
99     nn::fnd::InterlockedVariable<bit32> m_flag;
100     s32                                 m_indexForSleep;
101 };
102 
103 }}} // namesapce nn::net::osl
104 
105 #endif // __cplusplus
106 
107 // 以下、C 用宣言
108 
109 #include <nn/util/detail/util_CLibImpl.h>
110 
111 #ifdef NN_NET_OSL_EVENTFLAG_IS_LIGHT
112 #define NN_NET_OSL_EVENTFLAG_SIZE                   16
113 #define NN_NET_OSL_EVENTFLAG_ALIGNMENT_HOLDER_TYPE  u32
114 #else
115 #define NN_NET_OSL_EVENTFLAG_SIZE                   16
116 #define NN_NET_OSL_EVENTFLAG_ALIGNMENT_HOLDER_TYPE  u32
117 #endif
118 
119 NN_UTIL_DETAIL_CLIBIMPL_DEFINE_BUFFER_CLASS(nnnetOslEventFlag, nn::net::osl::EventFlag, NN_NET_OSL_EVENTFLAG_SIZE, NN_NET_OSL_EVENTFLAG_ALIGNMENT_HOLDER_TYPE);
120 
121 NN_EXTERN_C void nnnetOslEventFlagInitialize(nnnetOslEventFlag* this_);
122 NN_EXTERN_C bool nnnetOslEventFlagTryInitialize(nnnetOslEventFlag* this_);
123 NN_EXTERN_C void nnnetOslEventFlagSignal(nnnetOslEventFlag* this_, bit32 pattern);
124 NN_EXTERN_C bit32 nnnetOslEventFlagWaitSignal(nnnetOslEventFlag* this_, bit32 pattern, nnnetOslWaitMode mode, s64 timeout);
125 NN_EXTERN_C void nnnetOslEventFlagClearSignal(nnnetOslEventFlag* this_, bit32 pattern);
126 NN_EXTERN_C bit32 nnnetOslEventFlagWaitAndClear(nnnetOslEventFlag* this_, bit32 pattern, nnnetOslWaitMode mode, bit32 clear, s64 timeout);
127 NN_EXTERN_C void nnnetOslEventFlagFinalize(nnnetOslEventFlag* this_);
128 
129 #endif // NN_NET_OSL_OSL_EVENTFLAG_H_
130