10000 gh-76785: Add *Channel.is_closed by ericsnowcurrently · Pull Request #110606 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-76785: Add *Channel.is_closed #110606

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Oct 19, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add current interpreter info.
  • Loading branch information
ericsnowcurrently committed Oct 17, 2023
commit 7932bf8aff85471cc9426c619a2fffc3c29d4c66
38 changes: 37 additions & 1 deletion Modules/_xxinterpchannelsmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -2098,6 +2098,11 @@ struct channel_info {
struct {
// 1: closed; -1: closing
int closed;
struct {
// 1: associated; -1: released
int send;
int recv;
} cur;
} status;
Py_ssize_t count;
};
Expand All @@ -2108,6 +2113,13 @@ _channel_get_info(_channels *channels, int64_t cid, struct channel_info *info)
int err = 0;
*info = (struct channel_info){0};

// Get the current interpreter.
PyInterpreterState *interp = _get_current_interp();
if (interp == NULL) {
return -1;
}
Py_ssize_t interpid = PyInterpreterState_GetID(interp);

// Hold the global lock until we're done.
PyThread_acquire_lock(channels->mutex, WAIT_LOCK);

Expand Down Expand Up @@ -2140,6 +2152,22 @@ _channel_get_info(_channels *channels, int64_t cid, struct channel_info *info)
// Get the number of queued objects.
info->count = chan->queue->count;

// Get the current ends status.
_channelend *send = _channelend_find(chan->ends->send, interpid, NULL);
if (send == NULL) {
info->status.cur.send = 0;
}
else {
info->status.cur.send = send->open ? 1 : -1;
}
_channelend *recv = _channelend_find(chan->ends->recv, interpid, NULL);
if (recv == NULL) {
info->status.cur.recv = 0;
}
else {
info->status.cur.recv = recv->open ? 1 : -1;
}

finally:
PyThread_release_lock(channels->mutex);
return err;
Expand All @@ -2155,14 +2183,18 @@ static PyStructSequence_Field channel_info_fields[] = {
{"closing", "send is closed, recv is non-empty"},
{"closed", "both ends are closed"},
{"count", "queued objects"},
{"send_associated", "current interpreter is bound to the send end"},
{"send_released", "current interpreter *was* bound to the send end"},
{"recv_associated", "current interpreter is bound to the recv end"},
{"recv_released", "current interpreter *was* bound to the recv end"},
{0}
};

static PyStructSequence_Desc channel_info_desc = {
.name = "ChannelInfo",
.doc = channel_info_doc,
.fields = channel_info_fields,
.n_in_sequence = 4,
.n_in_sequence = 8,
};

static PyObject *
Expand Down Expand Up @@ -2196,6 +2228,10 @@ new_channel_info(PyObject *mod, struct channel_info *info)
SET_BOOL(info->status.closed == -1);
SET_BOOL(info->status.closed == 1);
SET_COUNT(info->count);
SET_BOOL(info->status.cur.send == 1);
SET_BOOL(info->status.cur.send == -1);
SET_BOOL(info->status.cur.recv == 1);
SET_BOOL(info->status.cur.recv == -1);
#undef SET_COUNT
#undef SET_BOOL
assert(!PyErr_Occurred());
Expand Down
0