~ken-vandine/content-hub/fix_pending_check

« back to all changes in this revision

Viewing changes to tests/cross_process_sync.h

  • Committer: Thomas Voß
  • Date: 2013-07-15 11:07:50 UTC
  • Revision ID: thomas.voss@canonical.com-20130715110750-l9395s2sdimj0ccw
Initial checkin.

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 Lesser 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 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
 * Authored by: Thomas Voß <thomas.voss@canonical.com>
 
17
 */
 
18
 
 
19
#ifndef CROSS_PROCESS_SYNC_H_
 
20
#define CROSS_PROCESS_SYNC_H_
 
21
 
 
22
#include <cstring>
 
23
#include <stdexcept>
 
24
 
 
25
#include <unistd.h>
 
26
 
 
27
namespace test
 
28
{
 
29
struct CrossProcessSync
 
30
{
 
31
    static const int read_fd = 0;
 
32
    static const int write_fd = 1;
 
33
 
 
34
    CrossProcessSync()
 
35
    {
 
36
        if (pipe(fds) < 0)
 
37
            throw std::runtime_error(strerror(errno));
 
38
    }
 
39
 
 
40
    ~CrossProcessSync() noexcept
 
41
    {
 
42
        ::close(fds[0]);
 
43
        ::close(fds[1]);
 
44
    }
 
45
 
 
46
    void signal_ready()
 
47
    {
 
48
        int value = 42;
 
49
        if (!write(fds[write_fd], std::addressof(value), sizeof(value)))
 
50
            throw std::runtime_error(::strerror(errno));
 
51
    }
 
52
 
 
53
    void wait_for_signal_ready() const
 
54
    {
 
55
        int value;
 
56
        if (!read(fds[read_fd], std::addressof(value), sizeof(value)))
 
57
            throw std::runtime_error(::strerror(errno));
 
58
    }
 
59
 
 
60
    int fds[2];
 
61
};
 
62
}
 
63
 
 
64
#endif // CROSS_PROCESS_SYNC_H_