In case any of my colleagues haven’t heard, I no longer work at Mozilla. I apologize for not sending out a company-wide farewell email. I wasn’t expecting my email access to be cut off so promptly.

Hence, this is no longer my work blog, but I still hope to contribute to the Mozilla Project (I am working on some patches as we speak), and I will continue to blog here about tech-related topics (and especially Firefox-related topics) on the occasion that I have something to say.

RAII helper macro

One of the most important idioms in C++ is “Resource Acquisition Is Initialization” (RAII), where the constructor of a class acquires a resource (like locking a mutex, or opening a file) and the corresponding destructor releases the resource.

Such classes are almost always used as local variables. The lifetime of the variable lasts from its point of declaration to the end of its innermost containing block.

A classic example is something like:

extern int n;
extern Mutex m;

void f() {
    Lock l(&m); // This is an RAII class.

The problem is, code written with these classes can easily be misunderstood.

1) Sometimes the variable is used solely for the side-effects of its constructor and destructor. To a naive coder, this can look like an unused variable, and they might be tempted to remove it.

2) In order to control where the object is destroyed, the coder sometimes needs to add another pair of braces (curly brackets) to create a new block scope. Something like:

void f() {

        Lock l(&m);


The problem is, it’s not always obvious why that extra block scope is there. (For trivial code, like the above, it’s obvious. But in real code, ‘l’ might not be the only variable defined in the block.) To a naive coder, it might even look like an unnecessary scope, and they might be tempted to remove it.

The usual solutions to this situation are: (a) Write a comment, or (b) trust people to understand what you meant. Those are both bad options.

That’s why I’m fond of the following MACRO.

#define with(decl) \
for (bool __f = true; __f; ) \
for (decl; __f; __f = false)

This allows you to write:

void f() {

    with (Lock l(&m)) {


This creates a clear association between the RAII object and the statements which depend on its existence. The code is less likely to be misunderstood.

I like to call this a with-statement (analogous to an if-statement). Any kind of variable declaration can go in the head of the with-statement (as long as it can appear in the head of a for-statement). The body of the with-statement executes once. The variable declared in the head of the statement is destroyed after the body executes.

Your code editor might even highlight ‘with‘ as a keyword, since it’s a keyword in Javascript (where it has a different — and deprecated — purpose).

I didn’t invent this kind of MACRO. (I think I first read about something similar in a Dr Dobb’s article.) I just find it really useful, and I hope you do too.

The Canadian, Day 5

Today I woke up on the outskirts of Greater Vancouver, judging by the signs in this industrial area. The steward has just announced we’ll be arriving in Vancouver in one hour.

Thus concludes my journey. Though the journey spanned five calendar days, I left late on Thursday night and I’m arriving early Monday morning, so it’s more like four nights and three days. (A total travelling time of 3 days and 14.5 hours.) Because I travelled over a weekend, I only lost one day of office time and gained a scenic weekend.

The trip roughly breaks down to one day in the forests of Ontario, one day across the plains of Manitoba and Saskatchewan, and one day across the mountains of Alberta and British Columbia.

In retrospect, I should have done my work on the second day, when my mobile internet connection was good and the scenery was less interesting. (Sorry, Manitoba. Sorry, Saskatchewan.)

From here I take a bus to Whistler to attend what Mozilla calls a “work week” — a collection of presentations, team meetings, and planning sessions. It ends with a party on Friday night, the theme of which is “lumberjack”. (Between the bears and the hipsters, don’t I already get enough of people dressing like lumberjacks back in Toronto?)

Because I’m a giant hypocrite, I’ll be flying back to Toronto. But I heartily recommend travel by Via Rail. Their rail passes (good for visiting multiple destinations, or doing a round trip) are an especially good deal.

I wonder what Kylie Minogue has to say about rail travel.

The Canadian, Day 4

The great thing about travelling by train is that there is almost zero environmental impact.

Before you say “but what about those giant diesel engines burning diesel”, let me explain that I’m talking about marginal impact. That is, this train was going to be running regardless of whether I was on it or not. By choosing to be on it, I’m not contributing any additional environmental impact.

That’s not true for a bus or plane. If 100 people suddenly decided to travel from Toronto to Vancouver by bus, the bus company would have to schedule another couple busses. If 100 extra people decided to travel by plane, the airline would have to schedule another plane. But if 100 people decided to all take the train, Via Rail can simply add another couple of cars to their existing scheduled train, with negligible environmental impact.

(Caveat: There are reports that Via Rail no longer does this, and Elizabeth May is not happy about it. See Point 1 in her letter.)

There are downsides too. It’s more expensive than travelling by plane, though still within my company’s travel policy. (Note that I’m travelling in Economy. Note also that if you’re a member of CAA or Hostelling International, you get 10% off Via Rail tickets.) It also takes more time, but it’s not unproductive time. You can get quite a lot done on the train.

The main downside is sleeping in a seat. I was fine for the first couple nights, but after that third night (last night), I’m starting to feel a bit rough. And I have one more night to go.

The cost of the Via Rail ticket includes one stopover in any city for as long as you want. If I had more time, I might have scheduled a stopover in Winnipeg or Jasper for a couple nights to recharge.

Update: Jasper
Apparently I slept past Saskatoon and Edmonton.

Jasper is beautiful, but you already knew that.

Update: Approaching Kamloops
I can’t help wishing I was seeing this scenery in winter instead of summer. Green mountains just look like big hills.

We’re back on tracks laid by the Canadian Northern Railway. It still boggles my mind that we’re following a path chosen by someone 100 years ago. I spend so much of my life surrounded by new technology, it’s strange to be using something created even just 3 or 4 generations ago.

The 1950s and 1960s saw the decline of passenger rail in Canada. It couldn’t compete with the rise of air travel and new highways like the Trans-Canada and the 401. The federal government created Via Rail in the 1970s to take over passenger operations from CN and CP, since those services were no longer profitable.

I’m not exactly sure why the government keeps Via Rail running, but I’m glad they do. Last time I travelled by air, security confiscated my penknife.

Musical Interlude IV
[This one would have made more sense last night. I might swap them in a later edit.]

The Canadian, Day 3

I expected to wake up in Winnipeg, but instead we’re stopped in the middle of nowhere. I can tell we’re in Manitoba, because the landscape has become flat and grassy instead of Ontario’s rocky forests and lakes, but I don’t see Winnipeg anywhere. Still no internet connection.

The train is moving again. We just passed a farm that seems to be raising cows and abandoned trucks.

Update: Winnipeg

Our train has a layover of about 3 hours in Winnipeg. The Via Rail station is located beside The Forks, a historic part of Winnipeg of significance to First Nations people. This site has been used as a meeting place for at least 6000 years. It’s especially active today because today is Aboriginal Day.

The site also hosts the Canadian Museum of Human Rights (which was still under construction last time I was here), a farmer’s market that is open today, and a couple historic rail cars. A pedestrian bridge (the Esplanade Riel) links it to Winnipeg’s French quarter, St Boniface, across the river. (Until 1971, St Boniface was its own predominantly-francophone city, a rarity in Western Canada.)

I didn’t spend much time looking about, however. I went straight to the Assiniboine Athletic Club, just two short blocks from the Via Rail station’s main entrance, to take a shower. This is the only opportunity to shower between Toronto and Vancouver. It’s $11 for a day pass to the gym. I would have had a workout too, but I didn’t pack any shorts.

Former rail car of the Temiskaming and Northern Ontario Railway

The rail car in the above picture used to belong to the Temiskaming and Northern Ontario Railway, later known as the Ontario Northland Railway. Owned by the Ontario government, it’s one of the few railways in Canada which isn’t privately owned. Unfortunately, the Ontario government shut down ONR’s passenger services last year. It now only provides freight service and occasional tourist service.

Update: Somewhere in Manitoba

Manitoba is more scenic than you’d think. We’re currently in a rather pleasant-looking valley.

We’re now on tracks that were built by Grand Trunk Pacific. These tracks go from Winnipeg to Prince Rupert in northern BC. Together, Grand Trunk Pacific (which operated west of Winnipeg) and National Transcontinental Railway (which operated east of Winnipeg) were the third and final transcontinental rail route across Canada.

Like most Canadian railways, they eventually went bankrupt and were nationalized by the Canadian government, becoming part of the government-owned Canadian National Railway. The only major railway to escape this fate was Canadian Pacific Railway, leading to today’s situation where CNR and CPR (usually now called CN and CP) dominate Canada’s rail industry as a duopoly. CN was privatized in 1995. Apparently the largest shareholder is now Bill Gates.

The GTP tracks are still well used. Prince Rupert is a popular port for shipments from China. Due to the shape of the Earth, Prince Rupert is closer to China than Vancouver is. It’s easier to see on a globe.

Musical Interlude III

The Canadian, Day 2

This is what I saw when I woke up.


(Taken with my Firefox OS phone.)

Update: Sudbury
So, it took us 8 hours to reach Sudbury. Sudbury is only about a 3 and half hour drive by car on the highway. You could call this a leisurely pace.

We are now on tracks that were originally owned by the Canadian Northern Railway (not to be confused with the Northern Railway of Canada, whose tracks we were on before). These tracks stretch across the country from Vancouver to Quebec City. Canadian Nothern Railway was the second railway to provide transcontinental service across Canada (the first being Canadian Pacific Railway, who still dominate Canada’s rail industry today).

Now these tracks are owned by CNR (Canadian National Railway).

Update: Hornepayne

If you look at a population map of Canada, you’ll see there’s a mostly unpopulated gap between Sault Ste Marie and Kenora, separating western Canada from eastern Canada. I’m currently in that gap.

I didn’t realize it would be so hard to get internet here. I haven’t managed to get an internet connection since leaving Sudbury, and I suspect I won’t be able to until we’re near Thunder Bay.

I’m supposed to be working today, but lack of internet is really hindering what I can do. I spent a while glued to my phone, hoping to glimpse a bar of service or two, but eventually gave up. It’s frustrating, because I’m trying to submit a patch for review, and I was hoping to get it approved before this weekend. I don’t like my chances of getting it reviewed next week when everyone’s busy at Whistler.

After a while, I went to the observation car and stared out the window, which I found relaxing. There seem to be countless beautiful lakes and rivers up here. We never seem to be far from a watercourse. I wonder if these rivers were a route used by fur traders in the early days of Nouvelle France.

The train stopped a couple times to drop off canoeists. I suspect we’re in a part of the country only accessible by rail.

I saw a beaver! You know, I’ve been in Canada all these years, and I think that’s the first time I’ve actually seen a beaver.

Most of the signs on this train are embossed with Braille. The signs use uncontracted Braille. I notice that the English is prefixed with dot-6 (⠠) while the French is prefixed with dot-46 (⠨). I wonder if that’s a standard convention.

Update: Sioux Lookout

We’re near Thunder Bay. For about 15 minutes we had cell phone and internet coverage, and everyone was staring to their phones.

We’re now on tracks that were part of the National Transcontinental Railway, built in 1885. This route spanned from Winnipeg to Moncton. The section from Winnipeg to Quebec City is almost a straight line stretching across northern Ontario and northern Quebec. The idea was that it would provide the quickest route from the Prairies to the Maritimes by bypassing cities like Toronto, Ottawa, Montreal. The railway was never successful.

Most of the railway is now abandoned and owned by no one. Parts of it are owned by CNR, and we’re currently on one of those parts.

Many francophone towns in Northern Ontario, like Kapuskasing and Hearst, lie along the abandoned portion of this railway. Those towns are now serviced by Highway 17.

But there are no highways along this section. There’s barely a road in sight.

Musical Interlude II