Skip to content

Commit b9f5591

Browse files
committed
net/local: replace net_lock with local_lock
remove the use of net_lock in the local module and decouple it from other network modules. Signed-off-by: zhanghongyu <zhanghongyu@xiaomi.com>
1 parent 17376f5 commit b9f5591

File tree

10 files changed

+82
-34
lines changed

10 files changed

+82
-34
lines changed

net/local/local.h

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,40 @@ extern "C"
196196

197197
EXTERN const struct sock_intf_s g_local_sockif;
198198

199+
/* Global protection lock for local socket */
200+
201+
extern mutex_t g_local_lock;
202+
203+
/****************************************************************************
204+
* Inline Functions
205+
****************************************************************************/
206+
207+
/****************************************************************************
208+
* Name: local_lock
209+
*
210+
* Description:
211+
* Take the global local socket lock
212+
*
213+
****************************************************************************/
214+
215+
static inline_function void local_lock(void)
216+
{
217+
nxmutex_lock(&g_local_lock);
218+
}
219+
220+
/****************************************************************************
221+
* Name: local_unlock
222+
*
223+
* Description:
224+
* Release the global local socket lock
225+
*
226+
****************************************************************************/
227+
228+
static inline_function void local_unlock(void)
229+
{
230+
nxmutex_unlock(&g_local_lock);
231+
}
232+
199233
/****************************************************************************
200234
* Public Function Prototypes
201235
****************************************************************************/

