001// Copyright (c) FIRST and other WPILib contributors. 002// Open Source Software; you can modify and/or share it under the terms of 003// the WPILib BSD license file in the root directory of this project. 004 005package edu.wpi.first.util; 006 007import java.io.IOException; 008import java.util.concurrent.atomic.AtomicBoolean; 009 010public class WPIUtilJNI { 011 static boolean libraryLoaded = false; 012 static RuntimeLoader<WPIUtilJNI> loader = null; 013 014 public static class Helper { 015 private static AtomicBoolean extractOnStaticLoad = new AtomicBoolean(true); 016 017 public static boolean getExtractOnStaticLoad() { 018 return extractOnStaticLoad.get(); 019 } 020 021 public static void setExtractOnStaticLoad(boolean load) { 022 extractOnStaticLoad.set(load); 023 } 024 } 025 026 static { 027 if (Helper.getExtractOnStaticLoad()) { 028 try { 029 loader = 030 new RuntimeLoader<>( 031 "wpiutiljni", RuntimeLoader.getDefaultExtractionRoot(), WPIUtilJNI.class); 032 loader.loadLibrary(); 033 } catch (IOException ex) { 034 ex.printStackTrace(); 035 System.exit(1); 036 } 037 libraryLoaded = true; 038 } 039 } 040 041 /** 042 * Force load the library. 043 * 044 * @throws IOException if the library failed to load 045 */ 046 public static synchronized void forceLoad() throws IOException { 047 if (libraryLoaded) { 048 return; 049 } 050 loader = 051 new RuntimeLoader<>( 052 "wpiutiljni", RuntimeLoader.getDefaultExtractionRoot(), WPIUtilJNI.class); 053 loader.loadLibrary(); 054 libraryLoaded = true; 055 } 056 057 public static native void writeStderr(String str); 058 059 public static native void enableMockTime(); 060 061 public static native void disableMockTime(); 062 063 public static native void setMockTime(long time); 064 065 public static native long now(); 066 067 public static native long getSystemTime(); 068 069 public static native int createEvent(boolean manualReset, boolean initialState); 070 071 public static native void destroyEvent(int eventHandle); 072 073 public static native void setEvent(int eventHandle); 074 075 public static native void resetEvent(int eventHandle); 076 077 public static native int createSemaphore(int initialCount, int maximumCount); 078 079 public static native void destroySemaphore(int semHandle); 080 081 public static native boolean releaseSemaphore(int semHandle, int releaseCount); 082 083 public static native long allocateRawFrame(); 084 085 public static native void freeRawFrame(long frame); 086 087 /** 088 * Waits for a handle to be signaled. 089 * 090 * @param handle handle to wait on 091 * @throws InterruptedException on failure (e.g. object was destroyed) 092 */ 093 public static native void waitForObject(int handle) throws InterruptedException; 094 095 /** 096 * Waits for a handle to be signaled, with timeout. 097 * 098 * @param handle handle to wait on 099 * @param timeout timeout in seconds 100 * @return True if timeout reached without handle being signaled 101 * @throws InterruptedException on failure (e.g. object was destroyed) 102 */ 103 public static native boolean waitForObjectTimeout(int handle, double timeout) 104 throws InterruptedException; 105 106 /** 107 * Waits for one or more handles to be signaled. 108 * 109 * <p>Invalid handles are treated as signaled; the returned array will have the handle error bit 110 * set for any invalid handles. 111 * 112 * @param handles array of handles to wait on 113 * @return array of signaled handles 114 * @throws InterruptedException on failure (e.g. no objects were signaled) 115 */ 116 public static native int[] waitForObjects(int[] handles) throws InterruptedException; 117 118 /** 119 * Waits for one or more handles to be signaled, with timeout. 120 * 121 * <p>Invalid handles are treated as signaled; the returned array will have the handle error bit 122 * set for any invalid handles. 123 * 124 * @param handles array of handles to wait on 125 * @param timeout timeout in seconds 126 * @return array of signaled handles; empty if timeout reached without any handle being signaled 127 * @throws InterruptedException on failure (e.g. no objects were signaled and no timeout) 128 */ 129 public static native int[] waitForObjectsTimeout(int[] handles, double timeout) 130 throws InterruptedException; 131}