46  The Faneuil Hall problem

This problem was written by Grant Hutchins, who was inspired by a friend who took her Oath of Citizenship at Faneuil Hall in Boston.

“There are three kinds of threads: immigrants, spectators, and a one judge. Immigrants must wait in line, check in, and then sit down. At some point, the judge enters the building. When the judge is in the building, no one may enter, and the immigrants may not leave. Spectators may leave. Once all immigrants check in, the judge can confirm the naturalization. After the confirmation, the immigrants pick up their certificates of U.S. Citizenship. The judge leaves at some point after the confirmation. Spectators may now enter as before. After immigrants get their certificates, they may leave.”

To make these requirements more specific, let’s give the threads some functions to execute, and put constraints on those functions.

46.1 Faneuil Hall Problem Hint

noJudge = Semaphore(1)
entered = 0
checked = 0
mutex = Semaphore(1)
confirmed = Semaphore(0)

noJudge acts as a turnstile for incoming immigrants and spectators; it also protects entered, which counts the number of immigrants in the room. checked counts the number of immigrants who have checked in; it is protected by mutex.

confirmed signals that the judge has executed confirm.

46.2 Faneuil Hall problem solution

Here is the code for immigrants:

noJudge.wait()
enter()
entered++
noJudge.signal()

mutex.wait()
checkIn()
checked++

if judge == 1 and entered == checked:
    allSignedIn.signal()              # and pass the mutex
else:
    mutex.signal()

sitDown()
confirmed.wait()

swear()
getCertificate()

noJudge.wait()
leave()
noJudge.signal()

Immigrants pass through a turnstile when they enter; while the judge is in the room, the turnstile is locked.

After entering, immigrants have to get mutex to check in and update checked. If there is a judge waiting, the last immigrant to check in signals allSignedIn and passes the mutex to the judge.

Here is the code for the judge:

noJudge.wait()
mutex.wait()

enter()
judge = 1

if entered > checked:
    mutex.signal()
    allSignedIn.wait()               # and get the mutex back.

confirm()
confirmed.signal(checked)
entered = checked = 0

leave()
judge = 0

mutex.signal()
noJudge.signal()

The judge holds noJudge to bar immigrants and spectators from entering, and mutex so he can access entered and checked.

If the judge arrives at an instant when everyone who has entered has also checked in, she can proceed immediately. Otherwise, she has to give up the mutex and wait. When the last immigrant checks in and signals allSignedIn, it is understood that the judge will get the mutex back.

After invoking confirm, the judge signals confirmed once for every immigrant who has checked in, and then resets the counters (an example of “I’ll do it for you”). Then the judge leaves and releases mutex and noJudge.

After the judge signals confirmed, immigrants invoke swear and getCertificate concurrently, and then wait for the noJudge turnstile to open before leaving.

The code for spectators is easy; the only constraint they have to obey is the noJudge turnstile.

noJudge.wait()
enter()
noJudge.signal()

spectate()

leave()

Note: in this solution it is possible for immigrants to get stuck, after they get their certificate, by another judge coming to swear in the next batch of immigrants. If that happens, they might have to wait through another swearing in-ceremony.

Puzzle: modify this solution to handle the additional constraint that after the judge leaves, all immigrants who have been sworn in must leave before the judge can enter again.

46.3 Extended Faneuil Hall Problem Hint

My solution uses the following additional variables:

exit = Semaphore(0)
allGone = Semaphore(0)

Since the extended problem involves an additional rendezvous, we can solve it with two semaphores.

One other hint: I found it useful to use the “pass the baton” pattern again.

46.4 Extended Faneuil Hall problem solution

The top half of this solution is the same as before. The difference starts at Line 21. Immigrants wait here for the judge to leave.

noJudge.wait()
enter()
entered++
noJudge.signal()

mutex.wait()
checkIn()
checked++

if judge = 1 and entered == checked:
    allSignedIn.signal()              # and pass the mutex
else:
    mutex.signal()

sitDown()
confirmed.wait()

swear()
getCertificate()

exit.wait()                          # and get the mutex
leave()
checked--
if checked == 0:
    allGone.signal()                 # and pass the mutex
else:
    exit.signal()                    # and pass the mutex

For the judge, the difference starts at Line 18. When the judge is ready to leave, she can’t release noJudge, because that would allow more immigrants, and possibly another judge, to enter. Instead, she signals exit, which allows one immigrant to leave, and passes mutex.

The immigrant that gets the signal decrements checked and then passes the baton to the next immigrant. The last immigrant to leave signals allGone and passes the mutex back to the judge. This pass-back is not strictly necessary, but it has the nice feature that the judge releases both mutex and noJudge to end the phase cleanly.

noJudge.wait()
mutex.wait()

enter()
judge = 1

if entered > checked:
    mutex.signal()
    allSignedIn.wait()               # and get the mutex back.

confirm()
confirmed.signal(checked)
entered = 0

leave()
judge = 0

exit.signal()                       # and pass the mutex
allGone.wait()                      # and get it back
mutex.signal()
noJudge.signal()

The spectator code for the extended problem is unchanged.