nsprpub/pr/tests/prpollml.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 * This test exercises the code that allocates and frees the syspoll_list
michael@0 8 * array of PRThread in the pthreads version. This test is intended to be
michael@0 9 * run under Purify to verify that there is no memory leak.
michael@0 10 */
michael@0 11
michael@0 12 #include "nspr.h"
michael@0 13
michael@0 14 #include <stdio.h>
michael@0 15 #include <stdlib.h>
michael@0 16 #include <string.h>
michael@0 17
michael@0 18 #ifdef SYMBIAN
michael@0 19 #define POLL_DESC_COUNT 128
michael@0 20 #else
michael@0 21 #define POLL_DESC_COUNT 256 /* This should be greater than the
michael@0 22 * STACK_POLL_DESC_COUNT macro in
michael@0 23 * ptio.c to cause syspoll_list to
michael@0 24 * be created. */
michael@0 25 #endif
michael@0 26
michael@0 27 static PRPollDesc pd[POLL_DESC_COUNT];
michael@0 28
michael@0 29 static void Test(void)
michael@0 30 {
michael@0 31 int i;
michael@0 32 PRInt32 rv;
michael@0 33 PRIntervalTime timeout;
michael@0 34
michael@0 35 timeout = PR_MillisecondsToInterval(10);
michael@0 36 /* cause syspoll_list to grow */
michael@0 37 for (i = 1; i <= POLL_DESC_COUNT; i++) {
michael@0 38 rv = PR_Poll(pd, i, timeout);
michael@0 39 if (rv != 0) {
michael@0 40 fprintf(stderr,
michael@0 41 "PR_Poll should time out but returns %d (%d, %d)\n",
michael@0 42 (int) rv, (int) PR_GetError(), (int) PR_GetOSError());
michael@0 43 exit(1);
michael@0 44 }
michael@0 45 }
michael@0 46 /* syspoll_list should be large enough for all these */
michael@0 47 for (i = POLL_DESC_COUNT; i >= 1; i--) {
michael@0 48 rv = PR_Poll(pd, i, timeout);
michael@0 49 if (rv != 0) {
michael@0 50 fprintf(stderr, "PR_Poll should time out but returns %d\n",
michael@0 51 (int) rv);
michael@0 52 exit(1);
michael@0 53 }
michael@0 54 }
michael@0 55 }
michael@0 56
michael@0 57 static void ThreadFunc(void *arg)
michael@0 58 {
michael@0 59 Test();
michael@0 60 }
michael@0 61
michael@0 62 int main(int argc, char **argv)
michael@0 63 {
michael@0 64 int i;
michael@0 65 PRThread *thread;
michael@0 66 PRFileDesc *sock;
michael@0 67 PRNetAddr addr;
michael@0 68
michael@0 69 memset(&addr, 0, sizeof(addr));
michael@0 70 addr.inet.family = PR_AF_INET;
michael@0 71 addr.inet.port = PR_htons(0);
michael@0 72 addr.inet.ip = PR_htonl(PR_INADDR_ANY);
michael@0 73 for (i = 0; i < POLL_DESC_COUNT; i++) {
michael@0 74 sock = PR_NewTCPSocket();
michael@0 75 if (sock == NULL) {
michael@0 76 fprintf(stderr, "PR_NewTCPSocket failed (%d, %d)\n",
michael@0 77 (int) PR_GetError(), (int) PR_GetOSError());
michael@0 78 fprintf(stderr, "Ensure the per process file descriptor limit "
michael@0 79 "is greater than %d.", POLL_DESC_COUNT);
michael@0 80 exit(1);
michael@0 81 }
michael@0 82 if (PR_Bind(sock, &addr) == PR_FAILURE) {
michael@0 83 fprintf(stderr, "PR_Bind failed (%d, %d)\n",
michael@0 84 (int) PR_GetError(), (int) PR_GetOSError());
michael@0 85 exit(1);
michael@0 86 }
michael@0 87 if (PR_Listen(sock, 5) == PR_FAILURE) {
michael@0 88 fprintf(stderr, "PR_Listen failed (%d, %d)\n",
michael@0 89 (int) PR_GetError(), (int) PR_GetOSError());
michael@0 90 exit(1);
michael@0 91 }
michael@0 92
michael@0 93 pd[i].fd = sock;
michael@0 94 pd[i].in_flags = PR_POLL_READ;
michael@0 95 }
michael@0 96
michael@0 97 /* first run the test on the primordial thread */
michael@0 98 Test();
michael@0 99
michael@0 100 /* then run the test on all three kinds of threads */
michael@0 101 thread = PR_CreateThread(PR_USER_THREAD, ThreadFunc, NULL,
michael@0 102 PR_PRIORITY_NORMAL, PR_LOCAL_THREAD, PR_JOINABLE_THREAD, 0);
michael@0 103 if (NULL == thread) {
michael@0 104 fprintf(stderr, "PR_CreateThread failed\n");
michael@0 105 exit(1);
michael@0 106 }
michael@0 107 if (PR_JoinThread(thread) == PR_FAILURE) {
michael@0 108 fprintf(stderr, "PR_JoinThread failed\n");
michael@0 109 exit(1);
michael@0 110 }
michael@0 111 thread = PR_CreateThread(PR_USER_THREAD, ThreadFunc, NULL,
michael@0 112 PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD, PR_JOINABLE_THREAD, 0);
michael@0 113 if (NULL == thread) {
michael@0 114 fprintf(stderr, "PR_CreateThread failed\n");
michael@0 115 exit(1);
michael@0 116 }
michael@0 117 if (PR_JoinThread(thread) == PR_FAILURE) {
michael@0 118 fprintf(stderr, "PR_JoinThread failed\n");
michael@0 119 exit(1);
michael@0 120 }
michael@0 121 thread = PR_CreateThread(PR_USER_THREAD, ThreadFunc, NULL,
michael@0 122 PR_PRIORITY_NORMAL, PR_GLOBAL_BOUND_THREAD, PR_JOINABLE_THREAD, 0);
michael@0 123 if (NULL == thread) {
michael@0 124 fprintf(stderr, "PR_CreateThread failed\n");
michael@0 125 exit(1);
michael@0 126 }
michael@0 127 if (PR_JoinThread(thread) == PR_FAILURE) {
michael@0 128 fprintf(stderr, "PR_JoinThread failed\n");
michael@0 129 exit(1);
michael@0 130 }
michael@0 131 for (i = 0; i < POLL_DESC_COUNT; i++) {
michael@0 132 if (PR_Close(pd[i].fd) == PR_FAILURE) {
michael@0 133 fprintf(stderr, "PR_Close failed\n");
michael@0 134 exit(1);
michael@0 135 }
michael@0 136 }
michael@0 137 PR_Cleanup();
michael@0 138 printf("PASS\n");
michael@0 139 return 0;
michael@0 140 }

mercurial