Bug 1807268 - Fix verifyOpenAllInNewTabsOptionTest UI test r=ohorvath
[gecko.git] / nsprpub / pr / src / io / prio.c
blob745d7721b9b13118cd659b271a02e697344fd5e0
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include "primpl.h"
8 #include <string.h> /* for memset() */
11 /************************************************************************/
13 PRLock *_pr_flock_lock;
14 PRCondVar *_pr_flock_cv;
16 #ifdef WINCE
18 * There are no stdin, stdout, stderr in Windows CE. INVALID_HANDLE_VALUE
19 * should cause all I/O functions on the handle to fail.
21 #define STD_INPUT_HANDLE ((DWORD)-10)
22 #define STD_OUTPUT_HANDLE ((DWORD)-11)
23 #define STD_ERROR_HANDLE ((DWORD)-12)
25 static HANDLE GetStdHandle(DWORD nStdHandle)
27 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
28 return INVALID_HANDLE_VALUE;
30 #endif
32 void _PR_InitIO(void)
34 const PRIOMethods *methods = PR_GetFileMethods();
36 _PR_InitFdCache();
38 _pr_flock_lock = PR_NewLock();
39 _pr_flock_cv = PR_NewCondVar(_pr_flock_lock);
41 #ifdef WIN32
42 _pr_stdin = PR_AllocFileDesc((PROsfd)GetStdHandle(STD_INPUT_HANDLE),
43 methods);
44 _pr_stdout = PR_AllocFileDesc((PROsfd)GetStdHandle(STD_OUTPUT_HANDLE),
45 methods);
46 _pr_stderr = PR_AllocFileDesc((PROsfd)GetStdHandle(STD_ERROR_HANDLE),
47 methods);
48 #ifdef WINNT
49 _pr_stdin->secret->md.sync_file_io = PR_TRUE;
50 _pr_stdout->secret->md.sync_file_io = PR_TRUE;
51 _pr_stderr->secret->md.sync_file_io = PR_TRUE;
52 #endif
53 #else
54 _pr_stdin = PR_AllocFileDesc(0, methods);
55 _pr_stdout = PR_AllocFileDesc(1, methods);
56 _pr_stderr = PR_AllocFileDesc(2, methods);
57 #endif
58 _PR_MD_INIT_FD_INHERITABLE(_pr_stdin, PR_TRUE);
59 _PR_MD_INIT_FD_INHERITABLE(_pr_stdout, PR_TRUE);
60 _PR_MD_INIT_FD_INHERITABLE(_pr_stderr, PR_TRUE);
62 _PR_MD_INIT_IO();
65 void _PR_CleanupIO(void)
67 PR_FreeFileDesc(_pr_stdin);
68 _pr_stdin = NULL;
69 PR_FreeFileDesc(_pr_stdout);
70 _pr_stdout = NULL;
71 PR_FreeFileDesc(_pr_stderr);
72 _pr_stderr = NULL;
74 if (_pr_flock_cv) {
75 PR_DestroyCondVar(_pr_flock_cv);
76 _pr_flock_cv = NULL;
78 if (_pr_flock_lock) {
79 PR_DestroyLock(_pr_flock_lock);
80 _pr_flock_lock = NULL;
83 _PR_CleanupFdCache();
86 PR_IMPLEMENT(PRFileDesc*) PR_GetSpecialFD(PRSpecialFD osfd)
88 PRFileDesc *result = NULL;
89 PR_ASSERT((int) osfd >= PR_StandardInput && osfd <= PR_StandardError);
91 if (!_pr_initialized) {
92 _PR_ImplicitInitialization();
95 switch (osfd)
97 case PR_StandardInput: result = _pr_stdin; break;
98 case PR_StandardOutput: result = _pr_stdout; break;
99 case PR_StandardError: result = _pr_stderr; break;
100 default:
101 (void)PR_SetError(PR_INVALID_ARGUMENT_ERROR, 0);
103 return result;
106 PR_IMPLEMENT(PRFileDesc*) PR_AllocFileDesc(
107 PROsfd osfd, const PRIOMethods *methods)
109 PRFileDesc *fd;
111 #ifdef XP_UNIX
113 * Assert that the file descriptor is small enough to fit in the
114 * fd_set passed to select
116 PR_ASSERT(osfd < FD_SETSIZE);
117 #endif
118 fd = _PR_Getfd();
119 if (fd) {
120 /* Initialize the members of PRFileDesc and PRFilePrivate */
121 fd->methods = methods;
122 fd->secret->state = _PR_FILEDESC_OPEN;
123 fd->secret->md.osfd = osfd;
124 #if defined(_WIN64)
125 fd->secret->alreadyConnected = PR_FALSE;
126 fd->secret->overlappedActive = PR_FALSE;
127 #endif
128 _PR_MD_INIT_FILEDESC(fd);
129 } else {
130 PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0);
133 return fd;
136 PR_IMPLEMENT(void) PR_FreeFileDesc(PRFileDesc *fd)
138 PR_ASSERT(fd);
139 _PR_Putfd(fd);
142 #if defined(_WIN64) && defined(WIN95)
144 PRFileDescList *_fd_waiting_for_overlapped_done = NULL;
145 PRLock *_fd_waiting_for_overlapped_done_lock = NULL;
147 void CheckOverlappedPendingSocketsAreDone()
149 if (!_fd_waiting_for_overlapped_done_lock ||
150 !_fd_waiting_for_overlapped_done) {
151 return;
154 PR_Lock(_fd_waiting_for_overlapped_done_lock);
156 PRFileDescList *cur = _fd_waiting_for_overlapped_done;
157 PRFileDescList *previous = NULL;
158 while (cur) {
159 PR_ASSERT(cur->fd->secret->overlappedActive);
160 PRFileDesc *fd = cur->fd;
161 DWORD rvSent;
162 if (GetOverlappedResult((HANDLE)fd->secret->md.osfd, &fd->secret->ol, &rvSent, FALSE) == TRUE) {
163 fd->secret->overlappedActive = PR_FALSE;
164 PR_LOG(_pr_io_lm, PR_LOG_MIN,
165 ("CheckOverlappedPendingSocketsAreDone GetOverlappedResult succeeded\n"));
166 } else {
167 DWORD err = WSAGetLastError();
168 PR_LOG(_pr_io_lm, PR_LOG_MIN,
169 ("CheckOverlappedPendingSocketsAreDone GetOverlappedResult failed %d\n", err));
170 if (err != ERROR_IO_INCOMPLETE) {
171 fd->secret->overlappedActive = PR_FALSE;
175 if (!fd->secret->overlappedActive) {
177 _PR_MD_CLOSE_SOCKET(fd->secret->md.osfd);
178 fd->secret->state = _PR_FILEDESC_CLOSED;
179 #ifdef _PR_HAVE_PEEK_BUFFER
180 if (fd->secret->peekBuffer) {
181 PR_ASSERT(fd->secret->peekBufSize > 0);
182 PR_DELETE(fd->secret->peekBuffer);
183 fd->secret->peekBufSize = 0;
184 fd->secret->peekBytes = 0;
186 #endif
188 PR_FreeFileDesc(fd);
190 if (previous) {
191 previous->next = cur->next;
192 } else {
193 _fd_waiting_for_overlapped_done = cur->next;
195 PRFileDescList *del = cur;
196 cur = cur->next;
197 PR_Free(del);
198 } else {
199 previous = cur;
200 cur = cur->next;
204 PR_Unlock(_fd_waiting_for_overlapped_done_lock);
206 #endif
209 ** Wait for some i/o to finish on one or more more poll descriptors.
211 PR_IMPLEMENT(PRInt32) PR_Poll(PRPollDesc *pds, PRIntn npds, PRIntervalTime timeout)
213 #if defined(_WIN64) && defined(WIN95)
214 // For each iteration check if TFO overlapped IOs are down.
215 CheckOverlappedPendingSocketsAreDone();
216 #endif
218 return(_PR_MD_PR_POLL(pds, npds, timeout));
222 ** Set the inheritance attribute of a file descriptor.
224 PR_IMPLEMENT(PRStatus) PR_SetFDInheritable(
225 PRFileDesc *fd,
226 PRBool inheritable)
228 #if defined(XP_UNIX) || defined(WIN32) || defined(XP_OS2)
230 * Only a non-layered, NSPR file descriptor can be inherited
231 * by a child process.
233 if (fd->identity != PR_NSPR_IO_LAYER) {
234 PR_SetError(PR_INVALID_ARGUMENT_ERROR, 0);
235 return PR_FAILURE;
237 if (fd->secret->inheritable != inheritable) {
238 if (_PR_MD_SET_FD_INHERITABLE(fd, inheritable) == PR_FAILURE) {
239 return PR_FAILURE;
241 fd->secret->inheritable = inheritable;
243 return PR_SUCCESS;
244 #else
245 PR_SetError(PR_NOT_IMPLEMENTED_ERROR, 0);
246 return PR_FAILURE;
247 #endif
251 ** This function only has a useful implementation in the debug build of
252 ** the pthreads version.
254 PR_IMPLEMENT(void) PT_FPrintStats(PRFileDesc *debug_out, const char *msg)
256 /* do nothing */
257 } /* PT_FPrintStats */