2 * Copyright (c) 2013 Nicira, Inc.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
23 #include "poll-loop.h"
24 #include "socket-util.h"
26 /* Initializes 'latch' as initially unset. */
28 latch_init(struct latch *latch)
30 xpipe_nonblocking(latch->fds);
33 /* Destroys 'latch'. */
35 latch_destroy(struct latch *latch)
41 /* Resets 'latch' to the unset state. Returns true if 'latch' was previously
42 * set, false otherwise. */
44 latch_poll(struct latch *latch)
46 char buffer[_POSIX_PIPE_BUF];
48 return read(latch->fds[0], buffer, sizeof buffer) > 0;
53 * Calls are not additive: a single latch_poll() clears out any number of
56 latch_set(struct latch *latch)
58 ignore(write(latch->fds[1], "", 1));
61 /* Returns true if 'latch' is set, false otherwise. Does not reset 'latch'
62 * to the unset state. */
64 latch_is_set(const struct latch *latch)
69 pfd.fd = latch->fds[0];
72 retval = poll(&pfd, 1, 0);
73 } while (retval < 0 && errno == EINTR);
75 return pfd.revents & POLLIN;
78 /* Causes the next poll_block() to wake up when 'latch' is set.
80 * ('where' is used in debug logging. Commonly one would use latch_wait() to
81 * automatically provide the caller's source file and line number for
84 latch_wait_at(const struct latch *latch, const char *where)
86 poll_fd_wait_at(latch->fds[0], 0, POLLIN, where);