Add atomic operations to replace pthread locks, saves ~20ns for each lock.