~andreas-pokorny/mir/libinput-integration

« back to all changes in this revision

Viewing changes to src/server/asio_main_loop.cpp

  • Committer: Andreas Pokorny
  • Date: 2015-01-16 08:46:11 UTC
  • mfrom: (2098.1.21 evdev-input-platform)
  • Revision ID: andreas.pokorny@canonical.com-20150116084611-x06398gryu87ojeh
merged prereq

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Copyright © 2013 Canonical Ltd.
3
 
 *
4
 
 * This program is free software: you can redistribute it and/or modify it
5
 
 * under the terms of the GNU General Public License version 3,
6
 
 * as published by the Free Software Foundation.
7
 
 *
8
 
 * This program is distributed in the hope that it will be useful,
9
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
 
 * GNU General Public License for more details.
12
 
 *
13
 
 * You should have received a copy of the GNU General Public License
14
 
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15
 
 *
16
 
 * Authored by: Alexandros Frantzis <alexandros.frantzis@canonical.com>
17
 
 */
18
 
 
19
 
#include "mir/asio_main_loop.h"
20
 
#include "mir/time/clock.h"
21
 
 
22
 
#include "boost/date_time/posix_time/conversion.hpp"
23
 
 
24
 
#include <cassert>
25
 
#include <mutex>
26
 
#include <condition_variable>
27
 
 
28
 
namespace
29
 
{
30
 
 
31
 
void synchronous_server_action(
32
 
    mir::ServerActionQueue& queue,
33
 
    boost::optional<std::thread::id> queue_thread_id,
34
 
    mir::ServerAction const& action)
35
 
{
36
 
    if (!queue_thread_id || *queue_thread_id == std::this_thread::get_id())
37
 
    {
38
 
        try { action(); } catch(...) {}
39
 
    }
40
 
    else
41
 
    {
42
 
        std::mutex done_mutex;
43
 
        bool done{false};
44
 
        std::condition_variable done_condition;
45
 
 
46
 
        queue.enqueue(&queue, [&]
47
 
            {
48
 
                std::lock_guard<std::mutex> lock(done_mutex);
49
 
 
50
 
                try { action(); } catch(...) {}
51
 
 
52
 
                done = true;
53
 
                done_condition.notify_one();
54
 
            });
55
 
 
56
 
        std::unique_lock<std::mutex> lock(done_mutex);
57
 
        done_condition.wait(lock, [&] { return done; });
58
 
    }
59
 
}
60
 
 
61
 
struct MirClockTimerTraits
62
 
{
63
 
    // TODO the clock used by the main loop is a global setting, this is a restriction
64
 
    // of boost::asio only allowing static methods inside the taits type.
65
 
    struct TimerServiceClockStorage
66
 
    {
67
 
    public:
68
 
        void set_clock(std::shared_ptr<mir::time::Clock const> const& clock)
69
 
        {
70
 
            std::lock_guard<std::mutex> lock(timer_service_mutex);
71
 
            auto stored_clock = timer_service_clock.lock();
72
 
            if (stored_clock && stored_clock != clock)
73
 
                BOOST_THROW_EXCEPTION(std::logic_error("A clock is already in use as time source for mir::AsioMainLoop"));
74
 
            timer_service_clock = clock;
75
 
        }
76
 
        mir::time::Timestamp now()
77
 
        {
78
 
            std::lock_guard<std::mutex> lock(timer_service_mutex);
79
 
            auto clock = timer_service_clock.lock();
80
 
            if (!clock)
81
 
                BOOST_THROW_EXCEPTION(std::logic_error("No clock available to create time stamp"));
82
 
            return clock->now();
83
 
        }
84
 
    private:
85
 
        std::mutex timer_service_mutex;
86
 
        std::weak_ptr<mir::time::Clock const> timer_service_clock;
87
 
    };
88
 
 
89
 
    static TimerServiceClockStorage clock_storage;
90
 
 
91
 
    static void set_clock(std::shared_ptr<mir::time::Clock const> const& clock)
92
 
    {
93
 
        clock_storage.set_clock(clock);
94
 
    }
95
 
 
96
 
    // time_traits interface required by boost::asio::deadline_timer{_service}
97
 
    typedef mir::time::Timestamp time_type;
98
 
    typedef std::chrono::milliseconds duration_type;
99
 
 
100
 
 
101
 
    static time_type now()
102
 
    {
103
 
        return clock_storage.now();
104
 
    }
105
 
 
106
 
    static time_type add(const time_type& t, const duration_type& d)
107
 
    {
108
 
        return t + d;
109
 
    }
110
 
 
111
 
    static duration_type subtract(const time_type& t1, const time_type& t2)
112
 
    {
113
 
        return std::chrono::duration_cast<duration_type>(t1 - t2);
114
 
    }
115
 
 
116
 
    static bool less_than(const time_type& t1, const time_type& t2)
117
 
    {
118
 
        return t1 < t2;
119
 
    }
120
 
 
121
 
    static boost::posix_time::time_duration to_posix_duration(
122
 
        const duration_type& d)
123
 
    {
124
 
        return boost::posix_time::millisec(d.count());
125
 
    }
126
 
};
127
 
 
128
 
MirClockTimerTraits::TimerServiceClockStorage MirClockTimerTraits::clock_storage;
129
 
 
130
 
typedef boost::asio::basic_deadline_timer<mir::time::Timestamp, MirClockTimerTraits> deadline_timer;
131
 
}
132
 
 
133
 
