Skip to content

Commit 36bce96

Browse files
committed
net: replace net_sem*wait with conn_dev_sem*wait to simplify code logic
optimize the current code format according to the previous net_xxx_wait implementation to reduce multiple calls of similar code Signed-off-by: zhanghongyu <[email protected]>
1 parent a13b12d commit 36bce96

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+196
-244
lines changed

include/nuttx/net/net.h

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -452,24 +452,6 @@ int net_sem_wait(FAR sem_t *sem);
452452

453453
int net_sem_timedwait_uninterruptible(FAR sem_t *sem, unsigned int timeout);
454454

455-
/****************************************************************************
456-
* Name: net_sem_wait_uninterruptible
457-
*
458-
* Description:
459-
* This function is wrapped version of net_sem_wait(), which is
460-
* uninterruptible and convenient for use.
461-
*
462-
* Input Parameters:
463-
* sem - A reference to the semaphore to be taken.
464-
*
465-
* Returned Value:
466-
* Zero (OK) is returned on success; a negated errno value is returned on
467-
* any failure.
468-
*
469-
****************************************************************************/
470-
471-
int net_sem_wait_uninterruptible(FAR sem_t *sem);
472-
473455
/****************************************************************************
474456
* Name: net_sem_timedwait2
475457
*

net/arp/arp_notify.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ int arp_wait(FAR struct arp_notify_s *notify, unsigned int timeout)
156156

157157
/* And wait for the ARP response (or a timeout). */
158158

159-
net_sem_timedwait_uninterruptible(&notify->nt_sem, timeout);
159+
nxsem_tickwait_uninterruptible(&notify->nt_sem, MSEC2TICK(timeout));
160160

161161
/* Then get the real result of the transfer */
162162

net/arp/arp_send.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -374,15 +374,15 @@ int arp_send(in_addr_t ipaddr)
374374
netdev_txnotify_dev(dev, ARP_POLL);
375375

376376
/* Wait for the send to complete or an error to occur.
377-
* net_sem_wait will also terminate if a signal is received.
377+
* nxsem_tickwait will also terminate if a signal is received.
378378
*/
379379

380380
netdev_unlock(dev);
381381

