0ff29b144ae32513ae4608a6aef15c705ee8c678
[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 support
21  */
22 #ifndef DPL_WAITABLE_HANDLE_WATCH_SUPPORT_H
23 #define DPL_WAITABLE_HANDLE_WATCH_SUPPORT_H
24
25 #include <dpl/waitable_event.h>
26 #include <dpl/waitable_handle.h>
27 #include <dpl/exception.h>
28 #include <dpl/recursive_mutex.h>
29 #include <list>
30 #include <map>
31
32 namespace DPL
33 {
34
35 class Thread;
36
37 class WaitableHandleWatchSupport
38 {
39 public:
40     class WaitableHandleListener
41     {
42     public:
43         virtual ~WaitableHandleListener() {}
44
45         virtual void OnWaitableHandleEvent(WaitableHandle waitableHandle, 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
84     typedef std::list<WaitableHandleWatcher> WaitableHandleListenerList;
85
86     struct WaitableHandleWatchers
87     {
88         WaitableHandleListenerList listeners;
89         size_t readListenersCount;
90         size_t writeListenersCount;
91
92         WaitableHandleWatchers()
93             : readListenersCount(0),
94               writeListenersCount(0)
95         {
96         }
97     };
98
99     typedef std::map<WaitableHandle, WaitableHandleWatchers> WaitableHandleWatchersMap;
100
101     // Waitable event watch support
102     mutable RecursiveMutex m_watchersMutex;
103     WaitableHandleWatchersMap m_watchersMap;
104     WaitableEvent m_watchersInvoker;
105     WaitableEvent m_watchersInvokerCommit;
106
107     // Invoke call
108     void CommitInvoker();
109
110 public:
111     /**
112      * Constructor
113      */
114     explicit WaitableHandleWatchSupport();
115
116     /**
117      * Destructor
118      */
119     virtual ~WaitableHandleWatchSupport();
120
121     /**
122      * Adds listener for specific waitable event
123      *
124      * @param[in] listener Listener to attach
125      * @param[in] waitableHandle Waitable handle to listen for changes
126      * @param[in] mode Type of changes to listen to
127      * @return none
128      * @see WaitMode::Type
129      */
130     void AddWaitableHandleWatch(WaitableHandleListener *listener, WaitableHandle waitableHandle, WaitMode::Type mode);
131
132     /**
133      * Remove listener for specific waitable event
134      *
135      * @param[in] listener Listener to detach
136      * @param[in] waitableHandle Waitable handle to unlisten for changes
137      * @param[in] mode Type of changes to unlisten to
138      * @return none
139      * @see WaitMode::Type
140      */
141     void RemoveWaitableHandleWatch(WaitableHandleListener *listener, WaitableHandle waitableHandle, WaitMode::Type mode);
142
143     /**
144      * Retrieve inherited context
145      *
146      * @return Inherited waitable handle watch support
147      */
148     static WaitableHandleWatchSupport *InheritedContext();
149 };
150
151 } // namespace DPL
152
153 #endif // DPL_WAITABLE_HANDLE_WATCH_SUPPORT_H