class mir::AsioMainLoop::SignalHandler
134
 
{
135
 
public:
136
 
    SignalHandler(boost::asio::io_service& io,
137
 
                  std::initializer_list<int> signals,
138
 
                  std::function<void(int)> const& handler)
139
 
        : signal_set{io},
140
 
          handler{handler}
141
 
    {
142
 
        for (auto sig : signals)
143
 
            signal_set.add(sig);
144
 
    }
145
 
 
146
 
    void async_wait()
147
 
    {
148
 
        signal_set.async_wait(
149
 
            std::bind(&SignalHandler::handle, this,
150
 
                      std::placeholders::_1, std::placeholders::_2));
151
 
    }
152
 
 
153
 
private:
154
 
    void handle(boost::system::error_code err, int sig)
155
 
    {
156
 
        if (!err)
157
 
        {
158
 
            handler(sig);
159
 
            signal_set.async_wait(
160
 
                std::bind(&SignalHandler::handle, this,
161
 
                          std::placeholders::_1, std::placeholders::_2));
162
 
        }
163
 
    }
164
 
 
165
 
    boost::asio::signal_set signal_set;
166
 
    std::function<void(int)> handler;
167
 
};
168
 
 
169
 
class mir::AsioMainLoop::FDHandler
170
 
{
171
 
public:
172
 
    FDHandler(boost::asio::io_service& io,
173
 
              std::initializer_list<int> fds,
174
 
              void const* owner,
175
 
              std::function<void(int)> const& handler)
176
 
        : handler{handler}, owner{owner}
177
 
    {
178
 
        for (auto fd : fds)
179
 
        {
180
 
            auto raw = new boost::asio::posix::stream_descriptor{io, fd};
181
 
            auto s = std::unique_ptr<boost::asio::posix::stream_descriptor>(raw);
182
 
            stream_descriptors.push_back(std::move(s));
183
 
        }
184
 
    }
185
 
 
186
 
    ~FDHandler()
187
 
    {
188
 
        for (auto & desc : stream_descriptors)
189
 
        {
190
 
            desc->release(); // release native handle which is not owned by main loop
191
 
        }
192
 
    }
193
 
 
194
 
    bool is_owned_by(void const* possible_owner) const
195
 
    {
196
 
        return owner == possible_owner;
197
 
    }
198
 
 
199
 
    static void async_wait(std::shared_ptr<FDHandler> const& fd_handler, ServerActionQueue& queue)
200
 
    {
201
 
        for (auto const& s : fd_handler->stream_descriptors)
202
 
            read_some(s.get(), fd_handler, queue);
203
 
    }
204
 
 
205
 
private:
206
 
    static void read_some(boost::asio::posix::stream_descriptor* s, std::weak_ptr<FDHandler> const& possible_fd_handler, ServerActionQueue& queue)
207
 
    {
208
 
        s->async_read_some(
209
 
            boost::asio::null_buffers(),
210
 
            [possible_fd_handler,s,&queue](boost::system::error_code err, size_t /*bytes*/)
211
 
            {
212
 
                if (err)
213
 
                    return;
214
 
 
215
 
                // The actual execution of the fd handler is moved to the action queue.This allows us to synchronously
216
 
                // unregister FDHandlers even when they are about to be executed. We do this because of the way Asio
217
 
                // copies and executes pending completion handlers.
218
 
                // In worst case during the call to unregister the FDHandler, it may still be executed, but not after
219
 
                // the unregister call returned.
220
 
                queue.enqueue(
221
 
                    s,
222
 
                    [possible_fd_handler, s, &queue]()
223
 
                    {
224
 
                        auto fd_handler = possible_fd_handler.lock();
225
 
                        if (!fd_handler)
226
 
                            return;
227
 
 
228
 
                        fd_handler->handler(s->native_handle());
229
 
                        fd_handler.reset();
230
 
 
231
 
                        if (possible_fd_handler.lock())
232
 
                            read_some(s, possible_fd_handler, queue);
233
 
                    });
234
 
            });
235
 
    }
236
 
 
237
 
    std::vector<std::unique_ptr<boost::asio::posix::stream_descriptor>> stream_descriptors;
238
 
    std::function<void(int)> handler;
239
 
    void const* owner;
240
 
};
241
 
 
242
 