382382
do
383383
{
384-
ret = net_sem_timedwait_uninterruptible(&state.snd_sem,
385-
CONFIG_ARP_SEND_DELAYMSEC);
384+
ret = nxsem_tickwait(&state.snd_sem,
385+
MSEC2TICK(CONFIG_ARP_SEND_DELAYMSEC));
386386
if (ret == -ETIMEDOUT)
387387
{
388388
arp_wait_cancel(&notify);

net/bluetooth/bluetooth_recvmsg.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -385,9 +385,10 @@ ssize_t bluetooth_recvmsg(FAR struct socket *psock, FAR struct msghdr *msg,
385385
state.ir_cb->event = bluetooth_recvfrom_eventhandler;
386386

387387
/* Wait for either the receive to complete or for an error/timeout to
388-
* occur. NOTES: (1) net_sem_wait will also terminate if a signal
389-
* is received, (2) the network is locked! It will be un-locked while
390-
* the task sleeps and automatically re-locked when the task restarts.
388+
* occur. NOTES: (1) conn_dev_sem_timedwait will also terminate if a
389+
* signal is received, (2) the network is locked! It will be un-locked
390+
* while the task sleeps and automatically re-locked when the task
391+
* restarts.
391392
*/
392393

393394
conn_dev_sem_timedwait(&state.ir_sem, true, UINT_MAX,

net/bluetooth/bluetooth_sendmsg.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,8 @@ static ssize_t bluetooth_sendto(FAR struct socket *psock,
347347
netdev_txnotify_dev(&radio->r_dev, BLUETOOTH_POLL);
348348

349349
/* Wait for the send to complete or an error to occur.
350-
* net_sem_wait will also terminate if a signal is received.
350+
* conn_dev_sem_timedwait will also terminate if a signal is
351+
* received.
351352
*/
352353

353354
ret = conn_dev_sem_timedwait(&state.is_sem, true, UINT_MAX,
@@ -371,9 +372,9 @@ static ssize_t bluetooth_sendto(FAR struct socket *psock,
371372
return state.is_sent;
372373
}
373374

374-
/* If net_sem_wait failed, then we were probably reawakened by a signal.
375-
* In this case, net_sem_wait will have returned negated errno
376-
* appropriately.
375+
/* If conn_dev_sem_timedwait failed, then we were probably reawakened by
376+
* a signal. In this case, conn_dev_sem_timedwait will have returned
377+
* negated errno appropriately.
377378
*/
378379

379380
if (ret < 0)

net/can/can_recvmsg.c

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,8 @@ static uint32_t can_recvfrom_eventhandler(FAR struct net_driver_s *dev,
360360
* Evaluate the result of the recv operations
361361
*
362362
* Input Parameters:
363-
* result The result of the net_sem_wait operation (may indicate EINTR)
363+
* result The result of the conn_dev_sem_timedwait operation
364+
* (may indicate EINTR)
364365
* pstate A pointer to the state structure to be initialized
365366
*
366367
* Returned Value:
@@ -384,9 +385,9 @@ static ssize_t can_recvfrom_result(int result,
384385
return pstate->pr_result;
385386
}
386387

387-
/* If net_sem_wait failed, then we were probably reawakened by a signal.
388-
* In this case, net_sem_wait will have returned negated errno
389-
* appropriately.
388+
/* If conn_dev_sem_timedwait failed, then we were probably reawakened by
389+
* a signal. In this case, conn_dev_sem_timedwait will have returned
390+
* negated errno appropriately.
390391
*/
391392

392393
if (result < 0)
@@ -518,14 +519,14 @@ ssize_t can_recvmsg(FAR struct socket *psock, FAR struct msghdr *msg,
518519
state.pr_cb->event = can_recvfrom_eventhandler;
519520

520521
/* Wait for either the receive to complete or for an error/timeout to
521-
* occur. NOTES: (1) net_sem_wait will also terminate if a signal
522-
* is received, (2) the network is locked! It will be un-locked while
523-
* the task sleeps and automatically re-locked when the task restarts.
522+
* occur. NOTES: (1) conn_dev_sem_timedwait will also terminate if a
523+
* signal is received, (2) the network is locked! It will be un-locked
524+
* while the task sleeps and automatically re-locked when the task
525+
* restarts.
524526
*/
525527

526-
conn_dev_unlock(&conn->sconn, dev);
527-
ret = net_sem_wait(&state.pr_sem);
528-
conn_dev_lock(&conn->sconn, dev);
528+
ret = conn_dev_sem_timedwait(&state.pr_sem, true, UINT_MAX,
529+
&conn->sconn, dev);
529530

530531
/* Make sure that no further events are processed */
531532

net/can/can_sendmsg.c

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -264,21 +264,20 @@ ssize_t can_sendmsg(FAR struct socket *psock, FAR struct msghdr *msg,
264264
netdev_txnotify_dev(dev, CAN_POLL);
265265

266266
/* Wait for the send to complete or an error to occur.
267-
* net_sem_timedwait will also terminate if a signal is received.
267+
* conn_dev_sem_timedwait will also terminate if a signal is received.
268268
*/
269269

270-
conn_dev_unlock(&conn->sconn, dev);
271270
if (_SS_ISNONBLOCK(conn->sconn.s_flags) || (flags & MSG_DONTWAIT) != 0)
272271
{
273-
ret = net_sem_timedwait(&state.snd_sem, 0);
272+
ret = conn_dev_sem_timedwait(&state.snd_sem, true, 0,
273+
&conn->sconn, dev);
274274
}
275275
else
276276
{
277-
ret = net_sem_timedwait(&state.snd_sem, UINT_MAX);
277+
ret = conn_dev_sem_timedwait(&state.snd_sem, true, UINT_MAX,
278+
&conn->sconn, dev);
278279
}
279280

280-
conn_dev_lock(&conn->sconn, dev);
281-
282281
/* Make sure that no further events are processed */
283282

284283
can_callback_free(dev, conn, state.snd_cb);
@@ -296,9 +295,9 @@ ssize_t can_sendmsg(FAR struct socket *psock, FAR struct msghdr *msg,
296295
return state.snd_sent;
297296
}
298297

299-
/* If net_sem_wait failed, then we were probably reawakened by a signal.
300-
* In this case, net_sem_wait will have returned negated errno
301-
* appropriately.
298+
/* If conn_dev_sem_timedwait failed, then we were probably reawakened by
299+
* a signal. In this case, conn_dev_sem_timedwait will have returned
300+
* negated errno appropriately.
302301
*/
303302

304303
if (ret < 0)

net/can/can_sendmsg_buffered.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,8 +263,9 @@ ssize_t can_sendmsg(FAR struct socket *psock, FAR struct msghdr *msg,
263263
goto errout_with_lock;
264264
}
265265

266-
ret = net_sem_timedwait_uninterruptible(&conn->sndsem,
267-
_SO_TIMEOUT(conn->sconn.s_sndtimeo));
266+
ret = conn_dev_sem_timedwait(&conn->sndsem, false,
267+
_SO_TIMEOUT(conn->sconn.s_sndtimeo),
268+
&conn->sconn, dev);
268269
if (ret < 0)
269270
{
270271
goto errout_with_lock;

net/devif/devif_callback.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -256,11 +256,12 @@ devif_callback_alloc(FAR struct net_driver_s *dev,
256256
*/
257257

258258
/* Note: dev->d_flags may be asynchronously changed by netdev_ifdown()
259-
* (in net/netdev/netdev_ioctl.c). Nevertheless, net_lock() / net_unlock()
260-
* are not required in netdev_ifdown() to prevent dev->d_flags from
261-
* asynchronous change here. There is not an issue because net_lock() and
262-
* net_unlock() present inside of devif_dev_event(). That should be enough
263-
* to de-allocate connection callbacks reliably on NETDEV_DOWN event.
259+
* (in net/netdev/netdev_ioctl.c). Nevertheless, netdev_lock() /
260+
* netdev_unlock() are not required in netdev_ifdown() to prevent
261+
* dev->d_flags from asynchronous change here. There is not an issue
262+
* because netdev_lock() and netdev_unlock() present inside of
263+
* devif_dev_event(). That should be enough to de-allocate connection
264+
* callbacks reliably on NETDEV_DOWN event.
264265
*/
265266

266267
if (dev && !netdev_verify(dev))

net/icmp/icmp_recvmsg.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -374,14 +374,13 @@ ssize_t icmp_recvmsg(FAR struct socket *psock, FAR struct msghdr *msg,
374374
state.recv_cb->event = recvfrom_eventhandler;
375375

376376
/* Wait for either the response to be received or for timeout to
377-
* occur. net_sem_timedwait will also terminate if a signal is
377+
* occur. conn_dev_sem_timedwait will also terminate if a signal is
378378
* received.
379379
*/
380380

381-
conn_dev_unlock(&conn->sconn, dev);
382-
ret = net_sem_timedwait(&state.recv_sem,
383-
_SO_TIMEOUT(conn->sconn.s_rcvtimeo));
384-
conn_dev_lock(&conn->sconn, dev);
381+
ret = conn_dev_sem_timedwait(&state.recv_sem, true,
382+
_SO_TIMEOUT(conn->sconn.s_rcvtimeo),
383+
&conn->sconn, dev);
385384
if (ret < 0)
386385
{
387386
state.recv_result = ret;

0 commit comments

Comments
 (0)