Panda: replace pthread_mutex with std::mutex (#19909)

old-commit-hash: 5594b8f3cb
vw-mqb-aeb
Dean Lee 5 years ago committed by GitHub
parent 9d1220b117
commit fe457a93d5
  1. 18
      selfdrive/boardd/panda.cc
  2. 3
      selfdrive/boardd/panda.h

@ -40,7 +40,6 @@ void panda_set_power(bool power){
Panda::Panda(){
int err;
err = pthread_mutex_init(&usb_lock, NULL);
if (err != 0) { goto fail; }
// init libusb
@ -83,10 +82,9 @@ fail:
}
Panda::~Panda(){
pthread_mutex_lock(&usb_lock);
std::lock_guard lk(usb_lock);
cleanup();
connected = false;
pthread_mutex_unlock(&usb_lock);
}
void Panda::cleanup(){
@ -117,14 +115,12 @@ int Panda::usb_write(uint8_t bRequest, uint16_t wValue, uint16_t wIndex, unsigne
return LIBUSB_ERROR_NO_DEVICE;
}
pthread_mutex_lock(&usb_lock);
std::lock_guard lk(usb_lock);
do {
err = libusb_control_transfer(dev_handle, bmRequestType, bRequest, wValue, wIndex, NULL, 0, timeout);
if (err < 0) handle_usb_issue(err, __func__);
} while (err < 0 && connected);
pthread_mutex_unlock(&usb_lock);
return err;
}
@ -132,12 +128,11 @@ int Panda::usb_read(uint8_t bRequest, uint16_t wValue, uint16_t wIndex, unsigned
int err;
const uint8_t bmRequestType = LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE;
pthread_mutex_lock(&usb_lock);
std::lock_guard lk(usb_lock);
do {
err = libusb_control_transfer(dev_handle, bmRequestType, bRequest, wValue, wIndex, data, wLength, timeout);
if (err < 0) handle_usb_issue(err, __func__);
} while (err < 0 && connected);
pthread_mutex_unlock(&usb_lock);
return err;
}
@ -150,7 +145,7 @@ int Panda::usb_bulk_write(unsigned char endpoint, unsigned char* data, int lengt
return 0;
}
pthread_mutex_lock(&usb_lock);
std::lock_guard lk(usb_lock);
do {
// Try sending can messages. If the receive buffer on the panda is full it will NAK
// and libusb will try again. After 5ms, it will time out. We will drop the messages.
@ -164,7 +159,6 @@ int Panda::usb_bulk_write(unsigned char endpoint, unsigned char* data, int lengt
}
} while(err != 0 && connected);
pthread_mutex_unlock(&usb_lock);
return transferred;
}
@ -176,7 +170,7 @@ int Panda::usb_bulk_read(unsigned char endpoint, unsigned char* data, int length
return 0;
}
pthread_mutex_lock(&usb_lock);
std::lock_guard lk(usb_lock);
do {
err = libusb_bulk_transfer(dev_handle, endpoint, data, length, &transferred, timeout);
@ -191,8 +185,6 @@ int Panda::usb_bulk_read(unsigned char endpoint, unsigned char* data, int length
} while(err != 0 && connected);
pthread_mutex_unlock(&usb_lock);
return transferred;
}

@ -3,6 +3,7 @@
#include <ctime>
#include <cstdint>
#include <pthread.h>
#include <mutex>
#include <libusb-1.0/libusb.h>
@ -41,7 +42,7 @@ class Panda {
private:
libusb_context *ctx = NULL;
libusb_device_handle *dev_handle = NULL;
pthread_mutex_t usb_lock;
std::mutex usb_lock;
void handle_usb_issue(int err, const char func[]);
void cleanup();

Loading…
Cancel
Save