Skip to content

Commit

Permalink
backport changes for cpyext from py3.10
Browse files Browse the repository at this point in the history
  • Loading branch information
mattip committed Oct 1, 2024
1 parent 9a41c85 commit 5b053ed
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 35 deletions.
3 changes: 1 addition & 2 deletions pypy/module/cpyext/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -1549,8 +1549,7 @@ def generate_decls_and_callbacks(db, prefix=''):
source_dir / "tupleobject.c",
]
if WIN32:
# separate_module_files.append(source_dir / "pythread_nt.c")
separate_module_files.append(source_dir / "pythread_win7.c")
separate_module_files.append(source_dir / "pythread_nt.c")
else:
separate_module_files.append(source_dir / "pythread_posix.c")

Expand Down
66 changes: 33 additions & 33 deletions rpython/translator/c/src/thread_win7.c
Original file line number Diff line number Diff line change
Expand Up @@ -119,24 +119,24 @@ PyCOND_BROADCAST(PyCOND_T *cv)
* Taken from Python/pytime.c
*/

typedef long long _PyTime_t;
#define _PyTime_MAX INT64_MAX
typedef long long _RPyTime_t;
#define _RPyTime_MAX INT64_MAX
typedef enum {
_PyTime_ROUND_FLOOR=0,
_PyTime_ROUND_CEILING=1,
_PyTime_ROUND_HALF_EVEN=2,
_PyTime_ROUND_UP=3,
_PyTime_ROUND_TIMEOUT = _PyTime_ROUND_UP
} _PyTime_round_t;
_RPyTime_ROUND_FLOOR=0,
_RPyTime_ROUND_CEILING=1,
_RPyTime_ROUND_HALF_EVEN=2,
_RPyTime_ROUND_UP=3,
_RPyTime_ROUND_TIMEOUT = _RPyTime_ROUND_UP
} _RPyTime_round_t;


