Hunting for open sip trunk (outbound)

Looking for working methods to hunt to open SIP trunk for outbound calls; there will be a number of trunks that each only support one call and many more extensions than trunks so there can be race conditions during call setup. A similar question was asked on the asterisk-users list three years ago but no final solution was posted. I would like this to work in 1.2.40 and up.

Quoted text from Aug. 2007 post:

Nicholas Blasgen wrote:

I’ve got 4 SIP phone lines with a call-limit of 2 for each. I’ve
written a handy macro to allow my users to dial a phone number and the
macro will figure out the next available line to use by first checking
if the GROUP() is over 2 and then checking to see if ChanIsAvail() as a
backup, and if it can’t use the line for either reason it goes to the
next line. The problem is that there are enough situations that the
Macro gets called twice without much time seperation. Both macros check
the group() number, it comes back as free, they check the line
availability and it’s open, and they try dialing. But because they both
started at more or less the same instant, they’ve both at the same stage
in the macro and sometimes (maybe 10% of the time) a macro will try
dialing on a line that’s already in use.

My question is this. Is it possible to tell Asterisk to execute part of
a macro as a block without allowing any other commands to be processed
during that time? Some way to LOCK the dialplan (as you’d do in SQL).
I want my macro to be able to execute the part of the code that checks
line status and then sets the GROUP() without allowing any other
dialplans from running during that time. Anyone know if this is a
current feature?

All replies appreciated,