Return error instead of using errno for threading

errno is problematic for Amiga libraries and is also not thread-safe on
some systems, even though it should.