sipwise / rtpengine

The Sipwise media proxy for Kamailio
GNU General Public License v3.0
784 stars 368 forks source link

A code problem #1800

Closed pengwang7 closed 7 months ago

pengwang7 commented 7 months ago

In the stream_fd_readable interface, the code is as follows:

// +1 to active read events. If it was zero then we handle it. If it was non-zero,
// another thread is already handling this socket and will process our event.
if (g_atomic_int_add(&sfd->active_read_events, 1) != 0)
    return;

......

// -1 active read events. If it's non-zero, another thread has received a read event,
// and we must handle it here.
if (!g_atomic_int_dec_and_test(&sfd->active_read_events))
    goto restart;

g_atomic_int_dec_and_test Decrements the value of atomic by 1. Think of this operation as an atomic version of { atomic -= 1; return (atomic == 0); }.

g_atomic_int_add Atomically adds val to the value of atomic. Think of this operation as an atomic version of { tmp = atomic; atomic += val; return tmp; }. The value of atomic before the add, signed.

The above instructions are from the glib manual

Q: using A = g_atomic_int_dec_and_test(&sfd->active_read_events);

sfd->active_read_events is initially set to 0.

Why is A equal to 0, yet it is still necessary to execute restart? I believe that when A equals 0, there is no need to execute restart, as there may not be any data. If I am mistaken, please explain the reason. Thank you.

rfuchs commented 7 months ago

g_atomic_int_dec_and_test doesn't return the new value of the atomic. It returns a boolean: true if the new value is zero.