source: vscpl/src/hu/varadiistvan/scpl/io/_posix/Waiter.h@ 33:ba64bedf956d

Last change on this file since 33:ba64bedf956d was 6:53519b47b166, checked in by István Váradi <ivaradi@…>, 12 years ago

Imported some further stuff from the IO code

File size: 4.7 KB
Line 
1// Copyright (c) 2013 by István Váradi
2
3// This file is part of VSCPL, a simple cross-platform utility library
4
5// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions are met:
7
8// 1. Redistributions of source code must retain the above copyright notice, this
9// list of conditions and the following disclaimer.
10// 2. Redistributions in binary form must reproduce the above copyright notice,
11// this list of conditions and the following disclaimer in the documentation
12// and/or other materials provided with the distribution.
13
14// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
15// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
18// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
23// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24
25// The views and conclusions contained in the software and documentation are those
26// of the authors and should not be interpreted as representing official policies,
27// either expressed or implied, of the FreeBSD Project.
28
29#ifndef HU_VARADIISTVAN_SCPL_IO_POSIX_WAITER_H
30#define HU_VARADIISTVAN_SCPL_IO_POSIX_WAITER_H
31//------------------------------------------------------------------------------
32
33#include "../Failable.h"
34
35#include <map>
36
37#include <cstdlib>
38
39#include <poll.h>
40
41//------------------------------------------------------------------------------
42
43namespace hu { namespace varadiistvan { namespace scpl { namespace io {
44
45//------------------------------------------------------------------------------
46
47class Waitable;
48
49//------------------------------------------------------------------------------
50
51/**
52 * Class that can be used to register waitable objects and then wait
53 * for them.
54 */
55class Waiter : public Failable
56{
57private:
58 /**
59 * Type for the set of waitables.
60 */
61 typedef std::map<int, Waitable*> waitables_t;
62
63 /**
64 * The set of waitables belonging to this waiter.
65 */
66 waitables_t waitables;
67
68 /**
69 * The number of waitables.
70 */
71 size_t numWaitables;
72
73public:
74 /**
75 * Construct the waiter.
76 */
77 Waiter();
78
79 /**
80 * Destroy the waiter. All waitables that still exists will be
81 * modified to point to no waiter.
82 */
83 ~Waiter();
84
85 /**
86 * Wait for any if the waitables to become ready or until a
87 * timeout expires.
88 *
89 * If any of the waitables are ready, the function returns immediately.
90 *
91 * @return true if any of the waitables has become ready, false if
92 * the timeout occurs.
93 */
94 bool wait(int timeout = -1);
95
96protected:
97 /**
98 * Add a waitable.
99 */
100 void add(Waitable* waitable);
101
102 /**
103 * Remove a waitable.
104 */
105 void remove(Waitable* waitable);
106
107private:
108 /**
109 * Determine if any of the waitables is ready.
110 */
111 bool hasReady() const;
112
113 /**
114 * Fill the given array of poll descriptors with those file
115 * descriptors that wait for an event.
116 *
117 * @param pollFDs the file descriptor array to fill. It should
118 * contain at least as many elements as the number of waitables.
119 *
120 * @return the number of valid poll file descriptors.
121 */
122 size_t setupPollFDs(pollfd* pollFDs);
123
124 /**
125 * Process the given array of poll descriptors. For each
126 * descriptor, if its events are not 0, the handleEvents()
127 * function of the corresponding waitable is called.
128 */
129 void processPollFDs(const pollfd* pollFDs, size_t size);
130
131 friend class Waitable;
132};
133
134//------------------------------------------------------------------------------
135// Inline definitions
136//------------------------------------------------------------------------------
137
138inline Waiter::Waiter() :
139 numWaitables(0)
140{
141}
142
143//------------------------------------------------------------------------------
144
145} /* namespace hu::varadiistvan::scpl::io */ } /* namespace hu::varadiistvan::scpl */ } /* namespace hu::varadiistvan */ } /* namespace hu */
146
147//------------------------------------------------------------------------------
148#endif // HU_VARADIISTVAN_SCPL_IO_POSIX_WAITER_H
149
150// Local Variables:
151// mode: C++
152// c-basic-offset: 4
153// indent-tabs-mode: nil
154// End:
Note: See TracBrowser for help on using the repository browser.