5
* This translation unit implements non-portable thread functions.
7
* --------------------------------------------------------------------------
9
* Pthreads-win32 - POSIX Threads Library for Win32
10
* Copyright(C) 1998 John E. Bossom
11
* Copyright(C) 1999,2005 Pthreads-win32 contributors
13
* Contact Email: rpj@callisto.canberra.edu.au
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
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.
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.
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
38
#include "implement.h"
45
* This routine causes a thread to delay execution for a specific period of time.
46
* This period ends at the current time plus the specified interval. The routine
47
* will not return before the end of the period is reached, but may return an
48
* arbitrary amount of time after the period has gone by. This can be due to
49
* system load, thread priorities, and system timer granularity.
51
* Specifying an interval of zero (0) seconds and zero (0) nanoseconds is
52
* allowed and can be used to force the thread to give up the processor or to
53
* deliver a pending cancelation request.
55
* The timespec structure contains the following two fields:
57
* tv_sec is an integer number of seconds.
58
* tv_nsec is an integer number of nanoseconds.
62
* If an error condition occurs, this routine returns an integer value indicating
63
* the type of error. Possible return values are as follows:
66
* Successful completion.
68
* The value specified by interval is invalid.
72
* The following code segment would wait for 5 and 1/2 seconds
74
* struct timespec tsWait;
78
* tsWait.tv_nsec = 500000000L;
79
* intRC = pthread_delay_np(&tsWait);
82
pthread_delay_np (struct timespec *interval)
85
DWORD secs_in_millisecs;
96
if (interval->tv_sec == 0L && interval->tv_nsec == 0L)
98
pthread_testcancel ();
100
pthread_testcancel ();
104
/* convert secs to millisecs */
105
secs_in_millisecs = interval->tv_sec * 1000L;
107
/* convert nanosecs to millisecs (rounding up) */
108
millisecs = (interval->tv_nsec + 999999L) / 1000000L;
110
#if defined(__WATCOMC__)
111
#pragma disable_message (124)
115
* Most compilers will issue a warning 'comparison always 0'
116
* because the variable type is unsigned, but we need to keep this
117
* for some reason I can't recall now.
119
if (0 > (wait_time = secs_in_millisecs + millisecs))
124
#if defined(__WATCOMC__)
125
#pragma enable_message (124)
128
if (NULL == (self = pthread_self ()).p)
133
sp = (ptw32_thread_t *) self.p;
135
if (sp->cancelState == PTHREAD_CANCEL_ENABLE)
138
* Async cancelation won't catch us until wait_time is up.
139
* Deferred cancelation will cancel us immediately.
142
(status = WaitForSingleObject (sp->cancelEvent, wait_time)))
147
(void) pthread_mutex_lock (&sp->cancelLock);
148
if (sp->state < PThreadStateCanceling)
150
sp->state = PThreadStateCanceling;
151
sp->cancelState = PTHREAD_CANCEL_DISABLE;
152
(void) pthread_mutex_unlock (&sp->cancelLock);
154
ptw32_throw (PTW32_EPS_CANCEL);
157
(void) pthread_mutex_unlock (&sp->cancelLock);
160
else if (status != WAIT_TIMEOUT)