thread-r0drv-freebsd.c revision 445a156b9ab321242ed8ad2ee143626c05865acf
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync/* $Id$ */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync/** @file
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * IPRT - Threads (Part 1), Ring-0 Driver, FreeBSD.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync/*
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * Copyright (c) 2007 knut st. osmundsen <bird-src-spam@anduin.net>
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync *
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * Permission is hereby granted, free of charge, to any person
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * obtaining a copy of this software and associated documentation
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * files (the "Software"), to deal in the Software without
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * restriction, including without limitation the rights to use,
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * copy, modify, merge, publish, distribute, sublicense, and/or sell
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * copies of the Software, and to permit persons to whom the
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * Software is furnished to do so, subject to the following
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * conditions:
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync *
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * The above copyright notice and this permission notice shall be
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * included in all copies or substantial portions of the Software.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync *
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * OTHER DEALINGS IN THE SOFTWARE.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync/*******************************************************************************
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync* Header Files *
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync*******************************************************************************/
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#include "the-freebsd-kernel.h"
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#include <iprt/thread.h>
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#include <iprt/err.h>
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#include <iprt/assert.h>
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#include "internal/thread.h"
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsyncRTDECL(RTNATIVETHREAD) RTThreadNativeSelf(void)
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync{
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync return (RTNATIVETHREAD)curthread;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync}
5a3672dca0301427a400aec8734acd70e2cf24a4vboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsyncRTDECL(int) RTThreadSleep(unsigned cMillies)
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync{
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync int rc;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync int cTicks;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync /*
5a3672dca0301427a400aec8734acd70e2cf24a4vboxsync * 0 ms sleep -> yield.
5a3672dca0301427a400aec8734acd70e2cf24a4vboxsync */
5a3672dca0301427a400aec8734acd70e2cf24a4vboxsync if (!cMillies)
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync RTThreadYield();
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync return VINF_SUCCESS;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync }
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync /*
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * Translate milliseconds into ticks and go to sleep.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if (cMillies != RT_INDEFINITE_WAIT)
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if (hz == 1000)
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync cTicks = cMillies;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync else if (hz == 100)
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync cTicks = cMillies / 10;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync else
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync int64_t cTicks64 = ((uint64_t)cMillies * hz) / 1000;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync cTicks = (int)cTicks64;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if (cTicks != cTicks64)
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync cTicks = INT_MAX;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync }
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync }
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync else
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync cTicks = 0; /* requires giant lock! */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync rc = tsleep((void *)RTThreadSleep,
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync PZERO | PCATCH,
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync "iprtsl", /* max 6 chars */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync cTicks);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync switch (rc)
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync case 0:
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync return VINF_SUCCESS;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync case EWOULDBLOCK:
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync return VERR_TIMEOUT;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync case EINTR:
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync case ERESTART:
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync return VERR_INTERRUPTED;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync default:
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync AssertMsgFailed(("%d\n", rc));
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync return VERR_NO_TRANSLATION;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync }
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync}
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsyncRTDECL(bool) RTThreadYield(void)
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync{
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync uio_yield();
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync return false; /** @todo figure this one ... */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync}
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsyncRTDECL(bool) RTThreadPreemptIsEnabled(RTTHREAD hThread)
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync{
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync Assert(hThread == NIL_RTTHREAD);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync return curthread->td_critnest == 0;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync}
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsyncRTDECL(bool) RTThreadPreemptIsPending(RTTHREAD hThread)
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync{
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync Assert(hThread == NIL_RTTHREAD);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync return curthread->td_owepreempt == 1;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync}
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsyncRTDECL(bool) RTThreadPreemptIsPendingTrusty(void)
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync{
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync /* yes, RTThreadPreemptIsPending is reliable. */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync return true;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync}
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsyncRTDECL(void) RTThreadPreemptDisable(PRTTHREADPREEMPTSTATE pState)
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync{
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync AssertPtr(pState);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync Assert(pState->uchDummy != 42);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync pState->uchDummy = 42;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync critical_enter();
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync}
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsyncRTDECL(void) RTThreadPreemptRestore(PRTTHREADPREEMPTSTATE pState)
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync{
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync AssertPtr(pState);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync Assert(pState->uchDummy == 42);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync pState->uchDummy = 0;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync critical_exit();
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync}
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync