]> granicus.if.org Git - libevent/blob - watch.c
Merge pull request #1315 from yogo1212/http_per_socket_bebcb
[libevent] / watch.c
1 /*
2  * Redistribution and use in source and binary forms, with or without
3  * modification, are permitted provided that the following conditions
4  * are met:
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.
12  *
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.
23  */
24
25 #include "event2/watch.h"
26 #include "event-internal.h"
27 #include "evthread-internal.h"
28
29 static inline struct evwatch *
30 evwatch_new(struct event_base *base, union evwatch_cb callback, void *arg, unsigned type)
31 {
32         struct evwatch *watcher = mm_malloc(sizeof(struct evwatch));
33         if (!watcher)
34                 return NULL;
35         watcher->base = base;
36         watcher->type = type;
37         watcher->callback = callback;
38         watcher->arg = arg;
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);
42         return watcher;
43 }
44
45 struct evwatch *
46 evwatch_prepare_new(struct event_base *base, evwatch_prepare_cb callback, void *arg)
47 {
48         union evwatch_cb cb = { .prepare = callback };
49         return evwatch_new(base, cb, arg, EVWATCH_PREPARE);
50 }
51
52 struct evwatch *
53 evwatch_check_new(struct event_base *base, evwatch_check_cb callback, void *arg)
54 {
55         union evwatch_cb cb = { .check = callback };
56         return evwatch_new(base, cb, arg, EVWATCH_CHECK);
57 }
58
59 struct event_base *
60 evwatch_base(struct evwatch *watcher)
61 {
62         return watcher->base;
63 }
64
65 void
66 evwatch_free(struct evwatch *watcher)
67 {
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);
71         mm_free(watcher);
72 }
73
74 int
75 evwatch_prepare_get_timeout(const struct evwatch_prepare_cb_info *info, struct timeval *timeout)
76 {
77         if (info->timeout) {
78                 *timeout = *(info->timeout);
79                 return 1;
80         }
81         return 0;
82 }