diff --git a/.github/workflows/dragonflybsd.yml b/.github/workflows/dragonflybsd.yml new file mode 100644 index 000000000..00bdb1348 --- /dev/null +++ b/.github/workflows/dragonflybsd.yml @@ -0,0 +1,34 @@ +name: DragonflyBSD + +on: + pull_request: + push: + release: + types: [published] + +jobs: + build: + + runs-on: ubuntu-latest + + concurrency: + group: ${{ github.ref }}-${{ github.base_ref }}-${{ github.head_ref }}-DragonflyBSD + cancel-in-progress: true + steps: + - uses: actions/checkout@v4 + with: + submodules: true + + - name: Tests + id: test + uses: vmactions/dragonflybsd-vm@v1 + with: + usesh: true + prepare: | + pkg install -y socat git gmake bash + run: | + cd $GITHUB_WORKSPACE + ./configure + gmake -j4 + gmake run + diff --git a/.github/workflows/freebsd.yml b/.github/workflows/freebsd.yml new file mode 100644 index 000000000..373c803dc --- /dev/null +++ b/.github/workflows/freebsd.yml @@ -0,0 +1,33 @@ +name: FreeBSD + +on: + pull_request: + push: + release: + types: [published] + +jobs: + build: + + runs-on: ubuntu-latest + + concurrency: + group: ${{ github.ref }}-${{ github.base_ref }}-${{ github.head_ref }}-FreeBSD + cancel-in-progress: true + steps: + - uses: actions/checkout@v2 + with: + submodules: true + + - name: Tests + uses: vmactions/freebsd-vm@v1 + with: + usesh: true + mem: 4096 + copyback: false + prepare: pkg install -y git curl unzip gmake llvm gsed bash perl5 + run: | + ./configure + gmake -j4 + gmake run + diff --git a/.github/workflows/netbsd.yml b/.github/workflows/netbsd.yml new file mode 100644 index 000000000..332906e0b --- /dev/null +++ b/.github/workflows/netbsd.yml @@ -0,0 +1,34 @@ +name: NetBSD + +on: + pull_request: + push: + release: + types: [published] + +jobs: + build: + + runs-on: ubuntu-latest + + concurrency: + group: ${{ github.ref }}-${{ github.base_ref }}-${{ github.head_ref }}-NetBSD + cancel-in-progress: true + steps: + - uses: actions/checkout@v4 + with: + submodules: true + + - name: Tests + id: test + uses: vmactions/netbsd-vm@v1 + with: + usesh: true + prepare: | + /usr/sbin/pkg_add curl bash git gmake + run: | + cd $GITHUB_WORKSPACE + ./configure + gmake -j4 + gmake run + diff --git a/.github/workflows/openbsd.yml b/.github/workflows/openbsd.yml new file mode 100644 index 000000000..6b930384e --- /dev/null +++ b/.github/workflows/openbsd.yml @@ -0,0 +1,35 @@ +name: OpenBSD + +on: + pull_request: + push: + release: + types: [published] + +jobs: + build: + + runs-on: ubuntu-latest + + concurrency: + group: ${{ github.ref }}-${{ github.base_ref }}-${{ github.head_ref }}-OpenBSD + cancel-in-progress: true + steps: + - uses: actions/checkout@v4 + with: + submodules: true + + - name: Tests + id: test + uses: vmactions/openbsd-vm@v1 + with: + usesh: true + prepare: | + pkg_add curl git gmake bash gcc + ln -sf /usr/local/bin/gmake /usr/local/bin/make || true + run: | + cd $GITHUB_WORKSPACE + ./configure + gmake -j4 + gmake run + diff --git a/.github/workflows/solaris.yml b/.github/workflows/solaris.yml new file mode 100644 index 000000000..9e8ffcc8b --- /dev/null +++ b/.github/workflows/solaris.yml @@ -0,0 +1,34 @@ +name: Solaris + +on: + pull_request: + push: + release: + types: [published] + +jobs: + build: + + runs-on: ubuntu-latest + + concurrency: + group: ${{ github.ref }}-${{ github.base_ref }}-${{ github.head_ref }}-Solaris + cancel-in-progress: true + steps: + - uses: actions/checkout@v4 + with: + submodules: true + + - name: Tests + id: test + uses: vmactions/solaris-vm@v1 + with: + usesh: true + prepare: | + pkgutil -y -i socat git gmake bash gcc4g++ || pkgutil -y -i socat git gmake bash gcc5g++ || pkg install -y developer/gcc || true + run: | + cd $GITHUB_WORKSPACE + bash ./configure + gmake -j4 + gmake run + diff --git a/configure b/configure index 3c82790b6..dc174d9ea 100755 --- a/configure +++ b/configure @@ -687,7 +687,11 @@ case "${os_host}" in *darwin*) os_host="macosx" ;; *linux*) os_host="linux" ;; *freebsd*) os_host="freebsd" ;; + *netbsd*) os_host="netbsd" ;; + *openbsd*) os_host="openbsd" ;; + *dragonfly*) os_host="dragonflybsd" ;; *bsd*) os_host="bsd" ;; + *sunos*) os_host="solaris" ;; *haiku*) os_host="haiku" ;; esac @@ -708,8 +712,10 @@ is_host() { # detect host architecture os_arch=`uname -m | tr '[A-Z]' '[a-z]'` -if test_eq "${os_arch}" "i686"; then +if test_eq "${os_arch}" "i686" "i86pc"; then os_arch="i386" +elif test_eq "${os_arch}" "amd64"; then + os_arch="x86_64" elif test_eq "${os_arch}" "aarch64" || test_eq "${os_arch}" "arm64"; then os_arch="arm64" elif string_contains "${os_arch}" "armv7"; then @@ -724,7 +730,7 @@ fi _target_plat_default=${os_host} if is_host "msys"; then _target_plat_default="mingw" -elif is_host "freebsd"; then +elif is_host "freebsd" "openbsd" "dragonflybsd" "netbsd"; then _target_plat_default="bsd" elif test_nz "${EMSDK}"; then _target_plat_default="wasm" @@ -756,7 +762,7 @@ _target_mode_default="release" _target_kind_default="static" # set the default project generator and build program -if is_host "freebsd" "bsd"; then +if is_host "freebsd" "netbsd" "openbsd" "dragonflybsd" "bsd" "solaris"; then project_generator="gmake" _make_program_default="gmake" _ninja_program_default="ninja" diff --git a/src/tbox/platform/posix/sched_affinity.c b/src/tbox/platform/posix/sched_affinity.c index b2956030e..f97e7ae1b 100644 --- a/src/tbox/platform/posix/sched_affinity.c +++ b/src/tbox/platform/posix/sched_affinity.c @@ -27,6 +27,9 @@ #include #include #include +#ifdef __NetBSD__ +#include +#endif /* ////////////////////////////////////////////////////////////////////////////////////// * implementation @@ -36,7 +39,23 @@ tb_bool_t tb_sched_setaffinity(tb_size_t pid, tb_cpuset_ref_t cpuset) // check tb_assert_and_check_return_val(cpuset, tb_false); - // set cpu affinity +#ifdef __NetBSD__ + // NetBSD uses cpuset_t API for process affinity + cpuset_t *cpu_set = cpuset_create(); + if (!cpu_set) + return tb_false; + + tb_int_t i; + for (i = 0; i < TB_CPUSET_SIZE; i++) + { + if (TB_CPUSET_ISSET(i, cpuset)) + cpuset_set(i, cpu_set); + } + tb_bool_t ok = cpuset_setaffinity(CPU_LEVEL_WHICH, CPU_WHICH_PID, (pid_t)pid, cpuset_size(cpu_set), cpu_set) == 0; + cpuset_destroy(cpu_set); + return ok; +#else + // Linux uses cpu_set_t API tb_int_t i; cpu_set_t cpu_set; CPU_ZERO(&cpu_set); @@ -46,13 +65,37 @@ tb_bool_t tb_sched_setaffinity(tb_size_t pid, tb_cpuset_ref_t cpuset) CPU_SET(i, &cpu_set); } return sched_setaffinity((pid_t)pid, sizeof(cpu_set_t), &cpu_set) == 0; +#endif } tb_bool_t tb_sched_getaffinity(tb_size_t pid, tb_cpuset_ref_t cpuset) { // check tb_assert_and_check_return_val(cpuset, tb_false); - // get cpu affinity +#ifdef __NetBSD__ + // NetBSD uses cpuset_t API for process affinity + cpuset_t *cpu_set = cpuset_create(); + if (!cpu_set) + return tb_false; + + if (cpuset_getaffinity(CPU_LEVEL_WHICH, CPU_WHICH_PID, (pid_t)pid, cpuset_size(cpu_set), cpu_set) != 0) + { + cpuset_destroy(cpu_set); + return tb_false; + } + + // save cpuset + tb_int_t i; + TB_CPUSET_ZERO(cpuset); + for (i = 0; i < TB_CPUSET_SIZE; i++) + { + if (cpuset_isset(i, cpu_set)) + TB_CPUSET_SET(i, cpuset); + } + cpuset_destroy(cpu_set); + return tb_true; +#else + // Linux uses cpu_set_t API cpu_set_t cpu_set; CPU_ZERO(&cpu_set); if (sched_getaffinity((pid_t)pid, sizeof(cpu_set_t), &cpu_set) != 0) @@ -67,4 +110,5 @@ tb_bool_t tb_sched_getaffinity(tb_size_t pid, tb_cpuset_ref_t cpuset) TB_CPUSET_SET(i, cpuset); } return tb_true; +#endif } diff --git a/src/tbox/platform/posix/thread_affinity.c b/src/tbox/platform/posix/thread_affinity.c index 71f087813..a4ea31607 100644 --- a/src/tbox/platform/posix/thread_affinity.c +++ b/src/tbox/platform/posix/thread_affinity.c @@ -27,6 +27,9 @@ #include "../thread.h" #include #include +#ifdef __NetBSD__ +#include +#endif /* ////////////////////////////////////////////////////////////////////////////////////// * implementation @@ -39,7 +42,23 @@ tb_bool_t tb_thread_setaffinity(tb_thread_ref_t thread, tb_cpuset_ref_t cpuset) // get thread pthread_t pthread = thread? *((pthread_t*)thread) : pthread_self(); - // set cpu affinity +#ifdef __NetBSD__ + // NetBSD uses cpuset_t API + cpuset_t *cpu_set = cpuset_create(); + if (!cpu_set) + return tb_false; + + tb_int_t i; + for (i = 0; i < TB_CPUSET_SIZE; i++) + { + if (TB_CPUSET_ISSET(i, cpuset)) + cpuset_set(i, cpu_set); + } + tb_bool_t ok = pthread_setaffinity_np(pthread, cpuset_size(cpu_set), cpu_set) == 0; + cpuset_destroy(cpu_set); + return ok; +#else + // Linux uses cpu_set_t API tb_int_t i; cpu_set_t cpu_set; CPU_ZERO(&cpu_set); @@ -49,6 +68,7 @@ tb_bool_t tb_thread_setaffinity(tb_thread_ref_t thread, tb_cpuset_ref_t cpuset) CPU_SET(i, &cpu_set); } return pthread_setaffinity_np(pthread, sizeof(cpu_set_t), &cpu_set) == 0; +#endif } tb_bool_t tb_thread_getaffinity(tb_thread_ref_t thread, tb_cpuset_ref_t cpuset) { @@ -58,7 +78,30 @@ tb_bool_t tb_thread_getaffinity(tb_thread_ref_t thread, tb_cpuset_ref_t cpuset) // get thread pthread_t pthread = thread? *((pthread_t*)thread) : pthread_self(); - // get cpu affinity +#ifdef __NetBSD__ + // NetBSD uses cpuset_t API + cpuset_t *cpu_set = cpuset_create(); + if (!cpu_set) + return tb_false; + + if (pthread_getaffinity_np(pthread, cpuset_size(cpu_set), cpu_set) != 0) + { + cpuset_destroy(cpu_set); + return tb_false; + } + + // save cpuset + tb_int_t i; + TB_CPUSET_ZERO(cpuset); + for (i = 0; i < TB_CPUSET_SIZE; i++) + { + if (cpuset_isset(i, cpu_set)) + TB_CPUSET_SET(i, cpuset); + } + cpuset_destroy(cpu_set); + return tb_true; +#else + // Linux uses cpu_set_t API cpu_set_t cpu_set; CPU_ZERO(&cpu_set); if (pthread_getaffinity_np(pthread, sizeof(cpu_set_t), &cpu_set) != 0) @@ -73,4 +116,5 @@ tb_bool_t tb_thread_getaffinity(tb_thread_ref_t thread, tb_cpuset_ref_t cpuset) TB_CPUSET_SET(i, cpuset); } return tb_true; +#endif } diff --git a/src/xmake.sh b/src/xmake.sh index 63b61a18a..4952bab35 100755 --- a/src/xmake.sh +++ b/src/xmake.sh @@ -37,6 +37,8 @@ if is_plat "mingw" "msys" "cygwin"; then add_syslinks "ws2_32" "pthread" "m" elif is_plat "haiku"; then add_syslinks "pthread" "network" "m" "c" +elif is_plat "bsd" "solaris"; then + add_syslinks "pthread" "m" else add_syslinks "pthread" "dl" "m" "c" fi