/[pcsx2_0.9.7]/trunk/3rdparty/w32pthreads/ptw32_relmillisecs.c
ViewVC logotype

Annotation of /trunk/3rdparty/w32pthreads/ptw32_relmillisecs.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 10 - (hide annotations) (download)
Mon Sep 6 11:40:06 2010 UTC (9 years, 10 months ago) by william
File MIME type: text/plain
File size: 3653 byte(s)
exported r3113 from ./upstream/trunk
1 william 10 /*
2     * ptw32_relmillisecs.c
3     *
4     * Description:
5     * This translation unit implements miscellaneous thread functions.
6     *
7     * --------------------------------------------------------------------------
8     *
9     * Pthreads-win32 - POSIX Threads Library for Win32
10     * Copyright(C) 1998 John E. Bossom
11     * Copyright(C) 1999,2005 Pthreads-win32 contributors
12     *
13     * Contact Email: rpj@callisto.canberra.edu.au
14     *
15     * The current list of contributors is contained
16     * in the file CONTRIBUTORS included with the source
17     * code distribution. The list can also be seen at the
18     * following World Wide Web location:
19     * http://sources.redhat.com/pthreads-win32/contributors.html
20     *
21     * This library is free software; you can redistribute it and/or
22     * modify it under the terms of the GNU Lesser General Public
23     * License as published by the Free Software Foundation; either
24     * version 2 of the License, or (at your option) any later version.
25     *
26     * This library is distributed in the hope that it will be useful,
27     * but WITHOUT ANY WARRANTY; without even the implied warranty of
28     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
29     * Lesser General Public License for more details.
30     *
31     * You should have received a copy of the GNU Lesser General Public
32     * License along with this library in the file COPYING.LIB;
33     * if not, write to the Free Software Foundation, Inc.,
34     * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
35     */
36    
37     #include "ptw32pch.h"
38     #ifndef NEED_FTIME
39     #include <sys/timeb.h>
40     #endif
41    
42    
43     INLINE DWORD
44     ptw32_relmillisecs (const struct timespec * abstime)
45     {
46     const int64_t NANOSEC_PER_MILLISEC = 1000000;
47     const int64_t MILLISEC_PER_SEC = 1000;
48     DWORD milliseconds;
49     int64_t tmpAbsMilliseconds;
50     int64_t tmpCurrMilliseconds;
51     #ifdef NEED_FTIME
52     struct timespec currSysTime;
53     FILETIME ft;
54     SYSTEMTIME st;
55     #else /* ! NEED_FTIME */
56     struct _timeb currSysTime;
57     #endif /* NEED_FTIME */
58    
59    
60     /*
61     * Calculate timeout as milliseconds from current system time.
62     */
63    
64     /*
65     * subtract current system time from abstime in a way that checks
66     * that abstime is never in the past, or is never equivalent to the
67     * defined INFINITE value (0xFFFFFFFF).
68     *
69     * Assume all integers are unsigned, i.e. cannot test if less than 0.
70     */
71     tmpAbsMilliseconds = (int64_t)abstime->tv_sec * MILLISEC_PER_SEC;
72     tmpAbsMilliseconds += ((int64_t)abstime->tv_nsec + (NANOSEC_PER_MILLISEC/2)) / NANOSEC_PER_MILLISEC;
73    
74     /* get current system time */
75    
76     #ifdef NEED_FTIME
77    
78     GetSystemTime(&st);
79     SystemTimeToFileTime(&st, &ft);
80     /*
81     * GetSystemTimeAsFileTime(&ft); would be faster,
82     * but it does not exist on WinCE
83     */
84    
85     ptw32_filetime_to_timespec(&ft, &currSysTime);
86    
87     tmpCurrMilliseconds = (int64_t)currSysTime.tv_sec * MILLISEC_PER_SEC;
88     tmpCurrMilliseconds += ((int64_t)currSysTime.tv_nsec + (NANOSEC_PER_MILLISEC/2))
89     / NANOSEC_PER_MILLISEC;
90    
91     #else /* ! NEED_FTIME */
92    
93     _ftime64_s(&currSysTime);
94    
95     tmpCurrMilliseconds = (int64_t) currSysTime.time * MILLISEC_PER_SEC;
96     tmpCurrMilliseconds += (int64_t) currSysTime.millitm;
97    
98     #endif /* NEED_FTIME */
99    
100     if (tmpAbsMilliseconds > tmpCurrMilliseconds)
101     {
102     milliseconds = (DWORD) (tmpAbsMilliseconds - tmpCurrMilliseconds);
103     if (milliseconds == INFINITE)
104     {
105     /* Timeouts must be finite */
106     milliseconds--;
107     }
108     }
109     else
110     {
111     /* The abstime given is in the past */
112     milliseconds = 0;
113     }
114    
115     return milliseconds;
116     }

  ViewVC Help
Powered by ViewVC 1.1.22