netdev-dpdk: fix mbuf leaks
[cascardo/ovs.git] / lib / latch-windows.c
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 "poll-loop.h"
24 #include "socket-util.h"
25
26 /* Initializes 'latch' as initially unset. */
27 void
28 latch_init(struct latch *latch)
29 {
30     latch->is_set = FALSE;
31     latch->wevent = CreateEvent(NULL, TRUE, FALSE, NULL);
32 }
33
34 /* Destroys 'latch'. */
35 void
36 latch_destroy(struct latch *latch)
37 {
38     latch->is_set = FALSE;
39     CloseHandle(latch->wevent);
40 }
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 {
47     bool is_set;
48
49     is_set = latch->is_set;
50     latch->is_set = FALSE;
51     ResetEvent(latch->wevent);
52     return is_set;
53 }
54
55 /* Sets 'latch'.
56  *
57  * Calls are not additive: a single latch_poll() clears out any number of
58  * latch_set(). */
59 void
60 latch_set(struct latch *latch)
61 {
62     latch->is_set = TRUE;
63     SetEvent(latch->wevent);
64 }
65
66 /* Returns true if 'latch' is set, false otherwise.  Does not reset 'latch'
67  * to the unset state. */
68 bool
69 latch_is_set(const struct latch *latch)
70 {
71     return latch->is_set;
72 }
73
74 /* Causes the next poll_block() to wake up when 'latch' is set.
75  *
76  * ('where' is used in debug logging.  Commonly one would use latch_wait() to
77  * automatically provide the caller's source file and line number for
78  * 'where'.) */
79 void
80 latch_wait_at(const struct latch *latch, const char *where)
81 {
82     poll_wevent_wait_at(latch->wevent, where);
83 }