The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | |
| 18 | /* |
| 19 | * System clock functions. |
| 20 | */ |
| 21 | |
| 22 | #include "JNIHelp.h" |
| 23 | #include "jni.h" |
| 24 | #include "android_runtime/AndroidRuntime.h" |
| 25 | |
| 26 | #include "utils/SystemClock.h" |
| 27 | |
| 28 | #include <sys/time.h> |
| 29 | #include <time.h> |
| 30 | |
| 31 | namespace android { |
| 32 | |
| 33 | /* |
| 34 | * native public static void setCurrentTimeMillis(long millis) |
| 35 | * |
| 36 | * Set the current time. This only works when running as root. |
| 37 | */ |
| 38 | static jboolean android_os_SystemClock_setCurrentTimeMillis(JNIEnv* env, |
| 39 | jobject clazz, jlong millis) |
| 40 | { |
| 41 | return (setCurrentTimeMillis(millis) == 0); |
| 42 | } |
| 43 | |
| 44 | /* |
| 45 | * native public static long uptimeMillis(); |
| 46 | */ |
| 47 | static jlong android_os_SystemClock_uptimeMillis(JNIEnv* env, |
| 48 | jobject clazz) |
| 49 | { |
| 50 | return (jlong)uptimeMillis(); |
| 51 | } |
| 52 | |
| 53 | /* |
| 54 | * native public static long elapsedRealtime(); |
| 55 | */ |
| 56 | static jlong android_os_SystemClock_elapsedRealtime(JNIEnv* env, |
| 57 | jobject clazz) |
| 58 | { |
| 59 | return (jlong)elapsedRealtime(); |
| 60 | } |
| 61 | |
| 62 | /* |
| 63 | * native public static long currentThreadTimeMillis(); |
| 64 | */ |
| 65 | static jlong android_os_SystemClock_currentThreadTimeMillis(JNIEnv* env, |
| 66 | jobject clazz) |
| 67 | { |
| 68 | #if defined(HAVE_POSIX_CLOCKS) |
| 69 | struct timespec tm; |
| 70 | |
| 71 | clock_gettime(CLOCK_THREAD_CPUTIME_ID, &tm); |
| 72 | |
| 73 | return tm.tv_sec * 1000LL + tm.tv_nsec / 1000000; |
| 74 | #else |
| 75 | struct timeval tv; |
| 76 | |
| 77 | gettimeofday(&tv, NULL); |
| 78 | return tv.tv_sec * 1000LL + tv.tv_usec / 1000; |
| 79 | #endif |
| 80 | } |
| 81 | |
| 82 | /* |
Romain Guy | 648bee1 | 2011-07-20 18:47:17 -0700 | [diff] [blame^] | 83 | * native public static long currentThreadTimeMicro(); |
| 84 | */ |
| 85 | static jlong android_os_SystemClock_currentThreadTimeMicro(JNIEnv* env, |
| 86 | jobject clazz) |
| 87 | { |
| 88 | #if defined(HAVE_POSIX_CLOCKS) |
| 89 | struct timespec tm; |
| 90 | |
| 91 | clock_gettime(CLOCK_THREAD_CPUTIME_ID, &tm); |
| 92 | |
| 93 | return tm.tv_sec * 1000000LL + tm.tv_nsec / 1000; |
| 94 | #else |
| 95 | struct timeval tv; |
| 96 | |
| 97 | gettimeofday(&tv, NULL); |
| 98 | return tv.tv_sec * 1000000LL + tv.tv_nsec / 1000; |
| 99 | #endif |
| 100 | } |
| 101 | |
| 102 | /* |
| 103 | * native public static long currentTimeMicro(); |
| 104 | */ |
| 105 | static jlong android_os_SystemClock_currentTimeMicro(JNIEnv* env, |
| 106 | jobject clazz) |
| 107 | { |
| 108 | struct timeval tv; |
| 109 | |
| 110 | gettimeofday(&tv, NULL); |
| 111 | return tv.tv_sec * 1000000LL + tv.tv_usec; |
| 112 | } |
| 113 | |
| 114 | /* |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 115 | * JNI registration. |
| 116 | */ |
| 117 | static JNINativeMethod gMethods[] = { |
| 118 | /* name, signature, funcPtr */ |
| 119 | { "setCurrentTimeMillis", "(J)Z", |
| 120 | (void*) android_os_SystemClock_setCurrentTimeMillis }, |
| 121 | { "uptimeMillis", "()J", |
| 122 | (void*) android_os_SystemClock_uptimeMillis }, |
| 123 | { "elapsedRealtime", "()J", |
| 124 | (void*) android_os_SystemClock_elapsedRealtime }, |
| 125 | { "currentThreadTimeMillis", "()J", |
| 126 | (void*) android_os_SystemClock_currentThreadTimeMillis }, |
Romain Guy | 648bee1 | 2011-07-20 18:47:17 -0700 | [diff] [blame^] | 127 | { "currentThreadTimeMicro", "()J", |
| 128 | (void*) android_os_SystemClock_currentThreadTimeMicro }, |
| 129 | { "currentTimeMicro", "()J", |
| 130 | (void*) android_os_SystemClock_currentTimeMicro }, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 131 | }; |
| 132 | int register_android_os_SystemClock(JNIEnv* env) |
| 133 | { |
| 134 | return AndroidRuntime::registerNativeMethods(env, |
| 135 | "android/os/SystemClock", gMethods, NELEM(gMethods)); |
| 136 | } |
| 137 | |
| 138 | }; // namespace android |
| 139 | |