A peek in the library: locking

Published June 22, 2004
Advertisement
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.
Previous Entry No Subject Here
Next Entry Testing
0 likes 0 comments

Comments

Nobody has left a comment. You can be the first!
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Profile
Author
Advertisement

Latest Entries

New bug to me

1696 views

Week/Class 9

1527 views

Week/Class 8

1568 views

Week/Class 7

1617 views

The promised files

1848 views

Week/Class 6

1314 views

Week/Session 5

1369 views

Week/Session 4

1311 views

On the soapbox

1410 views

Week/Session 3

1287 views
Advertisement