I'm still working through the wxWidgets bug, implementing multithreading. I already reported a possible snag with race conditions with users setting specific keyboard actions. However i've come accross a rather larger snag.
Calling plRemakePlot also generates a race condition, perhaps in some very complicated ways. From a quick look at the code there is some attempt to protect the buffer from being written to while it is being read. However a number of problems can occur. If the main thread calls plbuf_save while the GUI thread is running plRemakePlot then the plbuf_write and plbuf_read flags will be set in both functions causing them to be reset before one function has finished - i guess it's quite likely that a segfault can occur in such a
Also if the main thread attempts to draw while the GUI thread is running plRemakePlot then it looks to me as though various functions in plcore.c will check plbuf_write, find it is false and not write their commands to the buffer. This will presumably cause odd rendering bugs.
There is even a comment in the plRemakePlot source saying
// Change the status of the flags before checking for a buffer.
// Actually, more thought is needed if we want to support multithreaded
// code correctly, specifically the case where two threads are using
// the same plot stream (e.g. one thread is drawing the plot and another
// thread is processing window manager messages).
If we want to have multithreading work correctly then this is the time for more thought.
Does anyone have any suggestions? There are a few ways I can imagine making things work - I don't know how well each wil function or which will be best but i thought I'd throw them out as ideas for comment.
1) Try to threadsafe the plstream and/or the plbuf_buffer. Probably a good solution, but a lot of work and difficult to do in a cross platform way.
2) Create a function that creates a snapshot of the plbuf_buffer in a threadsafe way. This is also not very easy as the buffer could get reallocated during the copy or commands without data could be included without some sort of mutex. Again cross platform multithreading becomes an issue.
3)The wxWidgets device maintains its own version of plbuf_buffer. Could this be done by having the wxWidgets device have a local PLStream that it can use to call the functions in plbuf? This would allow code reuse. There is increased memory use here but it could be that this is the
Any other suggestions or comments?