Solve race between checking for empty and event.

Both the output and empty event may fire spuriously.
However the empty event may only be the most recent event if the queue
is empty.  Due to a race between the check for empty and invoking the
associated event (which can not hold the lock during execution) the code
performs a secondary empty() test after firing the event.  If the second
test shows the message queue not empty, an output event is generated,
to solve the race condition.