mirror of
https://github.com/Mercury-Language/mercury.git
synced 2025-12-13 12:53:53 +00:00
Implement some library predicates for Java using JNI.
Estimated hours taken: 13 Branches: main Implement some library predicates for Java using JNI. java/runtime/Native.java.in: A new class which uses JNI to provide any native functionality required by predicates of the standard library in Java. So far it only provides methods relating to timing. java/runtime/Native.c: Source code, written in C, which implements all the native methods of mercury.runtime.Native. Note that this implementation makes use of the existing C implementation of the equivalent functions. java/runtime/Mmakefile: Rules for compiling a shared object from Native.c. library/time.m: Implement the following predicates for Java using Native interface: time__c_clock/3 time__clocks_per_sec/1 time__times/7 library/benchmarking.m: Implement the following predicates for Java using Native interface: get_user_cpu_miliseconds/1 library/Mmakefile: Renamed to library/Mmakefile.in, so as to have access to FULLARCH constant. library/Mmakefile.in: Added rules for incorporating the Native shared object.
This commit is contained in:
35
java/runtime/Mmakefile
Normal file
35
java/runtime/Mmakefile
Normal file
@@ -0,0 +1,35 @@
|
||||
|
||||
# Copyright (C) 2004 The University of Melbourne.
|
||||
# This file may only be copied under the terms of the GNU Library General
|
||||
# Public License - see the file COPYING.LIB in the Mercury distribution.
|
||||
#
|
||||
# This Makefile compiles the shared object for use with mercury.runtime.Native
|
||||
#
|
||||
|
||||
MERCURY_DIR = ../..
|
||||
RUNTIME_DIR = $(MERCURY_DIR)/runtime
|
||||
|
||||
include $(MERCURY_DIR)/Mmake.common
|
||||
|
||||
NATIVE_PIC = Native.$(EXT_FOR_PIC_OBJECTS)
|
||||
NATIVE_SO = Native.$(EXT_FOR_SHARED_LIB)
|
||||
|
||||
MAIN_TARGET = $(NATIVE_SO)
|
||||
|
||||
PIC_OBJS = $(RUNTIME_DIR)/mercury_timing.$(EXT_FOR_PIC_OBJECTS) \
|
||||
$(NATIVE_PIC)
|
||||
|
||||
CFLAGS = -I$(RUNTIME_DIR)
|
||||
|
||||
$(NATIVE_SO): $(PIC_OBJS)
|
||||
$(LINK_SHARED_OBJ) -o $(NATIVE_SO) $(PIC_OBJS)
|
||||
|
||||
$(NATIVE_PIC): Native.c
|
||||
$(MGNUC) $(ALL_GRADEFLAGS) $(ALL_MGNUCFLAGS) \
|
||||
$(CFLAGS_FOR_PIC) -o $(NATIVE_PIC) -c Native.c
|
||||
|
||||
clean:
|
||||
rm -f $(PIC_OBJS)
|
||||
|
||||
realclean:
|
||||
rm -f $(PIC_OBJS) $(NATIVE_SO) *.class
|
||||
99
java/runtime/Native.c
Normal file
99
java/runtime/Native.c
Normal file
@@ -0,0 +1,99 @@
|
||||
/*
|
||||
** Copyright (C) 2004 The University of Melbourne.
|
||||
** This file may only be copied under the terms of the GNU Library General
|
||||
** Public License - see the file COPYING.LIB in the Mercury distribution.
|
||||
*/
|
||||
|
||||
/*
|
||||
** File: Native.c - Native code for java/runtime/Native.java
|
||||
*/
|
||||
|
||||
#include <jni.h>
|
||||
|
||||
/*
|
||||
* Class: Native
|
||||
* Method: clock
|
||||
* Signature: ()I
|
||||
*/
|
||||
JNIEXPORT jint JNICALL Java_mercury_runtime_Native_clock(JNIEnv *, jclass);
|
||||
|
||||
/*
|
||||
* Class: Native
|
||||
* Method: clocks_per_sec
|
||||
* Signature: ()I
|
||||
*/
|
||||
JNIEXPORT jint JNICALL Java_mercury_runtime_Native_clocks_1per_1sec(
|
||||
JNIEnv *, jclass);
|
||||
|
||||
/*
|
||||
* Class: Native
|
||||
* Method: times
|
||||
* Signature: ()[I
|
||||
*/
|
||||
JNIEXPORT jintArray JNICALL Java_mercury_runtime_Native_times(
|
||||
JNIEnv *, jclass);
|
||||
|
||||
/*
|
||||
* Class: Native
|
||||
* Method: get_user_cpu_miliseconds
|
||||
* Signature: ()I
|
||||
*/
|
||||
JNIEXPORT jint JNICALL Java_mercury_runtime_Native_get_1user_1cpu_1miliseconds(
|
||||
JNIEnv *, jclass);
|
||||
|
||||
#include "mercury_imp.h"
|
||||
#include "mercury_timing.h"
|
||||
|
||||
#include <time.h>
|
||||
#ifdef MR_HAVE_SYS_TYPES_H
|
||||
#include <sys/types.h>
|
||||
#endif
|
||||
#ifdef MR_HAVE_SYS_TIMES_H
|
||||
#include <sys/times.h>
|
||||
#endif
|
||||
#ifdef MR_HAVE_UNISTD_H
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
JNIEXPORT jint JNICALL
|
||||
Java_mercury_runtime_Native_clock(JNIEnv *env, jclass obj) {
|
||||
return (MR_Integer) clock();
|
||||
}
|
||||
|
||||
JNIEXPORT jint JNICALL
|
||||
Java_mercury_runtime_Native_clocks_1per_1sec(JNIEnv *env, jclass obj) {
|
||||
return CLOCKS_PER_SEC;
|
||||
}
|
||||
|
||||
JNIEXPORT jintArray JNICALL
|
||||
Java_mercury_runtime_Native_times(JNIEnv *env, jclass obj) {
|
||||
jint intarray[5];
|
||||
jintArray result;
|
||||
|
||||
#ifdef MR_HAVE_POSIX_TIMES
|
||||
struct tms t;
|
||||
|
||||
intarray[0] = (MR_Integer) times(&t);
|
||||
intarray[1] = (MR_Integer) t.tms_utime;
|
||||
intarray[2] = (MR_Integer) t.tms_stime;
|
||||
intarray[3] = (MR_Integer) t.tms_cutime;
|
||||
intarray[4] = (MR_Integer) t.tms_cstime;
|
||||
#else
|
||||
intarray[0] = -1;
|
||||
#endif
|
||||
|
||||
result = (*env)->NewIntArray(env, 5);
|
||||
if (result != NULL) {
|
||||
(*env)->SetIntArrayRegion(env, result, 0, 5, intarray);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
JNIEXPORT jint JNICALL
|
||||
Java_mercury_runtime_Native_get_1user_1cpu_1miliseconds(
|
||||
JNIEnv *env, jclass obj)
|
||||
{
|
||||
return MR_get_user_cpu_miliseconds();
|
||||
}
|
||||
|
||||
121
java/runtime/Native.java.in
Normal file
121
java/runtime/Native.java.in
Normal file
@@ -0,0 +1,121 @@
|
||||
//
|
||||
// Copyright (C) 2004 The University of Melbourne.
|
||||
// This file may only be copied under the terms of the GNU Library General
|
||||
// Public License - see the file COPYING.LIB in the Mercury distribution.
|
||||
//
|
||||
//
|
||||
// This class exists to provide any native functionality required by the Java
|
||||
// implementation. It uses JNI to access a shared object which it searches
|
||||
// for in all the directories listed in the current CLASSPATH.
|
||||
//
|
||||
// At the moment the only services provided are those relating to timing.
|
||||
//
|
||||
|
||||
package mercury.runtime;
|
||||
|
||||
public class Native {
|
||||
/*
|
||||
** SHARED_OBJ is the name of the shared object which contains all the
|
||||
** compiled native code.
|
||||
*/
|
||||
private static final java.lang.String SHARED_OBJ =
|
||||
"Native.@EXT_FOR_SHARED_LIB@";
|
||||
|
||||
/*
|
||||
** available and isAvailable() are true when native functionality
|
||||
** is available. (ie SHARED_OBJ was loaded successfully)
|
||||
*/
|
||||
private static boolean available = false;
|
||||
|
||||
public static boolean isAvailable() {
|
||||
return available;
|
||||
}
|
||||
|
||||
static {
|
||||
available = load_library();
|
||||
}
|
||||
|
||||
/*
|
||||
** load_library():
|
||||
** Searches all the directories listed in the classpath,
|
||||
** (including the directories containing each jar file) for
|
||||
** the shared object SHARED_OBJ and attempts to load this file
|
||||
** if found.
|
||||
** Also searches in the subdirectory Constants.MR_FULLARCH.
|
||||
** Returns true if successful, false otherwise.
|
||||
*/
|
||||
private static boolean load_library() {
|
||||
java.util.StringTokenizer classpath =
|
||||
new java.util.StringTokenizer(
|
||||
java.lang.System.getProperty("java.class.path")
|
||||
,java.lang.System.getProperty("path.separator")
|
||||
);
|
||||
|
||||
while (classpath.hasMoreTokens()) {
|
||||
java.io.File dir;
|
||||
java.io.File entry = new java.io.File(
|
||||
classpath.nextToken());
|
||||
|
||||
try {
|
||||
if (entry.isDirectory()) {
|
||||
dir = entry;
|
||||
} else {
|
||||
dir = entry.getParentFile();
|
||||
}
|
||||
if (dir == null) {
|
||||
dir = new java.io.File("");
|
||||
}
|
||||
|
||||
java.io.File match = new java.io.File(dir,
|
||||
SHARED_OBJ);
|
||||
if (match.exists() == false) {
|
||||
dir = new java.io.File(dir,
|
||||
mercury.runtime.Constants.
|
||||
MR_FULLARCH);
|
||||
match = new java.io.File(dir,
|
||||
SHARED_OBJ);
|
||||
}
|
||||
|
||||
java.lang.System.load(match.getAbsolutePath());
|
||||
return true;
|
||||
}
|
||||
catch (java.lang.Exception e) {
|
||||
continue;
|
||||
}
|
||||
} // while classpath.hasMoreTokens()
|
||||
|
||||
return false;
|
||||
} // load_library()
|
||||
|
||||
/*
|
||||
** clock():
|
||||
** Calls clock() from the <time.h> library.
|
||||
*/
|
||||
public static native int clock();
|
||||
|
||||
/*
|
||||
** clocks_per_sec():
|
||||
** Returns the number of "clocks" per sec as defined by
|
||||
** CLOCKS_PER_SEC.
|
||||
*/
|
||||
public static native int clocks_per_sec();
|
||||
|
||||
/*
|
||||
** times():
|
||||
** Calls times() from the <time.h> library. Results are returned
|
||||
** in an int array of the form:
|
||||
** { <return value>, utime, stime, cutime, cstime }
|
||||
** If POSIX times are not available, <return value> will be set
|
||||
** to -1, and the other values are undefined.
|
||||
** Returns null if the array cannot be constructed.
|
||||
*/
|
||||
public static native int[] times();
|
||||
|
||||
/*
|
||||
** get_user_cpu_miliseconds():
|
||||
** Native method to return the CPU time consumed by the process,
|
||||
** in miliseconds, from an arbitrary initial time.
|
||||
*/
|
||||
public static native int get_user_cpu_miliseconds();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user