static _PyTime_t
_PyTime_Divide(const _PyTime_t t, const _PyTime_t k,
const _PyTime_round_t round)
static _RPyTime_t
_PyTime_Divide(const _RPyTime_t t, const _RPyTime_t k,
const _RPyTime_round_t round)
{
assert(k > 1);
if (round == _PyTime_ROUND_HALF_EVEN) {
_PyTime_t x, r, abs_r;
if (round == _RPyTime_ROUND_HALF_EVEN) {
_RPyTime_t x, r, abs_r;
x = t / k;
r = t % k;
abs_r = abs(r);
Expand All @@ -150,15 +150,15 @@ _PyTime_Divide(const _PyTime_t t, const _PyTime_t k,
}
return x;
}
else if (round == _PyTime_ROUND_CEILING) {
else if (round == _RPyTime_ROUND_CEILING) {
if (t >= 0) {
return (t + k - 1) / k;
}
else {
return t / k;
}
}
else if (round == _PyTime_ROUND_FLOOR){
else if (round == _RPyTime_ROUND_FLOOR){
if (t >= 0) {
return t / k;
}
Expand All @@ -167,7 +167,7 @@ _PyTime_Divide(const _PyTime_t t, const _PyTime_t k,
}
}
else {
assert(round == _PyTime_ROUND_UP);
assert(round == _RPyTime_ROUND_UP);
if (t >= 0) {
return (t + k - 1) / k;
}
Expand All @@ -178,8 +178,8 @@ _PyTime_Divide(const _PyTime_t t, const _PyTime_t k,
}


_PyTime_t
_PyTime_AsMicroseconds(_PyTime_t t, _PyTime_round_t round)
static _RPyTime_t
_RPyTime_AsMicroseconds(_RPyTime_t t, _RPyTime_round_t round)
{
return _PyTime_Divide(t, 1000, round);
}
Expand All @@ -202,8 +202,8 @@ win_perf_counter_frequency(LONGLONG *pfrequency)
return -1;
}

if (frequency > _PyTime_MAX
|| frequency > (LONGLONG)_PyTime_MAX / (LONGLONG)SEC_TO_NS)
if (frequency > _RPyTime_MAX
|| frequency > (LONGLONG)_RPyTime_MAX / (LONGLONG)SEC_TO_NS)
{
return -1;
}
Expand All @@ -212,10 +212,10 @@ win_perf_counter_frequency(LONGLONG *pfrequency)
return 0;
}

static _PyTime_t
_PyTime_MulDiv(_PyTime_t ticks, _PyTime_t mul, _PyTime_t div)
static _RPyTime_t
_PyTime_MulDiv(_RPyTime_t ticks, _RPyTime_t mul, _RPyTime_t div)
{
_PyTime_t intpart, remaining;
_RPyTime_t intpart, remaining;
/* Compute (ticks * mul / div) in two parts to prevent integer overflow:
compute integer part, and then the remaining part.
Expand All @@ -230,7 +230,7 @@ _PyTime_MulDiv(_PyTime_t ticks, _PyTime_t mul, _PyTime_t div)
}

static int
py_get_win_perf_counter(_PyTime_t *tp)
py_get_win_perf_counter(_RPyTime_t *tp)
{
static LONGLONG frequency = 0;
if (frequency == 0) {
Expand All @@ -243,21 +243,21 @@ py_get_win_perf_counter(_PyTime_t *tp)
QueryPerformanceCounter(&now);
LONGLONG ticksll = now.QuadPart;

/* Make sure that casting LONGLONG to _PyTime_t cannot overflow,
/* Make sure that casting LONGLONG to _RPyTime_t cannot overflow,
* both types are signed */
_PyTime_t ticks;
_RPyTime_t ticks;
assert(sizeof(ticksll) <= sizeof(ticks));
ticks = (_PyTime_t)ticksll;
ticks = (_RPyTime_t)ticksll;

*tp = _PyTime_MulDiv(ticks, SEC_TO_NS, (_PyTime_t)frequency);
*tp = _PyTime_MulDiv(ticks, SEC_TO_NS, (_RPyTime_t)frequency);
return 0;
}


static _PyTime_t
static _RPyTime_t
_PyTime_GetPerfCounter(void)
{
_PyTime_t t;
_RPyTime_t t;
int res;
res = py_get_win_perf_counter(&t);
if (res < 0) {
Expand Down Expand Up @@ -327,12 +327,12 @@ EnterNonRecursiveMutex(PNRMUTEX mutex, RPY_TIMEOUT_T microseconds)
}
} else if (microseconds != 0) {
/* wait at least until the target */
_PyTime_t now_ns = _PyTime_GetPerfCounter();
_RPyTime_t now_ns = _PyTime_GetPerfCounter();
if (now_ns <= 0) {
gil_fatal("_PyTime_GetPerfCounter() <= 0", (int)now_ns);
}
/* This can fail to timeout if microseconds is too big */
_PyTime_t target_ns = now_ns + (microseconds * 1000);
_RPyTime_t target_ns = now_ns + (microseconds * 1000);
while (mutex->locked) {
if (PyCOND_TIMEDWAIT(&mutex->cv, &mutex->cs, microseconds) < 0) {
result = WAIT_FAILED;
Expand All @@ -341,7 +341,7 @@ EnterNonRecursiveMutex(PNRMUTEX mutex, RPY_TIMEOUT_T microseconds)
now_ns = _PyTime_GetPerfCounter();
if (target_ns <= now_ns)
break;
microseconds = _PyTime_AsMicroseconds(target_ns - now_ns, _PyTime_ROUND_TIMEOUT);
microseconds = _RPyTime_AsMicroseconds(target_ns - now_ns, _RPyTime_ROUND_TIMEOUT);
}
}
if (!mutex->locked) {
Expand Down

0 comments on commit 5b053ed

Please sign in to comment.