mirror of
https://github.com/troglobit/libuev.git
synced 2026-10-23 11:24:34 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
78995951ae | ||
|
|
3adc7a03c0 | ||
|
|
2abeb39fe8 | ||
|
|
1480976724 | ||
|
|
6ffa76c2f8 | ||
|
|
8a4e8c4af6 | ||
|
|
950015715d | ||
|
|
fb6ac1ecac | ||
|
|
0909decabc | ||
|
|
3452fbd381 | ||
|
|
f64561b708 | ||
|
|
b94023faed | ||
|
|
6637804b80 | ||
|
|
361b013da5 | ||
|
|
052b08e0b5 | ||
|
|
ca803bb12f | ||
|
|
6397edc2c4 | ||
|
|
cbd962c5b6 | ||
|
|
1f9558059a | ||
|
|
9e7f6865fc | ||
|
|
3b31079a1c | ||
|
|
63697e9eda | ||
|
|
ce3930f0cf | ||
|
|
bfd046054b | ||
|
|
6a40d51d26 | ||
|
|
f513408316 | ||
|
|
30b5faa318 | ||
|
|
fe8f3f19da | ||
|
|
71208b7e21 | ||
|
|
312b8257fb | ||
|
|
f29f3b7f38 | ||
|
|
cd4daf0d36 | ||
|
|
43acbadc13 |
No files matched your search
+1
-1
@@ -1,6 +1,6 @@
|
||||
bench
|
||||
test
|
||||
.*.d
|
||||
*.d
|
||||
*.o
|
||||
*~
|
||||
libuev.a
|
||||
|
||||
+4
-3
@@ -1,8 +1,9 @@
|
||||
# Travis CI integration -- https://travis-ci.org/troglobit/libuev
|
||||
# Defaults to GNU GCC and autotools: ./configure && make && make test
|
||||
language: c
|
||||
compiler:
|
||||
- gcc
|
||||
- clang
|
||||
|
||||
# We don't need to install packages, use dockerized build, quicker
|
||||
sudo: false
|
||||
|
||||
env:
|
||||
global:
|
||||
|
||||
+78
-4
@@ -3,8 +3,79 @@ Change Log
|
||||
|
||||
All notable changes to the project are documented in this file.
|
||||
|
||||
[v1.0.5]
|
||||
--------
|
||||
[v1.2.2] - 2015-08-30
|
||||
---------------------
|
||||
|
||||
Minor bugfix release.
|
||||
|
||||
### Changes
|
||||
- The `ifdef STATIC` in the Makefile has been removed. Now both the
|
||||
.a and .so libraries are built. The user may then select what to
|
||||
install. This is a change for convenience when using libuEv from
|
||||
a GNU Configure & Build based project.
|
||||
|
||||
### Fixes
|
||||
- Fix odd data ordering issue on Debian Jessie in new `uev_private_t`
|
||||
- Fix issue #6: Segfault when stopping timer before calling `uev_exit()`
|
||||
|
||||
|
||||
[v1.2.1] - 2015-07-02
|
||||
---------------------
|
||||
|
||||
### Changes
|
||||
- Private data members in `uev.h` have now been moved to a new file
|
||||
called `private.h`. This will hopefully make it easier to understand
|
||||
what a user of libuEv is allowed to play around with. Thanks to @vonj
|
||||
for the discussions around this!
|
||||
- All builds of libuEv now default to use -fPIC, this bloats the code
|
||||
slightly, but ensures that linking works for all use cases, withouth
|
||||
introducing unnecessary complexity.
|
||||
|
||||
### Fixes
|
||||
- Fix install/uninstall Makefile recipes so they work for both static
|
||||
and dynamic builds. Also, make sure to install all required headers.
|
||||
- Jakob Eriksson noticed that `O_CLOEXEC` does not exist in the Debian 6
|
||||
EGLIBC, but `EPOLL_CLOEXEC` does, and is also what `epoll_create1()`
|
||||
should use. Thank you @vonj!
|
||||
|
||||
|
||||
[v1.2.0] - 2015-06-09
|
||||
---------------------
|
||||
|
||||
API change in event callbacks and fix timers that never start.
|
||||
|
||||
### Changes
|
||||
- Remove first `uev_ctx_t *` argument in callbacks, incompatible API
|
||||
change! Please update all your callbacks if you upgrade.
|
||||
|
||||
### Fixes
|
||||
- Fix timers that accidentally broke in [v1.1.0].
|
||||
- Fixes to `bench.c`, it now actually listens to the pipe/socket.
|
||||
|
||||
|
||||
[v1.1.0] - 2015-03-04
|
||||
---------------------
|
||||
|
||||
Massively improved error handling.
|
||||
|
||||
### Changes
|
||||
- Handle case when user closes a descriptor *before* stopping a watcher.
|
||||
- Handle `EPOLLHUP` and `EPOLLERR`. Restart `epoll(7)` descriptor and
|
||||
all watchers when an error count reaches a MAX value -- handles stale
|
||||
descriptors or cases when kernel does not notice updated descriptors.
|
||||
- Return error when stopping a watcher fails.
|
||||
- Update [README.md] with new `uev_*_start()` functions.
|
||||
- Bump dev version to 1.1 due to the number of significant changes.
|
||||
|
||||
### Fixes
|
||||
- Remove `test.c` from `DISTFILES` in `Makefile`. You need the
|
||||
comeplete sources to build the examples now. Thanks to @karasz for
|
||||
the heads up on this and the musl libc issue with missing `queue.h`!
|
||||
- Fix broken link to [v1.0.5] in this file.
|
||||
|
||||
|
||||
[v1.0.5] - 2015-02-15
|
||||
---------------------
|
||||
|
||||
### Changes
|
||||
- Add `uev_*_start()` functions.
|
||||
@@ -85,8 +156,11 @@ v0.0.1 - 2012-03-17
|
||||
[Initial announcement] of [libuevent] by [Flemming Madsen] to the Lua
|
||||
users mailing list.
|
||||
|
||||
|
||||
[v1.0.4]: https://github.com/troglobit/libuev/compare/v1.0.4...v1.0.5
|
||||
[UNRELEASED]: https://github.com/troglobit/libuev/compare/v1.2.1...HEAD
|
||||
[v1.2.1]: https://github.com/troglobit/libuev/compare/v1.2.0...v1.2.1
|
||||
[v1.2.0]: https://github.com/troglobit/libuev/compare/v1.1.0...v1.2.0
|
||||
[v1.1.0]: https://github.com/troglobit/libuev/compare/v1.0.5...v1.1.0
|
||||
[v1.0.5]: https://github.com/troglobit/libuev/compare/v1.0.4...v1.0.5
|
||||
[v1.0.4]: https://github.com/troglobit/libuev/compare/v1.0.3...v1.0.4
|
||||
[v1.0.3]: https://github.com/troglobit/libuev/compare/v1.0.2...v1.0.3
|
||||
[v1.0.2]: https://github.com/troglobit/libuev/compare/v1.0.1...v1.0.2
|
||||
|
||||
@@ -1,31 +1,33 @@
|
||||
# libuEv main build file
|
||||
# libuEv -- Simple event loop for Linux
|
||||
#
|
||||
# Copyright (c) 2012 Flemming Madsen <flemming!madsen()madsensoft!dk>
|
||||
# Copyright (c) 2013-2015 Joachim Nilsson <troglobit()gmail!com>
|
||||
#
|
||||
# Permission is hereby granted, free of charge, to any person obtaining
|
||||
# a copy of this software and associated documentation files (the
|
||||
# "Software"), to deal in the Software without restriction, including
|
||||
# without limitation the rights to use, copy, modify, merge, publish,
|
||||
# distribute, sublicense, and/or sell copies of the Software, and to
|
||||
# permit persons to whom the Software is furnished to do so, subject to
|
||||
# the following conditions:
|
||||
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
# of this software and associated documentation files (the "Software"), to deal
|
||||
# in the Software without restriction, including without limitation the rights
|
||||
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
# copies of the Software, and to permit persons to whom the Software is
|
||||
# furnished to do so, subject to the following conditions:
|
||||
#
|
||||
# The above copyright notice and this permission notice shall be
|
||||
# included in all copies or substantial portions of the Software.
|
||||
# The above copyright notice and this permission notice shall be included in
|
||||
# all copies or substantial portions of the Software.
|
||||
#
|
||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
||||
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
||||
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
||||
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
# THE SOFTWARE.
|
||||
#
|
||||
.PHONY: all test clean joystick test examples
|
||||
.PHONY: all test clean joystick test examples bench
|
||||
|
||||
# Figure out root of library, unless used as submodule
|
||||
ROOTDIR ?= $(shell pwd)
|
||||
|
||||
#VERSION = $(shell git tag -l | tail -1)
|
||||
VERSION ?= 1.0.5
|
||||
VERSION ?= 1.2.2
|
||||
NAME = libuev
|
||||
PKG = $(NAME)-$(VERSION)
|
||||
ARCHIVE = $(PKG).tar.xz
|
||||
@@ -33,31 +35,36 @@ ARCHIVE = $(PKG).tar.xz
|
||||
CC ?= $(CROSS)gcc
|
||||
AR ?= $(CROSS)ar
|
||||
STRIP ?= $(CROSS)strip
|
||||
CFLAGS += -fPIC -Os
|
||||
MAKE := @$(MAKE)
|
||||
INSTALL := install --backup=off
|
||||
STRIPINST := $(INSTALL) -s --strip-program=$(CROSS)strip -m 0755
|
||||
|
||||
CFLAGS += -fPIC
|
||||
CFLAGS += -Os
|
||||
CPPFLAGS += -W -Wall -Werror
|
||||
ARFLAGS = crus
|
||||
JUNK = *~ *.bak *.map .*.d DEADJOE *.gdb *.elf core core.* *.html
|
||||
|
||||
MAKEFLAGS = --no-print-directory
|
||||
|
||||
ROOTDIR ?= $(shell pwd)
|
||||
LIBNAME = $(NAME)
|
||||
prefix ?= /usr/local
|
||||
libdir ?= $(prefix)/lib
|
||||
datadir ?= $(prefix)/share/doc/$(LIBNAME)
|
||||
incdir ?= $(prefix)/include
|
||||
DISTFILES = README CHANGELOG TODO LICENSE test.c
|
||||
HEADER = uev.h
|
||||
MAKEFLAGS = --no-print-directory --silent
|
||||
|
||||
DISTFILES = README CHANGELOG TODO LICENSE
|
||||
HEADERS = uev.h private.h queue.h
|
||||
OBJS := uev.o io.o timer.o signal.o
|
||||
SRCS := $(OBJS:.o=.c)
|
||||
DEPS := $(SRCS:.c=.d)
|
||||
JUNK = *~ *.bak *.map .*.d *.so* *.a DEADJOE *.gdb *.elf core core.* *.html
|
||||
|
||||
VER = 1
|
||||
LIBNAME = $(NAME)
|
||||
SOLIB = $(LIBNAME).so.$(VER)
|
||||
SYMLIB = $(LIBNAME).so
|
||||
STATICLIB = $(LIBNAME).a
|
||||
TARGET = $(STATICLIB) $(SOLIB)
|
||||
|
||||
# Default install paths
|
||||
prefix ?= /usr/local
|
||||
libdir ?= $(prefix)/lib
|
||||
datadir ?= $(prefix)/share/doc/$(LIBNAME)
|
||||
incdir ?= $(prefix)/include
|
||||
|
||||
export STATICLIB JUNK ROOTDIR CFLAGS CPPFLAGS
|
||||
|
||||
# This magic trick looks like a comment, but works on BSD PMake
|
||||
@@ -67,6 +74,8 @@ include rules.mk
|
||||
# Build rules
|
||||
all: $(TARGET)
|
||||
|
||||
$(OBJS): Makefile
|
||||
|
||||
$(SOLIB): Makefile $(OBJS)
|
||||
@printf " LINK $@\n"
|
||||
@$(CC) $(LDFLAGS) -shared -Wl,-soname=$@ -o $@ $(OBJS)
|
||||
@@ -75,31 +84,44 @@ $(STATICLIB): Makefile $(OBJS)
|
||||
@printf " ARCHIVE $@\n"
|
||||
@$(AR) $(ARFLAGS) $@ $(OBJS)
|
||||
|
||||
install: strip
|
||||
install-exec: all
|
||||
@printf " INSTALL $(DESTDIR)$(libdir)/$(SOLIB)\n"
|
||||
@install -d $(DESTDIR)$(libdir)
|
||||
@install $(SOLIB) $(DESTDIR)$(prefix)/lib/$(SOLIB)
|
||||
@install $(STATICLIB) $(DESTDIR)$(prefix)/lib/$(STATICLIB)
|
||||
@ln -sf $(SOLIB) $(DESTDIR)$(prefix)/lib/$(SYMLIB)
|
||||
@install -d $(DESTDIR)$(incdir)
|
||||
@install -m 0644 $(HEADER) $(DESTDIR)$(incdir)/$(HEADER)
|
||||
@install -d $(DESTDIR)$(datadir)
|
||||
@for file in $(DISTFILES); do \
|
||||
install -m 0644 $$file $(DESTDIR)$(datadir)/$$file; \
|
||||
@install $(SOLIB) $(DESTDIR)$(libdir)/$(SOLIB)
|
||||
@ln -sf $(SOLIB) $(DESTDIR)$(libdir)/$(SYMLIB)
|
||||
|
||||
install-dev:
|
||||
@install -d $(DESTDIR)$(incdir)/$(LIBNAME)
|
||||
@for file in $(HEADERS); do \
|
||||
printf " INSTALL $(DESTDIR)$(incdir)/$(LIBNAME)/$$file\n"; \
|
||||
install -m 0644 $$file $(DESTDIR)$(incdir)/$(LIBNAME)/$$file; \
|
||||
done
|
||||
@printf " INSTALL $(DESTDIR)$(libdir)/$(STATICLIB)\n"
|
||||
@install -d $(DESTDIR)$(libdir)
|
||||
@install $(STATICLIB) $(DESTDIR)$(libdir)/$(STATICLIB)
|
||||
@install -d $(DESTDIR)$(datadir)
|
||||
@for file in $(DISTFILES); do \
|
||||
printf " INSTALL $(DESTDIR)$(datadir)/$$file\n"; \
|
||||
install -m 0644 $$file $(DESTDIR)$(datadir)/$$file; \
|
||||
done
|
||||
|
||||
install: install-exec install-dev
|
||||
|
||||
uninstall:
|
||||
-@$(RM) -r $(DESTDIR)$(datadir)
|
||||
-@$(RM) -r $(DESTDIR)$(incdir)
|
||||
-@$(RM) $(DESTDIR)$(prefix)/lib/$(LIBNAME)*
|
||||
-@$(RM) -r $(DESTDIR)$(incdir)/$(LIBNAME)
|
||||
-@$(RM) $(DESTDIR)$(libdir)/$(SOLIB)
|
||||
-@$(RM) $(DESTDIR)$(libdir)/$(SYMLIB)
|
||||
-@$(RM) $(DESTDIR)$(libdir)/$(STATICLIB)
|
||||
|
||||
strip: $(TARGET)
|
||||
@printf " STRIP %s\n" $(TARGET)
|
||||
@$(STRIP) --strip-unneeded $(TARGET)
|
||||
@size $(TARGET)
|
||||
|
||||
bench: Makefile bench.o $(STATICLIB)
|
||||
@printf " BENCH %s\n" $(STATICLIB)
|
||||
@$(CC) $(CPPFLAGS) -g -o bench bench.c $(STATICLIB) && ./bench
|
||||
bench: Makefile $(STATICLIB)
|
||||
@printf " RUN %s\n" $@
|
||||
@$(CC) $(CPPFLAGS) -D_GNU_SOURCE -g -o bench bench.c $(STATICLIB) && ./bench
|
||||
|
||||
examples: all
|
||||
@$(MAKE) -C examples
|
||||
|
||||
@@ -24,14 +24,14 @@ often swing without consideration. Event loops and non-blocking I/O is
|
||||
often a far easier approach, as well as less error prone.
|
||||
|
||||
The purpose of many applications is, with a little logic sprinkled on
|
||||
top, to act upon: network packets entering an interface, timeouts
|
||||
expiring, mouse clicks, or other types of events. Such applications are
|
||||
often very well suited to use an event loop.
|
||||
top, to act on network packets entering an interface, timeouts expiring,
|
||||
mouse clicks, or other types of events. Such applications are often
|
||||
very well suited to use an event loop.
|
||||
|
||||
Applications that need to churn massively parallel algorithms are more
|
||||
suitable for running multiple (independent) threads on several CPU
|
||||
cores. However, threaded applications must deal with the side effects
|
||||
of concurrency, like: race conditions, deadlocks, live locks, etc.
|
||||
of concurrency, like race conditions, deadlocks, live locks, etc.
|
||||
Writing error free threaded applications is hard, debugging them can be
|
||||
even harder.
|
||||
|
||||
@@ -61,52 +61,82 @@ details, see [this article][4] at [lwn.net](http://lwn.net).
|
||||
API
|
||||
---
|
||||
|
||||
Here is the interface to [libuEv]. It handles three different types of
|
||||
events: I/O (files, sockets, message queues, etc.), timers, and signals.
|
||||
The C interface to [libuEv] is very simple. It handles three different
|
||||
types of events: I/O (files, sockets, message queues, etc.), timers, and
|
||||
signals. With a slight caveat on signals
|
||||
|
||||
```C
|
||||
/* Callback example, arg is passed from watcher's *_init()
|
||||
* w->fd holds the file descriptor, events is set by libuEv
|
||||
* to indicate if any of UEV_READ and/or UEV_WRITE is ready.
|
||||
*/
|
||||
void callback(uev_ctx_t *ctx, uev_t *w, void *arg, int events);
|
||||
/* Callback example, arg is passed from watcher's *_init()
|
||||
* w->fd holds the file descriptor, events is set by libuEv
|
||||
* to indicate if any of UEV_READ and/or UEV_WRITE is ready.
|
||||
*/
|
||||
void callback (uev_t *w, void *arg, int events);
|
||||
|
||||
/* Event loop functions, notice use of flags! */
|
||||
int uev_init (uev_ctx_t *ctx);
|
||||
int uev_exit (uev_ctx_t *ctx);
|
||||
int uev_run (uev_ctx_t *ctx, int flags); /* UEV_NONE, UEV_ONCE, and/or UEV_NONBLOCK */
|
||||
|
||||
/* I/O watcher: fd is non-blocking, events is UEV_READ and/or UEV_WRITE */
|
||||
int uev_io_init (uev_ctx_t *ctx, uev_t *w, uev_cb_t *cb, void *arg, int fd, int events);
|
||||
int uev_io_set (uev_t *w, int fd, int events);
|
||||
int uev_io_stop (uev_t *w);
|
||||
|
||||
/* Timer watcher: timeout and period in milliseconds */
|
||||
int uev_timer_init (uev_ctx_t *ctx, uev_t *w, uev_cb_t *cb, void *arg, int timeout, int period);
|
||||
int uev_timer_set (uev_t *w, int timeout, int period); /* Change timeout or period */
|
||||
int uev_timer_stop (uev_t *w); /* Stop a timer */
|
||||
|
||||
/* Signal watcher: signo is the signal to wait for, e.g., SIGTERM */
|
||||
int uev_signal_init(uev_ctx_t *ctx, uev_t *w, uev_cb_t *cb, void *arg, int signo);
|
||||
int uev_signal_set (uev_t *w, int signo); /* Change signal to wait for */
|
||||
int uev_signal_stop(uev_t *w); /* Stop signal watcher */
|
||||
/* Event loop functions, notice use of flags! */
|
||||
int uev_init (uev_ctx_t *ctx);
|
||||
int uev_exit (uev_ctx_t *ctx);
|
||||
int uev_run (uev_ctx_t *ctx, int flags); /* UEV_NONE, UEV_ONCE, and/or UEV_NONBLOCK */
|
||||
|
||||
/* I/O watcher: fd is non-blocking, events is UEV_READ and/or UEV_WRITE */
|
||||
int uev_io_init (uev_ctx_t *ctx, uev_t *w, uev_cb_t *cb, void *arg, int fd, int events);
|
||||
int uev_io_set (uev_t *w, int fd, int events);
|
||||
int uev_io_start (uev_t *w);
|
||||
int uev_io_stop (uev_t *w);
|
||||
|
||||
/* Timer watcher: timeout and period in milliseconds */
|
||||
int uev_timer_init (uev_ctx_t *ctx, uev_t *w, uev_cb_t *cb, void *arg, int timeout, int period);
|
||||
int uev_timer_set (uev_t *w, int timeout, int period); /* Change timeout or period */
|
||||
int uev_timer_start (uev_t *w); /* Restart a stopped timer */
|
||||
int uev_timer_stop (uev_t *w); /* Stop a timer */
|
||||
|
||||
/* Signal watcher: signo is the signal to wait for, e.g., SIGTERM */
|
||||
int uev_signal_init (uev_ctx_t *ctx, uev_t *w, uev_cb_t *cb, void *arg, int signo);
|
||||
int uev_signal_set (uev_t *w, int signo); /* Change signal to wait for */
|
||||
int uev_signal_start(uev_t *w); /* Restart a stopped signal watcher */
|
||||
int uev_signal_stop (uev_t *w); /* Stop signal watcher */
|
||||
```
|
||||
|
||||
To be able to set up callbacks to events the developer first need to
|
||||
create an *event context*, achieved by calling `uev_init()` with a
|
||||
pointer to a local `uev_ctx_t` variable.
|
||||
To monitor events the developer first creates an *event context*, this
|
||||
is achieved by calling `uev_init()` with a pointer to a (thread) local
|
||||
`uev_ctx_t` variable.
|
||||
|
||||
Events are monitored by watchers in [libuEv]. A watcher is nothing more
|
||||
than a mechnism that polls a file descriptor. Register a watcher with a
|
||||
callback to the event context by passing the `uev_ctx_t` variable, along
|
||||
with an `uev_t` variable to each event's `_init()` function.
|
||||
```C
|
||||
uev_ctx_t ctx;
|
||||
|
||||
When all watchers are registered call the event loop with `uev_run()`
|
||||
and the argument to the event context. The `flags` parameter is slightly
|
||||
mysterious, but can be used to integrate [libuEv] into another event loop.
|
||||
With `flags` set to `UEV_ONCE` the event loop returns after having
|
||||
served the first event. If `flags` is set to `UEV_ONCE | UEV_NONBLOCK`
|
||||
the event loop returns immediately if no event is available.
|
||||
uev_init(&ctx);
|
||||
```
|
||||
|
||||
Then, for each event to be monitored, a *watcher* is registered with the
|
||||
event context. The watcher, an `uev_t`, is initialized with the proper
|
||||
file descriptor to monitor, and an event callback function, by calling
|
||||
the event type's `_init()` function with the `uev_ctx_t` context.
|
||||
|
||||
```C
|
||||
void cleanup_exit(uev_t *w, void *arg, int events)
|
||||
{
|
||||
/* Graceful exit, with optional cleanup ... */
|
||||
uev_exit(w->ctx);
|
||||
}
|
||||
|
||||
uev_t termw;
|
||||
|
||||
uev_signal_init(&ctx, &term, cleanup_exit, NULL, SIGTERM);
|
||||
```
|
||||
|
||||
When all watchers are registered, call the *event loop* with `uev_run()`
|
||||
and the argument to the event context. The `flags` parameter can be
|
||||
used to integrate [libuEv] into another event loop. With `flags` set to
|
||||
`UEV_ONCE` the event loop returns after having served the first event.
|
||||
If `flags` is set to `UEV_ONCE | UEV_NONBLOCK` the event loop returns
|
||||
immediately if no event is available.
|
||||
|
||||
```C
|
||||
uev_run(&ctx, UEV_NONE);
|
||||
```
|
||||
|
||||
In case of errors, stream close, or peer shutdown, libuEv handles much
|
||||
internally, but also lets the callback run. This is useful for stateful
|
||||
connections to be able to detect EOF.
|
||||
|
||||
Summary:
|
||||
|
||||
@@ -120,7 +150,11 @@ Summary:
|
||||
bugs in event driven applications are due to sockets and files being
|
||||
opened in blocking mode. Be careful out there!
|
||||
|
||||
**Note 2:** As mentioned above, a certain amount of care is needed when
|
||||
**Note 2:** When closing a descriptor or socket, make sure to first stop
|
||||
your watcher, if possible. This will help prevent any nasty side
|
||||
effects on your program.
|
||||
|
||||
**Note 3:** As mentioned above, a certain amount of care is needed when
|
||||
dealing with signalfd. This means that if your application, for
|
||||
instance, uses `system()` you must redesign that to use `fork()`, and
|
||||
then in the child, unblock all signals blocked by your parent process,
|
||||
@@ -153,7 +187,7 @@ struct js_event {
|
||||
uint8_t number; /* axis/button number */
|
||||
} e;
|
||||
|
||||
static void joystick_cb(uev_ctx_t *ctx, uev_t *w, void *arg, int events)
|
||||
static void joystick_cb(uev_t *w, void *arg, int events)
|
||||
{
|
||||
read (w->fd, &e, sizeof(e));
|
||||
|
||||
@@ -259,4 +293,10 @@ used for inspiration is the very small [Picoev][9] by [Oku Kazuho].
|
||||
[Flemming Madsen]: http://www.madsensoft.dk
|
||||
[Dave Zarzycki, Apple]: http://www.youtube.com/watch?v=cD_s6Fjdri8
|
||||
|
||||
[libuEv] is developed and maintained by [Joachim Nilsson]
|
||||
[libuEv] is developed and maintained by [Joachim Nilsson].
|
||||
|
||||
<!--
|
||||
-- Local Variables:
|
||||
-- mode: markdown
|
||||
-- End:
|
||||
-->
|
||||
@@ -70,7 +70,7 @@ static int *pipes;
|
||||
static uev_t *evio;
|
||||
static uev_t *evto;
|
||||
|
||||
static void read_cb(uev_ctx_t *UNUSED(ctx), uev_t *w, void *arg, int UNUSED(events))
|
||||
static void read_cb(uev_t *w, void *arg, int UNUSED(events))
|
||||
{
|
||||
int idx, widx;
|
||||
u_char ch;
|
||||
@@ -96,7 +96,7 @@ static void read_cb(uev_ctx_t *UNUSED(ctx), uev_t *w, void *arg, int UNUSED(even
|
||||
}
|
||||
}
|
||||
|
||||
static void timer_cb(uev_ctx_t UNUSED(*ctx), uev_t UNUSED(*w), void UNUSED(*arg), int UNUSED(events))
|
||||
static void timer_cb(uev_t UNUSED(*w), void UNUSED(*arg), int UNUSED(events))
|
||||
{
|
||||
/* nop */
|
||||
}
|
||||
@@ -208,16 +208,17 @@ int main(int argc, char **argv)
|
||||
if (timers)
|
||||
uev_timer_init(&ctx, &evto[i], timer_cb, NULL, 0, 0);
|
||||
args[i].index = i;
|
||||
uev_io_init(&ctx, &evio[i], read_cb, &args[i], -1, UEV_READ);
|
||||
|
||||
#ifdef USE_PIPES
|
||||
if (pipe(cp) == -1) {
|
||||
if (pipe2(cp, O_NONBLOCK) == -1) {
|
||||
#else
|
||||
if (socketpair(AF_UNIX, SOCK_STREAM, 0, cp) == -1) {
|
||||
if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK, 0, cp) == -1) {
|
||||
#endif
|
||||
perror("pipe");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
uev_io_init(&ctx, &evio[i], read_cb, &args[i], cp[0], UEV_READ);
|
||||
}
|
||||
|
||||
for (i = 0; i < 2; i++)
|
||||
|
||||
+16
-17
@@ -2,24 +2,23 @@
|
||||
*
|
||||
* Copyright (c) 2014-2015 Joachim Nilsson <troglobit()gmail!com>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
||||
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
||||
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
||||
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <err.h>
|
||||
@@ -40,7 +39,7 @@ struct js_event {
|
||||
uint8_t number; /* axis/button number */
|
||||
} e;
|
||||
|
||||
static void joystick_cb(uev_ctx_t *UNUSED(ctx), uev_t *w, void *UNUSED(arg), int UNUSED(events))
|
||||
static void joystick_cb(uev_t *w, void *UNUSED(arg), int UNUSED(events))
|
||||
{
|
||||
if (read (w->fd, &e, sizeof(e)) < 0)
|
||||
errx(errno, "Failed reading joystick event");
|
||||
|
||||
+27
-22
@@ -2,24 +2,23 @@
|
||||
*
|
||||
* Copyright (c) 2015 Joachim Nilsson <troglobit@gmail.com>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
||||
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
||||
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
||||
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#define _GNU_SOURCE
|
||||
@@ -69,13 +68,13 @@ static int callback(arg_t *arg, int event, void *foo)
|
||||
return 42;
|
||||
}
|
||||
|
||||
static void sigsegv_cb(uev_ctx_t *UNUSED(ctx), uev_t *UNUSED(w), void *UNUSED(arg), int UNUSED(events))
|
||||
static void sigsegv_cb(uev_t *UNUSED(w), void *UNUSED(arg), int UNUSED(events))
|
||||
{
|
||||
warnx("PID %d caused segfault.", getpid());
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
static void sigchld_cb(uev_ctx_t *UNUSED(ctx), uev_t *UNUSED(w), void *UNUSED(arg), int UNUSED(events))
|
||||
static void sigchld_cb(uev_t *UNUSED(w), void *UNUSED(arg), int UNUSED(events))
|
||||
{
|
||||
pid_t pid = waitpid(-1, NULL, WNOHANG);
|
||||
|
||||
@@ -83,7 +82,7 @@ static void sigchld_cb(uev_ctx_t *UNUSED(ctx), uev_t *UNUSED(w), void *UNUSED(ar
|
||||
warnx("PID %d exited, bye.", pid);
|
||||
}
|
||||
|
||||
static void work_cb(uev_ctx_t *UNUSED(ctx), uev_t *UNUSED(w), void *arg, int UNUSED(events))
|
||||
static void work_cb(uev_t *UNUSED(w), void *arg, int UNUSED(events))
|
||||
{
|
||||
int status = 0;
|
||||
pid_t pid;
|
||||
@@ -102,13 +101,17 @@ static void work_cb(uev_ctx_t *UNUSED(ctx), uev_t *UNUSED(w), void *arg, int UNU
|
||||
if (WIFEXITED(status))
|
||||
printf("Child exited normally => %d\n", WEXITSTATUS(status));
|
||||
else if (WCOREDUMP(status))
|
||||
printf("Child crashed!\n");
|
||||
printf("Child crashed! %s\n", DO_SEGFAULT
|
||||
? "As expected, everything is OK."
|
||||
: "This should not happen!");
|
||||
else
|
||||
printf("Child did not exit normally!\n");
|
||||
}
|
||||
|
||||
static void exit_cb(uev_ctx_t *ctx, uev_t *UNUSED(w), void *UNUSED(arg), int UNUSED(events))
|
||||
static void exit_cb(uev_t *w, void *UNUSED(arg), int UNUSED(events))
|
||||
{
|
||||
printf("Process deadline reached, exiting!\n");
|
||||
uev_exit(ctx);
|
||||
uev_exit(w->ctx);
|
||||
}
|
||||
|
||||
int main(void)
|
||||
@@ -118,6 +121,8 @@ int main(void)
|
||||
|
||||
/* Initialize libuEv */
|
||||
uev_init(&ctx);
|
||||
|
||||
/* Setup callbacks */
|
||||
uev_signal_init(&ctx, &sigsegv_watcher, sigsegv_cb, NULL, SIGSEGV);
|
||||
uev_signal_init(&ctx, &sigchld_watcher, sigchld_cb, NULL, SIGCHLD);
|
||||
uev_timer_init(&ctx, &timeout_watcher, work_cb, &arg, 400, 0);
|
||||
|
||||
+23
-24
@@ -3,24 +3,23 @@
|
||||
* Copyright (c) 2012 Flemming Madsen <flemming!madsen()madsensoft!dk>
|
||||
* Copyright (c) 2013-2015 Joachim Nilsson <troglobit()gmail!com>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
||||
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
||||
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
||||
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <signal.h>
|
||||
@@ -40,33 +39,33 @@ static int in, out;
|
||||
static int period = 0;
|
||||
static uev_t *watchdog;
|
||||
|
||||
static void lifetime_cb(uev_ctx_t *ctx, uev_t UNUSED(*w), void *arg, int UNUSED(events))
|
||||
static void lifetime_cb(uev_t *w, void *arg, int UNUSED(events))
|
||||
{
|
||||
fprintf(stderr, "\nLifetime exceeded, program completed successfully! (arg:%p)\n", arg);
|
||||
uev_exit(ctx);
|
||||
uev_exit(w->ctx);
|
||||
}
|
||||
|
||||
/* The pipe watchdog, if it triggers we haven't received data in time. */
|
||||
static void timeout_cb(uev_ctx_t *ctx, uev_t *UNUSED(w), void *arg, int UNUSED(events))
|
||||
static void timeout_cb(uev_t *w, void *arg, int UNUSED(events))
|
||||
{
|
||||
watchdog = NULL;
|
||||
fprintf(stderr, "\nTimeout exceeded %p\n", arg);
|
||||
|
||||
// uev_timer_stop(w); <-- No need to stop timers with period=0 :)
|
||||
uev_exit(ctx);
|
||||
uev_exit(w->ctx);
|
||||
}
|
||||
|
||||
static void periodic_task(uev_ctx_t UNUSED(*ctx), uev_t UNUSED(*w), void UNUSED(*arg), int UNUSED(events))
|
||||
static void periodic_task(uev_t UNUSED(*w), void UNUSED(*arg), int UNUSED(events))
|
||||
{
|
||||
fprintf(stderr, "|");
|
||||
}
|
||||
|
||||
static void signal_cb(uev_ctx_t *UNUSED(ctx), uev_t *w, void *UNUSED(arg), int UNUSED(events))
|
||||
static void signal_cb(uev_t *w, void *UNUSED(arg), int UNUSED(events))
|
||||
{
|
||||
fprintf(stderr, w->signo == SIGINT ? "^Cv" : "^\v");
|
||||
}
|
||||
|
||||
static void pipe_read_cb(uev_ctx_t *UNUSED(ctx), uev_t UNUSED(*w), void UNUSED(*arg), int UNUSED(events))
|
||||
static void pipe_read_cb(uev_t UNUSED(*w), void UNUSED(*arg), int UNUSED(events))
|
||||
{
|
||||
int cnt;
|
||||
char msg[50];
|
||||
@@ -80,7 +79,7 @@ static void pipe_read_cb(uev_ctx_t *UNUSED(ctx), uev_t UNUSED(*w), void UNUSED(*
|
||||
fprintf(stderr, "%.*s.%d ", cnt, msg, cnt);
|
||||
}
|
||||
|
||||
static void pipe_write_cb(uev_ctx_t UNUSED(*ctx), uev_t *w, void *arg, int UNUSED(events))
|
||||
static void pipe_write_cb(uev_t *w, void *arg, int UNUSED(events))
|
||||
{
|
||||
my_t *my = arg;
|
||||
char *msg = "TESTING";
|
||||
|
||||
@@ -3,24 +3,23 @@
|
||||
* Copyright (c) 2012 Flemming Madsen <flemming!madsen()madsensoft!dk>
|
||||
* Copyright (c) 2013-2015 Joachim Nilsson <troglobit()gmail!com>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
||||
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
||||
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
||||
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <errno.h>
|
||||
@@ -62,11 +61,8 @@ int uev_io_init(uev_ctx_t *ctx, uev_t *w, uev_cb_t *cb, void *arg, int fd, int e
|
||||
*/
|
||||
int uev_io_set(uev_t *w, int fd, int events)
|
||||
{
|
||||
if (uev_io_stop(w))
|
||||
return -1;
|
||||
|
||||
/* Remove from internal list */
|
||||
LIST_REMOVE(w, link);
|
||||
/* Ignore any errors, only to clean up anything lingering ... */
|
||||
uev_io_stop(w);
|
||||
|
||||
return uev_io_init(w->ctx, w, (uev_cb_t *)w->cb, w->arg, fd, events);
|
||||
}
|
||||
@@ -90,9 +86,7 @@ int uev_io_start(uev_t *w)
|
||||
*/
|
||||
int uev_io_stop(uev_t *w)
|
||||
{
|
||||
int status = uev_watcher_stop(w);
|
||||
|
||||
return status;
|
||||
return uev_watcher_stop(w);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -0,0 +1,89 @@
|
||||
/* libuEv - Private methods and data types, do not use directly!
|
||||
*
|
||||
* Copyright (c) 2012 Flemming Madsen <flemming!madsen()madsensoft!dk>
|
||||
* Copyright (c) 2013-2015 Joachim Nilsson <troglobit()gmail!com>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#ifndef LIBUEV_PRIVATE_H_
|
||||
#define LIBUEV_PRIVATE_H_
|
||||
|
||||
#include <stdio.h>
|
||||
#include <sys/epoll.h>
|
||||
#include "queue.h" /* OpenBSD queue.h > old GLIBC version */
|
||||
|
||||
/* I/O, timer, or signal watcher */
|
||||
typedef enum {
|
||||
UEV_IO_TYPE = 1,
|
||||
UEV_TIMER_TYPE,
|
||||
UEV_SIGNAL_TYPE,
|
||||
} uev_type_t;
|
||||
|
||||
/* Event mask, used internally only. */
|
||||
#define UEV_EVENT_MASK (UEV_READ | UEV_WRITE)
|
||||
|
||||
/* Main libuEv context type */
|
||||
typedef struct {
|
||||
int running;
|
||||
int fd; /* For epoll() */
|
||||
uint32_t errors;
|
||||
LIST_HEAD(,uev) watchers;
|
||||
} uev_ctx_t;
|
||||
|
||||
/* Forward declare due to dependencys, don't try this at home kids. */
|
||||
struct uev;
|
||||
|
||||
/* This is used to hide all private data members in uev_t */
|
||||
#define uev_private_t \
|
||||
LIST_ENTRY(uev) link; /* For queue.h linked list */ \
|
||||
\
|
||||
int active; \
|
||||
int events; \
|
||||
\
|
||||
/* Watcher callback with optional argument */ \
|
||||
void (*cb)(struct uev *, void *, int); \
|
||||
void *arg; \
|
||||
\
|
||||
/* Timer watchers, time in milliseconds */ \
|
||||
int timeout; \
|
||||
int period; \
|
||||
\
|
||||
/* Signal watchers */ \
|
||||
int signo; \
|
||||
\
|
||||
/* Watcher type */ \
|
||||
uev_type_t
|
||||
|
||||
/* Internal API for dealing with generic watchers */
|
||||
int uev_watcher_init (uev_ctx_t *ctx, struct uev *w, uev_type_t type,
|
||||
void (*cb)(struct uev *, void *, int), void *arg,
|
||||
int fd, int events);
|
||||
int uev_watcher_start(struct uev *w);
|
||||
int uev_watcher_stop (struct uev *w);
|
||||
|
||||
#endif /* LIBUEV_PRIVATE_H_ */
|
||||
|
||||
/**
|
||||
* Local Variables:
|
||||
* version-control: t
|
||||
* indent-tabs-mode: t
|
||||
* c-file-style: "linux"
|
||||
* End:
|
||||
*/
|
||||
@@ -3,24 +3,23 @@
|
||||
* Copyright (c) 2012 Flemming Madsen <flemming!madsen()madsensoft!dk>
|
||||
* Copyright (c) 2013-2015 Joachim Nilsson <troglobit()gmail!com>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
||||
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
||||
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
||||
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <errno.h>
|
||||
|
||||
@@ -3,24 +3,23 @@
|
||||
* Copyright (c) 2012 Flemming Madsen <flemming!madsen()madsensoft!dk>
|
||||
* Copyright (c) 2013-2015 Joachim Nilsson <troglobit()gmail!com>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
||||
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
||||
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
||||
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <errno.h>
|
||||
@@ -123,11 +122,9 @@ int uev_timer_set(uev_t *w, int timeout, int period)
|
||||
msec2tspec(timeout, &time.it_value);
|
||||
msec2tspec(period, &time.it_interval);
|
||||
timerfd_settime(w->fd, 0, &time, NULL);
|
||||
|
||||
return uev_watcher_start(w);
|
||||
}
|
||||
|
||||
return 0;
|
||||
return uev_watcher_start(w);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -157,12 +154,12 @@ int uev_timer_start(uev_t *w)
|
||||
*/
|
||||
int uev_timer_stop(uev_t *w)
|
||||
{
|
||||
if (uev_watcher_stop(w))
|
||||
return -1;
|
||||
|
||||
/* Stop kernel timer */
|
||||
uev_timer_set(w, 0, 0);
|
||||
|
||||
if (uev_watcher_stop(w))
|
||||
return -1;
|
||||
|
||||
/* Close timerfd, will have to be reopened again on reset */
|
||||
close(w->fd);
|
||||
w->fd = -1;
|
||||
|
||||
@@ -3,27 +3,27 @@
|
||||
* Copyright (c) 2012 Flemming Madsen <flemming!madsen()madsensoft!dk>
|
||||
* Copyright (c) 2013-2015 Joachim Nilsson <troglobit()gmail!com>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
||||
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
||||
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
||||
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <errno.h>
|
||||
#include <fcntl.h> /* O_CLOEXEC */
|
||||
#include <string.h> /* memset() */
|
||||
#include <sys/epoll.h>
|
||||
#include <sys/signalfd.h> /* struct signalfd_siginfo */
|
||||
@@ -33,6 +33,28 @@
|
||||
|
||||
#define UNUSED(arg) arg __attribute__ ((unused))
|
||||
|
||||
|
||||
static int _init(uev_ctx_t *ctx, int close_old)
|
||||
{
|
||||
int fd = epoll_create1(EPOLL_CLOEXEC);
|
||||
|
||||
if (fd < 0)
|
||||
return -1;
|
||||
|
||||
if (close_old)
|
||||
close(ctx->fd);
|
||||
|
||||
ctx->fd = fd;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Simple check if a descriptor is still valid in the kernel */
|
||||
static int is_valid_fd(int fd)
|
||||
{
|
||||
return fcntl(fd, F_GETFL) != -1 || errno != EBADF;
|
||||
}
|
||||
|
||||
/* Private to libuEv, do not use directly! */
|
||||
int uev_watcher_init(uev_ctx_t *ctx, uev_t *w, uev_type_t type, uev_cb_t *cb, void *arg, int fd, int events)
|
||||
{
|
||||
@@ -41,16 +63,14 @@ int uev_watcher_init(uev_ctx_t *ctx, uev_t *w, uev_type_t type, uev_cb_t *cb, vo
|
||||
return -1;
|
||||
}
|
||||
|
||||
memset(w, 0, sizeof(*w));
|
||||
w->ctx = ctx;
|
||||
w->fd = fd;
|
||||
w->type = type;
|
||||
w->active = 0;
|
||||
w->fd = fd;
|
||||
w->cb = (void *)cb;
|
||||
w->arg = arg;
|
||||
w->events = events;
|
||||
|
||||
LIST_INSERT_HEAD(&w->ctx->watchers, w, link);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -67,13 +87,16 @@ int uev_watcher_start(uev_t *w)
|
||||
if (w->active)
|
||||
return 0;
|
||||
|
||||
ev.events = w->events;
|
||||
ev.events = w->events | EPOLLRDHUP;
|
||||
ev.data.ptr = w;
|
||||
if (epoll_ctl(w->ctx->fd, EPOLL_CTL_ADD, w->fd, &ev) < 0)
|
||||
return -1;
|
||||
|
||||
w->active = 1;
|
||||
|
||||
/* Add to internal list for bookkeeping */
|
||||
LIST_INSERT_HEAD(&w->ctx->watchers, w, link);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -88,10 +111,15 @@ int uev_watcher_stop(uev_t *w)
|
||||
if (!w->active)
|
||||
return 0;
|
||||
|
||||
/* Remove from kernel */
|
||||
epoll_ctl(w->ctx->fd, EPOLL_CTL_DEL, w->fd, NULL);
|
||||
w->active = 0;
|
||||
|
||||
/* Remove from internal list */
|
||||
LIST_REMOVE(w, link);
|
||||
|
||||
/* Remove from kernel */
|
||||
if (epoll_ctl(w->ctx->fd, EPOLL_CTL_DEL, w->fd, NULL) < 0)
|
||||
return -1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -103,17 +131,15 @@ int uev_watcher_stop(uev_t *w)
|
||||
*/
|
||||
int uev_init(uev_ctx_t *ctx)
|
||||
{
|
||||
int fd;
|
||||
|
||||
fd = epoll_create(1);
|
||||
if (fd < 0)
|
||||
if (!ctx) {
|
||||
errno = EINVAL;
|
||||
return -1;
|
||||
}
|
||||
|
||||
memset(ctx, 0, sizeof(*ctx));
|
||||
ctx->fd = fd;
|
||||
LIST_INIT(&ctx->watchers);
|
||||
|
||||
return 0;
|
||||
return _init(ctx, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -206,20 +232,54 @@ int uev_run(uev_ctx_t *ctx, int flags)
|
||||
|
||||
if (EINTR == errno)
|
||||
continue; /* Signalled, try again */
|
||||
exit:
|
||||
result = -1;
|
||||
ctx->running = 0;
|
||||
break;
|
||||
|
||||
/* Unrecoverable error, cleanup and exit with error. */
|
||||
uev_exit(ctx);
|
||||
return -1;
|
||||
}
|
||||
|
||||
for (i = 0; ctx->running && i < nfds; i++) {
|
||||
w = (uev_t *)events[i].data.ptr;
|
||||
|
||||
if (events[i].events & (EPOLLHUP | EPOLLERR)) {
|
||||
ctx->errors++;
|
||||
|
||||
if (ctx->errors >= 42) {
|
||||
uev_t *tmp, *retry = w;;
|
||||
|
||||
/* If not valid anymore, try to remove, ignore any errors. */
|
||||
if (!is_valid_fd(w->fd))
|
||||
uev_watcher_stop(w);
|
||||
|
||||
/* Must recreate epoll fd now ... */
|
||||
if (_init(ctx, 1)) {
|
||||
uev_exit(ctx);
|
||||
return -2;
|
||||
}
|
||||
|
||||
/* Restart watchers in new efd */
|
||||
LIST_FOREACH_SAFE(w, &ctx->watchers, link, tmp) {
|
||||
if (w->active) {
|
||||
w->active = 0;
|
||||
LIST_REMOVE(w, link);
|
||||
uev_watcher_start(w);
|
||||
}
|
||||
}
|
||||
uev_watcher_start(retry);
|
||||
|
||||
/* New efd, restart everything! */
|
||||
ctx->errors = 0;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
if (UEV_TIMER_TYPE == w->type) {
|
||||
uint64_t exp;
|
||||
|
||||
if (read(w->fd, &exp, sizeof(exp)) != sizeof(exp))
|
||||
goto exit;
|
||||
if (read(w->fd, &exp, sizeof(exp)) != sizeof(exp)) {
|
||||
uev_exit(ctx);
|
||||
return -3;
|
||||
}
|
||||
|
||||
if (!w->period)
|
||||
w->timeout = 0;
|
||||
@@ -229,12 +289,14 @@ int uev_run(uev_ctx_t *ctx, int flags)
|
||||
struct signalfd_siginfo fdsi;
|
||||
ssize_t sz = sizeof(fdsi);
|
||||
|
||||
if (read(w->fd, &fdsi, sz) != sz)
|
||||
goto exit;
|
||||
if (read(w->fd, &fdsi, sz) != sz) {
|
||||
uev_exit(ctx);
|
||||
return -4;
|
||||
}
|
||||
}
|
||||
|
||||
if (w->cb)
|
||||
w->cb((struct uev *)ctx, w, w->arg);
|
||||
w->cb(w, w->arg, events[i].events & UEV_EVENT_MASK);
|
||||
|
||||
if (UEV_TIMER_TYPE == w->type) {
|
||||
if (!w->timeout)
|
||||
|
||||
@@ -3,33 +3,29 @@
|
||||
* Copyright (c) 2012 Flemming Madsen <flemming!madsen()madsensoft!dk>
|
||||
* Copyright (c) 2013-2015 Joachim Nilsson <troglobit()gmail!com>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
||||
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
||||
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
||||
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#ifndef LIBUEV_UEV_H_
|
||||
#define LIBUEV_UEV_H_
|
||||
|
||||
#include <stdio.h>
|
||||
#include <sys/epoll.h>
|
||||
|
||||
#include "queue.h" /* OpenBSD queue.h > old GLIBC version */
|
||||
#include "private.h"
|
||||
|
||||
/* Max. number of simulateneous events */
|
||||
#define UEV_MAX_EVENTS 10
|
||||
@@ -43,53 +39,18 @@
|
||||
#define UEV_ONCE 1
|
||||
#define UEV_NONBLOCK 2
|
||||
|
||||
/* I/O, timer, or signal watcher */
|
||||
typedef enum {
|
||||
UEV_IO_TYPE = 1,
|
||||
UEV_TIMER_TYPE,
|
||||
UEV_SIGNAL_TYPE,
|
||||
} uev_type_t;
|
||||
|
||||
/* Forward declare due to dependencys, don't try this at home kids. */
|
||||
struct uev;
|
||||
|
||||
/* Main libuEv context type */
|
||||
typedef struct {
|
||||
int running;
|
||||
int fd; /* For epoll() */
|
||||
LIST_HEAD(,uev) watchers;
|
||||
} uev_ctx_t;
|
||||
|
||||
/* I/O event watcher */
|
||||
/* Event watcher */
|
||||
typedef struct uev {
|
||||
LIST_ENTRY(uev) link; /* For queue.h linked list */
|
||||
/* Private data for libuEv internal engine */
|
||||
uev_private_t type;
|
||||
|
||||
/* Common to all watchers */
|
||||
uev_ctx_t *ctx;
|
||||
uev_type_t type;
|
||||
int active;
|
||||
/* Public data for users to reference */
|
||||
int fd;
|
||||
int events;
|
||||
|
||||
/* Watcher callback with optional argument */
|
||||
void (*cb)(struct uev *, struct uev *, void *);
|
||||
void *arg;
|
||||
|
||||
/* Timer watchers, time in milliseconds */
|
||||
int timeout;
|
||||
int period;
|
||||
|
||||
/* Signal watchers */
|
||||
int signo;
|
||||
uev_ctx_t *ctx;
|
||||
} uev_t;
|
||||
|
||||
/* Generic callback for watchers */
|
||||
typedef void (uev_cb_t)(uev_ctx_t *ctx, uev_t *w, void *arg, int events);
|
||||
|
||||
/* Private methods, do not use directly! */
|
||||
int uev_watcher_init (uev_ctx_t *ctx, uev_t *w, uev_type_t type, uev_cb_t *cb, void *arg, int fd, int events);
|
||||
int uev_watcher_start (uev_t *w);
|
||||
int uev_watcher_stop (uev_t *w);
|
||||
typedef void (uev_cb_t)(uev_t *w, void *arg, int events);
|
||||
|
||||
/* Public interface */
|
||||
int uev_init (uev_ctx_t *ctx);
|
||||
|
||||
Reference in new issue
Block a user