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