/*
243
 
 * We need to define the constructor and destructor in the .cpp file,
244
 
 * so that we can use unique_ptr to hold SignalHandler. Otherwise, users
245
 
 * of AsioMainLoop end up creating constructors and destructors that
246
 
 * don't have complete type information for SignalHandler and fail
247
 
 * to compile.
248
 
 */
249
 
mir::AsioMainLoop::AsioMainLoop(std::shared_ptr<time::Clock> const& clock)
250
 
    : work{io}, clock(clock)
251
 
{
252
 
    MirClockTimerTraits::set_clock(clock);
253
 
}
254
 
 
255
 
mir::AsioMainLoop::~AsioMainLoop() noexcept(true)
256
 
{
257
 
}
258
 
 
259
 
void mir::AsioMainLoop::run()
260
 
{
261
 
    main_loop_thread = std::this_thread::get_id();
262
 
    io.run();
263
 
}
264
 
 
265
 
void mir::AsioMainLoop::stop()
266
 
{
267
 
    io.stop();
268
 
    main_loop_thread.reset();
269
 
}
270
 
 
271
 
void mir::AsioMainLoop::register_signal_handler(
272
 
    std::initializer_list<int> signals,
273
 
    std::function<void(int)> const& handler)
274
 
{
275
 
    assert(handler);
276
 
 
277
 
    auto sig_handler = std::unique_ptr<SignalHandler>{
278
 
        new SignalHandler{io, signals, handler}};
279
 
 
280
 
    sig_handler->async_wait();
281
 
 
282
 
    signal_handlers.push_back(std::move(sig_handler));
283
 
}
284
 
 
285
 
void mir::AsioMainLoop::register_fd_handler(
286
 
    std::initializer_list<int> fds,
287
 
    void const* owner,
288
 
    std::function<void(int)> const& handler)
289
 
{
290
 
    assert(handler);
291
 
 
292
 
    auto fd_handler = std::make_shared<FDHandler>(io, fds, owner, handler);
293
 
 
294
 
    FDHandler::async_wait(fd_handler, *this);
295
 
 
296
 
    std::lock_guard<std::mutex> lock(fd_handlers_mutex);
297
 
    fd_handlers.push_back(std::move(fd_handler));
298
 
}
299
 
 
300
 
void mir::AsioMainLoop::unregister_fd_handler(void const* owner)
301
 
{
302
 
    // synchronous_server_action makes sure that with the
303
 
    // completion of the method unregister_fd_handler the
304
 
    // handler will no longer be called.
305
 
    // There is a chance for a fd handler callback to happen before
306
 
    // the completion of this method.
307
 
    synchronous_server_action(
308
 
        *this,
309
 
        main_loop_thread,
310
 
        [this,owner]()
311
 
        {
312
 
            std::lock_guard<std::mutex> lock(fd_handlers_mutex);
313
 
            auto end_of_valid = remove_if(
314
 
                begin(fd_handlers),
315
 
                end(fd_handlers),
316
 
                [owner](std::shared_ptr<FDHandler> const& item)
317
 
                {
318
 
                    return item->is_owned_by(owner);
319
 
                });
320
 
            fd_handlers.erase(end_of_valid, end(fd_handlers));
321
 
        });
322
 
}
323
 
 
324
 
namespace
325
 
