michael@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /* michael@0: * This test exercises the code that allocates and frees the syspoll_list michael@0: * array of PRThread in the pthreads version. This test is intended to be michael@0: * run under Purify to verify that there is no memory leak. michael@0: */ michael@0: michael@0: #include "nspr.h" michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: #ifdef SYMBIAN michael@0: #define POLL_DESC_COUNT 128 michael@0: #else michael@0: #define POLL_DESC_COUNT 256 /* This should be greater than the michael@0: * STACK_POLL_DESC_COUNT macro in michael@0: * ptio.c to cause syspoll_list to michael@0: * be created. */ michael@0: #endif michael@0: michael@0: static PRPollDesc pd[POLL_DESC_COUNT]; michael@0: michael@0: static void Test(void) michael@0: { michael@0: int i; michael@0: PRInt32 rv; michael@0: PRIntervalTime timeout; michael@0: michael@0: timeout = PR_MillisecondsToInterval(10); michael@0: /* cause syspoll_list to grow */ michael@0: for (i = 1; i <= POLL_DESC_COUNT; i++) { michael@0: rv = PR_Poll(pd, i, timeout); michael@0: if (rv != 0) { michael@0: fprintf(stderr, michael@0: "PR_Poll should time out but returns %d (%d, %d)\n", michael@0: (int) rv, (int) PR_GetError(), (int) PR_GetOSError()); michael@0: exit(1); michael@0: } michael@0: } michael@0: /* syspoll_list should be large enough for all these */ michael@0: for (i = POLL_DESC_COUNT; i >= 1; i--) { michael@0: rv = PR_Poll(pd, i, timeout); michael@0: if (rv != 0) { michael@0: fprintf(stderr, "PR_Poll should time out but returns %d\n", michael@0: (int) rv); michael@0: exit(1); michael@0: } michael@0: } michael@0: } michael@0: michael@0: static void ThreadFunc(void *arg) michael@0: { michael@0: Test(); michael@0: } michael@0: michael@0: int main(int argc, char **argv) michael@0: { michael@0: int i; michael@0: PRThread *thread; michael@0: PRFileDesc *sock; michael@0: PRNetAddr addr; michael@0: michael@0: memset(&addr, 0, sizeof(addr)); michael@0: addr.inet.family = PR_AF_INET; michael@0: addr.inet.port = PR_htons(0); michael@0: addr.inet.ip = PR_htonl(PR_INADDR_ANY); michael@0: for (i = 0; i < POLL_DESC_COUNT; i++) { michael@0: sock = PR_NewTCPSocket(); michael@0: if (sock == NULL) { michael@0: fprintf(stderr, "PR_NewTCPSocket failed (%d, %d)\n", michael@0: (int) PR_GetError(), (int) PR_GetOSError()); michael@0: fprintf(stderr, "Ensure the per process file descriptor limit " michael@0: "is greater than %d.", POLL_DESC_COUNT); michael@0: exit(1); michael@0: } michael@0: if (PR_Bind(sock, &addr) == PR_FAILURE) { michael@0: fprintf(stderr, "PR_Bind failed (%d, %d)\n", michael@0: (int) PR_GetError(), (int) PR_GetOSError()); michael@0: exit(1); michael@0: } michael@0: if (PR_Listen(sock, 5) == PR_FAILURE) { michael@0: fprintf(stderr, "PR_Listen failed (%d, %d)\n", michael@0: (int) PR_GetError(), (int) PR_GetOSError()); michael@0: exit(1); michael@0: } michael@0: michael@0: pd[i].fd = sock; michael@0: pd[i].in_flags = PR_POLL_READ; michael@0: } michael@0: michael@0: /* first run the test on the primordial thread */ michael@0: Test(); michael@0: michael@0: /* then run the test on all three kinds of threads */ michael@0: thread = PR_CreateThread(PR_USER_THREAD, ThreadFunc, NULL, michael@0: PR_PRIORITY_NORMAL, PR_LOCAL_THREAD, PR_JOINABLE_THREAD, 0); michael@0: if (NULL == thread) { michael@0: fprintf(stderr, "PR_CreateThread failed\n"); michael@0: exit(1); michael@0: } michael@0: if (PR_JoinThread(thread) == PR_FAILURE) { michael@0: fprintf(stderr, "PR_JoinThread failed\n"); michael@0: exit(1); michael@0: } michael@0: thread = PR_CreateThread(PR_USER_THREAD, ThreadFunc, NULL, michael@0: PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD, PR_JOINABLE_THREAD, 0); michael@0: if (NULL == thread) { michael@0: fprintf(stderr, "PR_CreateThread failed\n"); michael@0: exit(1); michael@0: } michael@0: if (PR_JoinThread(thread) == PR_FAILURE) { michael@0: fprintf(stderr, "PR_JoinThread failed\n"); michael@0: exit(1); michael@0: } michael@0: thread = PR_CreateThread(PR_USER_THREAD, ThreadFunc, NULL, michael@0: PR_PRIORITY_NORMAL, PR_GLOBAL_BOUND_THREAD, PR_JOINABLE_THREAD, 0); michael@0: if (NULL == thread) { michael@0: fprintf(stderr, "PR_CreateThread failed\n"); michael@0: exit(1); michael@0: } michael@0: if (PR_JoinThread(thread) == PR_FAILURE) { michael@0: fprintf(stderr, "PR_JoinThread failed\n"); michael@0: exit(1); michael@0: } michael@0: for (i = 0; i < POLL_DESC_COUNT; i++) { michael@0: if (PR_Close(pd[i].fd) == PR_FAILURE) { michael@0: fprintf(stderr, "PR_Close failed\n"); michael@0: exit(1); michael@0: } michael@0: } michael@0: PR_Cleanup(); michael@0: printf("PASS\n"); michael@0: return 0; michael@0: }