VirtualBox

source: vbox/trunk/src/libs/xpcom18a4/nsprpub/pr/tests/pipeself.c@ 55761

最後變更 在這個檔案從55761是 1,由 vboxsync 提交於 55 年 前

import

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 7.7 KB
 
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2/* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 *
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
9 *
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
14 *
15 * The Original Code is the Netscape Portable Runtime (NSPR).
16 *
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1999-2000
20 * the Initial Developer. All Rights Reserved.
21 *
22 * Contributor(s):
23 *
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
35 *
36 * ***** END LICENSE BLOCK ***** */
37
38/*
39 * File: pipeself.c
40 *
41 * Description:
42 * This test has two threads communicating with each other using
43 * two unidirectional pipes. The primordial thread is the ping
44 * thread and the other thread is the pong thread. The ping
45 * thread writes "ping" to the pong thread and the pong thread
46 * writes "pong" back.
47 */
48
49#include "prio.h"
50#include "prerror.h"
51#include "prthread.h"
52
53#include <stdio.h>
54#include <stdlib.h>
55#include <string.h>
56
57#define NUM_ITERATIONS 10
58
59static PRFileDesc *ping_in, *ping_out;
60static PRFileDesc *pong_in, *pong_out;
61
62static void PongThreadFunc(void *arg)
63{
64 char buf[1024];
65 int idx;
66 PRInt32 nBytes;
67 PRStatus status;
68
69 for (idx = 0; idx < NUM_ITERATIONS; idx++) {
70 memset(buf, 0, sizeof(buf));
71 nBytes = PR_Read(pong_in, buf, sizeof(buf));
72 if (nBytes == -1) {
73 fprintf(stderr, "PR_Read failed\n");
74 exit(1);
75 }
76 printf("pong thread: received \"%s\"\n", buf);
77 if (nBytes != 5) {
78 fprintf(stderr, "pong thread: expected 5 bytes but got %d bytes\n",
79 nBytes);
80 exit(1);
81 }
82 if (strcmp(buf, "ping") != 0) {
83 fprintf(stderr, "pong thread: expected \"ping\" but got \"%s\"\n",
84 buf);
85 exit(1);
86 }
87 strcpy(buf, "pong");
88 printf("pong thread: sending \"%s\"\n", buf);
89 nBytes = PR_Write(pong_out, buf, 5);
90 if (nBytes == -1) {
91 fprintf(stderr, "PR_Write failed: (%d, %d)\n", PR_GetError(),
92 PR_GetOSError());
93 exit(1);
94 }
95 }
96
97 status = PR_Close(pong_in);
98 if (status == PR_FAILURE) {
99 fprintf(stderr, "PR_Close failed\n");
100 exit(1);
101 }
102 status = PR_Close(pong_out);
103 if (status == PR_FAILURE) {
104 fprintf(stderr, "PR_Close failed\n");
105 exit(1);
106 }
107}
108
109int main()
110{
111 PRStatus status;
112 PRThread *pongThread;
113 char buf[1024];
114 PRInt32 nBytes;
115 int idx;
116
117 status = PR_CreatePipe(&ping_in, &pong_out);
118 if (status == PR_FAILURE) {
119 fprintf(stderr, "PR_CreatePipe failed\n");
120 exit(1);
121 }
122 status = PR_CreatePipe(&pong_in, &ping_out);
123 if (status == PR_FAILURE) {
124 fprintf(stderr, "PR_CreatePipe failed\n");
125 exit(1);
126 }
127
128 pongThread = PR_CreateThread(PR_USER_THREAD, PongThreadFunc, NULL,
129 PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD, PR_JOINABLE_THREAD, 0);
130 if (pongThread == NULL) {
131 fprintf(stderr, "PR_CreateThread failed\n");
132 exit(1);
133 }
134
135 for (idx = 0; idx < NUM_ITERATIONS; idx++) {
136 strcpy(buf, "ping");
137 printf("ping thread: sending \"%s\"\n", buf);
138 nBytes = PR_Write(ping_out, buf, 5);
139 if (nBytes == -1) {
140 fprintf(stderr, "PR_Write failed: (%d, %d)\n", PR_GetError(),
141 PR_GetOSError());
142 exit(1);
143 }
144 memset(buf, 0, sizeof(buf));
145 nBytes = PR_Read(ping_in, buf, sizeof(buf));
146 if (nBytes == -1) {
147 fprintf(stderr, "PR_Read failed\n");
148 exit(1);
149 }
150 printf("ping thread: received \"%s\"\n", buf);
151 if (nBytes != 5) {
152 fprintf(stderr, "ping thread: expected 5 bytes but got %d bytes\n",
153 nBytes);
154 exit(1);
155 }
156 if (strcmp(buf, "pong") != 0) {
157 fprintf(stderr, "ping thread: expected \"pong\" but got \"%s\"\n",
158 buf);
159 exit(1);
160 }
161 }
162
163 status = PR_Close(ping_in);
164 if (status == PR_FAILURE) {
165 fprintf(stderr, "PR_Close failed\n");
166 exit(1);
167 }
168 status = PR_Close(ping_out);
169 if (status == PR_FAILURE) {
170 fprintf(stderr, "PR_Close failed\n");
171 exit(1);
172 }
173 status = PR_JoinThread(pongThread);
174 if (status == PR_FAILURE) {
175 fprintf(stderr, "PR_JoinThread failed\n");
176 exit(1);
177 }
178
179#ifdef XP_UNIX
180 /*
181 * Test PR_Available for pipes
182 */
183 status = PR_CreatePipe(&ping_in, &ping_out);
184 if (status == PR_FAILURE) {
185 fprintf(stderr, "PR_CreatePipe failed\n");
186 exit(1);
187 }
188 nBytes = PR_Write(ping_out, buf, 250);
189 if (nBytes == -1) {
190 fprintf(stderr, "PR_Write failed: (%d, %d)\n", PR_GetError(),
191 PR_GetOSError());
192 exit(1);
193 }
194 nBytes = PR_Available(ping_in);
195 if (nBytes < 0) {
196 fprintf(stderr, "PR_Available failed: (%d, %d)\n", PR_GetError(),
197 PR_GetOSError());
198 exit(1);
199 } else if (nBytes != 250) {
200 fprintf(stderr, "PR_Available: expected 250 bytes but got %d bytes\n",
201 nBytes);
202 exit(1);
203 }
204 printf("PR_Available: expected %d, got %d bytes\n",250, nBytes);
205 /* read some data */
206 nBytes = PR_Read(ping_in, buf, 7);
207 if (nBytes == -1) {
208 fprintf(stderr, "PR_Read failed\n");
209 exit(1);
210 }
211 /* check available data */
212 nBytes = PR_Available(ping_in);
213 if (nBytes < 0) {
214 fprintf(stderr, "PR_Available failed: (%d, %d)\n", PR_GetError(),
215 PR_GetOSError());
216 exit(1);
217 } else if (nBytes != (250 - 7)) {
218 fprintf(stderr, "PR_Available: expected 243 bytes but got %d bytes\n",
219 nBytes);
220 exit(1);
221 }
222 printf("PR_Available: expected %d, got %d bytes\n",243, nBytes);
223 /* read all data */
224 nBytes = PR_Read(ping_in, buf, sizeof(buf));
225 if (nBytes == -1) {
226 fprintf(stderr, "PR_Read failed\n");
227 exit(1);
228 } else if (nBytes != 243) {
229 fprintf(stderr, "PR_Read failed: expected %d, got %d bytes\n",
230 243, nBytes);
231 exit(1);
232 }
233 /* check available data */
234 nBytes = PR_Available(ping_in);
235 if (nBytes < 0) {
236 fprintf(stderr, "PR_Available failed: (%d, %d)\n", PR_GetError(),
237 PR_GetOSError());
238 exit(1);
239 } else if (nBytes != 0) {
240 fprintf(stderr, "PR_Available: expected 0 bytes but got %d bytes\n",
241 nBytes);
242 exit(1);
243 }
244 printf("PR_Available: expected %d, got %d bytes\n", 0, nBytes);
245
246 status = PR_Close(ping_in);
247 if (status == PR_FAILURE) {
248 fprintf(stderr, "PR_Close failed\n");
249 exit(1);
250 }
251 status = PR_Close(ping_out);
252 if (status == PR_FAILURE) {
253 fprintf(stderr, "PR_Close failed\n");
254 exit(1);
255 }
256#endif /* XP_UNIX */
257
258 printf("PASS\n");
259 return 0;
260}
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette