zhangmeng
2024-04-22 16935f4aebffdd1b6580b844391a0aa0f4f3012b
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
//
// Copyright 2020 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"
 
// We pack the wfd and rfd into a uint64_t so that we can update the pair
// atomically and use nni_atomic_cas64, to be lock free.
#define WFD(fds) ((int) ((fds) &0xffffffffu))
#define RFD(fds) ((int) (((fds) >> 32u) & 0xffffffffu))
#define FD_JOIN(wfd, rfd) ((uint64_t)(wfd) + ((uint64_t)(rfd) << 32u))
 
void
nni_pollable_init(nni_pollable *p)
{
    nni_atomic_init_bool(&p->p_raised);
    nni_atomic_set64(&p->p_fds, (uint64_t) -1);
}
 
void
nni_pollable_fini(nni_pollable *p)
{
    uint64_t fds;
 
    fds = nni_atomic_get64(&p->p_fds);
    if (fds != (uint64_t) -1) {
        int rfd, wfd;
        // Read in the high order, write in the low order.
        rfd = RFD(fds);
        wfd = WFD(fds);
        nni_plat_pipe_close(rfd, wfd);
    }
}
 
int
nni_pollable_alloc(nni_pollable **pp)
{
    nni_pollable *p;
    if ((p = NNI_ALLOC_STRUCT(p)) == NULL) {
        return (NNG_ENOMEM);
    }
    nni_pollable_init(p);
    *pp = p;
    return (0);
}
 
void
nni_pollable_free(nni_pollable *p)
{
    if (p == NULL) {
        return;
    }
    nni_pollable_fini(p);
    NNI_FREE_STRUCT(p);
}
 
void
nni_pollable_raise(nni_pollable *p)
{
    if (p == NULL) {
        return;
    }
    if (!nni_atomic_swap_bool(&p->p_raised, true)) {
        uint64_t fds;
        if ((fds = nni_atomic_get64(&p->p_fds)) != (uint64_t) -1) {
            nni_plat_pipe_raise(WFD(fds));
        }
    }
}
 
void
nni_pollable_clear(nni_pollable *p)
{
    if (p == NULL) {
        return;
    }
    if (nni_atomic_swap_bool(&p->p_raised, false)) {
        uint64_t fds;
        if ((fds = nni_atomic_get64(&p->p_fds)) != (uint64_t) -1) {
            nni_plat_pipe_clear(RFD(fds));
        }
    }
}
 
int
nni_pollable_getfd(nni_pollable *p, int *fdp)
{
    if (p == NULL) {
        return (NNG_EINVAL);
    }
 
    for (;;) {
        int      rfd;
        int      wfd;
        int      rv;
        uint64_t fds;
 
        if ((fds = nni_atomic_get64(&p->p_fds)) != (uint64_t) -1) {
            *fdp = RFD(fds);
            return (0);
        }
        if ((rv = nni_plat_pipe_open(&wfd, &rfd)) != 0) {
            return (rv);
        }
        fds = FD_JOIN(wfd, rfd);
 
        if (nni_atomic_cas64(&p->p_fds, (uint64_t) -1, fds)) {
            if (nni_atomic_get_bool(&p->p_raised)) {
                nni_plat_pipe_raise(wfd);
            }
            *fdp = rfd;
            return (0);
        }
 
        // Someone beat us.  Close ours, and try again.
        nni_plat_pipe_close(wfd, rfd);
    }
}