fix includes for freebsd
freebsd's qsort_r is different from linux
use pthread_self() for thread id on non-linux systems