Skip to content

Commit

Permalink
wait for thread to terminate before closing pin fd
Browse files Browse the repository at this point in the history
  • Loading branch information
2bndy5 committed Mar 19, 2024
1 parent 0df788c commit 062d011
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
6 changes: 4 additions & 2 deletions utility/RPi/interrupt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ IrqChipCache::IrqChipCache()
IrqChipCache::~IrqChipCache()
{
for (std::map<rf24_gpio_pin_t, IrqPinCache>::iterator i = irqCache.begin(); i != irqCache.end(); ++i) {
pthread_cancel(i->second.id);
pthread_cancel(i->second.id); // send cancel request
pthread_join(i->second, NULL); // wait till thread terminates
close(i->second.fd);
}
irqCache.clear();
Expand Down Expand Up @@ -180,7 +181,8 @@ int detachInterrupt(rf24_gpio_pin_t pin)
if (cachedPin == irqCache.end()) {
return 0; // pin not in cache; just exit
}
pthread_cancel(cachedPin->second.id);
pthread_cancel(cachedPin->second.id); // send cancel request
pthread_join(cachedPin->second.id, NULL); // wait till thread terminates
close(cachedPin->second.fd);
irqCache.erase(cachedPin);
return 1;
Expand Down
6 changes: 4 additions & 2 deletions utility/SPIDEV/interrupt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ struct IrqChipCache : public GPIOChipCache
~IrqChipCache()
{
for (std::map<rf24_gpio_pin_t, IrqPinCache>::iterator i = irqCache.begin(); i != irqCache.end(); ++i) {
pthread_cancel(i->second.id);
pthread_cancel(i->second.id); // send cancel request
pthread_join(i->second.id, NULL); // wait till thread terminates
close(i->second.fd);
}
irqCache.clear();
Expand Down Expand Up @@ -160,7 +161,8 @@ int detachInterrupt(rf24_gpio_pin_t pin)
if (cachedPin == irqCache.end()) {
return 0; // pin not in cache; just exit
}
pthread_cancel(cachedPin->second.id);
pthread_cancel(cachedPin->second.id); // send cancel request
pthread_join(cachedPin->second.id, NULL); // wait till thread terminates
close(cachedPin->second.fd);
irqCache.erase(cachedPin);
return 1;
Expand Down

0 comments on commit 062d011

Please sign in to comment.