3

I want to write a plugin architecture for Linux. I've tried researching how to do it, but I actually keep running into information for more complex plugin architecture then what I need, I want only a very basic implementation.

To explain what I'm doing, I have a program that accepts and process input from various sources, multiple instances of the same program will likely run with each instance accepting a different input source. I want to do some level of error checking and correction but the logic for such error checking will vary dependent on input source. So I want my program to open a plugin for the specific source it's reading in (the plugin name would presumable be in the config file) and run an error check method provided by library. This is more basic than most of the architectures and information I see for plugins because

  • It's not cross platform. I know I’ll be using x86 linux platform, and could even define an exact compiler if necessary
  • The 'plugin' is very basic, it could be as little as a single method provided by each plugin.

However, there are two things I must have

1) It must be fast. I'll be reading in streaming data at high volume and need to process it quickly. I've crossed out the use of scripting agents for this very reason, I was afraid the overhead of translating scripting logic for every input may be to great

2) I must be able to detect an update to an existing .SO and load the new version without stopping the program.

I'm still semi-new to c++ in general and so am a little apprehensive about trying to develop anything too complicated. I'm looking to determine what the easiest viable solution is for me.

I've considered Boost.Extension, but it may actually be overkill for what I need. I'm trying to decide if it's better to just define the .SO by hand rather than trying to use the Boost framework due to how simple my use case is. There also are some small headaches with using Boost where I work, I can do it but I would have to jump through a few hoops to get permission.

So can anyone tell me if 1) plugin architecture is really what I should be trying here (is there an easier solution?) 2) if they think Boost.Extensions is the best approach 3) can point me to any documents that describe how to make a platform-specific plugin architecture (I've seen plenty talking about platform independent, but I don't need to do anything that complicated). 4) what is the best way to ensure I load a newer version of a library while running? preferable a method that avoids an if statement every time I accept input due to the earlier stated need for fast processing.

dsollen
  • 6,046
  • 6
  • 43
  • 84
  • No time for a thorough answer right now, but I'd say yes for 1 (using pipes, shared memory or some other IPC mechanism is not simpler), no for 2 (would be good if you're targeting windows however), 3) `man dlopen`, 4) you'll likely need to poll yourself for the file timestamp from time to time. – Alexandre C. Jul 11 '12 at 16:47
  • 2
    For 4) I think there's options to watch certain files for changes, or atleast on Windows there's somethign like that. – Xeo Jul 11 '12 at 16:49
  • @Xeo: didn't think of that. There's `inotify` for linux. – Alexandre C. Jul 11 '12 at 16:49
  • Have you taken a look at LLVM? Scripting would be less of an issue with it. – Ignacio Vazquez-Abrams Jul 11 '12 at 16:51

1 Answers1

6

1) Yes. In your case, this is a simple solution. Linux has a traditionally simple API, and writing/using shared libraries is usually straightforward (with a few caveats, see below).

2) Since you're targeting Linux, there's no need for a platform independent abstraction. Platform independent shared library management is full of gory details, and here you're not concerned with this. Boost Extensions may turn out less simple than a linux only solution.

3) Use dlopen and dlsym with C functions (ie. learn about extern "C" and name mangling beforehand). There are plenty of tutorials out there dealing with C++ and dlsym/dlopen (like this one). Basically, the simpler your interface is, the better (best is a few C functions and no classes).

4) You may have a look at inotify, which is the Linux standard for filesystem event notification. Add a watch per library you have loaded, you'll get notified of any change by a handler. The handler should only set a flag for the main loop to reload the library if needed.

Alexandre C.
  • 55,948
  • 11
  • 128
  • 197