Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
e14679c7
Commit
e14679c7
authored
Oct 05, 2017
by
pdox
Committed by
Benjamin Peterson
Oct 05, 2017
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
closes bpo-31596: Add an interface for pthread_getcpuclockid(3) (#3756)
parent
55fd0660
Changes
7
Show whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
84 additions
and
0 deletions
+84
-0
Doc/library/time.rst
Doc/library/time.rst
+16
-0
Lib/test/test_time.py
Lib/test/test_time.py
+20
-0
Misc/NEWS.d/next/Library/2017-09-26-11-38-52.bpo-31596.50Eyel.rst
...S.d/next/Library/2017-09-26-11-38-52.bpo-31596.50Eyel.rst
+1
-0
Modules/timemodule.c
Modules/timemodule.c
+32
-0
configure
configure
+11
-0
configure.ac
configure.ac
+1
-0
pyconfig.h.in
pyconfig.h.in
+3
-0
No files found.
Doc/library/time.rst
View file @
e14679c7
...
...
@@ -162,6 +162,22 @@ The module defines the following functions and data items:
:func:`perf_counter` or :func:`process_time` instead, depending on your
requirements, to have a well defined behaviour.
.. function:: pthread_getcpuclockid(thread_id)
Return the *clk_id* of the thread-specific CPU-time clock for the specified *thread_id*.
Use :func:`threading.get_ident` or the :attr:`~threading.Thread.ident`
attribute of :class:`threading.Thread` objects to get a suitable value
for *thread_id*.
.. warning::
Passing an invalid or expired *thread_id* may result in
undefined behavior, such as segmentation fault.
Availability: Unix (see the man page for :manpage:`pthread_getcpuclockid(3)` for
further information)
.. versionadded:: 3.7
.. function:: clock_getres(clk_id)
...
...
Lib/test/test_time.py
View file @
e14679c7
...
...
@@ -7,6 +7,7 @@ import platform
import
sys
import
sysconfig
import
time
import
threading
import
unittest
try
:
import
_testcapi
...
...
@@ -80,6 +81,25 @@ class TimeTestCase(unittest.TestCase):
b
=
time
.
clock_gettime
(
time
.
CLOCK_MONOTONIC
)
self
.
assertLessEqual
(
a
,
b
)
@
unittest
.
skipUnless
(
hasattr
(
time
,
'pthread_getcpuclockid'
),
'need time.pthread_getcpuclockid()'
)
@
unittest
.
skipUnless
(
hasattr
(
time
,
'clock_gettime'
),
'need time.clock_gettime()'
)
@
unittest
.
skipUnless
(
hasattr
(
time
,
'CLOCK_THREAD_CPUTIME_ID'
),
'need time.CLOCK_THREAD_CPUTIME_ID'
)
def
test_pthread_getcpuclockid
(
self
):
clk_id
=
time
.
pthread_getcpuclockid
(
threading
.
get_ident
())
self
.
assertTrue
(
type
(
clk_id
)
is
int
)
self
.
assertNotEqual
(
clk_id
,
time
.
CLOCK_THREAD_CPUTIME_ID
)
# This should suffice to show that both calls are measuring the same clock.
t1
=
time
.
clock_gettime
(
clk_id
)
t2
=
time
.
clock_gettime
(
time
.
CLOCK_THREAD_CPUTIME_ID
)
t3
=
time
.
clock_gettime
(
clk_id
)
t4
=
time
.
clock_gettime
(
time
.
CLOCK_THREAD_CPUTIME_ID
)
self
.
assertLessEqual
(
t1
,
t2
)
self
.
assertLessEqual
(
t2
,
t3
)
self
.
assertLessEqual
(
t3
,
t4
)
@
unittest
.
skipUnless
(
hasattr
(
time
,
'clock_getres'
),
'need time.clock_getres()'
)
def
test_clock_getres
(
self
):
...
...
Misc/NEWS.d/next/Library/2017-09-26-11-38-52.bpo-31596.50Eyel.rst
0 → 100644
View file @
e14679c7
Added pthread_getcpuclockid() to the time module
Modules/timemodule.c
View file @
e14679c7
...
...
@@ -20,6 +20,10 @@
#include <io.h>
#endif
#if defined(HAVE_PTHREAD_H)
# include <pthread.h>
#endif
#if defined(__WATCOMC__) && !defined(__QNX__)
#include <i86.h>
#else
...
...
@@ -221,6 +225,31 @@ PyDoc_STRVAR(clock_getres_doc,
Return the resolution (precision) of the specified clock clk_id."
);
#endif
/* HAVE_CLOCK_GETRES */
#ifdef HAVE_PTHREAD_GETCPUCLOCKID
static
PyObject
*
time_pthread_getcpuclockid
(
PyObject
*
self
,
PyObject
*
args
)
{
unsigned
long
thread_id
;
int
err
;
clockid_t
clk_id
;
if
(
!
PyArg_ParseTuple
(
args
,
"k:pthread_getcpuclockid"
,
&
thread_id
))
{
return
NULL
;
}
err
=
pthread_getcpuclockid
((
pthread_t
)
thread_id
,
&
clk_id
);
if
(
err
)
{
errno
=
err
;
PyErr_SetFromErrno
(
PyExc_OSError
);
return
NULL
;
}
return
PyLong_FromLong
(
clk_id
);
}
PyDoc_STRVAR
(
pthread_getcpuclockid_doc
,
"pthread_getcpuclockid(thread_id) -> int
\n
\
\n
\
Return the clk_id of a thread's CPU time clock."
);
#endif
/* HAVE_PTHREAD_GETCPUCLOCKID */
static
PyObject
*
time_sleep
(
PyObject
*
self
,
PyObject
*
obj
)
{
...
...
@@ -1287,6 +1316,9 @@ static PyMethodDef time_methods[] = {
#endif
#ifdef HAVE_CLOCK_GETRES
{
"clock_getres"
,
time_clock_getres
,
METH_VARARGS
,
clock_getres_doc
},
#endif
#ifdef HAVE_PTHREAD_GETCPUCLOCKID
{
"pthread_getcpuclockid"
,
time_pthread_getcpuclockid
,
METH_VARARGS
,
pthread_getcpuclockid_doc
},
#endif
{
"sleep"
,
time_sleep
,
METH_O
,
sleep_doc
},
{
"gmtime"
,
time_gmtime
,
METH_VARARGS
,
gmtime_doc
},
...
...
configure
View file @
e14679c7
...
...
@@ -10480,6 +10480,17 @@ if test "x$ac_cv_func_pthread_atfork" = xyes; then :
#define HAVE_PTHREAD_ATFORK 1
_ACEOF
fi
done
for
ac_func
in
pthread_getcpuclockid
do
:
ac_fn_c_check_func
"
$LINENO
"
"pthread_getcpuclockid"
"ac_cv_func_pthread_getcpuclockid"
if
test
"x
$ac_cv_func_pthread_getcpuclockid
"
=
xyes
;
then
:
cat
>>
confdefs.h
<<
_ACEOF
#define HAVE_PTHREAD_GETCPUCLOCKID 1
_ACEOF
fi
done
...
...
configure.ac
View file @
e14679c7
...
...
@@ -3025,6 +3025,7 @@ if test "$posix_threads" = "yes"; then
;;
esac])
AC_CHECK_FUNCS(pthread_atfork)
AC_CHECK_FUNCS(pthread_getcpuclockid)
fi
...
...
pyconfig.h.in
View file @
e14679c7
...
...
@@ -694,6 +694,9 @@
/* Defined for Solaris 2.6 bug in pthread header. */
#undef HAVE_PTHREAD_DESTRUCTOR
/* Define to 1 if you have the `pthread_getcpuclockid' function. */
#undef HAVE_PTHREAD_GETCPUCLOCKID
/* Define to 1 if you have the <pthread.h> header file. */
#undef HAVE_PTHREAD_H
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment