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  /**
084   * Waits for a handle to be signaled.
085   *
086   * @param handle handle to wait on
087   * @throws InterruptedException on failure (e.g. object was destroyed)
088   */
089  public static native void waitForObject(int handle) throws InterruptedException;
090
091  /**
092   * Waits for a handle to be signaled, with timeout.
093   *
094   * @param handle handle to wait on
095   * @param timeout timeout in seconds
096   * @return True if timeout reached without handle being signaled
097   * @throws InterruptedException on failure (e.g. object was destroyed)
098   */
099  public static native boolean waitForObjectTimeout(int handle, double timeout)
100      throws InterruptedException;
101
102  /**
103   * Waits for one or more handles to be signaled.
104   *
105   * <p>Invalid handles are treated as signaled; the returned array will have the handle error bit
106   * set for any invalid handles.
107   *
108   * @param handles array of handles to wait on
109   * @return array of signaled handles
110   * @throws InterruptedException on failure (e.g. no objects were signaled)
111   */
112  public static native int[] waitForObjects(int[] handles) throws InterruptedException;
113
114  /**
115   * Waits for one or more handles to be signaled, with timeout.
116   *
117   * <p>Invalid handles are treated as signaled; the returned array will have the handle error bit
118   * set for any invalid handles.
119   *
120   * @param handles array of handles to wait on
121   * @param timeout timeout in seconds
122   * @return array of signaled handles; empty if timeout reached without any handle being signaled
123   * @throws InterruptedException on failure (e.g. no objects were signaled and no timeout)
124   */
125  public static native int[] waitForObjectsTimeout(int[] handles, double timeout)
126      throws InterruptedException;
127}