Skip to content
Merged
Prev Previous commit
Next Next commit
Use a static initializer init queues/channels_init()/_fini().
  • Loading branch information
ericsnowcurrently committed Oct 21, 2024
commit 202b75d8d2d120c18c6ff3f388bd12e92b9849b7
20 changes: 14 additions & 6 deletions Modules/_interpchannelsmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -1351,19 +1351,27 @@ _channels_init(_channels *channels, PyThread_type_lock mutex)
{
assert(mutex != NULL);
assert(channels->mutex == NULL);
channels->mutex = mutex;
channels->head = NULL;
channels->numopen = 0;
channels->next_id = 0;
*channels = (_channels){
.mutex = mutex,
.head = NULL,
.numopen = 0,
.next_id = 0,
};
}

static void
_channels_fini(_channels *channels, PyThread_type_lock *p_mutex)
{
PyThread_type_lock mutex = channels->mutex;
assert(mutex != NULL);

PyThread_acquire_lock(mutex, WAIT_LOCK);
assert(channels->numopen == 0);
assert(channels->head == NULL);
*p_mutex = channels->mutex;
channels->mutex = NULL;
*channels = (_channels){0};
PyThread_release_lock(mutex);

*p_mutex = mutex;
}

static int64_t
Expand Down
30 changes: 16 additions & 14 deletions Modules/_interpqueuesmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -847,27 +847,29 @@ _queues_init(_queues *queues, PyThread_type_lock mutex)
{
assert(mutex != NULL);
assert(queues->mutex == NULL);
queues->mutex = mutex;
queues->head = NULL;
queues->count = 0;
queues->next_id = 1;
*queues = (_queues){
.mutex = mutex,
.head = NULL,
.count = 0,
.next_id = 1,
};
}

static void
_queues_fini(_queues *queues, PyThread_type_lock *p_mutex)
{
assert(queues->mutex != NULL);
PyThread_type_lock mutex = queues->mutex;
assert(mutex != NULL);

PyThread_acquire_lock(mutex, WAIT_LOCK);
if (queues->count > 0) {
PyThread_acquire_lock(queues->mutex, WAIT_LOCK);
assert((queues->count == 0) != (queues->head != NULL));
_queueref *head = queues->head;
queues->head = NULL;
queues->count = 0;
PyThread_release_lock(queues->mutex);
_queuerefs_clear(head);
assert(queues->head != NULL);
_queuerefs_clear(queues->head);
}
*p_mutex = queues->mutex;
queues->mutex = NULL;
*queues = (_queues){0};
PyThread_release_lock(mutex);

*p_mutex = mutex;
}

static int64_t
Expand Down