Coverage Report

Created: 2025-10-10 06:39

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openvswitch/lib/latch-unix.c
Line
Count
Source
1
/*
2
 * Copyright (c) 2013 Nicira, Inc.
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at:
7
 *
8
 *     http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16
17
#include <config.h>
18
19
#include "latch.h"
20
#include <errno.h>
21
#include <poll.h>
22
#include <unistd.h>
23
#include "openvswitch/poll-loop.h"
24
#include "openvswitch/util.h"
25
#include "socket-util.h"
26
27
/* Initializes 'latch' as initially unset. */
28
void
29
latch_init(struct latch *latch)
30
0
{
31
0
    xpipe_nonblocking(latch->fds);
32
0
}
33
34
/* Destroys 'latch'. */
35
void
36
latch_destroy(struct latch *latch)
37
0
{
38
0
    close(latch->fds[0]);
39
0
    close(latch->fds[1]);
40
0
}
41
42
/* Resets 'latch' to the unset state.  Returns true if 'latch' was previously
43
 * set, false otherwise. */
44
bool
45
latch_poll(struct latch *latch)
46
0
{
47
0
    char latch_buffer[16];
48
0
    bool result = false;
49
0
    int ret;
50
51
0
    do {
52
0
        ret = read(latch->fds[0], &latch_buffer, sizeof latch_buffer);
53
0
        result |= ret > 0;
54
    /* Repeat as long as read() reads a full buffer. */
55
0
    } while (ret == sizeof latch_buffer);
56
57
0
    return result;
58
0
}
59
60
/* Sets 'latch'.
61
 *
62
 * Calls are not additive: a single latch_poll() clears out any number of
63
 * latch_set(). */
64
void
65
latch_set(struct latch *latch)
66
0
{
67
0
    ovs_ignore(write(latch->fds[1], "", 1));
68
0
}
69
70
/* Returns true if 'latch' is set, false otherwise.  Does not reset 'latch'
71
 * to the unset state. */
72
bool
73
latch_is_set(const struct latch *latch)
74
0
{
75
0
    struct pollfd pfd = {0};
76
0
    int retval;
77
78
0
    pfd.fd = latch->fds[0];
79
0
    pfd.events = POLLIN;
80
0
    do {
81
0
        retval = poll(&pfd, 1, 0);
82
0
    } while (retval < 0 && errno == EINTR);
83
84
0
    return pfd.revents & POLLIN;
85
0
}
86
87
/* Causes the next poll_block() to wake up when 'latch' is set.
88
 *
89
 * ('where' is used in debug logging.  Commonly one would use latch_wait() to
90
 * automatically provide the caller's source file and line number for
91
 * 'where'.) */
92
void
93
latch_wait_at(const struct latch *latch, const char *where)
94
0
{
95
    poll_fd_wait_at(latch->fds[0], POLLIN, where);
96
0
}