Make the library thread-safe.

* src/channel-main.c (init_channel): Initialize threads.
* src/key-main.c (init_key): Likewise.
* src/session-main.c (init_session): Likewise.
* src/threads.c, src/threads.c: New files.