1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/nsprpub/pr/tests/prpollml.c Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,140 @@ 1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +/* 1.10 + * This test exercises the code that allocates and frees the syspoll_list 1.11 + * array of PRThread in the pthreads version. This test is intended to be 1.12 + * run under Purify to verify that there is no memory leak. 1.13 + */ 1.14 + 1.15 +#include "nspr.h" 1.16 + 1.17 +#include <stdio.h> 1.18 +#include <stdlib.h> 1.19 +#include <string.h> 1.20 + 1.21 +#ifdef SYMBIAN 1.22 +#define POLL_DESC_COUNT 128 1.23 +#else 1.24 +#define POLL_DESC_COUNT 256 /* This should be greater than the 1.25 + * STACK_POLL_DESC_COUNT macro in 1.26 + * ptio.c to cause syspoll_list to 1.27 + * be created. */ 1.28 +#endif 1.29 + 1.30 +static PRPollDesc pd[POLL_DESC_COUNT]; 1.31 + 1.32 +static void Test(void) 1.33 +{ 1.34 + int i; 1.35 + PRInt32 rv; 1.36 + PRIntervalTime timeout; 1.37 + 1.38 + timeout = PR_MillisecondsToInterval(10); 1.39 + /* cause syspoll_list to grow */ 1.40 + for (i = 1; i <= POLL_DESC_COUNT; i++) { 1.41 + rv = PR_Poll(pd, i, timeout); 1.42 + if (rv != 0) { 1.43 + fprintf(stderr, 1.44 + "PR_Poll should time out but returns %d (%d, %d)\n", 1.45 + (int) rv, (int) PR_GetError(), (int) PR_GetOSError()); 1.46 + exit(1); 1.47 + } 1.48 + } 1.49 + /* syspoll_list should be large enough for all these */ 1.50 + for (i = POLL_DESC_COUNT; i >= 1; i--) { 1.51 + rv = PR_Poll(pd, i, timeout); 1.52 + if (rv != 0) { 1.53 + fprintf(stderr, "PR_Poll should time out but returns %d\n", 1.54 + (int) rv); 1.55 + exit(1); 1.56 + } 1.57 + } 1.58 +} 1.59 + 1.60 +static void ThreadFunc(void *arg) 1.61 +{ 1.62 + Test(); 1.63 +} 1.64 + 1.65 +int main(int argc, char **argv) 1.66 +{ 1.67 + int i; 1.68 + PRThread *thread; 1.69 + PRFileDesc *sock; 1.70 + PRNetAddr addr; 1.71 + 1.72 + memset(&addr, 0, sizeof(addr)); 1.73 + addr.inet.family = PR_AF_INET; 1.74 + addr.inet.port = PR_htons(0); 1.75 + addr.inet.ip = PR_htonl(PR_INADDR_ANY); 1.76 + for (i = 0; i < POLL_DESC_COUNT; i++) { 1.77 + sock = PR_NewTCPSocket(); 1.78 + if (sock == NULL) { 1.79 + fprintf(stderr, "PR_NewTCPSocket failed (%d, %d)\n", 1.80 + (int) PR_GetError(), (int) PR_GetOSError()); 1.81 + fprintf(stderr, "Ensure the per process file descriptor limit " 1.82 + "is greater than %d.", POLL_DESC_COUNT); 1.83 + exit(1); 1.84 + } 1.85 + if (PR_Bind(sock, &addr) == PR_FAILURE) { 1.86 + fprintf(stderr, "PR_Bind failed (%d, %d)\n", 1.87 + (int) PR_GetError(), (int) PR_GetOSError()); 1.88 + exit(1); 1.89 + } 1.90 + if (PR_Listen(sock, 5) == PR_FAILURE) { 1.91 + fprintf(stderr, "PR_Listen failed (%d, %d)\n", 1.92 + (int) PR_GetError(), (int) PR_GetOSError()); 1.93 + exit(1); 1.94 + } 1.95 + 1.96 + pd[i].fd = sock; 1.97 + pd[i].in_flags = PR_POLL_READ; 1.98 + } 1.99 + 1.100 + /* first run the test on the primordial thread */ 1.101 + Test(); 1.102 + 1.103 + /* then run the test on all three kinds of threads */ 1.104 + thread = PR_CreateThread(PR_USER_THREAD, ThreadFunc, NULL, 1.105 + PR_PRIORITY_NORMAL, PR_LOCAL_THREAD, PR_JOINABLE_THREAD, 0); 1.106 + if (NULL == thread) { 1.107 + fprintf(stderr, "PR_CreateThread failed\n"); 1.108 + exit(1); 1.109 + } 1.110 + if (PR_JoinThread(thread) == PR_FAILURE) { 1.111 + fprintf(stderr, "PR_JoinThread failed\n"); 1.112 + exit(1); 1.113 + } 1.114 + thread = PR_CreateThread(PR_USER_THREAD, ThreadFunc, NULL, 1.115 + PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD, PR_JOINABLE_THREAD, 0); 1.116 + if (NULL == thread) { 1.117 + fprintf(stderr, "PR_CreateThread failed\n"); 1.118 + exit(1); 1.119 + } 1.120 + if (PR_JoinThread(thread) == PR_FAILURE) { 1.121 + fprintf(stderr, "PR_JoinThread failed\n"); 1.122 + exit(1); 1.123 + } 1.124 + thread = PR_CreateThread(PR_USER_THREAD, ThreadFunc, NULL, 1.125 + PR_PRIORITY_NORMAL, PR_GLOBAL_BOUND_THREAD, PR_JOINABLE_THREAD, 0); 1.126 + if (NULL == thread) { 1.127 + fprintf(stderr, "PR_CreateThread failed\n"); 1.128 + exit(1); 1.129 + } 1.130 + if (PR_JoinThread(thread) == PR_FAILURE) { 1.131 + fprintf(stderr, "PR_JoinThread failed\n"); 1.132 + exit(1); 1.133 + } 1.134 + for (i = 0; i < POLL_DESC_COUNT; i++) { 1.135 + if (PR_Close(pd[i].fd) == PR_FAILURE) { 1.136 + fprintf(stderr, "PR_Close failed\n"); 1.137 + exit(1); 1.138 + } 1.139 + } 1.140 + PR_Cleanup(); 1.141 + printf("PASS\n"); 1.142 + return 0; 1.143 +}