ipbus / ipbus-software

Software that implements a reliable high-performance control link for particle physics electronics, based on the IPbus protocol
https://ipbus.web.cern.ch
GNU General Public License v3.0
22 stars 24 forks source link

SigBusGuard: SIBGUS handler class #278

Closed tswilliams closed 1 year ago

tswilliams commented 1 year ago

The SIGBUS signal can be received when errors occur during reads/writes from mmap'ed device files. This branch adds the SigBusGuard class, which implements a signal handler that will throw a SigBusError exception whenever SIGBUS is received. The ipbusmmap-2.0 client was also updated to protect against SIGBUS with this class.

Intended usage: An instance of SigBusGuard class should be created before any set of reads/writes from the mmap'ed device file, and the reads/writes themselves should be implemented in a std::function/lambda that's passed to the protect method, along with a message which will be used for the exception that's thrown. I.e:

uhal::SigBusGuard guard;
guard.protect([&]{
  // Put mmap'ed device file reads/writes here
}, "An error occurred when reading from address 0x... in file ...");

Note: The SIGBUS signal handler is only defined for the lifetime of the SigBusGuard instance, and to ensure the the signals are handled by the appropriate thread a static mutex is is locked in the SigBusGuard contructor and unlocked in the destructor. As a result only one SigBusGuard instance can be alive at any given time, and so a deadlock will occur if e.g. the user were to attempt to create multiple instances in a single function, e.g:

uhal::SigBusGuard guard1;
uhal::SigBusGuard guard2;
// Execution would get stuck on the line above; code below this line would never be reached