adaptation of patches from https://gist.github.com/zomo/946001

A few small changes to support Mac OS X out of the box. Zomo had an implementation of the macro I didn't, so I just started with that patch. From what I read, librt is automatically included with anything remotely modern.