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/. */
8 #include <string.h> /* for memset() */
11 /************************************************************************/
13 PRLock
*_pr_flock_lock
;
14 PRCondVar
*_pr_flock_cv
;
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
;
34 const PRIOMethods
*methods
= PR_GetFileMethods();
38 _pr_flock_lock
= PR_NewLock();
39 _pr_flock_cv
= PR_NewCondVar(_pr_flock_lock
);
42 _pr_stdin
= PR_AllocFileDesc((PROsfd
)GetStdHandle(STD_INPUT_HANDLE
),
44 _pr_stdout
= PR_AllocFileDesc((PROsfd
)GetStdHandle(STD_OUTPUT_HANDLE
),
46 _pr_stderr
= PR_AllocFileDesc((PROsfd
)GetStdHandle(STD_ERROR_HANDLE
),
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
;
54 _pr_stdin
= PR_AllocFileDesc(0, methods
);
55 _pr_stdout
= PR_AllocFileDesc(1, methods
);
56 _pr_stderr
= PR_AllocFileDesc(2, methods
);
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
);
65 void _PR_CleanupIO(void)
67 PR_FreeFileDesc(_pr_stdin
);
69 PR_FreeFileDesc(_pr_stdout
);
71 PR_FreeFileDesc(_pr_stderr
);
75 PR_DestroyCondVar(_pr_flock_cv
);
79 PR_DestroyLock(_pr_flock_lock
);
80 _pr_flock_lock
= NULL
;
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();
97 case PR_StandardInput
: result
= _pr_stdin
; break;
98 case PR_StandardOutput
: result
= _pr_stdout
; break;
99 case PR_StandardError
: result
= _pr_stderr
; break;
101 (void)PR_SetError(PR_INVALID_ARGUMENT_ERROR
, 0);
106 PR_IMPLEMENT(PRFileDesc
*) PR_AllocFileDesc(
107 PROsfd osfd
, const PRIOMethods
*methods
)
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
);
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
;
125 fd
->secret
->alreadyConnected
= PR_FALSE
;
126 fd
->secret
->overlappedActive
= PR_FALSE
;
128 _PR_MD_INIT_FILEDESC(fd
);
130 PR_SetError(PR_OUT_OF_MEMORY_ERROR
, 0);
136 PR_IMPLEMENT(void) PR_FreeFileDesc(PRFileDesc
*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
) {
154 PR_Lock(_fd_waiting_for_overlapped_done_lock
);
156 PRFileDescList
*cur
= _fd_waiting_for_overlapped_done
;
157 PRFileDescList
*previous
= NULL
;
159 PR_ASSERT(cur
->fd
->secret
->overlappedActive
);
160 PRFileDesc
*fd
= cur
->fd
;
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"));
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;
191 previous
->next
= cur
->next
;
193 _fd_waiting_for_overlapped_done
= cur
->next
;
195 PRFileDescList
*del
= cur
;
204 PR_Unlock(_fd_waiting_for_overlapped_done_lock
);
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();
218 return(_PR_MD_PR_POLL(pds
, npds
, timeout
));
222 ** Set the inheritance attribute of a file descriptor.
224 PR_IMPLEMENT(PRStatus
) PR_SetFDInheritable(
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);
237 if (fd
->secret
->inheritable
!= inheritable
) {
238 if (_PR_MD_SET_FD_INHERITABLE(fd
, inheritable
) == PR_FAILURE
) {
241 fd
->secret
->inheritable
= inheritable
;
245 PR_SetError(PR_NOT_IMPLEMENTED_ERROR
, 0);
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
)
257 } /* PT_FPrintStats */