Index: runtime/platform/thread_linux.cc |
diff --git a/runtime/platform/thread_linux.cc b/runtime/platform/thread_linux.cc |
index acb78c71ed5423a0daeebcde32bb5c959b74adca..11e40de82331b363b4c050ac1ac05f9d1f7c5fa8 100644 |
--- a/runtime/platform/thread_linux.cc |
+++ b/runtime/platform/thread_linux.cc |
@@ -8,6 +8,7 @@ |
#include "platform/thread.h" |
#include <errno.h> // NOLINT |
+#include <sys/resource.h> // NOLINT |
#include <sys/time.h> // NOLINT |
#include "platform/assert.h" |
@@ -39,10 +40,10 @@ namespace dart { |
#endif |
-static void ComputeTimeSpec(struct timespec* ts, int64_t millis) { |
- int64_t secs = millis / kMillisecondsPerSecond; |
+static void ComputeTimeSpecMicros(struct timespec* ts, int64_t micros) { |
+ int64_t secs = micros / kMicrosecondsPerSecond; |
int64_t nanos = |
- (millis - (secs * kMillisecondsPerSecond)) * kNanosecondsPerMillisecond; |
+ (micros - (secs * kMicrosecondsPerSecond)) * kNanosecondsPerMicrosecond; |
int result = clock_gettime(CLOCK_MONOTONIC, ts); |
ASSERT(result == 0); |
ts->tv_sec += secs; |
@@ -54,6 +55,11 @@ static void ComputeTimeSpec(struct timespec* ts, int64_t millis) { |
} |
+static void ComputeTimeSpec(struct timespec* ts, int64_t millis) { |
+ ComputeTimeSpec(ts, millis * kMicrosecondsPerMillisecond); |
+} |
+ |
+ |
class ThreadStartData { |
public: |
ThreadStartData(Thread::ThreadStartFunction function, |
@@ -144,6 +150,22 @@ intptr_t Thread::GetMaxStackSize() { |
} |
+ThreadId Thread::GetCurrentThreadId() { |
+ return pthread_self(); |
+} |
+ |
+ |
+void Thread::GetThreadCpuUsage(ThreadId thread_id, int64_t* cpu_usage) { |
+ ASSERT(thread_id == GetCurrentThreadId()); |
+ ASSERT(cpu_usage != NULL); |
+ struct timespec ts; |
+ int r = clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts); |
+ ASSERT(r == 0); |
+ *cpu_usage = (ts.tv_sec * kNanosecondsPerSecond + ts.tv_nsec) / |
+ kNanosecondsPerMicrosecond; |
+} |
+ |
+ |
Mutex::Mutex() { |
pthread_mutexattr_t attr; |
int result = pthread_mutexattr_init(&attr); |
@@ -255,15 +277,20 @@ void Monitor::Exit() { |
Monitor::WaitResult Monitor::Wait(int64_t millis) { |
+ return WaitMicros(millis * kMicrosecondsPerMillisecond); |
+} |
+ |
+ |
+Monitor::WaitResult Monitor::WaitMicros(int64_t micros) { |
// TODO(iposva): Do we need to track lock owners? |
Monitor::WaitResult retval = kNotified; |
- if (millis == 0) { |
+ if (micros == kNoTimeout) { |
// Wait forever. |
int result = pthread_cond_wait(data_.cond(), data_.mutex()); |
VALIDATE_PTHREAD_RESULT(result); |
} else { |
struct timespec ts; |
- ComputeTimeSpec(&ts, millis); |
+ ComputeTimeSpecMicros(&ts, micros); |
int result = pthread_cond_timedwait(data_.cond(), data_.mutex(), &ts); |
ASSERT((result == 0) || (result == ETIMEDOUT)); |
if (result == ETIMEDOUT) { |