Use pthread keys for thread local storage
This interface is slower but also more portable, and more importantly
it provides a way to specify destructor that is called when a thread
is canceled so we stop leaking memory.
Signed-off-by: Simo Sorce <firstname.lastname@example.org>
Reviewed-by: Robbie Harwood <email@example.com>