Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Panda: replace pthread_mutex with std::mutex #19909

Merged
merged 1 commit into from
Jan 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 5 additions & 13 deletions selfdrive/boardd/panda.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -83,10 +82,9 @@ Panda::Panda(){
}

Panda::~Panda(){
pthread_mutex_lock(&usb_lock);
std::lock_guard lk(usb_lock);
cleanup();
connected = false;
pthread_mutex_unlock(&usb_lock);
}

void Panda::cleanup(){
Expand Down Expand Up @@ -117,27 +115,24 @@ 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;
}

int Panda::usb_read(uint8_t bRequest, uint16_t wValue, uint16_t wIndex, unsigned char *data, uint16_t wLength, unsigned int timeout) {
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;
}
Expand All @@ -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.
Expand All @@ -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;
}

Expand All @@ -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);
Expand All @@ -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;
}

Expand Down
3 changes: 2 additions & 1 deletion selfdrive/boardd/panda.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <ctime>
#include <cstdint>
#include <pthread.h>
#include <mutex>

#include <libusb-1.0/libusb.h>

Expand Down Expand Up @@ -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();

Expand Down