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/. */
5
6 /*
7 * File: primblok.c
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)
11 */
12
13 #if !defined(WINNT)
14
15 #include <stdio.h>
16
main(int argc,char ** argv)17 int main(int argc, char **argv)
18 {
19 printf("This test is not relevant on this platform\n");
20 return 0;
21 }
22
23 #else /* WINNT */
24
25 #include "nspr.h"
26
27 #include <windows.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31
32 #define TEST_FILE_NAME "primblok.dat"
33
34 /* use InterlockedExchange to update this variable */
35 static LONG iothread_done;
36
IOThread(void * arg)37 static void PR_CALLBACK IOThread(void *arg)
38 {
39 PRFileDesc *fd;
40 char buf[32];
41 PRInt32 nbytes;
42
43 /* Give the primordial thread one second to block */
44 Sleep(1000);
45
46 /*
47 * See if our PR_Write call will hang when the primordial
48 * thread is blocking in a native blocking function.
49 */
50 fd = PR_Open(TEST_FILE_NAME, PR_WRONLY|PR_CREATE_FILE, 0666);
51 if (NULL == fd) {
52 fprintf(stderr, "PR_Open failed\n");
53 exit(1);
54 }
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);
61 exit(1);
62 }
63 if (PR_Close(fd) == PR_FAILURE) {
64 fprintf(stderr, "PR_Close failed\n");
65 exit(1);
66 }
67 if (PR_Delete(TEST_FILE_NAME) == PR_FAILURE) {
68 fprintf(stderr, "PR_Delete failed\n");
69 exit(1);
70 }
71
72 /* Tell the main thread that we are done */
73 InterlockedExchange(&iothread_done, 1);
74 }
75
main(int argc,char ** argv)76 int main(int argc, char **argv)
77 {
78 PRThread *iothread;
79
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");
86 exit(1);
87 }
88
89 /*
90 * Block in a native blocking function.
91 * Give iothread 5 seconds to finish its task.
92 */
93 Sleep(5000);
94
95 /*
96 * Is iothread done or is it hung?
97 *
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.
101 */
102 if (InterlockedExchange(&iothread_done, 1) == 0) {
103 fprintf(stderr, "iothread is hung\n");
104 fprintf(stderr, "FAILED\n");
105 exit(1);
106 }
107
108 if (PR_JoinThread(iothread) == PR_FAILURE) {
109 fprintf(stderr, "PR_JoinThread failed\n");
110 exit(1);
111 }
112 printf("PASSED\n");
113 return 0;
114 } /* main */
115
116 #endif /* WINNT */
117