2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * @file waitable_handle_watch_support.h
18 * @author Przemyslaw Dobrowolski (p.dobrowolsk@samsung.com)
20 * @brief This file is the implementation file of waitable handle watch
23 #ifndef DPL_WAITABLE_HANDLE_WATCH_SUPPORT_H
24 #define DPL_WAITABLE_HANDLE_WATCH_SUPPORT_H
26 #include <dpl/waitable_event.h>
27 #include <dpl/waitable_handle.h>
28 #include <dpl/exception.h>
29 #include <dpl/recursive_mutex.h>
36 class WaitableHandleWatchSupport
39 class WaitableHandleListener
42 virtual ~WaitableHandleListener() {}
44 virtual void OnWaitableHandleEvent(WaitableHandle waitableHandle,
45 WaitMode::Type mode) = 0;
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;
54 // Waitable handle ex list
55 WaitableHandleListEx WaitableWatcherHandles() const;
57 // Perform actions for signaled waitable handle
58 // Called in execution context, after
59 void HandleWatcher(WaitableHandle waitableHandle, WaitMode::Type mode);
61 // Perform actions after invoker was signaled
62 void InvokerFinished();
64 // Get invoker context
65 virtual Thread *GetInvokerThread() = 0;
67 // Invoke direct invoker
68 virtual void HandleDirectInvoker() = 0;
71 // Waitable event watchers
72 struct WaitableHandleWatcher
74 WaitableHandleListener *listener;
77 WaitableHandleWatcher(WaitableHandleListener *l, WaitMode::Type m) :
83 typedef std::list<WaitableHandleWatcher> WaitableHandleListenerList;
85 struct WaitableHandleWatchers
87 WaitableHandleListenerList listeners;
88 size_t readListenersCount;
89 size_t writeListenersCount;
91 WaitableHandleWatchers() :
92 readListenersCount(0),
93 writeListenersCount(0)
97 typedef std::map<WaitableHandle,
98 WaitableHandleWatchers> WaitableHandleWatchersMap;
100 // Waitable event watch support
101 mutable RecursiveMutex m_watchersMutex;
102 WaitableHandleWatchersMap m_watchersMap;
103 WaitableEvent m_watchersInvoker;
104 WaitableEvent m_watchersInvokerCommit;
107 void CommitInvoker();
113 explicit WaitableHandleWatchSupport();
118 virtual ~WaitableHandleWatchSupport();
121 * Adds listener for specific waitable event
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
127 * @see WaitMode::Type
129 void AddWaitableHandleWatch(WaitableHandleListener *listener,
130 WaitableHandle waitableHandle,
131 WaitMode::Type mode);
134 * Remove listener for specific waitable event
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
140 * @see WaitMode::Type
142 void RemoveWaitableHandleWatch(WaitableHandleListener *listener,
143 WaitableHandle waitableHandle,
144 WaitMode::Type mode);
147 * Retrieve inherited context
149 * @return Inherited waitable handle watch support
151 static WaitableHandleWatchSupport *InheritedContext();
155 #endif // DPL_WAITABLE_HANDLE_WATCH_SUPPORT_H