This is a difficult problem to describe so please let me know if anything is unclear.
I am trying to solve a possible deadlock situation in my C++ app and I am having trouble visualizing an appropriate solution. The restrictions placed on me by the two libraries I am trying to connect make my problem very complex and troublesome but it can all boil down to a simple diagram. Quite simply I have a situation like so
Python | Thread 1 Thread 2
|
Action 1 -|-> GIL LOCK --> Random Calls
Action 2 | GIL LOCK <----------------------------- [Action 2]
| Action 1 -- signals ---> Do_Action_1
| Wait Forever Wait on Action 2
Action 2 happens to be a timer firing, the library I am using has this timer code that will call functions after a set amount of time.
Trouble occurs when a timer fires while I am destroying the timer handler (Action 1). The timer handler will wait for the timers to finish their functions and the timer functions cannot finish because they are waiting to call a python function. In order to get into python they need to take the GIL which is being held by the timer handler destruction action.
I am wondering if its possible for me from Thread 1 to interrupt its wait on the GIL and unwind its stack to put it out of harms way of this deadlock. Although I fear going down to far into specific OS implementations I just can not think of any better solution.
In other words, I want to cancel the Action 2 call from thread 1. Is this in ANY way possible?
I should mention that I cannot edit the operation in Thread 2 ie, I cannot edit that library that is managing that thread. I can modify python's GIL lock to maybe be a try lock or a timed lock or maybe even a wait on a condition but that would be really hard to pull off.
I suppose the best solution would be to modify python to wait on the gil and a named condition that my Thread 1 can signal when I want to cancel its wait on the GIL. But before going down that route I want to know if I am missing anything.