Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(9)

Delta Between Two Patch Sets: Modules/_threadmodule.c

Issue 22043: Use a monotonic clock to compute timeouts
Left Patch Set: Created 3 years, 2 months ago
Right Patch Set: Created 3 years ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « Lib/trace.py ('k') | Modules/gcmodule.c » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 1
2 /* Thread module */ 2 /* Thread module */
3 /* Interface to Sjoerd's portable C thread library */ 3 /* Interface to Sjoerd's portable C thread library */
4 4
5 #include "Python.h" 5 #include "Python.h"
6 #include "structmember.h" /* offsetof */ 6 #include "structmember.h" /* offsetof */
7 7
8 #ifndef WITH_THREAD 8 #ifndef WITH_THREAD
9 #error "Error! The rest of Python is not compiled with thread support." 9 #error "Error! The rest of Python is not compiled with thread support."
10 #error "Rerun configure, adding a --with-threads option." 10 #error "Rerun configure, adding a --with-threads option."
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
45 /* Helper to acquire an interruptible lock with a timeout. If the lock acquire 45 /* Helper to acquire an interruptible lock with a timeout. If the lock acquire
46 * is interrupted, signal handlers are run, and if they raise an exception, 46 * is interrupted, signal handlers are run, and if they raise an exception,
47 * PY_LOCK_INTR is returned. Otherwise, PY_LOCK_ACQUIRED or PY_LOCK_FAILURE 47 * PY_LOCK_INTR is returned. Otherwise, PY_LOCK_ACQUIRED or PY_LOCK_FAILURE
48 * are returned, depending on whether the lock can be acquired withing the 48 * are returned, depending on whether the lock can be acquired withing the
49 * timeout. 49 * timeout.
50 */ 50 */
51 static PyLockStatus 51 static PyLockStatus
52 acquire_timed(PyThread_type_lock lock, PY_TIMEOUT_T microseconds) 52 acquire_timed(PyThread_type_lock lock, PY_TIMEOUT_T microseconds)
53 { 53 {
54 PyLockStatus r; 54 PyLockStatus r;
55 _PyTime_timespec curtime; 55 _PyTime_timeval curtime;
56 _PyTime_timespec endtime; 56 _PyTime_timeval endtime;
57 57
58 58
59 if (microseconds > 0) { 59 if (microseconds > 0) {
60 _PyTime_monotonic(&endtime); 60 _PyTime_monotonic(&endtime);
61 endtime.tv_sec += microseconds / (1000 * 1000); 61 endtime.tv_sec += microseconds / (1000 * 1000);
62 endtime.tv_nsec += microseconds % (1000 * 1000) * 1000; 62 endtime.tv_usec += microseconds % (1000 * 1000);
Charles-François Natali 2014/07/31 07:33:20 An extra pair of parenthesis wouldn't hurt here, s
haypo 2014/07/31 11:17:44 Agreed. Maybe we need to provide more helps to co
63 } 63 }
64 64
65 65
66 do { 66 do {
67 /* first a simple non-blocking try without releasing the GIL */ 67 /* first a simple non-blocking try without releasing the GIL */
68 r = PyThread_acquire_lock_timed(lock, 0, 0); 68 r = PyThread_acquire_lock_timed(lock, 0, 0);
69 if (r == PY_LOCK_FAILURE && microseconds != 0) { 69 if (r == PY_LOCK_FAILURE && microseconds != 0) {
70 Py_BEGIN_ALLOW_THREADS 70 Py_BEGIN_ALLOW_THREADS
71 r = PyThread_acquire_lock_timed(lock, microseconds, 1); 71 r = PyThread_acquire_lock_timed(lock, microseconds, 1);
72 Py_END_ALLOW_THREADS 72 Py_END_ALLOW_THREADS
73 } 73 }
74 74
75 if (r == PY_LOCK_INTR) { 75 if (r == PY_LOCK_INTR) {
76 /* Run signal handlers if we were interrupted. Propagate 76 /* Run signal handlers if we were interrupted. Propagate
77 * exceptions from signal handlers, such as KeyboardInterrupt, by 77 * exceptions from signal handlers, such as KeyboardInterrupt, by
78 * passing up PY_LOCK_INTR. */ 78 * passing up PY_LOCK_INTR. */
79 if (Py_MakePendingCalls() < 0) { 79 if (Py_MakePendingCalls() < 0) {
80 return PY_LOCK_INTR; 80 return PY_LOCK_INTR;
81 } 81 }
82 82
83 /* If we're using a timeout, recompute the timeout after processing 83 /* If we're using a timeout, recompute the timeout after processing
84 * signals, since those can take time. */ 84 * signals, since those can take time. */
85 if (microseconds > 0) { 85 if (microseconds > 0) {
86 _PyTime_monotonic(&curtime); 86 _PyTime_monotonic(&curtime);
87 microseconds = ((endtime.tv_sec - curtime.tv_sec) * 1000000 + 87 microseconds = ((endtime.tv_sec - curtime.tv_sec) * 1000000 +
88 (endtime.tv_nsec - curtime.tv_nsec) / 1000); 88 (endtime.tv_usec - curtime.tv_usec));
89 89
90 /* Check for negative values, since those mean block forever. 90 /* Check for negative values, since those mean block forever.
91 */ 91 */
92 if (microseconds <= 0) { 92 if (microseconds <= 0) {
93 r = PY_LOCK_FAILURE; 93 r = PY_LOCK_FAILURE;
94 } 94 }
95 } 95 }
96 } 96 }
97 } while (r == PY_LOCK_INTR); /* Retry if we were interrupted. */ 97 } while (r == PY_LOCK_INTR); /* Retry if we were interrupted. */
98 98
(...skipping 1307 matching lines...) Expand 10 before | Expand all | Expand 10 after
1406 nb_threads = 0; 1406 nb_threads = 0;
1407 1407
1408 str_dict = PyUnicode_InternFromString("__dict__"); 1408 str_dict = PyUnicode_InternFromString("__dict__");
1409 if (str_dict == NULL) 1409 if (str_dict == NULL)
1410 return NULL; 1410 return NULL;
1411 1411
1412 /* Initialize the C thread library */ 1412 /* Initialize the C thread library */
1413 PyThread_init_thread(); 1413 PyThread_init_thread();
1414 return m; 1414 return m;
1415 } 1415 }
LEFTRIGHT

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7