Initialize Tizen 2.3
[framework/web/wrt-commons.git] / modules / core / include / dpl / waitable_handle_watch_support.h
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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  * @file        waitable_handle_watch_support.h
18  * @author      Przemyslaw Dobrowolski (p.dobrowolsk@samsung.com)
19  * @version     1.0
20  * @brief       This file is the implementation file of waitable handle watch
21  * support
22  */
23 #ifndef DPL_WAITABLE_HANDLE_WATCH_SUPPORT_H
24 #define DPL_WAITABLE_HANDLE_WATCH_SUPPORT_H
25
26 #include <dpl/waitable_event.h>
27 #include <dpl/waitable_handle.h>
28 #include <dpl/exception.h>
29 #include <list>
30 #include <map>
31 #include <mutex>
32
33 namespace DPL {
34 class Thread;
35
36 class WaitableHandleWatchSupport
37 {
38   public:
39     class WaitableHandleListener
40     {
41       public:
42         virtual ~WaitableHandleListener() {}
43
44         virtual void OnWaitableHandleEvent(WaitableHandle waitableHandle,
45                                            WaitMode::Type mode) = 0;
46     };
47
48   protected:
49     // Invoker waitable handle
50     // Signaled by Add/Remove methods
51     // After being signaled one must call Handle invoke to reset invoker
52     WaitableHandle WaitableInvokerHandle() const;
53
54     // Waitable handle ex list
55     WaitableHandleListEx WaitableWatcherHandles() const;
56
57     // Perform actions for signaled waitable handle
58     // Called in execution context, after
59     void HandleWatcher(WaitableHandle waitableHandle, WaitMode::Type mode);
60
61     // Perform actions after invoker was signaled
62     void InvokerFinished();
63
64     // Get invoker context
65     virtual Thread *GetInvokerThread() = 0;
66
67     // Invoke direct invoker
68     virtual void HandleDirectInvoker() = 0;
69
70   private:
71     // Waitable event watchers
72     struct WaitableHandleWatcher
73     {
74         WaitableHandleListener *listener;
75         WaitMode::Type mode;
76
77         WaitableHandleWatcher(WaitableHandleListener *l, WaitMode::Type m) :
78             listener(l),
79             mode(m)
80         {}
81     };
82
83     typedef std::list<WaitableHandleWatcher> WaitableHandleListenerList;
84
85     struct WaitableHandleWatchers
86     {
87         WaitableHandleListenerList listeners;
88         size_t readListenersCount;
89         size_t writeListenersCount;
90
91         WaitableHandleWatchers() :
92             readListenersCount(0),
93             writeListenersCount(0)
94         {}
95     };
96
97     typedef std::map<WaitableHandle,
98                      WaitableHandleWatchers> WaitableHandleWatchersMap;
99
100     // Waitable event watch support
101     mutable std::recursive_mutex m_watchersMutex;
102     WaitableHandleWatchersMap m_watchersMap;
103     WaitableEvent m_watchersInvoker;
104     WaitableEvent m_watchersInvokerCommit;
105
106     // Invoke call
107     void CommitInvoker();
108
109   public:
110     /**
111      * Constructor
112      */
113     explicit WaitableHandleWatchSupport();
114
115     /**
116      * Destructor
117      */
118     virtual ~WaitableHandleWatchSupport();
119
120     /**
121      * Adds listener for specific waitable event
122      *
123      * @param[in] listener Listener to attach
124      * @param[in] waitableHandle Waitable handle to listen for changes
125      * @param[in] mode Type of changes to listen to
126      * @return none
127      * @see WaitMode::Type
128      */
129     void AddWaitableHandleWatch(WaitableHandleListener *listener,
130                                 WaitableHandle waitableHandle,
131                                 WaitMode::Type mode);
132
133     /**
134      * Remove listener for specific waitable event
135      *
136      * @param[in] listener Listener to detach
137      * @param[in] waitableHandle Waitable handle to unlisten for changes
138      * @param[in] mode Type of changes to unlisten to
139      * @return none
140      * @see WaitMode::Type
141      */
142     void RemoveWaitableHandleWatch(WaitableHandleListener *listener,
143                                    WaitableHandle waitableHandle,
144                                    WaitMode::Type mode);
145
146     /**
147      * Retrieve inherited context
148      *
149      * @return Inherited waitable handle watch support
150      */
151     static WaitableHandleWatchSupport *InheritedContext();
152 };
153 } // namespace DPL
154
155 #endif // DPL_WAITABLE_HANDLE_WATCH_SUPPORT_H