Or perhaps more accurately, TicketLocker?
I had been hoping to use malebogia for accessing the assorted globals in the code I'm working on, but this is a showstopeer for me in that regard. Or am I missing something (again)?
import malebolgia/lockers
type AObj = object
name: string
type ARef = ref AObj
proc `=destroy`(x: AObj) =
echo "Destroying AObj with name ", x.name
`=destroy`(x.name)
proc newARef(name: string): ARef =
result.new()
result.name = name
var globalA = initLocker(newARef("A#1"))
proc p2() =
echo "Entered proc p2"
lock globalA as a:
echo "Accessing globalA in p2 with name ", a.name
echo "Now out of lock scope in p2"
proc p1() =
echo "Entered proc p1"
lock globalA as a:
echo "Accessing globalA in p1 with name ", a.name
echo "Calling p2() in the lock scope"
p2()
echo "... Returned from p2() in the lock scope"
echo "Now out of lock scope in p1"
echo "Calling p2() from top level"
p2() # This works okay
echo "... Returned from p2() in top level"
echo "Calling p1() from top level"
p1() # This hangs at the lock statement in p2
echo "... Returned from p1() in top level"
echo "... Done"
I don't believe in re-entrant locks, they mostly hide sloppy thinking.
I can't disagree :P.
In your example, distinguish between p2 and lockedP2 that calls p2 and you'll be fine.
That works in the toy example above, but unfortunately not so much in the code base I'm dealing with. The situation I encountered has a lock being acquired in widely separated code locations, but in the same execution path. And I don't know where else a similar situation might occur.
The code base currently executes almost entirely in the main thread, but uses threadvars as a hack to avoid gc-safety complaints when accessing what are actually globals. My focus at the moment is to replace the threadvars by properly protected globals, on the way to making the code base thread safe and ultimately multi-threaded.
I had hoped that I could use malebogia for that purpose right off the bat, but I guess I'll use rlocks for now.
I'll return to malebogia later, when I'm ready to make serious changes to the design.