Home | History | Annotate | Download | only in jni
      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 /*
     83  * JNI registration.
     84  */
     85 static JNINativeMethod gMethods[] = {
     86     /* name, signature, funcPtr */
     87     { "setCurrentTimeMillis",      "(J)Z",
     88             (void*) android_os_SystemClock_setCurrentTimeMillis },
     89     { "uptimeMillis",      "()J",
     90             (void*) android_os_SystemClock_uptimeMillis },
     91     { "elapsedRealtime",      "()J",
     92             (void*) android_os_SystemClock_elapsedRealtime },
     93     { "currentThreadTimeMillis",      "()J",
     94             (void*) android_os_SystemClock_currentThreadTimeMillis },
     95 };
     96 int register_android_os_SystemClock(JNIEnv* env)
     97 {
     98     return AndroidRuntime::registerNativeMethods(env,
     99             "android/os/SystemClock", gMethods, NELEM(gMethods));
    100 }
    101 
    102 }; // namespace android
    103 
    104