threading: use atomic increment when building semaphore object

Required moving atomic defines to the top of threading.h, removing the need
for potentially redundant includes of unistd.h