1 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
---|
2 | /* ***** BEGIN LICENSE BLOCK *****
|
---|
3 | * Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
---|
4 | *
|
---|
5 | * The contents of this file are subject to the Mozilla Public License Version
|
---|
6 | * 1.1 (the "License"); you may not use this file except in compliance with
|
---|
7 | * the License. You may obtain a copy of the License at
|
---|
8 | * http://www.mozilla.org/MPL/
|
---|
9 | *
|
---|
10 | * Software distributed under the License is distributed on an "AS IS" basis,
|
---|
11 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
---|
12 | * for the specific language governing rights and limitations under the
|
---|
13 | * License.
|
---|
14 | *
|
---|
15 | * The Original Code is the Netscape Portable Runtime (NSPR).
|
---|
16 | *
|
---|
17 | * The Initial Developer of the Original Code is
|
---|
18 | * Netscape Communications Corporation.
|
---|
19 | * Portions created by the Initial Developer are Copyright (C) 2000
|
---|
20 | * the Initial Developer. All Rights Reserved.
|
---|
21 | *
|
---|
22 | * Contributor(s):
|
---|
23 | *
|
---|
24 | * Alternatively, the contents of this file may be used under the terms of
|
---|
25 | * either the GNU General Public License Version 2 or later (the "GPL"), or
|
---|
26 | * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
---|
27 | * in which case the provisions of the GPL or the LGPL are applicable instead
|
---|
28 | * of those above. If you wish to allow use of your version of this file only
|
---|
29 | * under the terms of either the GPL or the LGPL, and not to allow others to
|
---|
30 | * use your version of this file under the terms of the MPL, indicate your
|
---|
31 | * decision by deleting the provisions above and replace them with the notice
|
---|
32 | * and other provisions required by the GPL or the LGPL. If you do not delete
|
---|
33 | * the provisions above, a recipient may use your version of this file under
|
---|
34 | * the terms of any one of the MPL, the GPL or the LGPL.
|
---|
35 | *
|
---|
36 | * ***** END LICENSE BLOCK ***** */
|
---|
37 |
|
---|
38 | /*
|
---|
39 | * File: primblok.c
|
---|
40 | * Purpose: testing whether the primordial thread can block in a
|
---|
41 | * native blocking function without affecting the correct
|
---|
42 | * functioning of NSPR I/O functions (Bugzilla bug #30746)
|
---|
43 | */
|
---|
44 |
|
---|
45 | #if !defined(WINNT)
|
---|
46 |
|
---|
47 | #include <stdio.h>
|
---|
48 |
|
---|
49 | int main()
|
---|
50 | {
|
---|
51 | printf("This test is not relevant on this platform\n");
|
---|
52 | return 0;
|
---|
53 | }
|
---|
54 |
|
---|
55 | #else /* WINNT */
|
---|
56 |
|
---|
57 | #include "nspr.h"
|
---|
58 |
|
---|
59 | #include <windows.h>
|
---|
60 | #include <stdio.h>
|
---|
61 | #include <stdlib.h>
|
---|
62 | #include <string.h>
|
---|
63 |
|
---|
64 | #define TEST_FILE_NAME "primblok.dat"
|
---|
65 |
|
---|
66 | /* use InterlockedExchange to update this variable */
|
---|
67 | static LONG iothread_done;
|
---|
68 |
|
---|
69 | static void PR_CALLBACK IOThread(void *arg)
|
---|
70 | {
|
---|
71 | PRFileDesc *fd;
|
---|
72 | char buf[32];
|
---|
73 | PRInt32 nbytes;
|
---|
74 |
|
---|
75 | /* Give the primordial thread one second to block */
|
---|
76 | Sleep(1000);
|
---|
77 |
|
---|
78 | /*
|
---|
79 | * See if our PR_Write call will hang when the primordial
|
---|
80 | * thread is blocking in a native blocking function.
|
---|
81 | */
|
---|
82 | fd = PR_Open(TEST_FILE_NAME, PR_WRONLY|PR_CREATE_FILE, 0666);
|
---|
83 | if (NULL == fd) {
|
---|
84 | fprintf(stderr, "PR_Open failed\n");
|
---|
85 | exit(1);
|
---|
86 | }
|
---|
87 | memset(buf, 0xaf, sizeof(buf));
|
---|
88 | fprintf(stderr, "iothread: calling PR_Write\n");
|
---|
89 | nbytes = PR_Write(fd, buf, sizeof(buf));
|
---|
90 | fprintf(stderr, "iothread: PR_Write returned\n");
|
---|
91 | if (nbytes != sizeof(buf)) {
|
---|
92 | fprintf(stderr, "PR_Write returned %d\n", nbytes);
|
---|
93 | exit(1);
|
---|
94 | }
|
---|
95 | if (PR_Close(fd) == PR_FAILURE) {
|
---|
96 | fprintf(stderr, "PR_Close failed\n");
|
---|
97 | exit(1);
|
---|
98 | }
|
---|
99 | if (PR_Delete(TEST_FILE_NAME) == PR_FAILURE) {
|
---|
100 | fprintf(stderr, "PR_Delete failed\n");
|
---|
101 | exit(1);
|
---|
102 | }
|
---|
103 |
|
---|
104 | /* Tell the main thread that we are done */
|
---|
105 | InterlockedExchange(&iothread_done, 1);
|
---|
106 | }
|
---|
107 |
|
---|
108 | int main()
|
---|
109 | {
|
---|
110 | PRThread *iothread;
|
---|
111 |
|
---|
112 | /* Must be a global thread */
|
---|
113 | iothread = PR_CreateThread(
|
---|
114 | PR_USER_THREAD, IOThread, NULL, PR_PRIORITY_NORMAL,
|
---|
115 | PR_GLOBAL_THREAD, PR_JOINABLE_THREAD, 0);
|
---|
116 | if (iothread == NULL) {
|
---|
117 | fprintf(stderr, "cannot create thread\n");
|
---|
118 | exit(1);
|
---|
119 | }
|
---|
120 |
|
---|
121 | /*
|
---|
122 | * Block in a native blocking function.
|
---|
123 | * Give iothread 5 seconds to finish its task.
|
---|
124 | */
|
---|
125 | Sleep(5000);
|
---|
126 |
|
---|
127 | /*
|
---|
128 | * Is iothread done or is it hung?
|
---|
129 | *
|
---|
130 | * I'm actually only interested in reading the value
|
---|
131 | * of iothread_done. I'm using InterlockedExchange as
|
---|
132 | * a thread-safe way to read iothread_done.
|
---|
133 | */
|
---|
134 | if (InterlockedExchange(&iothread_done, 1) == 0) {
|
---|
135 | fprintf(stderr, "iothread is hung\n");
|
---|
136 | fprintf(stderr, "FAILED\n");
|
---|
137 | exit(1);
|
---|
138 | }
|
---|
139 |
|
---|
140 | if (PR_JoinThread(iothread) == PR_FAILURE) {
|
---|
141 | fprintf(stderr, "PR_JoinThread failed\n");
|
---|
142 | exit(1);
|
---|
143 | }
|
---|
144 | printf("PASSED\n");
|
---|
145 | return 0;
|
---|
146 | } /* main */
|
---|
147 |
|
---|
148 | #endif /* WINNT */
|
---|