{
326
 
class AlarmImpl : public mir::time::Alarm
327
 
{
328
 
public:
329
 
    AlarmImpl(boost::asio::io_service& io,
330
 
              std::chrono::milliseconds delay,
331
 
              std::function<void(void)> callback);
332
 
 
333
 
    AlarmImpl(boost::asio::io_service& io,
334
 
              mir::time::Timestamp time_point,
335
 
              std::function<void(void)> callback);
336
 
 
337
 
    AlarmImpl(boost::asio::io_service& io,
338
 
              std::function<void(void)> callback);
339
 
 
340
 
    ~AlarmImpl() noexcept override;
341
 
 
342
 
    bool cancel() override;
343
 
    State state() const override;
344
 
 
345
 
    bool reschedule_in(std::chrono::milliseconds delay) override;
346
 
    bool reschedule_for(mir::time::Timestamp time_point) override;
347
 
private:
348
 
    void stop();
349
 
    bool cancel_unlocked();
350
 
    void update_timer();
351
 
    struct InternalState
352
 
    {
353
 
        explicit InternalState(std::function<void(void)> callback)
354
 
            : callback{callback}, state{pending}
355
 
        {
356
 
        }
357
 
 
358
 
        std::mutex m;
359
 
        int callbacks_running = 0;
360
 
        std::condition_variable callback_done;
361
 
        std::function<void(void)> const callback;
362
 
        State state;
363
 
    };
364
 
 
365
 
    ::deadline_timer timer;
366
 
    std::shared_ptr<InternalState> const data;
367
 
    std::function<void(boost::system::error_code const& ec)> const handler;
368
 
 
369
 
    friend auto make_handler(std::weak_ptr<InternalState> possible_data)
370
 
    -> std::function<void(boost::system::error_code const& ec)>;
371
 
};
372
 
 
373
 
auto make_handler(std::weak_ptr<AlarmImpl::InternalState> possible_data)
374
 
-> std::function<void(boost::system::error_code const& ec)>
375
 
{
376
 
    // Awkwardly, we can't stop the async_wait handler from being called
377
 
    // on a destroyed AlarmImpl. This means we need to wedge a weak_ptr
378
 
    // into the async_wait callback.
379
 
    return [possible_data](boost::system::error_code const& ec)
380
 
    {
381
 
        if (!ec)
382
 
        {
383
 
            if (auto data = possible_data.lock())
384
 
            {
385
 
                std::unique_lock<decltype(data->m)> lock(data->m);
386
 
                if (data->state == mir::time::Alarm::pending)
387
 
                {
388
 
                    data->state = mir::time::Alarm::triggered;
389
 
                    ++data->callbacks_running;
390
 
                    lock.unlock();
391
 
                    data->callback();
392
 
                    lock.lock();
393
 
                    --data->callbacks_running;
394
 
                    data->callback_done.notify_all();
395
 
                }
396
 
            }
397
 
        }
398
 
    };
399
 
}
400
 
 
401
 
AlarmImpl::AlarmImpl(boost::asio::io_service& io,
402
 
                     std::chrono::milliseconds delay,
403
 
                     std::function<void ()> callback)
404
 
    : AlarmImpl(io, callback)
405
 
{
406
 
    reschedule_in(delay);
407
 
}
408
 
 
409
 
AlarmImpl::AlarmImpl(boost::asio::io_service& io,
410
 
                     mir::time::Timestamp time_point,
411
 
                     std::function<void ()> callback)
412
 
    : AlarmImpl(io, callback)
413
 
{
414
 
    reschedule_for(time_point);
415
 
}
416
 
 
417
 
AlarmImpl::AlarmImpl(boost::asio::io_service& io,
418
 
                     std::function<void(void)> callback)
419
 
    : timer{io},
420
 
      data{std::make_shared<InternalState>(callback)},
421
 
      handler{make_handler(data)}
422
 
{
423
 
    data->state = triggered;
424
 
}
425
 
 
426
 
AlarmImpl::~AlarmImpl() noexcept
427
 
{
428
 
    AlarmImpl::stop();
429
 
}
430
 
 
431
 
void AlarmImpl::stop()
432
 
{
433
 
    std::unique_lock<decltype(data->m)> lock(data->m);
434
 
 
435
 
    while (data->callbacks_running > 0)
436
 
        data->callback_done.wait(lock);
437
 
 
438
 
    cancel_unlocked();
439
 
}
440
 
 
441
 
bool AlarmImpl::cancel()
442
 
{
443
 
    std::lock_guard<decltype(data->m)> lock(data->m);
444
 
    return cancel_unlocked();
445
 
}
446
 
 
447
 
bool AlarmImpl::cancel_unlocked()
448
 
{
449
 
    if (data->state == triggered)
450
 
        return false;
451
 
 
452
 
    data->state = cancelled;
453
 
    timer.cancel();
454
 
    return true;
455
 
}
456
 
 
457
 
mir::time::Alarm::State AlarmImpl::state() const
458
 
{
459
 
    std::lock_guard<decltype(data->m)> lock(data->m);
460
 
 
461
 
    return data->state;
462
 
}
463
 
 
464
 
bool AlarmImpl::reschedule_in(std::chrono::milliseconds delay)
465
 
{
466
 
    bool cancelling = timer.expires_from_now(delay);
467
 
    update_timer();
468
 
    return cancelling;
469
 
}
470
 
 
471
 
bool AlarmImpl::reschedule_for(mir::time::Timestamp time_point)
472
 
{
473
 
    bool cancelling = timer.expires_at(time_point);
474
 
    update_timer();
475
 
    return cancelling;
476
 
}
477
 
 
478
 
void AlarmImpl::update_timer()
479
 
{
480
 
    std::lock_guard<decltype(data->m)> lock(data->m);
481
 
    data->state = pending;
482
 
    timer.async_wait(handler);
483
 
}
484
 
}
485
 
 
486
 
