backout 29799f914cab, Bug 917642 - [Helix] Please update the helix blobs
[gecko.git] / nsprpub / pr / tests / primblok.c
blobe036572baee48cac2eb8b651b04452fb5c85705f
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 /*
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)
13 #if !defined(WINNT)
15 #include <stdio.h>
17 int main(int argc, char **argv)
19 printf("This test is not relevant on this platform\n");
20 return 0;
23 #else /* WINNT */
25 #include "nspr.h"
27 #include <windows.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
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)
39 PRFileDesc *fd;
40 char buf[32];
41 PRInt32 nbytes;
43 /* Give the primordial thread one second to block */
44 Sleep(1000);
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);
51 if (NULL == fd) {
52 fprintf(stderr, "PR_Open failed\n");
53 exit(1);
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);
63 if (PR_Close(fd) == PR_FAILURE) {
64 fprintf(stderr, "PR_Close failed\n");
65 exit(1);
67 if (PR_Delete(TEST_FILE_NAME) == PR_FAILURE) {
68 fprintf(stderr, "PR_Delete failed\n");
69 exit(1);
72 /* Tell the main thread that we are done */
73 InterlockedExchange(&iothread_done, 1);
76 int main(int argc, char **argv)
78 PRThread *iothread;
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);
90 * Block in a native blocking function.
91 * Give iothread 5 seconds to finish its task.
93 Sleep(5000);
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");
105 exit(1);
108 if (PR_JoinThread(iothread) == PR_FAILURE) {
109 fprintf(stderr, "PR_JoinThread failed\n");
110 exit(1);
112 printf("PASSED\n");
113 return 0;
114 } /* main */
116 #endif /* WINNT */