Bump the mac_chromium_gn_rel bot to 100% experimental in the CQ.
[chromium-blink-merge.git] / native_client_sdk / src / libraries / third_party / pthreads-win32 / pthread_delay_np.c
blobe6c96d8070bb2d4f4e4ece30bd3b1163a4ef9ddc
1 /*
2 * pthreads_delay_np.c
4 * Description:
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
37 #include "pthread.h"
38 #include "implement.h"
41 * pthread_delay_np
43 * DESCRIPTION
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.
60 * Return Values
62 * If an error condition occurs, this routine returns an integer value indicating
63 * the type of error. Possible return values are as follows:
65 * 0
66 * Successful completion.
67 * [EINVAL]
68 * The value specified by interval is invalid.
70 * Example
72 * The following code segment would wait for 5 and 1/2 seconds
74 * struct timespec tsWait;
75 * int intRC;
77 * tsWait.tv_sec = 5;
78 * tsWait.tv_nsec = 500000000L;
79 * intRC = pthread_delay_np(&tsWait);
81 int
82 pthread_delay_np (struct timespec *interval)
84 DWORD wait_time;
85 DWORD secs_in_millisecs;
86 DWORD millisecs;
87 DWORD status;
88 pthread_t self;
89 ptw32_thread_t * sp;
91 if (interval == NULL)
93 return EINVAL;
96 if (interval->tv_sec == 0L && interval->tv_nsec == 0L)
98 pthread_testcancel ();
99 Sleep (0);
100 pthread_testcancel ();
101 return (0);
104 /* convert secs to millisecs */
105 secs_in_millisecs = (DWORD)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)
112 #endif
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))
121 return EINVAL;
124 #if defined(__WATCOMC__)
125 #pragma enable_message (124)
126 #endif
128 if (NULL == (self = pthread_self ()).p)
130 return ENOMEM;
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.
141 if (WAIT_OBJECT_0 ==
142 (status = WaitForSingleObject (sp->cancelEvent, wait_time)))
144 ptw32_mcs_local_node_t stateLock;
146 * Canceling!
148 ptw32_mcs_lock_acquire (&sp->stateLock, &stateLock);
149 if (sp->state < PThreadStateCanceling)
151 sp->state = PThreadStateCanceling;
152 sp->cancelState = PTHREAD_CANCEL_DISABLE;
153 ptw32_mcs_lock_release (&stateLock);
155 ptw32_throw (PTW32_EPS_CANCEL);
158 ptw32_mcs_lock_release (&stateLock);
159 return ESRCH;
161 else if (status != WAIT_TIMEOUT)
163 return EINVAL;
166 else
168 Sleep (wait_time);
171 return (0);