265 Commits

Author SHA1 Message Date
Nick Mathewson
4e8eb6a595 When a signal callback is activated to run multiple times, allow event_base_loopbreak to work even before they all have run.
Found by Abilio Marques.
2011-09-09 20:57:54 -04:00
Nick Mathewson
5b18f13048 Make rate limiting work with common_timeout logic 2011-08-24 16:17:05 -04:00
Nick Mathewson
e7fe92709e Merge remote-tracking branch 'github/20_global_locks_init' into patches-2.0 2011-07-04 12:16:08 -04:00
Nick Mathewson
09fe97da3b Replace an assertion for event_base_free(NULL) with a check-and-warn
event_base_free(NULL) means "free the current event base".
Previously, it would assert if there was no 'current' base.  Now it
just warns and returns.

Reported by Gilad Benjamini
2011-06-08 14:24:45 -04:00
Nick Mathewson
27ce38b618 Avoid a segfault when all methods are disabled or broken 2011-06-01 17:28:21 -04:00
Nick Mathewson
b683cae3cb Avoid race-condition when initializing global locks
Previously, we did stuff like
   if (!lock)
      EVTHREAD_ALLOC_LOCK(lock,0);
for the evsig base global lock, the arc4random lock, and the debug_map
lock.  But that's potentially racy!  Instead, we move the
responisiblity for global lock initialization to the functions where
we set up the lock callbacks.

