Reuse and extend the existing signal waker pipe mechanism to add user
defined signal handling functionality to uloop.
This commit introduces two new api functions `uloop_signal_add()` and
`uloop_signal_remove()` along with a new structure type `uloop_signal`
to allow adding and removing arbitrary signal handlers.
Registered signal handlers are maintained in a linked list and matched
by their signo member value which allows registering multiple handlers
for the same signal numbers.
Upon registering a new signal handler, the existing handler is saved
in the `uloop_signal` structure. When removing the user defined signal
handler, the original behavior is restored.
The Lua binding has been updated as well to support the new signal
handler mechanism.
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
So far, the only way to implement periodic interval timers was to use
one-shot uloop_timeout timers which are rearmed within their completion
callback immediately on expiration.
While simple, this approach is not very precise and interval lengths will
slowly drift over time, due to callback execution overhead, scheduling
granularity etc.
In order to make uloop provide stable and precise interval timer
capabilities, this commit introduces a new `uloop_interval` structure
along with the new related `uloop_interval_set()`, `uloop_interval_cancel()`
and `uloop_interval_remaining()` api functions.
Periodic timers are implemented using the timerfd facility an Linux and
kqueue EVFILT_TIMER events on macOS/BSD.
The Lua binding has been updated to include support for the new timer type
as well.
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
Instead of having to adjust the index repeatedly as the stack is
manipulated, use absolute addressing for the function arguments, so they
stay the same throughout the call. Zero functional change, just
subjectively easier to follow variables.
Signed-off-by: Karl Palsson <karlp@etactica.com>
Actually check for flags being valid, instead of simply ignoring the
call if flags was zero.
Use standard lua checks for the function argument, so you can get a
normal "argument #2 was invalid, expected function, got xxx" instead of
the vague, "invalid arg list"
Signed-off-by: Karl Palsson <karlp@etactica.com>
Consistently handle allocation failures. Some functions are changed to
return bool or int instead of void to allow returning an error.
Also fix a buffer size miscalculation in lua/uloop and use _exit() instead
of exit() on errors after forking.
Signed-off-by: Matthias Schiffer <mschiffer@universe-factory.net>
Different distributions have different names for the lua 5.1 package.
Use cmake's built in pkg-config support to search for the first one,
rather than running it explicitly and searching for a single version.
Signed-off-by: Karl Palsson <karlp@remake.is>
fd and timeout lua object has a __gc method in its metatable. After the object
is freed and the another new object use the same reference in __uloop_cb and
__uloop_fds, the new object will be freed by the old __gc of the old object
when garbag collecting.
Signed-off-by: Xiongfei(Alex) Guo <xfguo@credosemi.com>
When you call the fd_add, it will return an object with `delete` method.
So you can delete that event if you want.
Signed-off-by: Xiongfei(Alex) Guo <xfguo@credosemi.com>
Use uloop.fd_add like this:
local socket = require "socket"
udp = socket.udp()
uloop.fd_add(
udp, -- socket
function( -- callback function
ufd, -- socket object when register the fd
events -- uloop events. eg. uloop.ULOOP_READ .
)
local words, msg_or_ip, port_or_nil = ufd:receivefrom()
print('Recv UDP packet from '..msg_or_ip..':'..port_or_nil..' : '..words)
end,
uloop.ULOOP_READ -- event you want to listen
)
The `examples/uloop-example.lua` show an example of this work.
Signed-off-by: Xiongfei(Alex) Guo <xfguo@credosemi.com>