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 * Purpose: testing whether the primordial thread can block in a
9 * native blocking function without affecting the correct
10 * functioning of NSPR I/O functions (Bugzilla bug #30746)
17 int main(int argc
, char **argv
)
19 printf("This test is not relevant on this platform\n");
32 #define TEST_FILE_NAME "primblok.dat"
34 /* use InterlockedExchange to update this variable */
35 static LONG iothread_done
;
37 static void PR_CALLBACK
IOThread(void *arg
)
43 /* Give the primordial thread one second to block */
47 * See if our PR_Write call will hang when the primordial
48 * thread is blocking in a native blocking function.
50 fd
= PR_Open(TEST_FILE_NAME
, PR_WRONLY
|PR_CREATE_FILE
, 0666);
52 fprintf(stderr
, "PR_Open failed\n");
55 memset(buf
, 0xaf, sizeof(buf
));
56 fprintf(stderr
, "iothread: calling PR_Write\n");
57 nbytes
= PR_Write(fd
, buf
, sizeof(buf
));
58 fprintf(stderr
, "iothread: PR_Write returned\n");
59 if (nbytes
!= sizeof(buf
)) {
60 fprintf(stderr
, "PR_Write returned %d\n", nbytes
);
63 if (PR_Close(fd
) == PR_FAILURE
) {
64 fprintf(stderr
, "PR_Close failed\n");
67 if (PR_Delete(TEST_FILE_NAME
) == PR_FAILURE
) {
68 fprintf(stderr
, "PR_Delete failed\n");
72 /* Tell the main thread that we are done */
73 InterlockedExchange(&iothread_done
, 1);
76 int main(int argc
, char **argv
)
80 /* Must be a global thread */
81 iothread
= PR_CreateThread(
82 PR_USER_THREAD
, IOThread
, NULL
, PR_PRIORITY_NORMAL
,
83 PR_GLOBAL_THREAD
, PR_JOINABLE_THREAD
, 0);
84 if (iothread
== NULL
) {
85 fprintf(stderr
, "cannot create thread\n");
90 * Block in a native blocking function.
91 * Give iothread 5 seconds to finish its task.
96 * Is iothread done or is it hung?
98 * I'm actually only interested in reading the value
99 * of iothread_done. I'm using InterlockedExchange as
100 * a thread-safe way to read iothread_done.
102 if (InterlockedExchange(&iothread_done
, 1) == 0) {
103 fprintf(stderr
, "iothread is hung\n");
104 fprintf(stderr
, "FAILED\n");
108 if (PR_JoinThread(iothread
) == PR_FAILURE
) {
109 fprintf(stderr
, "PR_JoinThread failed\n");