copy from libevent

just for fun