Thread support

Added thread support and basic semaphore infrastructure. Added more
kernel support for threads and context switching.