unix, windows: add thread-local storage API

Uses the pthread_key_{create,delete} and pthread_{get,set}specific
functions on UNIX platforms, TlsAlloc/TlsFree/TlsGetValue/TlsSetValue
on Windows.

Fixes #904.

Suggested reviewer: @piscisaureus