| 1 | /* $Id: kHlpBareThread.c 24 2009-02-08 13:58:54Z bird $ */
|
|---|
| 2 | /** @file
|
|---|
| 3 | * kHlpBare - Thread Manipulation.
|
|---|
| 4 | */
|
|---|
| 5 |
|
|---|
| 6 | /*
|
|---|
| 7 | * Copyright (c) 2006-2007 knut st. osmundsen <bird-kStuff-spam@anduin.net>
|
|---|
| 8 | *
|
|---|
| 9 | * This file is part of kStuff.
|
|---|
| 10 | *
|
|---|
| 11 | * kStuff is free software; you can redistribute it and/or
|
|---|
| 12 | * modify it under the terms of the GNU Lesser General Public
|
|---|
| 13 | * License as published by the Free Software Foundation; either
|
|---|
| 14 | * version 2.1 of the License, or (at your option) any later version.
|
|---|
| 15 | *
|
|---|
| 16 | * In addition to the permissions in the GNU Lesser General Public
|
|---|
| 17 | * License, you are granted unlimited permission to link the compiled
|
|---|
| 18 | * version of this file into combinations with other programs, and to
|
|---|
| 19 | * distribute those combinations without any restriction coming from
|
|---|
| 20 | * the use of this file.
|
|---|
| 21 | *
|
|---|
| 22 | * kStuff is distributed in the hope that it will be useful,
|
|---|
| 23 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 24 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|---|
| 25 | * Lesser General Public License for more details.
|
|---|
| 26 | *
|
|---|
| 27 | * You should have received a copy of the GNU Lesser General Public
|
|---|
| 28 | * License along with kStuff; if not, write to the Free Software
|
|---|
| 29 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
|---|
| 30 | * 02110-1301, USA
|
|---|
| 31 | */
|
|---|
| 32 |
|
|---|
| 33 | /*******************************************************************************
|
|---|
| 34 | * Header Files *
|
|---|
| 35 | *******************************************************************************/
|
|---|
| 36 | #include <k/kHlpThread.h>
|
|---|
| 37 |
|
|---|
| 38 | #if K_OS == K_OS_DARWIN
|
|---|
| 39 | # include <mach/mach_time.h>
|
|---|
| 40 |
|
|---|
| 41 | #elif K_OS == K_OS_LINUX
|
|---|
| 42 | # include <k/kHlpSys.h>
|
|---|
| 43 |
|
|---|
| 44 | #elif K_OS == K_OS_OS2
|
|---|
| 45 | # define INCL_BASE
|
|---|
| 46 | # define INCL_ERRORS
|
|---|
| 47 | # include <os2.h>
|
|---|
| 48 | #elif K_OS == K_OS_WINDOWS
|
|---|
| 49 | # include <Windows.h>
|
|---|
| 50 | #else
|
|---|
| 51 | # error "port me"
|
|---|
| 52 | #endif
|
|---|
| 53 |
|
|---|
| 54 |
|
|---|
| 55 | /**
|
|---|
| 56 | * Sleep for a number of milliseconds.
|
|---|
| 57 | * @param cMillies Number of milliseconds to sleep.
|
|---|
| 58 | */
|
|---|
| 59 | void kHlpSleep(unsigned cMillies)
|
|---|
| 60 | {
|
|---|
| 61 | #if K_OS == K_OS_DARWIN
|
|---|
| 62 | static struct mach_timebase_info s_Info;
|
|---|
| 63 | static KBOOL s_fNanoseconds = K_UNKNOWN;
|
|---|
| 64 | KU64 uNow = mach_absolute_time();
|
|---|
| 65 | KU64 uDeadline;
|
|---|
| 66 | KU64 uPeriod;
|
|---|
| 67 |
|
|---|
| 68 | if (s_fNanoseconds == K_UNKNOWN)
|
|---|
| 69 | {
|
|---|
| 70 | if (mach_timebase_info(&s_Info))
|
|---|
| 71 | s_fNanoseconds = K_TRUE; /* the easy way out */
|
|---|
| 72 | else if (s_Info.denom == s_Info.numer)
|
|---|
| 73 | s_fNanoseconds = K_TRUE;
|
|---|
| 74 | else
|
|---|
| 75 | s_fNanoseconds = K_FALSE;
|
|---|
| 76 | }
|
|---|
| 77 |
|
|---|
| 78 | uPeriod = (KU64)cMillies * 1000 * 1000;
|
|---|
| 79 | if (!s_fNanoseconds)
|
|---|
| 80 | uPeriod = (double)uPeriod * s_Info.denom / s_Info.numer; /* Use double to avoid 32-bit trouble. */
|
|---|
| 81 | uDeadline = uNow + uPeriod;
|
|---|
| 82 | mach_wait_until(uDeadline);
|
|---|
| 83 |
|
|---|
| 84 | #elif K_OS == K_OS_LINUX
|
|---|
| 85 | /** @todo find the right syscall... */
|
|---|
| 86 |
|
|---|
| 87 | #elif K_OS == K_OS_OS2
|
|---|
| 88 | DosSleep(cMillies);
|
|---|
| 89 | #elif K_OS == K_OS_WINDOWS
|
|---|
| 90 | Sleep(cMillies);
|
|---|
| 91 | #else
|
|---|
| 92 | usleep(cMillies * 1000);
|
|---|
| 93 | #endif
|
|---|
| 94 | }
|
|---|
| 95 |
|
|---|