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

Contents of /trunk/3rdparty/w32pthreads/pthread_delay_np.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 31 - (show annotations) (download)
Tue Sep 7 03:24:11 2010 UTC (9 years, 11 months ago) by william
File MIME type: text/plain
File size: 4827 byte(s)
committing r3113 initial commit again...
1 /*
2 * pthreads_delay_np.c
3 *
4 * Description:
5 * This translation unit implements non-portable 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
39 /*
40 * pthread_delay_np
41 *
42 * DESCRIPTION
43 *
44 * This routine causes a thread to delay execution for a specific period of time.
45 * This period ends at the current time plus the specified interval. The routine
46 * will not return before the end of the period is reached, but may return an
47 * arbitrary amount of time after the period has gone by. This can be due to
48 * system load, thread priorities, and system timer granularity.
49 *
50 * Specifying an interval of zero (0) seconds and zero (0) nanoseconds is
51 * allowed and can be used to force the thread to give up the processor or to
52 * deliver a pending cancelation request.
53 *
54 * The timespec structure contains the following two fields:
55 *
56 * tv_sec is an integer number of seconds.
57 * tv_nsec is an integer number of nanoseconds.
58 *
59 * Return Values
60 *
61 * If an error condition occurs, this routine returns an integer value indicating
62 * the type of error. Possible return values are as follows:
63 *
64 * 0
65 * Successful completion.
66 * [EINVAL]
67 * The value specified by interval is invalid.
68 *
69 * Example
70 *
71 * The following code segment would wait for 5 and 1/2 seconds
72 *
73 * struct timespec tsWait;
74 * int intRC;
75 *
76 * tsWait.tv_sec = 5;
77 * tsWait.tv_nsec = 500000000L;
78 * intRC = pthread_delay_np(&tsWait);
79 */
80 int
81 pthread_delay_np (struct timespec *interval)
82 {
83 DWORD wait_time;
84 DWORD secs_in_millisecs;
85 DWORD millisecs;
86 DWORD status;
87 pthread_t self;
88 ptw32_thread_t * sp;
89
90 if (interval == NULL)
91 {
92 return EINVAL;
93 }
94
95 if (interval->tv_sec == 0L && interval->tv_nsec == 0L)
96 {
97 pthread_testcancel ();
98 Sleep (0);
99 pthread_testcancel ();
100 return (0);
101 }
102
103 /* convert secs to millisecs */
104 secs_in_millisecs = interval->tv_sec * 1000L;
105
106 /* convert nanosecs to millisecs (rounding up) */
107 millisecs = (interval->tv_nsec + 999999L) / 1000000L;
108
109 #if defined(__WATCOMC__)
110 #pragma disable_message (124)
111 #endif
112
113 /*
114 * Most compilers will issue a warning 'comparison always 0'
115 * because the variable type is unsigned, but we need to keep this
116 * for some reason I can't recall now.
117 */
118 if (0 > (wait_time = secs_in_millisecs + millisecs))
119 {
120 return EINVAL;
121 }
122
123 #if defined(__WATCOMC__)
124 #pragma enable_message (124)
125 #endif
126
127 if (NULL == (self = pthread_self ()).p)
128 {
129 return ENOMEM;
130 }
131
132 sp = (ptw32_thread_t *) self.p;
133
134 if (sp->cancelState == PTHREAD_CANCEL_ENABLE)
135 {
136 /*
137 * Async cancelation won't catch us until wait_time is up.
138 * Deferred cancelation will cancel us immediately.
139 */
140 if (WAIT_OBJECT_0 ==
141 (status = WaitForSingleObject (sp->cancelEvent, wait_time)))
142 {
143 /*
144 * Canceling!
145 */
146 (void) pthread_mutex_lock (&sp->cancelLock);
147 if (sp->state < PThreadStateCanceling)
148 {
149 sp->state = PThreadStateCanceling;
150 sp->cancelState = PTHREAD_CANCEL_DISABLE;
151 (void) pthread_mutex_unlock (&sp->cancelLock);
152
153 ptw32_throw (PTW32_EPS_CANCEL);
154 }
155
156 (void) pthread_mutex_unlock (&sp->cancelLock);
157 return ESRCH;
158 }
159 else if (status != WAIT_TIMEOUT)
160 {
161 return EINVAL;
162 }
163 }
164 else
165 {
166 Sleep (wait_time);
167 }
168
169 return (0);
170 }

  ViewVC Help
Powered by ViewVC 1.1.22