Commit 9fb4ab90 authored by Olivier Crête's avatar Olivier Crête
Browse files

pseudotcp: Remove unnecessary mutex locking

parent 241f5af1
......@@ -626,13 +626,16 @@ static const gchar *pseudo_tcp_state_get_name (PseudoTcpState state);
static gboolean pseudo_tcp_state_has_sent_fin (PseudoTcpState state);
static gboolean pseudo_tcp_state_has_received_fin (PseudoTcpState state);
static gboolean pseudo_tcp_state_has_received_fin_ack (PseudoTcpState state);
static gboolean pseudo_tcp_socket_is_closed_locked (PseudoTcpSocket *self);
static gint pseudo_tcp_socket_get_available_bytes_locked (
PseudoTcpSocket *self);
static PseudoTcpSource *
pseudo_tcp_source_new (PseudoTcpSocket *socket, GIOCondition condition,
GCancellable *cancellable);
static void
pseudo_tcp_source_update (PseudoTcpSocket *self,
PseudoTcpSource *pseudo_tcp_source);
PseudoTcpSource *pseudo_tcp_source, gboolean locked);
static gboolean
notify_pseudo_tcp_socket_base (GDatagramBased *datagram_based,
......@@ -917,6 +920,9 @@ pseudo_tcp_socket_set_property (GObject *object,
{
PseudoTcpSocket *self = PSEUDO_TCP_SOCKET (object);
if (!(pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY)))
g_rec_mutex_lock (&self->priv->mutex);
switch (property_id) {
case PROP_CONVERSATION:
self->priv->conv = g_value_get_uint (value);
......@@ -967,6 +973,9 @@ pseudo_tcp_socket_set_property (GObject *object,
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
break;
}
if (!(pspec->flags & (G_PARAM_CONSTRUCT | G_PARAM_CONSTRUCT_ONLY)))
g_rec_mutex_unlock (&self->priv->mutex);
}
static void
......@@ -1709,7 +1718,7 @@ notify_pseudo_tcp_socket_base (GDatagramBased *datagram_based,
GError *error = NULL;
/* Check if we’ve closed. */
if (pseudo_tcp_socket_is_closed (self)) {
if (pseudo_tcp_socket_is_closed_locked (self)) {
has_closed = TRUE;
continue;
}
......@@ -1887,7 +1896,7 @@ pseudo_tcp_socket_recv(PseudoTcpSocket *self, char * buffer, size_t len)
}
/* Return 0 if FIN-ACK is not supported but the socket has been closed. */
if (!priv->support_fin_ack && pseudo_tcp_socket_is_closed (self)) {
if (!priv->support_fin_ack && pseudo_tcp_socket_is_closed_locked (self)) {
retval = 0;
goto done;
}
......@@ -2053,7 +2062,7 @@ pseudo_tcp_socket_shutdown (PseudoTcpSocket *self, PseudoTcpShutdown how)
/* Local user initiating the close: RFC 793, §3.5, Cases 1 and 3.
* If there is pending receive data, send RST instead of FIN;
* see RFC 1122, §4.2.2.13. */
if (pseudo_tcp_socket_get_available_bytes (self) > 0) {
if (pseudo_tcp_socket_get_available_bytes_locked (self) > 0) {
closedown (self, ECONNABORTED, CLOSEDOWN_LOCAL);
} else {
queue_fin_message (self);
......@@ -2920,7 +2929,7 @@ adjust_tcp_clock (PseudoTcpSocket *self)
priv = self->priv;
if (pseudo_tcp_socket_is_closed (self)) {
if (pseudo_tcp_socket_is_closed_locked (self)) {
return;
}
......@@ -2973,7 +2982,7 @@ update_sources (PseudoTcpSocket *self)
for (i = 0; i < priv->sources->len; i++) {
PseudoTcpSource *source = priv->sources->pdata[i];
pseudo_tcp_source_update (self, source);
pseudo_tcp_source_update (self, source, TRUE);
}
}
......@@ -3465,14 +3474,22 @@ resize_receive_buffer (PseudoTcpSocket *self, guint32 new_size)
update_sources (self);
}
static gint
pseudo_tcp_socket_get_available_bytes_locked (PseudoTcpSocket *self)
{
PseudoTcpSocketPrivate *priv = self->priv;
return pseudo_tcp_fifo_get_buffered (&priv->rbuf);
}
gint
pseudo_tcp_socket_get_available_bytes (PseudoTcpSocket *self)
{
PseudoTcpSocketPrivate *priv = self->priv;
guint ret;
gint ret;
g_rec_mutex_lock (&priv->mutex);
ret = pseudo_tcp_fifo_get_buffered (&priv->rbuf);
ret = pseudo_tcp_socket_get_available_bytes_locked (self);
g_rec_mutex_unlock (&priv->mutex);
return ret;
......@@ -3667,6 +3684,14 @@ set_state_closed (PseudoTcpSocket *self, guint32 err)
}
}
static gboolean
pseudo_tcp_socket_is_closed_locked (PseudoTcpSocket *self)
{
PseudoTcpSocketPrivate *priv = self->priv;
return (priv->state == TCP_CLOSED);
}
gboolean
pseudo_tcp_socket_is_closed (PseudoTcpSocket *self)
{
......@@ -3674,7 +3699,7 @@ pseudo_tcp_socket_is_closed (PseudoTcpSocket *self)
gboolean ret;
g_rec_mutex_lock (&priv->mutex);
ret = (priv->state == TCP_CLOSED);
ret = pseudo_tcp_socket_is_closed_locked (self);
g_rec_mutex_unlock (&priv->mutex);
return ret;
......@@ -3702,15 +3727,13 @@ pseudo_tcp_socket_condition_check (PseudoTcpSocket *self,
priv = self->priv;
g_rec_mutex_lock (&self->priv->mutex);
#if 0
DEBUG (PSEUDO_TCP_DEBUG_NORMAL,
"Socket %p conditions: has_sent_fin: %u, has_received_fin: %u, "
"get_available_bytes: %d, get_available_send_space: %" G_GSIZE_FORMAT,
self, pseudo_tcp_state_has_sent_fin (priv->state),
pseudo_tcp_state_has_received_fin (priv->state),
pseudo_tcp_socket_get_available_bytes (self),
pseudo_tcp_socket_get_available_bytes_locked (self),
pseudo_tcp_socket_get_available_send_space (self));
#endif
......@@ -3718,7 +3741,7 @@ pseudo_tcp_socket_condition_check (PseudoTcpSocket *self,
retval |= G_IO_HUP;
if ((condition & G_IO_IN) &&
(pseudo_tcp_socket_get_available_bytes (self) > 0 ||
(pseudo_tcp_socket_get_available_bytes_locked (self) > 0 ||
pseudo_tcp_state_has_received_fin (priv->state)) &&
!pseudo_tcp_state_has_sent_fin (priv->state))
retval |= G_IO_IN;
......@@ -3733,8 +3756,6 @@ pseudo_tcp_socket_condition_check (PseudoTcpSocket *self,
retval |= G_IO_ERR;
#endif
g_rec_mutex_unlock (&self->priv->mutex);
return retval;
}
......@@ -3772,16 +3793,21 @@ struct _PseudoTcpSource {
static void
pseudo_tcp_source_update (PseudoTcpSocket *self,
PseudoTcpSource *pseudo_tcp_source)
PseudoTcpSource *pseudo_tcp_source, gboolean locked)
{
GIOCondition available_conditions, prev_pending_condition;
if (self == NULL) {
available_conditions = G_IO_HUP;
} else {
if (!locked)
g_rec_mutex_lock (&self->priv->mutex);
available_conditions =
pseudo_tcp_socket_condition_check (self,
pseudo_tcp_source->condition_mask);
if (!locked)
g_rec_mutex_unlock (&self->priv->mutex);
}
#if 0
......@@ -3830,7 +3856,7 @@ pseudo_tcp_source_dispatch (GSource *source, GSourceFunc callback,
if (socket == NULL)
return G_SOURCE_REMOVE;
pseudo_tcp_source_update (socket, pseudo_tcp_source);
pseudo_tcp_source_update (socket, pseudo_tcp_source, FALSE);
if (pseudo_tcp_source->condition_mask & G_IO_IN && socket != NULL) {
retval = func (G_OBJECT (socket->priv->input_stream), user_data);
......@@ -3925,7 +3951,7 @@ pseudo_tcp_source_new (PseudoTcpSocket *socket, GIOCondition condition,
pseudo_tcp_source->condition_mask = condition;
pseudo_tcp_source->pending_condition = 0;
pseudo_tcp_source_update (socket, pseudo_tcp_source);
pseudo_tcp_source_update (socket, pseudo_tcp_source, FALSE);
/* Add a cancellable source. */
if (cancellable != NULL) {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment