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 support
22 #ifndef DPL_WAITABLE_HANDLE_WATCH_SUPPORT_H
23 #define DPL_WAITABLE_HANDLE_WATCH_SUPPORT_H
25 #include <dpl/waitable_event.h>
26 #include <dpl/waitable_handle.h>
27 #include <dpl/exception.h>
28 #include <dpl/recursive_mutex.h>
37 class WaitableHandleWatchSupport
40 class WaitableHandleListener
43 virtual ~WaitableHandleListener() {}
45 virtual void OnWaitableHandleEvent(WaitableHandle waitableHandle, 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)
84 typedef std::list<WaitableHandleWatcher> WaitableHandleListenerList;
86 struct WaitableHandleWatchers
88 WaitableHandleListenerList listeners;
89 size_t readListenersCount;
90 size_t writeListenersCount;
92 WaitableHandleWatchers()
93 : readListenersCount(0),
94 writeListenersCount(0)
99 typedef std::map<WaitableHandle, WaitableHandleWatchers> WaitableHandleWatchersMap;
101 // Waitable event watch support
102 mutable RecursiveMutex m_watchersMutex;
103 WaitableHandleWatchersMap m_watchersMap;
104 WaitableEvent m_watchersInvoker;
105 WaitableEvent m_watchersInvokerCommit;
108 void CommitInvoker();
114 explicit WaitableHandleWatchSupport();
119 virtual ~WaitableHandleWatchSupport();
122 * Adds listener for specific waitable event
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
128 * @see WaitMode::Type
130 void AddWaitableHandleWatch(WaitableHandleListener *listener, WaitableHandle waitableHandle, WaitMode::Type mode);
133 * Remove listener for specific waitable event
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
139 * @see WaitMode::Type
141 void RemoveWaitableHandleWatch(WaitableHandleListener *listener, WaitableHandle waitableHandle, WaitMode::Type mode);
144 * Retrieve inherited context
146 * @return Inherited waitable handle watch support
148 static WaitableHandleWatchSupport *InheritedContext();
153 #endif // DPL_WAITABLE_HANDLE_WATCH_SUPPORT_H