Jump to content
  • Advertisement
Sign in to follow this  
  • entries
    195
  • comments
    198
  • views
    104180

A peek in the library: locking

Sign in to follow this  
SiCrane

85 views

In one of the earlier versions of my code library, I just used loki's ClassLevelLockable<> class for some of my locking needs. Unfortunately, Loki uses static objects to initialize the mutexes form ClassLevelLockable<> which has order of intialization issues that I ran into fairly fast. So that had to go. In order to control the initialization issue, I added code to delay mutex creation until first request, in a classic singleton creation method. Of course, in order to do so in a multithreaded environment, you need locking to guarantee that two objects aren't created. Generally you'd use a per class lock to do so, which is what I was trying to implement in the first place. Therefore I introduced a master lock.

The master lock is another singleton type class, albeit one with special privledges in my library. In a multi-threaded build, every translation unit that includes a header that uses my code library will attempt to initialize the master lock as one of the first things it does. This is done by the same mechanism that std::cout and friends get intitialized, an object is created in every translation unit that checks to see if the master lock is initialized, if it's not, then it intializes it, and then the rest of the objects in that translation unit may be initialized. This relies on the fact that objects within a translation unit have deterministic initialization order.

The code for it is pretty simple. Every header includes a file that has this code in it:

class MasterLockInitializer {
public:
MasterLockInitializer();
~MasterLockInitializer();
private:
MasterLockInitializer(const MasterLockInitializer &);
MasterLockInitializer & operator=(const MasterLockInitializer &);
};
namespace {
// every translation unit gets a master lock initializer
MasterLockInitializer mli;
}

Then in one of the source files there's the moral equivalent of:

namespace {
int load_count = 0;
Mutex * the_master_mutex = 0;
}

Mutex & get_master_mutex() {
return *the_master_mutex;
}

MasterLockInitializer::MasterLockInitializer() {
if (load_count == 0) {
the_master_mutex = new Mutex();
}
load_count++;
}

MasterLockInitializer::~MasterLockInitializer() {
load_count--;
if (load_count == 0) {
delete the_master_mutex;
the_master_mutex = 0;
}
}

On some days I think this is overkill. On other days it seems overly fragile. Right now it works, and that's what's important.
Sign in to follow this  


0 Comments


Recommended Comments

There are no comments to display.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!