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: * File: pipeself.c michael@0: * michael@0: * Description: michael@0: * This test has two threads communicating with each other using michael@0: * two unidirectional pipes. The primordial thread is the ping michael@0: * thread and the other thread is the pong thread. The ping michael@0: * thread writes "ping" to the pong thread and the pong thread michael@0: * writes "pong" back. michael@0: */ michael@0: michael@0: #include "prio.h" michael@0: #include "prerror.h" michael@0: #include "prthread.h" michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: #define NUM_ITERATIONS 10 michael@0: michael@0: static PRFileDesc *ping_in, *ping_out; michael@0: static PRFileDesc *pong_in, *pong_out; michael@0: michael@0: static void PongThreadFunc(void *arg) michael@0: { michael@0: char buf[1024]; michael@0: int idx; michael@0: PRInt32 nBytes; michael@0: PRStatus status; michael@0: michael@0: for (idx = 0; idx < NUM_ITERATIONS; idx++) { michael@0: memset(buf, 0, sizeof(buf)); michael@0: nBytes = PR_Read(pong_in, buf, sizeof(buf)); michael@0: if (nBytes == -1) { michael@0: fprintf(stderr, "PR_Read failed\n"); michael@0: exit(1); michael@0: } michael@0: printf("pong thread: received \"%s\"\n", buf); michael@0: if (nBytes != 5) { michael@0: fprintf(stderr, "pong thread: expected 5 bytes but got %d bytes\n", michael@0: nBytes); michael@0: exit(1); michael@0: } michael@0: if (strcmp(buf, "ping") != 0) { michael@0: fprintf(stderr, "pong thread: expected \"ping\" but got \"%s\"\n", michael@0: buf); michael@0: exit(1); michael@0: } michael@0: strcpy(buf, "pong"); michael@0: printf("pong thread: sending \"%s\"\n", buf); michael@0: nBytes = PR_Write(pong_out, buf, 5); michael@0: if (nBytes == -1) { michael@0: fprintf(stderr, "PR_Write failed: (%d, %d)\n", PR_GetError(), michael@0: PR_GetOSError()); michael@0: exit(1); michael@0: } michael@0: } michael@0: michael@0: status = PR_Close(pong_in); michael@0: if (status == PR_FAILURE) { michael@0: fprintf(stderr, "PR_Close failed\n"); michael@0: exit(1); michael@0: } michael@0: status = PR_Close(pong_out); michael@0: if (status == PR_FAILURE) { michael@0: fprintf(stderr, "PR_Close failed\n"); michael@0: exit(1); michael@0: } michael@0: } michael@0: michael@0: int main(int argc, char **argv) michael@0: { michael@0: PRStatus status; michael@0: PRThread *pongThread; michael@0: char buf[1024]; michael@0: PRInt32 nBytes; michael@0: int idx; michael@0: michael@0: status = PR_CreatePipe(&ping_in, &pong_out); michael@0: if (status == PR_FAILURE) { michael@0: fprintf(stderr, "PR_CreatePipe failed\n"); michael@0: exit(1); michael@0: } michael@0: status = PR_CreatePipe(&pong_in, &ping_out); michael@0: if (status == PR_FAILURE) { michael@0: fprintf(stderr, "PR_CreatePipe failed\n"); michael@0: exit(1); michael@0: } michael@0: michael@0: pongThread = PR_CreateThread(PR_USER_THREAD, PongThreadFunc, NULL, michael@0: PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD, PR_JOINABLE_THREAD, 0); michael@0: if (pongThread == NULL) { michael@0: fprintf(stderr, "PR_CreateThread failed\n"); michael@0: exit(1); michael@0: } michael@0: michael@0: for (idx = 0; idx < NUM_ITERATIONS; idx++) { michael@0: strcpy(buf, "ping"); michael@0: printf("ping thread: sending \"%s\"\n", buf); michael@0: nBytes = PR_Write(ping_out, buf, 5); michael@0: if (nBytes == -1) { michael@0: fprintf(stderr, "PR_Write failed: (%d, %d)\n", PR_GetError(), michael@0: PR_GetOSError()); michael@0: exit(1); michael@0: } michael@0: memset(buf, 0, sizeof(buf)); michael@0: nBytes = PR_Read(ping_in, buf, sizeof(buf)); michael@0: if (nBytes == -1) { michael@0: fprintf(stderr, "PR_Read failed\n"); michael@0: exit(1); michael@0: } michael@0: printf("ping thread: received \"%s\"\n", buf); michael@0: if (nBytes != 5) { michael@0: fprintf(stderr, "ping thread: expected 5 bytes but got %d bytes\n", michael@0: nBytes); michael@0: exit(1); michael@0: } michael@0: if (strcmp(buf, "pong") != 0) { michael@0: fprintf(stderr, "ping thread: expected \"pong\" but got \"%s\"\n", michael@0: buf); michael@0: exit(1); michael@0: } michael@0: } michael@0: michael@0: status = PR_Close(ping_in); michael@0: if (status == PR_FAILURE) { michael@0: fprintf(stderr, "PR_Close failed\n"); michael@0: exit(1); michael@0: } michael@0: status = PR_Close(ping_out); michael@0: if (status == PR_FAILURE) { michael@0: fprintf(stderr, "PR_Close failed\n"); michael@0: exit(1); michael@0: } michael@0: status = PR_JoinThread(pongThread); michael@0: if (status == PR_FAILURE) { michael@0: fprintf(stderr, "PR_JoinThread failed\n"); michael@0: exit(1); michael@0: } michael@0: michael@0: #if defined(XP_UNIX) && !defined(SYMBIAN) michael@0: /* michael@0: * Test PR_Available for pipes michael@0: */ michael@0: status = PR_CreatePipe(&ping_in, &ping_out); michael@0: if (status == PR_FAILURE) { michael@0: fprintf(stderr, "PR_CreatePipe failed\n"); michael@0: exit(1); michael@0: } michael@0: nBytes = PR_Write(ping_out, buf, 250); michael@0: if (nBytes == -1) { michael@0: fprintf(stderr, "PR_Write failed: (%d, %d)\n", PR_GetError(), michael@0: PR_GetOSError()); michael@0: exit(1); michael@0: } michael@0: nBytes = PR_Available(ping_in); michael@0: if (nBytes < 0) { michael@0: fprintf(stderr, "PR_Available failed: (%d, %d)\n", PR_GetError(), michael@0: PR_GetOSError()); michael@0: exit(1); michael@0: } else if (nBytes != 250) { michael@0: fprintf(stderr, "PR_Available: expected 250 bytes but got %d bytes\n", michael@0: nBytes); michael@0: exit(1); michael@0: } michael@0: printf("PR_Available: expected %d, got %d bytes\n",250, nBytes); michael@0: /* read some data */ michael@0: nBytes = PR_Read(ping_in, buf, 7); michael@0: if (nBytes == -1) { michael@0: fprintf(stderr, "PR_Read failed\n"); michael@0: exit(1); michael@0: } michael@0: /* check available data */ michael@0: nBytes = PR_Available(ping_in); michael@0: if (nBytes < 0) { michael@0: fprintf(stderr, "PR_Available failed: (%d, %d)\n", PR_GetError(), michael@0: PR_GetOSError()); michael@0: exit(1); michael@0: } else if (nBytes != (250 - 7)) { michael@0: fprintf(stderr, "PR_Available: expected 243 bytes but got %d bytes\n", michael@0: nBytes); michael@0: exit(1); michael@0: } michael@0: printf("PR_Available: expected %d, got %d bytes\n",243, nBytes); michael@0: /* read all data */ michael@0: nBytes = PR_Read(ping_in, buf, sizeof(buf)); michael@0: if (nBytes == -1) { michael@0: fprintf(stderr, "PR_Read failed\n"); michael@0: exit(1); michael@0: } else if (nBytes != 243) { michael@0: fprintf(stderr, "PR_Read failed: expected %d, got %d bytes\n", michael@0: 243, nBytes); michael@0: exit(1); michael@0: } michael@0: /* check available data */ michael@0: nBytes = PR_Available(ping_in); michael@0: if (nBytes < 0) { michael@0: fprintf(stderr, "PR_Available failed: (%d, %d)\n", PR_GetError(), michael@0: PR_GetOSError()); michael@0: exit(1); michael@0: } else if (nBytes != 0) { michael@0: fprintf(stderr, "PR_Available: expected 0 bytes but got %d bytes\n", michael@0: nBytes); michael@0: exit(1); michael@0: } michael@0: printf("PR_Available: expected %d, got %d bytes\n", 0, nBytes); michael@0: michael@0: status = PR_Close(ping_in); michael@0: if (status == PR_FAILURE) { michael@0: fprintf(stderr, "PR_Close failed\n"); michael@0: exit(1); michael@0: } michael@0: status = PR_Close(ping_out); michael@0: if (status == PR_FAILURE) { michael@0: fprintf(stderr, "PR_Close failed\n"); michael@0: exit(1); michael@0: } michael@0: #endif /* XP_UNIX */ michael@0: michael@0: printf("PASS\n"); michael@0: return 0; michael@0: }