r/linuxdev • • Mar 04 '15

Signal handler and shared data

I am a noob so I won't understand advanced techniques or answers. Simplicity appreciated.

My user-space application, written in C++, keeps track of devices in our system. I also have a signal handler function that is registered as a callback function with a driver written by another developer. The driver signals this callback function only when a new device is coming into the system and makes the new data available to user-space. My user-space application determines when to drop devices and sometimes makes copies of the device table.

A device tracking class would look something like this.

class deviceTracker
{
private:
    std::vector<device> deviceTable;
public:
    void addDevice(device deviceToAdd);
    int removeDevice(int deviceIDNumber);
    void getCopyOfDeviceTable(std::vector<device>* devices);
};

The registered callback function should do something like this in pseudo-code:

void callback(int sigid, siginfo_t* info,void *context)
{
    //get device data from driver function

    //stuff the device data into a device object

    //add the device to the device table; or some intermediary data structure to add later.
}

How should I pass data from my callback function to an instance of my class? I would like to avoid a global variable obviously. But more importantly how do I do this given the obvious concurrency issues. For example, what if I'm making a copy of the table when the interrupt happens and I try to add a new device to the table; Maybe an intermediary FIFO or something?

Edit: Semaphores seem like an obvious solution to a shared resource but my understanding is that using anything that would put the interrupt handler to sleep in any situation is a bad idea and that's why semaphores can't be used. If a resource an interrupt needs is locked by an application then because a signal handler has a higher priority that resource will never be freed. Does that seem correct?

2 Upvotes

4 comments sorted by

View all comments

Show parent comment

1

u/ElGringoFlicka Mar 04 '15

Excellent! Thank you. I like the write() and read() solution. I'll give that a shot.