2 * Redistribution and use in source and binary forms, with or without
3 * modification, are permitted provided that the following conditions
5 * 1. Redistributions of source code must retain the above copyright
6 * notice, this list of conditions and the following disclaimer.
7 * 2. Redistributions in binary form must reproduce the above copyright
8 * notice, this list of conditions and the following disclaimer in the
9 * documentation and/or other materials provided with the distribution.
10 * 3. The name of the author may not be used to endorse or promote products
11 * derived from this software without specific prior written permission.
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 #include "event2/watch.h"
26 #include "event-internal.h"
27 #include "evthread-internal.h"
29 static inline struct evwatch *
30 evwatch_new(struct event_base *base, union evwatch_cb callback, void *arg, unsigned type)
32 struct evwatch *watcher = mm_malloc(sizeof(struct evwatch));
37 watcher->callback = callback;
39 EVBASE_ACQUIRE_LOCK(base, th_base_lock);
40 TAILQ_INSERT_TAIL(&base->watchers[type], watcher, next);
41 EVBASE_RELEASE_LOCK(base, th_base_lock);
46 evwatch_prepare_new(struct event_base *base, evwatch_prepare_cb callback, void *arg)
48 union evwatch_cb cb = { .prepare = callback };
49 return evwatch_new(base, cb, arg, EVWATCH_PREPARE);
53 evwatch_check_new(struct event_base *base, evwatch_check_cb callback, void *arg)
55 union evwatch_cb cb = { .check = callback };
56 return evwatch_new(base, cb, arg, EVWATCH_CHECK);
60 evwatch_base(struct evwatch *watcher)
66 evwatch_free(struct evwatch *watcher)
68 EVBASE_ACQUIRE_LOCK(watcher->base, th_base_lock);
69 TAILQ_REMOVE(&watcher->base->watchers[watcher->type], watcher, next);
70 EVBASE_RELEASE_LOCK(watcher->base, th_base_lock);
75 evwatch_prepare_get_timeout(const struct evwatch_prepare_cb_info *info, struct timeval *timeout)
78 *timeout = *(info->timeout);