Squashed 'third_party/allwpilib_2019/' changes from bd05dfa1c..99e4f7dd2

99e4f7dd2 Fix SPI CS1 not working correctly (#1614)
60c2f5905 C++ CameraServer: initialize default Usb camera device number (#1601)
d55ca191b CameraServer: Add switched camera support (#1600)
e8b24717c C++ Shuffleboard fixes (#1595)
182758c05 Fix Ultrasonic sensor runner thread (#1598)
74f7ba04b Java PIDBase: Make setPIDSourceType and getPIDSourceType public (#1599)
997d4fdf4 Update HAL GetStackTrace to properly report user errors (#1594)
76d9e2663 uv: Add reuse to pipe (#1577)
a230c814c Add support for WPILib vscode extension for allwpilib C++ intellisense (#1590)
12cb77cd7 Fix DS Comm button tests (#1591)
8a9822a96 Allow multiple instances of the same PDP (#1582)
a9371a758 Fix missing exposure property on windows USB cameras (#1571)
6992f5421 cscore: Avoid crash if invalid (null) source set in MjpegServer (#1585)
43696956d Fix Watchdog incorrectly resetting expiration flag upon disable (#1580)
ae3fd5ada Fix docs search having unspecified module directory (#1568)
404666b29 Fix Halsim DS Button Format (#1583)
1eb4c99d1 Update README for 2019 changes (#1569)
910b9f3af Add support for camera descriptions on windows (#1572)
09d90b02f Remove prints and unecessary items from windows GetNetworkInterfaces (#1573)
0e1f9c2ed ntcore: Read ini escaped quotes correctly (#1579)
f156a0011 wpiutil uv: Pass IPC value of listening pipe to accepted pipe (#1576)
4a6087ed5 Disable watchdog test on mac (#1578)
88a09dd13 cscore: Handle USB camera integer menus (#1561)
7d1959636 Changed terminology from "Overload" to "Override" (#1563)

Change-Id: If58e497053b7e80f1f3d6182ad6060a186616a0f
git-subtree-dir: third_party/allwpilib_2019
git-subtree-split: 99e4f7dd2c497d24016b7cb0e6fe16b04d2db1e8
diff --git a/hal/src/main/native/athena/PDP.cpp b/hal/src/main/native/athena/PDP.cpp
index ff1eb81..22ea8c1 100644
--- a/hal/src/main/native/athena/PDP.cpp
+++ b/hal/src/main/native/athena/PDP.cpp
@@ -9,6 +9,8 @@
 
 #include <memory>
 
+#include <wpi/mutex.h>
+
 #include "HALInitializer.h"
 #include "PortsInternal.h"
 #include "hal/CANAPI.h"
@@ -106,9 +108,16 @@
   } bits;
 };
 
+static wpi::mutex pdpHandleMutex;
+static HAL_PDPHandle pdpHandles[kNumPDPModules];
+
 namespace hal {
 namespace init {
-void InitializePDP() {}
+void InitializePDP() {
+  for (int i = 0; i < kNumPDPModules; i++) {
+    pdpHandles[i] = HAL_kInvalidHandle;
+  }
+}
 }  // namespace init
 }  // namespace hal
 
@@ -121,6 +130,13 @@
     return HAL_kInvalidHandle;
   }
 
+  std::lock_guard<wpi::mutex> lock(pdpHandleMutex);
+
+  if (pdpHandles[module] != HAL_kInvalidHandle) {
+    *status = 0;
+    return pdpHandles[module];
+  }
+
   auto handle = HAL_InitializeCAN(manufacturer, module, deviceType, status);
 
   if (*status != 0) {
@@ -128,10 +144,21 @@
     return HAL_kInvalidHandle;
   }
 
+  pdpHandles[module] = handle;
+
   return handle;
 }
 
-void HAL_CleanPDP(HAL_PDPHandle handle) { HAL_CleanCAN(handle); }
+void HAL_CleanPDP(HAL_PDPHandle handle) {
+  HAL_CleanCAN(handle);
+
+  for (int i = 0; i < kNumPDPModules; i++) {
+    if (pdpHandles[i] == handle) {
+      pdpHandles[i] = HAL_kInvalidHandle;
+      return;
+    }
+  }
+}
 
 HAL_Bool HAL_CheckPDPModule(int32_t module) {
   return module < kNumPDPModules && module >= 0;