(Rationale: We already require that you set up the locking callbacks
before you create any event_base, and that you do so exatly once.)
2011-04-22 14:06:33 -04:00
Nick Mathewson
5209fadfd0 Disable lazy gettimeofday/clock_gettime comparison for now 2011-03-07 22:59:19 -05:00
Nick Mathewson
a459ef70ec Have event_base_gettimeofday_cached() always return wall-clock time
Based on code by Dave Hart
2011-03-03 15:48:26 -05:00
Nick Mathewson
5beeec9d43 Correctly notify the main thread when activating an event from a subthread 2010-12-01 21:28:03 -05:00
Nick Mathewson
2e5a175bf3 Merge remote branch 'github/20_once_fixes' 2010-11-25 23:03:46 -05:00
Nick Mathewson
652024b6b1 Remove _event_initialized(); make event_initialized() a function(); make it consistent on windows and non-windows 2010-11-23 13:08:07 -05:00
Nick Mathewson
3ab578f848 Make sure the CLOEXEC flag is set on fds we open for base notification 2010-11-20 01:41:34 -05:00
Nick Mathewson
2d5e1bd0be Do not let EVLOOP_ONCE exit the loop until all deferred callbacks have run 2010-11-14 19:32:13 -05:00
Nick Mathewson
0617a81820 Make EVLOOP_ONCE ignore internal events
Merely getting an internal notification event from having an event
added or deleted from another thread should not cause
event_base_loop(base, EVLOOP_ONCE) to exit; previously, it did.
2010-11-14 19:25:54 -05:00
Nick Mathewson
b81217f78d Fix signal handler types for win64. 2010-10-27 17:37:32 -04:00
Nick Mathewson
19c71e7454 Fix som event_warns that should have been event_warnx 2010-10-27 10:36:08 -04:00
Nick Mathewson
e56ff65af1 Fix a minor syntax error that most compilers didn't care about 2010-10-26 11:01:58 -04:00
Nick Mathewson
7ad90f6a25 Merge branch '20_internal_prio' 2010-10-09 00:02:31 -04:00
Nick Mathewson
e1198997bc Make event.c debugging messages report fds 2010-09-21 22:44:39 -04:00
Nick Mathewson
90651b327a Put internal events at highest priority
(If we allow user events to starve internal events, then internal events
never actually happen, signals don't get acked, etc)
2010-09-17 00:24:50 -04:00
Nick Mathewson
720bd933c8 Warn when using the error-prone EV_SIGNAL interface in an error-prone way. Also, fix a couple of race conditions in signal.c
When using the signal.c signal backend, Libevent currently only allows
one event_base to actually receive signals at a time.  (This has been
the behavior since at least 1.4 and probably much earlier.)  Now, we
detect and warn if you're likely to be racing about which signal goes
to which thread.

We also add a lock to control modifications of the evsig_base field,
to avoid race conditions like those found by Jason Toffaletti.

Also, more comments.  Comments are good.
2010-09-15 01:12:01 -04:00
Christopher Davis
d98511c027 Make event_base_virtual_del() notify the base if needed 2010-09-09 14:37:44 -04:00
Christopher Davis
71b6801421 Don't decrement virutal event count twice in connect_complete. 2010-09-08 20:33:21 -07:00
Nick Mathewson
3658b1696d Merge remote branch 'chrisd/iocp-fixes4'
Conflicts:
	test/regress_thread.c
2010-09-08 14:12:12 -04:00
Nick Mathewson
4632b78e01 Minimize calls to base_notify implementation functions, thereby avoiding needless syscalls
The trick here is that if we already told the base to wake up, and it
hasn't woken up yet, we don't need to tell it to wake up again.  This
should help lots with inherently multithreaded code like IOCP.
2010-09-08 13:22:55 -04:00
Nick Mathewson
c7a06bfaee Avoid needlessly calling evthread_notify_base() when the loop is not running
Also make sure that we always hold the base lock when calling evthread_notify_base.
2010-09-08 13:09:40 -04:00
Christopher Davis
17a14f1af2 Only process up to MAX_DEFERRED deferred_cbs at a time.
If threads queue callbacks while event_process_deferred_callbacks is
running, the loop may spin long enough to significantly skew timers.
A unit test stressing this behavior is also in this commit.
2010-09-08 01:22:22 -07:00
Christopher Davis
2447fe8886 Add event_config_set_num_cpus_hint for tuning thread pools, etc. 2010-09-08 01:22:22 -07:00
Christopher Davis
76f7e7ae74 Some IOCP bufferevent tweaks.
- Increment reference count of bufferevents before initiating overlapped
  operations to prevent the destructor from being called while operations
  are pending. The only portable way of canceling overlapped ops is to
  close the socket.

- Translate error codes to WSA* codes.

- Better handling of errors.

- Add an interface to add and del "virtual" events. Because IOCP
  bufferevents don't register any events with the base, the event loop
  has no way of knowing they exist. This causes the loop to terminate
  prematurely. event_base_{add,del}_virtual increment/decrement base's
  event count so the loop runs while there are any enabled IOCP
  bufferevents.
2010-09-08 01:22:22 -07:00
Christopher Davis
d844242f9b Stop IOCP when freeing the event_base. 2010-09-08 01:22:21 -07:00
Nick Mathewson
495ed66705 Close th_notify_fds and open a new pair on reinit
After a fork, you want subthreads to wake up the event_base in the
child process, not to have the child process and the main process
fight over who wakes up whom.

Related to a problem found by Nicholas Marriott while debugging
3048812.
2010-09-02 11:00:27 -04:00
Nick Mathewson
d61b2f3386 Fix an issue with forking and signal socketpairs in select/poll backends
Nicholas Marriott identifies an issue where a signal socketpair
doesn't get recreated if the event backend doesn't set event_reinit.

See bug 3048812

There may be a similar issue with respect to th_notify_fd
2010-08-26 15:11:35 -04:00
Nick Mathewson
f1074b776a Detect events with no ev_base; warn instead of crashing 2010-08-23 12:01:45 -04:00
Nick Mathewson
e0972c2138 Use conditions instead of current_event_lock to fix a deadlock.
Avi Bab correctly noted as bug 3044479 the fact that any thread
blocking on current_event_lock will do so while holding
th_base_lock, making it impossible for the currently running event's
callback to call any other functions that require th_base_lock.

This patch switches the current_event_lock code to instead use a
condition variable that we wait on if we're trying to mess with
a currently-executing event, and that we signal when we're done
executing a callback if anybody is waiting on it.
2010-08-17 13:18:18 -04:00
Nick Mathewson
ec347b9225 Move event-config.h to include/event2
This change means that all required include files are in event2, and
all files not in event2/* are optional.
2010-08-06 20:21:27 -04:00
Nick Mathewson
42a8c71112 Build more cleanly with NetBSDs that dislike toupper(char)
To be fair, when char can be signed, if toupper doesn't take negative
characters, toupper(char) is a very bad idea.  So let's just use the
nice safe EVUTIL_TOUPPER instead.  (It explicitly only upcases ASCII,
but we only use it for identifiers that we know to be ASCII anyway).
2010-07-31 17:10:04 -04:00
Nick Mathewson
fc5e0a2344 Don't race when calling event_active/event_add on a running signal event
There was previously no lock protecting the signal event's
ev_ncalls/ev_pncalls fields, which were accessed by all of
event_signal_closure, event_add_internal, event_del_internal, and
event_active_nolock.  This patch fixes this race by using the
current_event_lock in the same way it's used to prevent
event_del_internal from touching an event that's currently running.
2010-07-05 14:39:39 -04:00
Nick Mathewson
61e1eeec6f Tidy up the code in evthread_make_base_notifiable a little
Previously, we had a few conditional statements with the "if (...)"
inside an #ifdef and the body outside.  That kind of thing is hard to
read.
2010-07-05 13:24:45 -04:00
Nick Mathewson
a5bc15b2e1 Drain th_notify_fd[0] more bytes at a time. 2010-07-05 13:24:12 -04:00
Nick Mathewson
17522d2af8 Fix a deadlock related to event-base notification. Diagnosed by Zhou Li, Avi Bab, and Scott Lamb.
The problem was that the thread doing the notification could block on
write in evthread_notify_base_default while holding the th_base_lock.
The main thread would never drain th_notify_fd[0], since it would need
th_base_lock to actually trigger events.
2010-07-05 13:17:47 -04:00
Nick Mathewson
6d195109eb Avoid event_del on uninitialized event in event_base_free
This was mostly harmless, since the event was cleared with calloc, but
still it's not a correct thing to do.
2010-05-13 15:40:43 -04:00
Nick Mathewson
c16e68448c Rename current_base symbol to event_global_current_base_
The "current_base" symbol was never actually declared in an exported
header; it's hideously deprecated, and it was the one remaining
exported symbol (fwict) that was prefixed with neither ev nor
bufferevent nor _ev nor _bufferevent.

codesearch.google.com turns up no actual attempts to use our
current_base from outside libevent.
2010-05-04 13:27:36 -04:00
Nick Mathewson
99e50e90bd Fix symbol conflict between mm_*() macros and libmm
Our mm_malloc, mm_calloc, etc functions were all exported, since C
hasn't got a nice portable way to say "we want to use this function
inside our library but not export it to others".  But they apparently
conflict with anything else that calls its symbols mm_*, as libmm does.

This patch renames the mm_*() functions to event_mm_*_(, and defines
maros in mm_internal so that all the code we have that uses mm_*()
will still work.  New code should also prefer the mm_*() macro names.

Reported by Gernot Tenchio.  Fixes sf bug 2996541
2010-05-04 12:57:40 -04:00
Frank Denis
71afc52580 Fix nonstandard TAILQ_FOREACH_REVERSE() definition
Every current BSD system providing TAILQ_* macros define
TAILQ_FOREACH_REVERSE in this order:

TAILQ_FOREACH_REVERSE(var, head, field, headname)

However, libevent defines it in another order:

TAILQ_FOREACH_REVERSE(var, head, headname, field)

Here's a trivial patch to have libevent compatible with stock queue.h headers.

-Frank.

[From sourceforge patch 2995179. codesearch.google.com confirms that
the only people defining TAILQ_FOREACH_REVERSE our way are people
using it in a compatibility header like us.  Did we copy this from
OpenSSH or something?]

-Nick
2010-05-03 11:40:09 -04:00
Nick Mathewson
9ecf0d486d Catch attempts to enable debug_mode too late
Debug mode needs to be enabled before any event is setup or any
event_base is created.  Otherwise, we will not have recorded when events
were first setup or added, and so it will look like a bug later when we
delete or free them.

I have already confused myself because of this requirement, so let's
make Libevent catch it for the next poor forgetful developer like me.
2010-04-28 12:20:23 -04:00
Nick Mathewson
06a4443abe Unit-test every evbuffer_add_file() implementation.
Previously, we'd only test the default one, even if the others were still
compiled in.
2010-04-09 15:28:26 -04:00
Nick Mathewson
b557b175c0 Detect and refuse reentrant event_base_loop() calls
Calling event_base_loop on a base from inside a callback invoked by
that same base, or from two threads at once, has long been a way to
get exceedingly hard-to-diagnose errors.  This patch adds code to
detect such reentrant invocatinos, and exit quickly with a warning
that should explain what went wrong.
2010-03-21 13:28:48 -04:00
Nick Mathewson
70a44b61bb Avoid a spurious close(-1) on Linux
On Linux, we use only one fd to do main-thread signaling (since we have
eventfd()), so we don't need to close th_notify_fd[1] as we would if we were
using a socketpair.
2010-03-13 00:31:14 -05:00
Nick Mathewson
cdd4c4905b Try to comment some of the event code more 2010-03-12 00:38:50 -05:00
Nick Mathewson
2c2618d858 more whitespace normalization 2010-03-05 13:00:15 -05:00