~unity-api-team/storage-framework/vivid

« back to all changes in this revision

Viewing changes to include/unity/storage/provider/internal/MainLoopExecutor.h

  • Committer: Bileto Bot
  • Author(s): James Henstridge
  • Date: 2016-08-04 07:19:51 UTC
  • mfrom: (8.25.12 merge-devel)
  • Revision ID: ci-train-bot@canonical.com-20160804071951-3mfrcheyjvif6o99
Initial release of storage framework.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2016 Canonical Ltd
 
3
 *
 
4
 * This program is free software: you can redistribute it and/or modify
 
5
 * it under the terms of the GNU Lesser General Public License version 3 as
 
6
 * 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 Lesser General Public License for more details.
 
12
 *
 
13
 * You should have received a copy of the GNU Lesser General Public License
 
14
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
15
 *
 
16
 * Authors: James Henstridge <james.henstridge@canonical.com>
 
17
 */
 
18
 
 
19
#pragma once
 
20
 
 
21
#include <boost/version.hpp>
 
22
 
 
23
#if BOOST_VERSION >= 105600
 
24
#  define SF_SUPPORTS_EXECUTORS
 
25
#endif
 
26
#ifdef SF_SUPPORTS_EXECUTORS
 
27
#  include <boost/thread/executor.hpp>
 
28
#endif
 
29
#include <QObject>
 
30
 
 
31
#include <functional>
 
32
 
 
33
namespace unity
 
34
{
 
35
namespace storage
 
36
{
 
37
namespace provider
 
38
{
 
39
namespace internal
 
40
{
 
41
 
 
42
#ifdef SF_SUPPORTS_EXECUTORS
 
43
 
 
44
/* Declare future continuations like so to execute within the event
 
45
 * loop if possible:
 
46
 *
 
47
 *   auto f2 = f.then(EXEC_IN_MAIN [](decltype(f) f) { ... });
 
48
 *
 
49
 * On Boost >= 1.56, this will use a custom executor to run the
 
50
 * continuation as an event in the main thread.  On older versions,
 
51
 * the continuation will be executed in a new thread.
 
52
 */
 
53
 
 
54
#define EXEC_IN_MAIN MainLoopExecutor::instance(),
 
55
 
 
56
class MainLoopExecutor : public QObject, public boost::executors::executor {
 
57
    Q_OBJECT
 
58
public:
 
59
    static MainLoopExecutor& instance();
 
60
 
 
61
    void submit(work&& closure) override;
 
62
    void close() override;
 
63
    bool closed() override;
 
64
    bool try_executing_one() override;
 
65
 
 
66
    bool event(QEvent *event) override;
 
67
 
 
68
private:
 
69
    MainLoopExecutor();
 
70
    void execute(work& closure) noexcept;
 
71
 
 
72
    Q_DISABLE_COPY(MainLoopExecutor)
 
73
};
 
74
 
 
75
#else
 
76
 
 
77
#define EXEC_IN_MAIN /*nothing*/
 
78
 
 
79
#endif
 
80
 
 
81
}
 
82
}
 
83
}
 
84
}