nsprpub/pr/tests/primblok.c

Wed, 31 Dec 2014 07:53:36 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:53:36 +0100
branch
TOR_BUG_3246
changeset 5
4ab42b5ab56c
permissions
-rw-r--r--

Correct small whitespace inconsistency, lost while renaming variables.

michael@0 1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 /*
michael@0 7 * File: primblok.c
michael@0 8 * Purpose: testing whether the primordial thread can block in a
michael@0 9 * native blocking function without affecting the correct
michael@0 10 * functioning of NSPR I/O functions (Bugzilla bug #30746)
michael@0 11 */
michael@0 12
michael@0 13 #if !defined(WINNT)
michael@0 14
michael@0 15 #include <stdio.h>
michael@0 16
michael@0 17 int main(int argc, char **argv)
michael@0 18 {
michael@0 19 printf("This test is not relevant on this platform\n");
michael@0 20 return 0;
michael@0 21 }
michael@0 22
michael@0 23 #else /* WINNT */
michael@0 24
michael@0 25 #include "nspr.h"
michael@0 26
michael@0 27 #include <windows.h>
michael@0 28 #include <stdio.h>
michael@0 29 #include <stdlib.h>
michael@0 30 #include <string.h>
michael@0 31
michael@0 32 #define TEST_FILE_NAME "primblok.dat"
michael@0 33
michael@0 34 /* use InterlockedExchange to update this variable */
michael@0 35 static LONG iothread_done;
michael@0 36
michael@0 37 static void PR_CALLBACK IOThread(void *arg)
michael@0 38 {
michael@0 39 PRFileDesc *fd;
michael@0 40 char buf[32];
michael@0 41 PRInt32 nbytes;
michael@0 42
michael@0 43 /* Give the primordial thread one second to block */
michael@0 44 Sleep(1000);
michael@0 45
michael@0 46 /*
michael@0 47 * See if our PR_Write call will hang when the primordial
michael@0 48 * thread is blocking in a native blocking function.
michael@0 49 */
michael@0 50 fd = PR_Open(TEST_FILE_NAME, PR_WRONLY|PR_CREATE_FILE, 0666);
michael@0 51 if (NULL == fd) {
michael@0 52 fprintf(stderr, "PR_Open failed\n");
michael@0 53 exit(1);
michael@0 54 }
michael@0 55 memset(buf, 0xaf, sizeof(buf));
michael@0 56 fprintf(stderr, "iothread: calling PR_Write\n");
michael@0 57 nbytes = PR_Write(fd, buf, sizeof(buf));
michael@0 58 fprintf(stderr, "iothread: PR_Write returned\n");
michael@0 59 if (nbytes != sizeof(buf)) {
michael@0 60 fprintf(stderr, "PR_Write returned %d\n", nbytes);
michael@0 61 exit(1);
michael@0 62 }
michael@0 63 if (PR_Close(fd) == PR_FAILURE) {
michael@0 64 fprintf(stderr, "PR_Close failed\n");
michael@0 65 exit(1);
michael@0 66 }
michael@0 67 if (PR_Delete(TEST_FILE_NAME) == PR_FAILURE) {
michael@0 68 fprintf(stderr, "PR_Delete failed\n");
michael@0 69 exit(1);
michael@0 70 }
michael@0 71
michael@0 72 /* Tell the main thread that we are done */
michael@0 73 InterlockedExchange(&iothread_done, 1);
michael@0 74 }
michael@0 75
michael@0 76 int main(int argc, char **argv)
michael@0 77 {
michael@0 78 PRThread *iothread;
michael@0 79
michael@0 80 /* Must be a global thread */
michael@0 81 iothread = PR_CreateThread(
michael@0 82 PR_USER_THREAD, IOThread, NULL, PR_PRIORITY_NORMAL,
michael@0 83 PR_GLOBAL_THREAD, PR_JOINABLE_THREAD, 0);
michael@0 84 if (iothread == NULL) {
michael@0 85 fprintf(stderr, "cannot create thread\n");
michael@0 86 exit(1);
michael@0 87 }
michael@0 88
michael@0 89 /*
michael@0 90 * Block in a native blocking function.
michael@0 91 * Give iothread 5 seconds to finish its task.
michael@0 92 */
michael@0 93 Sleep(5000);
michael@0 94
michael@0 95 /*
michael@0 96 * Is iothread done or is it hung?
michael@0 97 *
michael@0 98 * I'm actually only interested in reading the value
michael@0 99 * of iothread_done. I'm using InterlockedExchange as
michael@0 100 * a thread-safe way to read iothread_done.
michael@0 101 */
michael@0 102 if (InterlockedExchange(&iothread_done, 1) == 0) {
michael@0 103 fprintf(stderr, "iothread is hung\n");
michael@0 104 fprintf(stderr, "FAILED\n");
michael@0 105 exit(1);
michael@0 106 }
michael@0 107
michael@0 108 if (PR_JoinThread(iothread) == PR_FAILURE) {
michael@0 109 fprintf(stderr, "PR_JoinThread failed\n");
michael@0 110 exit(1);
michael@0 111 }
michael@0 112 printf("PASSED\n");
michael@0 113 return 0;
michael@0 114 } /* main */
michael@0 115
michael@0 116 #endif /* WINNT */

mercurial