std::unique_ptr<mir::time::Alarm> mir::AsioMainLoop::notify_in(std::chrono::milliseconds delay,
487
 
                                                               std::function<void()> callback)
488
 
{
489
 
    return std::unique_ptr<mir::time::Alarm>{new AlarmImpl{io, delay, callback}};
490
 
}
491
 
 
492
 
std::unique_ptr<mir::time::Alarm> mir::AsioMainLoop::notify_at(mir::time::Timestamp time_point,
493
 
                                                               std::function<void()> callback)
494
 
{
495
 
    return std::unique_ptr<mir::time::Alarm>{new AlarmImpl{io, time_point, callback}};
496
 
}
497
 
 
498
 
std::unique_ptr<mir::time::Alarm> mir::AsioMainLoop::create_alarm(std::function<void()> callback)
499
 
{
500
 
    return std::unique_ptr<mir::time::Alarm>{new AlarmImpl{io, callback}};
501
 
}
502
 
 
503
 
void mir::AsioMainLoop::enqueue(void const* owner, ServerAction const& action)
504
 
{
505
 
    {
506
 
        std::lock_guard<std::mutex> lock{server_actions_mutex};
507
 
        server_actions.push_back({owner, action});
508
 
    }
509
 
 
510
 
    io.post([this] { process_server_actions(); });
511
 
}
512
 
 
513
 
void mir::AsioMainLoop::pause_processing_for(void const* owner)
514
 
{
515
 
    std::lock_guard<std::mutex> lock{server_actions_mutex};
516
 
    do_not_process.insert(owner);
517
 
}
518
 
 
519
 
void mir::AsioMainLoop::resume_processing_for(void const* owner)
520
 
{
521
 
    {
522
 
        std::lock_guard<std::mutex> lock{server_actions_mutex};
523
 
        do_not_process.erase(owner);
524
 
    }
525
 
 
526
 
    io.post([this] { process_server_actions(); });
527
 
}
528
 
 
529
 
void mir::AsioMainLoop::process_server_actions()
530
 
{
531
 
    std::unique_lock<std::mutex> lock{server_actions_mutex};
532
 
 
533
 
    size_t i = 0;
534
 
 
535
 
    while (i < server_actions.size())
536
 
    {
537
 
        /* 
538
 
         * It's safe to use references to elements, since std::deque<>
539
 
         * guarantees that references remain valid after appends, which is
540
 
         * the only operation that can be performed on server_actions outside
541
 
         * this function (in AsioMainLoop::post()).
542
 
         */
543
 
        auto const& owner = server_actions[i].first;
544
 
        auto const& action = server_actions[i].second;
545
 
 
546
 
        if (do_not_process.find(owner) == do_not_process.end())
547
 
        {
548
 
            lock.unlock();
549
 
            action();
550
 
            lock.lock();
551
 
            /*
552
 
             * This erase is always ok, since outside this function
553
 
             * we only append to server_actions, i.e., our index i
554
 
             * is guaranteed to remain valid and correct.
555
 
             */
556
 
            server_actions.erase(server_actions.begin() + i);
557
 
        }
558
 
        else
559
 
        {
560
 
            ++i;
561
 
        }
562
 
    }
563
 
}