~thomas-voss/location-service/fix-settings-not-being-applied

« back to all changes in this revision

Viewing changes to tests/cross_process_sync.h

  • Committer: Thomas Voß
  • Date: 2013-05-28 14:20:45 UTC
  • Revision ID: thomas.voss@canonical.com-20130528142045-kq5umqdmm4o53vwk
Initial push.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#ifndef CROSS_PROCESS_SYNC_H_
 
2
#define CROSS_PROCESS_SYNC_H_
 
3
 
 
4
#include <cstring>
 
5
#include <stdexcept>
 
6
 
 
7
#include <unistd.h>
 
8
 
 
9
namespace test
 
10
{
 
11
struct CrossProcessSync
 
12
{
 
13
    static const int read_fd = 0;
 
14
    static const int write_fd = 1;
 
15
 
 
16
    CrossProcessSync()
 
17
    {
 
18
        if (pipe(fds) < 0)
 
19
            throw std::runtime_error(strerror(errno));
 
20
    }
 
21
 
 
22
    ~CrossProcessSync() noexcept
 
23
    {
 
24
        ::close(fds[0]);
 
25
        ::close(fds[1]);
 
26
    }
 
27
 
 
28
    void signal_ready()
 
29
    {
 
30
        int value = 42;
 
31
        if (!write(fds[write_fd], std::addressof(value), sizeof(value)))
 
32
            throw std::runtime_error(::strerror(errno));
 
33
    }
 
34
 
 
35
    void wait_for_signal_ready() const
 
36
    {
 
37
        int value;
 
38
        if (!read(fds[read_fd], std::addressof(value), sizeof(value)))
 
39
            throw std::runtime_error(::strerror(errno));
 
40
    }
 
41
 
 
42
    int fds[2];
 
43
};
 
44
}
 
45
 
 
46
#endif // CROSS_PROCESS_SYNC_H_