blob: a121ba3f7052ca6ab154aa2e8e3957bfb40ea7ed (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
|
//
// Copyright 2018 Staysail Systems, Inc. <info@staysail.tech>
// Copyright 2018 Capitar IT Group BV <info@capitar.com>
//
// This software is supplied under the terms of the MIT License, a
// copy of which should be located in the distribution where this
// file was obtained (LICENSE.txt). A copy of the license may also be
// found online at https://opensource.org/licenses/MIT.
//
#include "core/nng_impl.h"
struct nni_pollable {
int p_rfd;
int p_wfd;
nni_mtx p_lock;
bool p_raised;
bool p_open;
};
int
nni_pollable_alloc(nni_pollable **pp)
{
nni_pollable *p;
if ((p = NNI_ALLOC_STRUCT(p)) == NULL) {
return (NNG_ENOMEM);
}
p->p_open = false;
p->p_raised = false;
nni_mtx_init(&p->p_lock);
*pp = p;
return (0);
}
void
nni_pollable_free(nni_pollable *p)
{
if (p == NULL) {
return;
}
if (p->p_open) {
nni_plat_pipe_close(p->p_rfd, p->p_wfd);
}
nni_mtx_fini(&p->p_lock);
NNI_FREE_STRUCT(p);
}
void
nni_pollable_raise(nni_pollable *p)
{
if (p == NULL) {
return;
}
nni_mtx_lock(&p->p_lock);
if (!p->p_raised) {
p->p_raised = true;
if (p->p_open) {
nni_mtx_unlock(&p->p_lock);
nni_plat_pipe_raise(p->p_wfd);
return;
}
}
nni_mtx_unlock(&p->p_lock);
}
void
nni_pollable_clear(nni_pollable *p)
{
if (p == NULL) {
return;
}
nni_mtx_lock(&p->p_lock);
if (p->p_raised) {
p->p_raised = false;
if (p->p_open) {
nni_mtx_unlock(&p->p_lock);
nni_plat_pipe_clear(p->p_rfd);
return;
}
}
nni_mtx_unlock(&p->p_lock);
}
int
nni_pollable_getfd(nni_pollable *p, int *fdp)
{
if (p == NULL) {
return (NNG_EINVAL);
}
nni_mtx_lock(&p->p_lock);
if (!p->p_open) {
int rv;
if ((rv = nni_plat_pipe_open(&p->p_wfd, &p->p_rfd)) != 0) {
nni_mtx_unlock(&p->p_lock);
return (rv);
}
p->p_open = true;
if (p->p_raised) {
nni_plat_pipe_raise(p->p_wfd);
}
}
nni_mtx_unlock(&p->p_lock);
*fdp = p->p_rfd;
return (0);
}
|