thread2-r0drv-freebsd.c revision d272a8d07d3f79b66760a49b8be4eae380ecd519
4714N/A/* $Id$ */
4714N/A/** @file
4714N/A * IPRT - Threads (Part 2), Ring-0 Driver, FreeBSD.
4714N/A */
4714N/A
4714N/A/*
4714N/A * Copyright (c) 2007 knut st. osmundsen <bird-src-spam@anduin.net>
4714N/A *
4714N/A * Permission is hereby granted, free of charge, to any person
4714N/A * obtaining a copy of this software and associated documentation
4714N/A * files (the "Software"), to deal in the Software without
4714N/A * restriction, including without limitation the rights to use,
4714N/A * copy, modify, merge, publish, distribute, sublicense, and/or sell
4714N/A * copies of the Software, and to permit persons to whom the
4714N/A * Software is furnished to do so, subject to the following
4714N/A * conditions:
4714N/A *
4714N/A * The above copyright notice and this permission notice shall be
4714N/A * included in all copies or substantial portions of the Software.
4714N/A *
4714N/A * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
4714N/A * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
4714N/A * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
4714N/A * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
4714N/A * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
4714N/A * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
4714N/A * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
4714N/A * OTHER DEALINGS IN THE SOFTWARE.
4714N/A */
4714N/A
4714N/A/*******************************************************************************
4714N/A* Header Files *
4776N/A*******************************************************************************/
4714N/A#include "the-freebsd-kernel.h"
4714N/A
4714N/A#include <iprt/thread.h>
4714N/A#include <iprt/err.h>
4714N/A#include <iprt/assert.h>
4714N/A
4714N/A#include "internal/thread.h"
4714N/A
4714N/A
4714N/Aint rtThreadNativeInit(void)
4714N/A{
4714N/A return VINF_SUCCESS;
4776N/A}
4714N/A
4714N/A
4714N/ARTDECL(RTTHREAD) RTThreadSelf(void)
4714N/A{
4714N/A return rtThreadGetByNative(RTThreadNativeSelf());
4714N/A}
4714N/A
4714N/A
4714N/Aint rtThreadNativeSetPriority(PRTTHREADINT pThread, RTTHREADTYPE enmType)
4714N/A{
4714N/A int iPriority;
4714N/A
4714N/A switch (enmType)
4714N/A {
4776N/A case RTTHREADTYPE_INFREQUENT_POLLER: iPriority = PZERO + 8; break;
4714N/A case RTTHREADTYPE_EMULATION: iPriority = PZERO + 4; break;
4776N/A case RTTHREADTYPE_DEFAULT: iPriority = PZERO; break;
4714N/A case RTTHREADTYPE_MSG_PUMP: iPriority = PZERO - 4; break;
4714N/A case RTTHREADTYPE_IO: iPriority = PRIBIO; break;
4714N/A case RTTHREADTYPE_TIMER: iPriority = PRI_MIN_KERN; break;
4714N/A default:
4714N/A AssertMsgFailed(("enmType=%d\n", enmType));
4714N/A return VERR_INVALID_PARAMETER;
4714N/A }
4714N/A
4714N/A#if __FreeBSD_version < 700000
4714N/A /* Do like they're doing in subr_ntoskrnl.c... */
4714N/A mtx_lock_spin(&sched_lock);
4714N/A#else
4714N/A thread_lock(curthread);
4714N/A#endif
4776N/A sched_prio(curthread, iPriority);
4776N/A#if __FreeBSD_version < 600000
4776N/A curthread->td_base_pri = iPriority;
4776N/A#endif
4776N/A#if __FreeBSD_version < 700000
4776N/A mtx_unlock_spin(&sched_lock);
4714N/A#else
4714N/A thread_unlock(curthread);
4776N/A#endif
4776N/A
4776N/A return VINF_SUCCESS;
4776N/A}
4714N/A
4776N/A
4776N/Aint rtThreadNativeAdopt(PRTTHREADINT pThread)
4776N/A{
4714N/A NOREF(pThread);
4714N/A /* There is nothing special that needs doing here, but the
4714N/A user really better know what he's cooking. */
4714N/A return VINF_SUCCESS;
4714N/A}
4714N/A
4714N/A
4714N/A/**
4714N/A * Native thread main function.
4714N/A *
4714N/A * @param pvThreadInt The thread structure.
4714N/A */
4714N/Astatic void rtThreadNativeMain(void *pvThreadInt)
4714N/A{
4714N/A const struct thread *Self = curthread;
4714N/A PRTTHREADINT pThreadInt = (PRTTHREADINT)pvThreadInt;
4714N/A int rc;
4714N/A
4714N/A rc = rtThreadMain(pThreadInt, (RTNATIVETHREAD)Self, &pThreadInt->szName[0]);
4714N/A
4714N/A#if __FreeBSD_version >= 800002
4714N/A kproc_exit(rc);
4776N/A#else
4714N/A kthread_exit(rc);
4714N/A#endif
4714N/A}
4714N/A
4714N/A
4714N/Aint rtThreadNativeCreate(PRTTHREADINT pThreadInt, PRTNATIVETHREAD pNativeThread)
4714N/A{
4714N/A int rc;
4714N/A struct proc *pProc;
4714N/A
4714N/A#if __FreeBSD_version >= 800002
4714N/A rc = kproc_create(rtThreadNativeMain, pThreadInt, &pProc, RFHIGHPID, 0, "%s", pThreadInt->szName);
4714N/A#else
4714N/A rc = kthread_create(rtThreadNativeMain, pThreadInt, &pProc, RFHIGHPID, 0, "%s", pThreadInt->szName);
4776N/A#endif
4776N/A if (!rc)
4714N/A {
4714N/A *pNativeThread = (RTNATIVETHREAD)FIRST_THREAD_IN_PROC(pProc);
4714N/A rc = VINF_SUCCESS;
4714N/A }
4714N/A else
4714N/A rc = RTErrConvertFromErrno(rc);
4714N/A return rc;
4714N/A}
4714N/A
4714N/A