|SocketSendTask| acquires a reference to |UnixSocketConsumer| on
the main thread and releases this reference on the I/O thread;
leading to race-conditions. This patch adds
- atomicity for ref-counting of |UnixSocketConsumer|, and
- stricter tests in the destructor of this class.
This patch cleans up runnables and tasks in UnixSocket.cpp. Every
runnable's name now ends in Runnable. There are a base classes for
runnables and tasks that hold reference to UnixSocketImpl and from
which all runnables and tasks inherit.
We cannot use blocking file-descriptor I/O on the I/O thread. This
patch adds an assertion to UnixFdWatcher the tests for the O_NONBLOCK
flag, when installing a file descriptor. In UnixFileWatcher, the
Open method tests for the O_NONBLOCK flag for the opened file.
File-descriptor flags for UnixSocketImpl et al are currently set by
UnixSocketImpl itself. Later patches should move this into the
methods of connector classes.
This patch cleans up the implementation of UnixSocketImpl by
- inlining all calls to SetUpIO,
- removing the unsued constant SOCKET_RETRY_TIME_MS, and
- separating logging from Bluetooth.
The base class UnixSocketWatcher handles the connection state of
the socket. UnixSocketImpl overrides UnixSocketWatcher's callback
methods for implementing it's functionality.
UnixSocketConsumer now maintains a delay for re-establishing closed
connection. The initial delay is zero, so that the first connect call
is processed immediately. At the first failed attempt to connect, the
delay is set to ~1 second, and is increased by a factor of 2 on each
successive error until it reaches ~1 minute.
The UnixSocketConsumer code resets the delay to zero if it is lower
than the time that the connection was open. This heuristics protects
against cases where the peer establishes a connection, and closes it
shortly afterwards. Incompatible versions of rild show this behavior.
UnixSocketImpl, which mostly runs on the I/O thread, doesn't control
its reference to UnixSocketConsumer. If the connection status is
stored in UnixSocketConsumer, the I/O thread can't read it safely.
This patch duplicates the connection status in UnixSocketImpl, where
reading from the I/O thread is safe. Methods of UnixSocketImpl don't
need to access mConsumer any longer to obtain the connection status.
We used to allocate memory on the stack when reading from a file
descriptor and copied the result into an instance of UnixSocketRawData.
This patch
- cleans up the interface of UnixSocketRawData,
- removes the large stack allocation (64KiB), and
- removes the unnecessary memcpy.
Other memcpys for sending data have been moved into the constructor
of UnixSocketRawData.
--HG--
extra : rebase_source : 46ed1c73481732c3f3350bf0bedb56d376c24e98
r=tbsaunde for accessible
r=jmuizelaar for gfx
r=roc for layout
r=glandium for mozglue
r=jduell for netwerk
r=khuey for everything else
This is a mechanical change made with sed. Later patches in this queue
clean up the whitespace errors and so on.
For "actively calling CloseSocket()" and "passively noticing the socket has been closed",
we want to ensure that the process of closing a socket are the same. Therefore I use a
runnable to call CloseSocket() on the main thread when it can't read from the socket.
When closing a socket from within the main thread, we need to make
sure that the I/O thread does not operate on the related instance of
UnixSocketImpl.
With this patch, the main thread posts a SocketCloseTask to the I/O
thread. The SocketCloseTask removes the socket from the I/O thread's
list of watched file descriptors and dispatches an instance of
DeleteInstanceRunnable for the socket's UnixSocketImpl, which cleans
up the data structures. These steps serialize the close operation
within the I/O thread, and ensure that the main thread processed all
other dispatched runnables that may use the UnixSocketImpl.
The UnixSocketImpl currently polls the socket file descriptor while
listening for incoming connections and schedules itself to run again
if no connection requests have been received.
This behavior interferes with closing the socket and deleting the
socket structure in the main thread. It can happen that the I/O thread
dispatches a SocketAcceptTask to poll the listening socket and the
main thread dispatches a DeleteInstanceRunnable for the UnixSocketImpl,
such that the delete operation gets dispatched before the poll
operation. The latter then operates on the just deleted UnixSocketImpl.
With this patch, the I/O thread watches the listing socket for incoming
connection requests and only attempts to run accept when connection
requests are pending. This allows to serialize polling and close
operations within the I/O thread in a sound order.
A side effect of this patch is that we don't constantly run code for
polling the listing socket, which should result in less CPU overhead
and save battery power.
The Bluetooth system internally uses UnixSocketImpl when transfering
files. When Bluetooth gets disabled during a file transfer, the IPC code
deletes any related instance of UnixSocketImpl. This can happen before all
pending SocketReceiveTasks have been processed by the main thread. The
implementation of SocketReceiveTask uses a reference to the instance of
UnixSocketImpl that has just deen disabled. This results in a segmantation
fault.
This patch fixes the problem by scheduling the delete operation for
UnixSocketImpl to be executed after any pending SocketReceiveTasks.