Skip to content
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
refactor
  • Loading branch information
kumaraditya303 authored Aug 16, 2022
commit 4c6ffcf0a1bc5425674f8bad32aaabd362dcba60
44 changes: 24 additions & 20 deletions Python/getargs.c
Original file line number Diff line number Diff line change
Expand Up @@ -1973,25 +1973,11 @@ new_kwtuple(const char * const *keywords, int total, int pos)
return kwtuple;
}

static int
parser_init(struct _PyArg_Parser *parser)
static inline int
_parser_init(struct _PyArg_Parser *parser)
Comment thread
kumaraditya303 marked this conversation as resolved.
Outdated
{
const char * const *keywords = parser->keywords;
assert(keywords != NULL);
// volatile as it can be modified by other threads
if (*((volatile int *)&parser->initialized)) {
assert(parser->kwtuple != NULL);
return 1;
}
PyThread_acquire_lock(_PyRuntime.getargs.mutex, WAIT_LOCK);
// Check again if another thread initialized the parser
// while we were waiting for the lock.
if (*((volatile int *)&parser->initialized)) {
assert(parser->kwtuple != NULL);
PyThread_release_lock(_PyRuntime.getargs.mutex);
return 1;
}

assert(parser->pos == 0 &&
(parser->format == NULL || parser->fname == NULL) &&
parser->custom_msg == NULL &&
Expand All @@ -2000,7 +1986,6 @@ parser_init(struct _PyArg_Parser *parser)

int len, pos;
if (scan_keywords(keywords, &len, &pos) < 0) {
PyThread_release_lock(_PyRuntime.getargs.mutex);
return 0;
}

Expand All @@ -2010,7 +1995,6 @@ parser_init(struct _PyArg_Parser *parser)
assert(parser->fname == NULL);
if (parse_format(parser->format, len, pos,
&fname, &custommsg, &min, &max) < 0) {
PyThread_release_lock(_PyRuntime.getargs.mutex);
return 0;
}
}
Expand All @@ -2024,7 +2008,6 @@ parser_init(struct _PyArg_Parser *parser)
if (kwtuple == NULL) {
kwtuple = new_kwtuple(keywords, len, pos);
if (kwtuple == NULL) {
PyThread_release_lock(_PyRuntime.getargs.mutex);
return 0;
}
owned = 1;
Expand All @@ -2043,10 +2026,31 @@ parser_init(struct _PyArg_Parser *parser)
parser->next = static_arg_parsers;
static_arg_parsers = parser;
parser->initialized = owned ? 1 : -1;
Comment thread
kumaraditya303 marked this conversation as resolved.
Outdated
PyThread_release_lock(_PyRuntime.getargs.mutex);
return 1;
}

static int
parser_init(struct _PyArg_Parser *parser)
{
// volatile as it can be modified by other threads
// and should not be optimized or reordered by compiler
if (*((volatile int *)&parser->initialized)) {
assert(parser->kwtuple != NULL);
return 1;
}
PyThread_acquire_lock(_PyRuntime.getargs.mutex, WAIT_LOCK);
// Check again if another thread initialized the parser
// while we were waiting for the lock.
if (*((volatile int *)&parser->initialized)) {
assert(parser->kwtuple != NULL);
PyThread_release_lock(_PyRuntime.getargs.mutex);
return 1;
}
int ret = _parser_init(parser);
PyThread_release_lock(_PyRuntime.getargs.mutex);
return ret;
}

static void
parser_clear(struct _PyArg_Parser *parser)
{
Expand Down