Back to index

lightning-sunbird  0.9+nobinonly
ioconthr.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) 1998-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 is a test for the io continuation thread machinery
00040  * in pthreads.
00041  */
00042 
00043 #include "nspr.h"
00044 #include <stdio.h>
00045 
00046 int num_threads = 10;  /* must be an even number */
00047 PRThreadScope thread_scope = PR_GLOBAL_THREAD;
00048 
00049 void ThreadFunc(void *arg)
00050 {
00051     PRFileDesc *fd = (PRFileDesc *) arg;
00052     char buf[1024];
00053     PRInt32 nbytes;
00054     PRErrorCode err;
00055 
00056     nbytes = PR_Recv(fd, buf, sizeof(buf), 0, PR_SecondsToInterval(20));
00057     if (nbytes == -1) {
00058         err = PR_GetError();
00059         if (err != PR_PENDING_INTERRUPT_ERROR) {
00060             fprintf(stderr, "PR_Recv failed: (%d, %d)\n",
00061                     err, PR_GetOSError());
00062             PR_ProcessExit(1);
00063         }
00064         /*
00065          * After getting an I/O interrupt, this thread must
00066          * close the fd before it exits due to a limitation
00067          * of our NT implementation.
00068          */
00069         if (PR_Close(fd) == PR_FAILURE) {
00070             fprintf(stderr, "PR_Close failed\n");
00071             PR_ProcessExit(1);
00072         }
00073     } else {
00074         fprintf(stderr, "PR_Recv received %d bytes!?\n", nbytes);
00075         PR_ProcessExit(1);
00076     }
00077 }
00078 
00079 int main(int argc, char **argv)
00080 {
00081     PRFileDesc **fds;
00082     PRThread **threads;
00083     PRIntervalTime start, elapsed;
00084     int index;
00085 
00086     fds = (PRFileDesc **) PR_MALLOC(2 * num_threads * sizeof(PRFileDesc *));
00087     PR_ASSERT(fds != NULL);
00088     threads = (PRThread **) PR_MALLOC(num_threads * sizeof(PRThread *));
00089     PR_ASSERT(threads != NULL);
00090 
00091     for (index = 0; index < num_threads; index++) {
00092         if (PR_NewTCPSocketPair(&fds[2 * index]) == PR_FAILURE) {
00093             fprintf(stderr, "PR_NewTCPSocket failed\n");
00094             PR_ProcessExit(1);
00095         }
00096         threads[index] = PR_CreateThread(
00097                 PR_USER_THREAD, ThreadFunc, fds[2 * index],
00098                 PR_PRIORITY_NORMAL, thread_scope, PR_JOINABLE_THREAD, 0);
00099         if (NULL == threads[index]) {
00100             fprintf(stderr, "PR_CreateThread failed\n");
00101             PR_ProcessExit(1);
00102         }
00103     }
00104 
00105     /* Let the threads block in PR_Recv */
00106     PR_Sleep(PR_SecondsToInterval(2));
00107 
00108     printf("Interrupting the threads\n");
00109     fflush(stdout);
00110     start = PR_IntervalNow();
00111     for (index = 0; index < num_threads; index++) {
00112         if (PR_Interrupt(threads[index]) == PR_FAILURE) {
00113             fprintf(stderr, "PR_Interrupt failed\n");
00114             PR_ProcessExit(1);
00115         }
00116     }
00117     for (index = 0; index < num_threads; index++) {
00118         if (PR_JoinThread(threads[index]) == PR_FAILURE) {
00119             fprintf(stderr, "PR_JoinThread failed\n");
00120             PR_ProcessExit(1);
00121         }
00122     }
00123     elapsed = (PRIntervalTime)(PR_IntervalNow() - start);
00124     printf("Threads terminated in %d milliseconds\n",
00125             PR_IntervalToMilliseconds(elapsed));
00126     fflush(stdout);
00127     
00128     /* We are being very generous and allow 10 seconds. */
00129     if (elapsed >= PR_SecondsToInterval(10)) {
00130         fprintf(stderr, "Interrupting threads took longer than 10 seconds!!\n");
00131         PR_ProcessExit(1);
00132     }
00133 
00134     for (index = 0; index < num_threads; index++) {
00135         /* fds[2 * index] was passed to and closed by threads[index]. */
00136         if (PR_Close(fds[2 * index + 1]) == PR_FAILURE) {
00137             fprintf(stderr, "PR_Close failed\n");
00138             PR_ProcessExit(1);
00139         }
00140     }
00141     PR_DELETE(threads);
00142     PR_DELETE(fds);
00143     printf("PASS\n");
00144     PR_Cleanup();
00145     return 0;
00146 }