Back to index

lightning-sunbird  0.9+nobinonly
prpollml.c
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is the Netscape Portable Runtime (NSPR).
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 2000
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 /*
00039  * This test exercises the code that allocates and frees the syspoll_list
00040  * array of PRThread in the pthreads version.  This test is intended to be
00041  * run under Purify to verify that there is no memory leak.
00042  */
00043 
00044 #include "nspr.h"
00045 
00046 #include <stdio.h>
00047 #include <stdlib.h>
00048 #include <string.h>
00049 
00050 #define POLL_DESC_COUNT 256  /* This should be greater than the
00051                               * STACK_POLL_DESC_COUNT macro in
00052                               * ptio.c to cause syspoll_list to
00053                               * be created. */
00054 
00055 static PRPollDesc pd[POLL_DESC_COUNT];
00056 
00057 static void Test(void)
00058 {
00059     int i;
00060     PRInt32 rv;
00061     PRIntervalTime timeout;
00062 
00063     timeout = PR_MillisecondsToInterval(10);
00064     /* cause syspoll_list to grow */
00065     for (i = 1; i <= POLL_DESC_COUNT; i++) {
00066         rv = PR_Poll(pd, i, timeout);
00067         if (rv != 0) {
00068             fprintf(stderr,
00069                 "PR_Poll should time out but returns %d (%d, %d)\n",
00070                 rv, PR_GetError(), PR_GetOSError());
00071             exit(1);
00072         }
00073     }
00074     /* syspoll_list should be large enough for all these */
00075     for (i = POLL_DESC_COUNT; i >= 1; i--) {
00076         rv = PR_Poll(pd, i, timeout);
00077         if (rv != 0) {
00078             fprintf(stderr, "PR_Poll should time out but returns %d\n", rv);
00079             exit(1);
00080         }
00081     }
00082 }
00083 
00084 static void ThreadFunc(void *arg)
00085 {
00086     Test();
00087 }
00088 
00089 int main(int argc, char **argv)
00090 {
00091     int i;
00092     PRThread *thread;
00093     PRFileDesc *sock;
00094     PRNetAddr addr;
00095 
00096     memset(&addr, 0, sizeof(addr));
00097     addr.inet.family = PR_AF_INET;
00098     addr.inet.port = PR_htons(0);
00099     addr.inet.ip = PR_htonl(PR_INADDR_ANY);
00100     for (i = 0; i < POLL_DESC_COUNT; i++) {
00101         sock = PR_NewTCPSocket();
00102         if (sock == NULL) {
00103             fprintf(stderr, "PR_NewTCPSocket failed\n");
00104             exit(1);
00105         }
00106         if (PR_Bind(sock, &addr) == PR_FAILURE) {
00107             fprintf(stderr, "PR_Bind failed\n");
00108             exit(1);
00109         }
00110         if (PR_Listen(sock, 5) == PR_FAILURE) {
00111             fprintf(stderr, "PR_Listen failed\n");
00112             exit(1);
00113         }
00114     
00115         pd[i].fd = sock;
00116         pd[i].in_flags = PR_POLL_READ;
00117     }
00118 
00119     /* first run the test on the primordial thread */
00120     Test();
00121 
00122     /* then run the test on all three kinds of threads */
00123     thread = PR_CreateThread(PR_USER_THREAD, ThreadFunc, NULL,
00124             PR_PRIORITY_NORMAL, PR_LOCAL_THREAD, PR_JOINABLE_THREAD, 0);
00125     if (NULL == thread) {
00126         fprintf(stderr, "PR_CreateThread failed\n");
00127         exit(1);
00128     }
00129     if (PR_JoinThread(thread) == PR_FAILURE) {
00130         fprintf(stderr, "PR_JoinThread failed\n");
00131         exit(1);
00132     }
00133     thread = PR_CreateThread(PR_USER_THREAD, ThreadFunc, NULL,
00134             PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD, PR_JOINABLE_THREAD, 0);
00135     if (NULL == thread) {
00136         fprintf(stderr, "PR_CreateThread failed\n");
00137         exit(1);
00138     }
00139     if (PR_JoinThread(thread) == PR_FAILURE) {
00140         fprintf(stderr, "PR_JoinThread failed\n");
00141         exit(1);
00142     }
00143     thread = PR_CreateThread(PR_USER_THREAD, ThreadFunc, NULL,
00144             PR_PRIORITY_NORMAL, PR_GLOBAL_BOUND_THREAD, PR_JOINABLE_THREAD, 0);
00145     if (NULL == thread) {
00146         fprintf(stderr, "PR_CreateThread failed\n");
00147         exit(1);
00148     }
00149     if (PR_JoinThread(thread) == PR_FAILURE) {
00150         fprintf(stderr, "PR_JoinThread failed\n");
00151         exit(1);
00152     }
00153     for (i = 0; i < POLL_DESC_COUNT; i++) {
00154         if (PR_Close(pd[i].fd) == PR_FAILURE) {
00155             fprintf(stderr, "PR_Close failed\n");
00156             exit(1);
00157         }
00158     }
00159     PR_Cleanup();
00160     printf("PASS\n");
00161     return 0;
00162 }