Back to index

lightning-sunbird  0.9+nobinonly
pipeself.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) 1999-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  * File: pipeself.c
00040  *
00041  * Description:
00042  * This test has two threads communicating with each other using
00043  * two unidirectional pipes.  The primordial thread is the ping
00044  * thread and the other thread is the pong thread.  The ping
00045  * thread writes "ping" to the pong thread and the pong thread
00046  * writes "pong" back.
00047  */
00048 
00049 #include "prio.h"
00050 #include "prerror.h"
00051 #include "prthread.h"
00052 
00053 #include <stdio.h>
00054 #include <stdlib.h>
00055 #include <string.h>
00056 
00057 #define NUM_ITERATIONS 10
00058 
00059 static PRFileDesc *ping_in, *ping_out;
00060 static PRFileDesc *pong_in, *pong_out;
00061 
00062 static void PongThreadFunc(void *arg)
00063 {
00064     char buf[1024];
00065     int idx;
00066     PRInt32 nBytes;
00067     PRStatus status;
00068 
00069     for (idx = 0; idx < NUM_ITERATIONS; idx++) {
00070         memset(buf, 0, sizeof(buf));
00071         nBytes = PR_Read(pong_in, buf, sizeof(buf));
00072         if (nBytes == -1) {
00073             fprintf(stderr, "PR_Read failed\n");
00074             exit(1);
00075         }
00076         printf("pong thread: received \"%s\"\n", buf);
00077         if (nBytes != 5) {
00078             fprintf(stderr, "pong thread: expected 5 bytes but got %d bytes\n",
00079                     nBytes);
00080             exit(1);
00081         }
00082         if (strcmp(buf, "ping") != 0) {
00083             fprintf(stderr, "pong thread: expected \"ping\" but got \"%s\"\n",
00084                     buf);
00085             exit(1);
00086         }
00087         strcpy(buf, "pong");
00088         printf("pong thread: sending \"%s\"\n", buf);
00089         nBytes = PR_Write(pong_out, buf, 5);
00090         if (nBytes == -1) {
00091             fprintf(stderr, "PR_Write failed: (%d, %d)\n", PR_GetError(),
00092                     PR_GetOSError());
00093             exit(1);
00094         }
00095     }
00096 
00097     status = PR_Close(pong_in);
00098     if (status == PR_FAILURE) {
00099         fprintf(stderr, "PR_Close failed\n");
00100         exit(1);
00101     }
00102     status = PR_Close(pong_out);
00103     if (status == PR_FAILURE) {
00104         fprintf(stderr, "PR_Close failed\n");
00105         exit(1);
00106     }
00107 }
00108 
00109 int main()
00110 {
00111     PRStatus status;
00112     PRThread *pongThread;
00113     char buf[1024];
00114     PRInt32 nBytes;
00115     int idx;
00116 
00117     status = PR_CreatePipe(&ping_in, &pong_out);
00118     if (status == PR_FAILURE) {
00119         fprintf(stderr, "PR_CreatePipe failed\n");
00120         exit(1);
00121     }
00122     status = PR_CreatePipe(&pong_in, &ping_out);
00123     if (status == PR_FAILURE) {
00124         fprintf(stderr, "PR_CreatePipe failed\n");
00125         exit(1);
00126     }
00127 
00128     pongThread = PR_CreateThread(PR_USER_THREAD, PongThreadFunc, NULL,
00129             PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD, PR_JOINABLE_THREAD, 0);
00130     if (pongThread == NULL) {
00131         fprintf(stderr, "PR_CreateThread failed\n");
00132         exit(1);
00133     }
00134 
00135     for (idx = 0; idx < NUM_ITERATIONS; idx++) {
00136         strcpy(buf, "ping");
00137         printf("ping thread: sending \"%s\"\n", buf);
00138         nBytes = PR_Write(ping_out, buf, 5);
00139         if (nBytes == -1) {
00140             fprintf(stderr, "PR_Write failed: (%d, %d)\n", PR_GetError(),
00141                     PR_GetOSError());
00142             exit(1);
00143         }
00144         memset(buf, 0, sizeof(buf));
00145         nBytes = PR_Read(ping_in, buf, sizeof(buf));
00146         if (nBytes == -1) {
00147             fprintf(stderr, "PR_Read failed\n");
00148             exit(1);
00149         }
00150         printf("ping thread: received \"%s\"\n", buf);
00151         if (nBytes != 5) {
00152             fprintf(stderr, "ping thread: expected 5 bytes but got %d bytes\n",
00153                     nBytes);
00154             exit(1);
00155         }
00156         if (strcmp(buf, "pong") != 0) {
00157             fprintf(stderr, "ping thread: expected \"pong\" but got \"%s\"\n",
00158                     buf);
00159             exit(1);
00160         }
00161     }
00162 
00163     status = PR_Close(ping_in);
00164     if (status == PR_FAILURE) {
00165         fprintf(stderr, "PR_Close failed\n");
00166         exit(1);
00167     }
00168     status = PR_Close(ping_out);
00169     if (status == PR_FAILURE) {
00170         fprintf(stderr, "PR_Close failed\n");
00171         exit(1);
00172     }
00173     status = PR_JoinThread(pongThread);
00174     if (status == PR_FAILURE) {
00175         fprintf(stderr, "PR_JoinThread failed\n");
00176         exit(1);
00177     }
00178 
00179 #ifdef XP_UNIX
00180        /*
00181         * Test PR_Available for pipes
00182         */
00183     status = PR_CreatePipe(&ping_in, &ping_out);
00184     if (status == PR_FAILURE) {
00185         fprintf(stderr, "PR_CreatePipe failed\n");
00186         exit(1);
00187     }
00188        nBytes = PR_Write(ping_out, buf, 250);
00189        if (nBytes == -1) {
00190               fprintf(stderr, "PR_Write failed: (%d, %d)\n", PR_GetError(),
00191                             PR_GetOSError());
00192               exit(1);
00193        }
00194        nBytes = PR_Available(ping_in);
00195        if (nBytes < 0) {
00196               fprintf(stderr, "PR_Available failed: (%d, %d)\n", PR_GetError(),
00197                             PR_GetOSError());
00198               exit(1);
00199        } else if (nBytes != 250) {
00200               fprintf(stderr, "PR_Available: expected 250 bytes but got %d bytes\n",
00201                             nBytes);
00202               exit(1);
00203        }
00204        printf("PR_Available: expected %d, got %d bytes\n",250, nBytes);
00205        /* read some data */
00206        nBytes = PR_Read(ping_in, buf, 7);
00207        if (nBytes == -1) {
00208               fprintf(stderr, "PR_Read failed\n");
00209               exit(1);
00210        }
00211        /* check available data */
00212        nBytes = PR_Available(ping_in);
00213        if (nBytes < 0) {
00214               fprintf(stderr, "PR_Available failed: (%d, %d)\n", PR_GetError(),
00215                             PR_GetOSError());
00216               exit(1);
00217        } else if (nBytes != (250 - 7)) {
00218               fprintf(stderr, "PR_Available: expected 243 bytes but got %d bytes\n",
00219                             nBytes);
00220               exit(1);
00221        }
00222        printf("PR_Available: expected %d, got %d bytes\n",243, nBytes);
00223        /* read all data */
00224        nBytes = PR_Read(ping_in, buf, sizeof(buf));
00225        if (nBytes == -1) {
00226               fprintf(stderr, "PR_Read failed\n");
00227               exit(1);
00228        } else if (nBytes != 243) {
00229               fprintf(stderr, "PR_Read failed: expected %d, got %d bytes\n",
00230                                                  243, nBytes);
00231               exit(1);
00232        }
00233        /* check available data */
00234        nBytes = PR_Available(ping_in);
00235        if (nBytes < 0) {
00236               fprintf(stderr, "PR_Available failed: (%d, %d)\n", PR_GetError(),
00237                             PR_GetOSError());
00238               exit(1);
00239        } else if (nBytes != 0) {
00240               fprintf(stderr, "PR_Available: expected 0 bytes but got %d bytes\n",
00241                             nBytes);
00242               exit(1);
00243        }
00244        printf("PR_Available: expected %d, got %d bytes\n", 0, nBytes);
00245 
00246     status = PR_Close(ping_in);
00247     if (status == PR_FAILURE) {
00248         fprintf(stderr, "PR_Close failed\n");
00249         exit(1);
00250     }
00251     status = PR_Close(ping_out);
00252     if (status == PR_FAILURE) {
00253         fprintf(stderr, "PR_Close failed\n");
00254         exit(1);
00255     }
00256 #endif /* XP_UNIX */
00257 
00258     printf("PASS\n");
00259     return 0;
00260 }