net/local/local_accept.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,9 @@ static int local_waitlisten(FAR struct local_conn_s *server)
5656
{
5757
/* No.. wait for a connection or a signal */
5858

59-
ret = net_sem_wait(&server->lc_waitsem);
59+
local_unlock();
60+
ret = nxsem_wait(&server->lc_waitsem);
61+
local_lock();
6062
if (ret < 0)
6163
{
6264
return ret;
@@ -125,6 +127,7 @@ int local_accept(FAR struct socket *psock, FAR struct sockaddr *addr,
125127

126128
/* Loop as necessary if we have to wait for a connection */
127129

130+
local_lock();
128131
for (; ; )
129132
{
130133
/* Are there pending connections. Remove the accept from the
@@ -161,6 +164,7 @@ int local_accept(FAR struct socket *psock, FAR struct sockaddr *addr,
161164
ret = local_set_nonblocking(conn);
162165
}
163166

167+
local_unlock();
164168
return ret;
165169
}
166170

@@ -174,6 +178,7 @@ int local_accept(FAR struct socket *psock, FAR struct sockaddr *addr,
174178
{
175179
/* Yes.. return EAGAIN */
176180

181+
local_unlock();
177182
return -EAGAIN;
178183
}
179184

@@ -182,6 +187,7 @@ int local_accept(FAR struct socket *psock, FAR struct sockaddr *addr,
182187
ret = local_waitlisten(server);
183188
if (ret < 0)
184189
{
190+
local_unlock();
185191
return ret;
186192
}
187193
}

net/local/local_bind.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,14 @@ int psock_local_bind(FAR struct socket *psock,
6666

6767
/* Check if local address is already in use */
6868

69-
net_lock();
69+
local_lock();
7070
if (local_findconn(conn, unaddr) != NULL)
7171
{
72-
net_unlock();
72+
local_unlock();
7373
return -EADDRINUSE;
7474
}
7575

76-
net_unlock();
76+
local_unlock();
7777

7878
/* Save the address family */
7979

net/local/local_conn.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,14 @@
3838

3939
#include "local/local.h"
4040

41+
/****************************************************************************
42+
* Public Data
43+
****************************************************************************/
44+
45+
/* Global protection lock for local socket */
46+
47+
mutex_t g_local_lock = NXMUTEX_INITIALIZER;
48+
4149
/****************************************************************************
4250
* Private Data
4351
****************************************************************************/
@@ -168,6 +176,7 @@ FAR struct local_conn_s *local_alloc(void)
168176

169177
nxmutex_init(&conn->lc_sendlock);
170178
nxmutex_init(&conn->lc_polllock);
179+
nxrmutex_init(&conn->lc_conn.s_lock);
171180

172181
#ifdef CONFIG_NET_LOCAL_SCM
173182
conn->lc_cred.pid = nxsched_getpid();
@@ -347,6 +356,7 @@ void local_free(FAR struct local_conn_s *conn)
347356

348357
nxmutex_destroy(&conn->lc_sendlock);
349358
nxmutex_destroy(&conn->lc_polllock);
359+
nxrmutex_destroy(&conn->lc_conn.s_lock);
350360

351361
/* And free the connection structure */
352362

net/local/local_connect.c

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,7 @@ static int inline local_stream_connect(FAR struct local_conn_s *client,
8888
return -ECONNREFUSED;
8989
}
9090

91-
net_lock();
9291
ret = local_alloc_accept(server, client, &conn);
93-
net_unlock();
9492
if (ret < 0)
9593
{
9694
nerr("ERROR: Failed to alloc accept conn %s: %d\n",
@@ -151,9 +149,9 @@ static int inline local_stream_connect(FAR struct local_conn_s *client,
151149
errout_with_conn:
152150
local_release_fifos(conn);
153151
client->lc_state = LOCAL_STATE_BOUND;
154-
net_lock();
152+
local_lock();
155153
local_free(conn);
156-
net_unlock();
154+
local_unlock();
157155

158156
return ret;
159157
}
@@ -175,7 +173,7 @@ int32_t local_generate_instance_id(void)
175173
static int32_t g_next_instance_id = 0;
176174
int32_t id;
177175

178-
/* Called from local_connect with net_lock held. */
176+
/* Called from local_connect with local_lock held. */
179177

180178
id = g_next_instance_id++;
181179
if (g_next_instance_id < 0)
@@ -232,7 +230,7 @@ int psock_local_connect(FAR struct socket *psock,
232230

233231
/* Find the matching server connection */
234232

235-
net_lock();
233+
local_lock();
236234
while ((conn = local_nextconn(conn)) != NULL)
237235
{
238236
/* Self found, continue */
@@ -275,20 +273,20 @@ int psock_local_connect(FAR struct socket *psock,
275273
ret = local_stream_connect(client, conn,
276274
_SS_ISNONBLOCK(client->lc_conn.s_flags));
277275

278-
net_unlock();
276+
local_unlock();
279277
return ret;
280278
}
281279

282280
break;
283281

284282
default: /* Bad, memory must be corrupted */
285283
DEBUGPANIC(); /* PANIC if debug on */
286-
net_unlock();
284+
local_unlock();
287285
return -EINVAL;
288286
}
289287
}
290288

291-
net_unlock();
289+
local_unlock();
292290
ret = nx_stat(unpath, &buf, 1);
293291
return ret < 0 ? ret : -ECONNREFUSED;
294292
}

net/local/local_listen.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,14 @@ int local_listen(FAR struct socket *psock, int backlog)
8080
return -EOPNOTSUPP;
8181
}
8282

83-
net_lock();
83+
local_lock();
8484

8585
/* Some sanity checks */
8686

8787
if (server->lc_proto != SOCK_STREAM ||
8888
server->lc_state == LOCAL_STATE_UNBOUND)
8989
{
90-
net_unlock();
90+
local_unlock();
9191
return -EOPNOTSUPP;
9292
}
9393

@@ -114,7 +114,7 @@ int local_listen(FAR struct socket *psock, int backlog)
114114
server->lc_state = LOCAL_STATE_LISTENING;
115115
}
116116

117-
net_unlock();
117+
local_unlock();
118118

119119
return OK;
120120
}

net/local/local_recvmsg.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ static void local_recvctl(FAR struct local_conn_s *conn,
145145
int *fds;
146146
int i;
147147

148-
net_lock();
148+
local_lock();
149149

150150
if (conn->lc_peer == NULL)
151151
{
@@ -198,7 +198,7 @@ static void local_recvctl(FAR struct local_conn_s *conn,
198198
}
199199

200200
out:
201-
net_unlock();
201+
local_unlock();
202202
}
203203
#endif /* CONFIG_NET_LOCAL_SCM */
204204

net/local/local_release.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ int local_release(FAR struct local_conn_s *conn)
5858
/* There should be no references on this structure */
5959

6060
DEBUGASSERT(conn->lc_crefs == 0);
61-
net_lock();
61+
local_lock();
6262

6363
#ifdef CONFIG_NET_LOCAL_STREAM
6464
/* We should not bet here with state LOCAL_STATE_ACCEPT. That is an
@@ -97,6 +97,6 @@ int local_release(FAR struct local_conn_s *conn)
9797
/* Free the connection structure */
9898

9999
local_free(conn);
100-
net_unlock();
100+
local_unlock();
101101
return OK;
102102
}

net/local/local_sendmsg.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ static int local_sendctl(FAR struct local_conn_s *conn,
8383
int ret;
8484
int i = 0;
8585

86-
net_lock();
86+
local_lock();
8787
peer = conn->lc_peer;
8888
if (peer == NULL)
8989
{
@@ -137,12 +137,12 @@ static int local_sendctl(FAR struct local_conn_s *conn,
137137
}
138138
}
139139

140-
net_unlock();
140+
local_unlock();
141141
return count;
142142

143143
fail:
144144
local_freectl(conn, i);
145-
net_unlock();
145+
local_unlock();
146146
return ret;
147147
}
148148
#endif /* CONFIG_NET_LOCAL_SCM */
@@ -311,17 +311,17 @@ static ssize_t local_sendto(FAR struct socket *psock,
311311
return -EISCONN;
312312
}
313313

314-
net_lock();
314+
local_lock();
315315

316316
server = local_findconn(conn, unaddr);
317317
if (server == NULL)
318318
{
319-
net_unlock();
319+
local_unlock();
320320
nerr("ERROR: No such file or directory\n");
321321
return -ENOENT;
322322
}
323323

324-
net_unlock();
324+
local_unlock();
325325

326326
/* Make sure that dgram is sent safely */
327327

@@ -455,9 +455,9 @@ ssize_t local_sendmsg(FAR struct socket *psock, FAR struct msghdr *msg,
455455

456456
if (len < 0 && count > 0)
457457
{
458-
net_lock();
458+
local_lock();
459459
local_freectl(conn, count);
460-
net_unlock();
460+
local_unlock();
461461
}
462462
#else
463463
len = to ? local_sendto(psock, buf, len, flags, to, tolen) :

net/local/local_sockif.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -132,9 +132,9 @@ static int local_sockif_alloc(FAR struct socket *psock)
132132
/* Allocate the local connection structure */
133133

134134
FAR struct local_conn_s *conn;
135-
net_lock();
135+
local_lock();
136136
conn = local_alloc();
137-
net_unlock();
137+
local_unlock();
138138
if (conn == NULL)
139139
{
140140
/* Failed to reserve a connection structure */
@@ -655,7 +655,7 @@ static int local_setsockopt(FAR struct socket *psock, int level, int option,
655655
return -EINVAL;
656656
}
657657

658-
net_lock();
658+
local_lock();
659659

660660
/* Only SOCK_STREAM sockets need set the send buffer size */
661661

@@ -681,7 +681,7 @@ static int local_setsockopt(FAR struct socket *psock, int level, int option,
681681
}
682682
#endif
683683

684-
net_unlock();
684+
local_unlock();
685685

686686
return ret;
687687
}
@@ -696,7 +696,7 @@ static int local_setsockopt(FAR struct socket *psock, int level, int option,
696696
return -EINVAL;
697697
}
698698

699-
net_lock();
699+
local_lock();
700700

701701
rcvsize = *(FAR const int *)value;
702702
#ifdef CONFIG_NET_LOCAL_DGRAM
@@ -740,7 +740,7 @@ static int local_setsockopt(FAR struct socket *psock, int level, int option,
740740
conn->lc_rcvsize = rcvsize;
741741
}
742742

743-
net_unlock();
743+
local_unlock();
744744

745745
return ret;
746746
}

0 commit comments

Comments
 (0)