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.util.ArrayList;
008import java.util.List;
009import java.util.concurrent.locks.ReentrantLock;
010
011public class EventVector {
012  private final ReentrantLock m_lock = new ReentrantLock();
013  private final List<Integer> m_events = new ArrayList<>();
014
015  /**
016   * Adds an event to the event vector.
017   *
018   * @param handle The event to add
019   */
020  public void add(int handle) {
021    m_lock.lock();
022    try {
023      m_events.add(handle);
024    } finally {
025      m_lock.unlock();
026    }
027  }
028
029  /**
030   * Removes an event from the event vector.
031   *
032   * @param handle The event to remove
033   */
034  public void remove(int handle) {
035    m_lock.lock();
036    try {
037      m_events.removeIf(x -> x == handle);
038    } finally {
039      m_lock.unlock();
040    }
041  }
042
043  /** Wakes up all events in the event vector. */
044  public void wakeup() {
045    m_lock.lock();
046    try {
047      for (Integer eventHandle : m_events) {
048        WPIUtilJNI.setEvent(eventHandle);
049      }
050    } finally {
051      m_lock.unlock();
052    }
053  }
054}