📄 runtime2_md.c
字号:
/*0001*//*
/*0002./ * Copyright (c) 1998-2001 Sun Microsystems, Inc. All Rights Reserved.
/*0003./ *
/*0004./ * This software is the confidential and proprietary information of Sun
/*0005./ * Microsystems, Inc. ("Confidential Information"). You shall not
/*0006./ * disclose such Confidential Information and shall use it only in
/*0007./ * accordance with the terms of the license agreement you entered into
/*0008./ * with Sun.
/*0009./ *
/*0010./ * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
/*0011./ * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
/*0012./ * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
/*0013./ * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
/*0014./ * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
/*0015./ * THIS SOFTWARE OR ITS DERIVATIVES.
/*0016./ *
/*0017./ */
/*0018*/
/*0019*//*=========================================================================
/*0020./ * KVM
/*0021./ *=========================================================================
/*0022./ * SYSTEM: KVM
/*0023./ * SUBSYSTEM: Machine-specific implementations needed by virtual machine
/*0024./ * FILE: runtime_md2.c
/*0025./ * AUTHOR: Nik Shaylor
/*0026./ *
/*0027./ * NOTE: These functions have been split out from runtime_md.c
/*0028./ * because of conflicts between windows.h and global.h
/*0029./ *=======================================================================*/
/*0030*/
/*0031*//*=========================================================================
/*0032./ * Include files
/*0033./ *=======================================================================*/
/*0034*/
/*0035*/#include <machine_md.h>
/*0036*/#include <main.h>
/*0037*/
/*0038*/#include <stdlib.h>
/*0039*/#include <string.h>
/*0040*/#include <time.h>
/*0041*/
/*0042*/#define WIN32_LEAN_AND_MEAN
/*0043*/#define NOMSG
/*0044*/#include <windows.h>
/*0045*/#include <process.h>
/*0046*/#ifdef GCC
/*0047*/#include <winsock.h>
/*0048*/#else
/*0049*/#include <winsock2.h>
/*0050*/#endif
/*0051*/
/*0052*/#define MAXCALENDARFLDS 15
/*0053*/
/*0054*/#define YEAR 1
/*0055*/#define MONTH 2
/*0056*/#define DAY_OF_MONTH 5
/*0057*/#define HOUR 10
/*0058*/#define MINUTE 12
/*0059*/#define SECOND 13
/*0060*/#define MILLISECOND 14
/*0061*/
/*0062*/static unsigned long date[MAXCALENDARFLDS];
/*0063*/
/*0064*//*=========================================================================
/*0065./ * Functions
/*0066./ *=======================================================================*/
/*0067*/
/*0068*//*=========================================================================
/*0069./ * FUNCTION: Calendar_md()
/*0070./ * TYPE: machine-specific implementation of native function
/*0071./ * OVERVIEW: Initializes the calendar fields, which represent the
/*0072./ * Calendar related attributes of a date.
/*0073./ * INTERFACE:
/*0074./ * parameters: none
/*0075./ * returns: none
/*0076./ * AUTHOR: Tasneem Sayeed
/*0077./ *=======================================================================*/
/*0078*/
/*0079*/unsigned long *
/*0080*/Calendar_md(void)
/*0081*/{
/*0082*/ SYSTEMTIME st;
/*0083*/
/*0084*/ /* initialize */
/*0085*/ memset(&st, 0, sizeof(st));
/*0086*/ memset(&date, 0, sizeof(date));
/*0087*/
/*0088*/ GetSystemTime(&st);
/*0089*/
/*0090*/ // initialize calendar fields
/*0091*/ date[YEAR] = st.wYear;
/*0092*/ date[MONTH] = st.wMonth;
/*0093*/ date[DAY_OF_MONTH] = st.wDay;
/*0094*/ date[HOUR] = st.wHour;
/*0095*/ date[MINUTE] = st.wMinute;
/*0096*/ date[SECOND] = st.wSecond;
/*0097*/ date[MILLISECOND] = st.wMilliseconds;
/*0098*/ return date;
/*0099*/}
/*0100*/
/*0101*//*=========================================================================
/*0102./ * FUNCTION: Yield_md()
/*0103./ * TYPE: machine-specific implementation of native function
/*0104./ * OVERVIEW: Yield the current thread
/*0105./ * INTERFACE:
/*0106./ * parameters: none
/*0107./ * returns: current time, in centiseconds since startup
/*0108./ *=======================================================================*/
/*0109*/
/*0110*/void Yield_md(void) {
/*0111*/ Sleep(0);
/*0112*/}
/*0113*/
/*0114*//*=========================================================================
/*0115./ * Critical section operations for asynchronous (non-blocking) methods
/*0116./ *=======================================================================*/
/*0117*/
/*0118*/#if ASYNCHRONOUS_NATIVE_FUNCTIONS
/*0119*/
/*0120*//*
/*0121./ * Windows mutex object
/*0122./ */
/*0123*/CRITICAL_SECTION csect;
/*0124*/
/*0125*//*=========================================================================
/*0126./ * FUNCTION: enterSystemCriticalSection()
/*0127./ * TYPE: machine-specific implementation of native function
/*0128./ * OVERVIEW: Wait on the system mutex
/*0129./ * INTERFACE:
/*0130./ * parameters: none
/*0131./ * returns: none
/*0132./ *=======================================================================*/
/*0133*/
/*0134*/void enterSystemCriticalSection(void) {
/*0135*/ EnterCriticalSection( &csect );
//\\ 硂琌system call
/*0136*/}
/*0137*/
/*0138*//*=========================================================================
/*0139./ * FUNCTION: exitSystemCriticalSection()
/*0140./ * TYPE: machine-specific implementation of native function
/*0141./ * OVERVIEW: Release the system mutex
/*0142./ * INTERFACE:
/*0143./ * parameters: none
/*0144./ * returns: none
/*0145./ *=======================================================================*/
/*0146*/
/*0147*/void exitSystemCriticalSection(void) {
/*0148*/ LeaveCriticalSection( &csect );
/*0149*/}
/*0150*/
/*0151*//*=========================================================================
/*0152./ * Async thread functions
/*0153./ *=======================================================================*/
/*0154*/
/*0155*/void fatalError(char *);
/*0156*/
/*0157*//*
/*0158./ * Handle for thread blocking
/*0159./ */
/*0160*/HANDLE EventHandle;
/*0161*/
/*0162*//*=========================================================================
/*0163./ * FUNCTION: asyncThread()
/*0164./ * TYPE: local function
/*0165./ * OVERVIEW: Main I/O thread processing loop
/*0166./ * INTERFACE:
/*0167./ * parameters: none
/*0168./ * returns: none
/*0169./ *=======================================================================*/
/*0170*/
/*0171*/static void asyncThread(void) {
/*0172*/ for(;;) {
/*0173*/ if (WaitForSingleObject(EventHandle, INFINITE) == WAIT_FAILED) {
/*0174*/ fatalError("Could not WaitForSingleObject");
/*0175*/ }
/*0176*/ processAcyncThread();
/*0177*/ }
/*0178*/}
/*0179*/
/*0180*//*=========================================================================
/*0181./ * FUNCTION: beginthreads()
/*0182./ * TYPE: local function
/*0183./ * OVERVIEW: Start 5 I/O thread
/*0184./ * INTERFACE:
/*0185./ * parameters: none
/*0186./ * returns: none
/*0187./ *=======================================================================*/
/*0188*/
/*0189*/static void beginthreads() {
/*0190*/ int i;
/*0191*/ EventHandle = CreateEvent(0, FALSE, FALSE, 0);
/*0192*/ for(i = 0 ; i < 5 ; i++) {
/*0193*/ _beginthread((void (*))asyncThread, 0, 0);
/*0194*/ }
/*0195*/}
/*0196*/
/*0197*//*=========================================================================
/*0198./ * FUNCTION: releaseAsyncThread()
/*0199./ * TYPE: machine-specific implementation of native function
/*0200./ * OVERVIEW: Release an I/O thread
/*0201./ * INTERFACE:
/*0202./ * parameters: none
/*0203./ * returns: none
/*0204./ *=======================================================================*/
/*0205*/
/*0206*/void releaseAsyncThread(void) {
/*0207*/ if(SetEvent(EventHandle) == FALSE)
/*0208*/ fatalError("Could not SetEvent");
/*0209*/}
/*0210*/
/*0211*/#endif /* ASYNCHRONOUS_NATIVE_FUNCTIONS */
/*0212*/
/*0213*//*=========================================================================
/*0214./ * FUNCTION: runtime2_md_init()
/*0215./ * TYPE: Initialization routine
/*0216./ * OVERVIEW: Function for initializing the definitions in this file.
/*0217./ * INTERFACE:
/*0218./ * parameters: none
/*0219./ * returns: none
/*0220./ *=======================================================================*/
/*0221*/
/*0222*//* We need to make sure runtime2_md_init is only called once when
/*0223./ * -jam -repeat option is supplied.
/*0224./ */
/*0225*/static int runtime2_md_inited = FALSE;
/*0226*/
/*0227*/void runtime2_md_init(void) {
/*0228*/ if (runtime2_md_inited) {
/*0229*/ return;
/*0230*/ }
/*0231*/#if ASYNCHRONOUS_NATIVE_FUNCTIONS
/*0232*/ InitializeCriticalSection(&csect);
/*0233*/ beginthreads();
/*0234*/#endif
/*0235*/ runtime2_md_inited = TRUE;
/*0236*/}
/*0237*/
/*0238*/#define FT2INT64(ft) \
/*0239*/ ((ulong64)(ft).dwHighDateTime << 32 | (ulong64)(ft).dwLowDateTime)
/*0240*/
/*0241*//*=========================================================================
/*0242./ * FUNCTION: sysTimeMillis()
/*0243./ * TYPE: time
/*0244./ * OVERVIEW: Get system time from Windows.
/*0245./ * INTERFACE:
/*0246./ * parameters: none
/*0247./ * returns: time in milliseconds
/*0248./ *=======================================================================*/
/*0249*/
/*0250*/ulong64
/*0251*/ sysTimeMillis(void) {
/*0252*/ static ulong64 fileTime_1_1_70 = 0;
/*0253*/ SYSTEMTIME st0;
/*0254*/ FILETIME ft0;
/*0255*/
/*0256*/ if (fileTime_1_1_70 == 0) {
/*0257*/ /*
/*0258./ * Initialize fileTime_1_1_70 -- the Win32 file time of midnight
/*0259./ * 1/1/70.
/*0260./ */
/*0261*/ memset(&st0, 0, sizeof(st0));
/*0262*/ st0.wYear = 1970;
/*0263*/ st0.wMonth = 1;
/*0264*/ st0.wDay = 1;
/*0265*/ SystemTimeToFileTime(&st0, &ft0);
/*0266*/ fileTime_1_1_70 = FT2INT64(ft0);
/*0267*/ }
/*0268*/
/*0269*/ GetSystemTime(&st0);
/*0270*/ SystemTimeToFileTime(&st0, &ft0);
/*0271*/
/*0272*/ /* file times are in 100ns increments, i.e. .0001ms */
/*0273*/ return (FT2INT64(ft0) - fileTime_1_1_70) / 10000;
/*0274*/}
/*0275*/
/*0276*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -