Subversion Repositories HelenOS

Rev

Rev 1113 | Rev 1128 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
447 decky 1
/*
2
 * Copyright (C) 2005 Martin Decky
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 *
9
 * - Redistributions of source code must retain the above copyright
10
 *   notice, this list of conditions and the following disclaimer.
11
 * - Redistributions in binary form must reproduce the above copyright
12
 *   notice, this list of conditions and the following disclaimer in the
13
 *   documentation and/or other materials provided with the distribution.
14
 * - The name of the author may not be used to endorse or promote products
15
 *   derived from this software without specific prior written permission.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 */
28
 
713 decky 29
#include "version.h"
954 palkovsky 30
#include <ipc.h>
988 palkovsky 31
#include <stdio.h>
32
#include <unistd.h>
33
#include <stdlib.h>
1028 palkovsky 34
#include <ns.h>
1065 jermar 35
#include <thread.h>
1113 palkovsky 36
#include <psthread.h>
1111 jermar 37
#include <futex.h>
999 palkovsky 38
 
1089 palkovsky 39
int a;
1111 jermar 40
atomic_t ftx;
1089 palkovsky 41
 
1065 jermar 42
extern void utest(void *arg);
43
void utest(void *arg)
44
{
1081 jermar 45
	printf("Uspace thread started.\n");
1111 jermar 46
	if (futex_down(&ftx) < 0)
47
		printf("Futex failed.\n");
48
	if (futex_up(&ftx) < 0)
49
		printf("Futex failed.\n");
50
 
51
	printf("%s in good condition.\n", __FUNCTION__);
52
 
1065 jermar 53
	for (;;)
54
		;
55
}
1049 cejka 56
 
995 cejka 57
static void test_printf(void)
58
{
59
	printf("Simple text.\n");
60
	printf("Now insert '%s' string.\n","this");
1073 cejka 61
	printf("Signed formats on uns. numbers: '%d', '%+d', '% d', '%u' (,+, ,u)\n", 321, 321, 321, 321);
62
	printf("Signed formats on sig. numbers: '%d', '%+d', '% d', '%u' (,+, ,u)\n", -321, -321, -321, -321);
63
	printf("Signed with different sized: '%hhd', '%hd', '%d', '%ld', %lld;\n", -3, -32, -321, -32101l, -3210123ll);
64
	printf("And now... '%hhd' byte! '%hd' word! '%d' int! \n", 11, 11111, 1111111111);
65
	printf("Different bases: %#hx, %#hu, %#ho and %#hb\n", 123, 123, 123, 123);
66
	printf("Different bases signed: %#hx, %#hu, %#ho and %#hb\n", -123, -123, -123, -123);
67
	printf("'%llX' llX! Another '%llx' llx! \n", 0x1234567887654321ll, 0x1234567887654321ll);
68
	printf("'%llX' with 64bit value and '%x' with 32 bit value. \n", 0x1234567887654321ll, 0x12345678 );
69
	printf("'%llx' 64bit, '%x' 32bit, '%hhx' 8bit, '%hx' 16bit, '%llX' 64bit and '%s' string.\n", 0x1234567887654321ll, 0x12345678, 0x12, 0x1234, 0x1234567887654321ull, "Lovely string" );
1049 cejka 70
 
995 cejka 71
	printf("Thats all, folks!\n");
72
}
999 palkovsky 73
 
1049 cejka 74
 
1028 palkovsky 75
extern char _heap;
994 jermar 76
static void test_mremap(void)
988 palkovsky 77
{
78
	printf("Writing to good memory\n");
79
	mremap(&_heap, 120000, 0);
80
	printf("%P\n", ((char *)&_heap));
81
	printf("%P\n", ((char *)&_heap) + 80000);
82
	*(((char *)&_heap) + 80000) = 10;
83
	printf("Making small\n");
84
	mremap(&_heap, 16000, 0);
85
	printf("Failing..\n");
86
	*((&_heap) + 80000) = 10;
87
 
88
	printf("memory done\n");
89
}
90
/*
91
static void test_sbrk(void)
92
{
93
	printf("Writing to good memory\n");
94
	printf("Got: %P\n", sbrk(120000));
95
	printf("%P\n", ((char *)&_heap));
96
	printf("%P\n", ((char *)&_heap) + 80000);
97
	*(((char *)&_heap) + 80000) = 10;
98
	printf("Making small, got: %P\n",sbrk(-120000));
99
	printf("Testing access to heap\n");
100
	_heap = 10;
101
	printf("Failing..\n");
102
	*((&_heap) + 80000) = 10;
103
 
104
	printf("memory done\n");
105
}
106
*/
107
/*
108
static void test_malloc(void)
109
{
110
	char *data;
111
 
112
	data = malloc(10);
113
	printf("Heap: %P, data: %P\n", &_heap, data);
114
	data[0] = 'a';
115
	free(data);
116
}
117
*/
118
 
1028 palkovsky 119
 
120
static void test_ping(void)
121
{
122
	ipcarg_t result;
123
	int retval;
124
 
1091 palkovsky 125
	printf("Pinging\n");
1028 palkovsky 126
	retval = ipc_call_sync(PHONE_NS, NS_PING, 0xbeef,&result);
127
	printf("Retval: %d - received: %P\n", retval, result);
128
}
129
 
1091 palkovsky 130
static void got_answer(void *private, int retval, ipc_call_t *data)
999 palkovsky 131
{
1073 cejka 132
	printf("Retval: %d...%s...%zX, %zX\n", retval, private,
999 palkovsky 133
	       IPC_GET_ARG1(*data), IPC_GET_ARG2(*data));
134
}
135
static void test_async_ipc(void)
136
{
1028 palkovsky 137
	ipc_call_t data;
999 palkovsky 138
	int i;
139
 
140
	printf("Sending ping\n");
141
	ipc_call_async_2(PHONE_NS, NS_PING, 1, 0xbeefbee2,
142
			 "Pong1", got_answer);
143
	ipc_call_async_2(PHONE_NS, NS_PING, 2, 0xbeefbee4, 
144
			 "Pong2", got_answer);
145
	ipc_call_async_2(PHONE_NS, NS_PING, 3, 0xbeefbee4, 
146
			 "Pong3", got_answer);
147
	ipc_call_async_2(PHONE_NS, NS_PING, 4, 0xbeefbee4, 
148
			 "Pong4", got_answer);
149
	ipc_call_async_2(PHONE_NS, NS_PING, 5, 0xbeefbee4, 
150
			 "Pong5", got_answer);
151
	ipc_call_async_2(PHONE_NS, NS_PING, 6, 0xbeefbee4, 
152
			 "Pong6", got_answer);
153
	printf("Waiting forever...\n");
154
	for (i=0; i<100;i++)
155
		printf(".");
156
	printf("\n");
157
	ipc_wait_for_call(&data, NULL);
158
	printf("Received call???\n");
159
}
160
 
1028 palkovsky 161
 
1091 palkovsky 162
static void got_answer_2(void *private, int retval, ipc_call_t *data)
1028 palkovsky 163
{
164
	printf("Pong\n");
165
}
166
static void test_advanced_ipc(void)
167
{
168
	int res;
1091 palkovsky 169
	ipcarg_t phonead;
1028 palkovsky 170
	ipc_callid_t callid;
171
	ipc_call_t data;
1061 palkovsky 172
	int i;
1028 palkovsky 173
 
174
	printf("Asking 0 to connect to me...\n");
1091 palkovsky 175
	res = ipc_connect_to_me(0, 1, 2, &phonead);
176
	printf("Result: %d - phonead: %llu\n", res, phonead);
1061 palkovsky 177
	for (i=0; i < 100; i++) {
1028 palkovsky 178
		printf("----------------\n");
179
		ipc_call_async(PHONE_NS, NS_PING_SVC, 0, "prov",
180
			       got_answer_2);
181
		callid = ipc_wait_for_call(&data, NULL);
182
		printf("Received ping\n");
183
		ipc_answer(callid, 0, 0, 0);
1061 palkovsky 184
	}
1089 palkovsky 185
//	callid = ipc_wait_for_call(&data, NULL);
1028 palkovsky 186
}
187
 
1061 palkovsky 188
static void test_connection_ipc(void)
189
{
190
	int res;
191
	ipcarg_t result;
1091 palkovsky 192
	int phoneid;
1061 palkovsky 193
 
194
	printf("Starting connect...\n");
195
	res = ipc_connect_me_to(PHONE_NS, 10, 20);
196
	printf("Connected: %d\n", res);
197
	printf("pinging.\n");
198
	res = ipc_call_sync(res, NS_PING, 0xbeef,&result);
1089 palkovsky 199
	printf("Retval: %d - received: %X\n", res, result);
1061 palkovsky 200
 
201
}
202
 
1089 palkovsky 203
static void test_hangup(void)
204
{
205
	int phoneid;
206
	ipc_call_t data;
207
	ipc_callid_t callid;
208
	int i;
209
 
210
	printf("Starting connect...\n");
211
	phoneid = ipc_connect_me_to(PHONE_NS, 10, 20);
212
	printf("Phoneid: %d, pinging\n", phoneid);
213
	ipc_call_async_2(PHONE_NS, NS_PING, 1, 0xbeefbee2,
214
			 "Pong1", got_answer);
215
	printf("Hangin up\n");
216
	ipc_hangup(phoneid);
217
	printf("Connecting\n");
218
	phoneid = ipc_connect_me_to(PHONE_NS, 10, 20);
219
	printf("Newphid: %d\n", phoneid);
220
	for (i=0; i < 1000; i++) {
221
		if ((callid=ipc_wait_for_call(&data, IPC_WAIT_NONBLOCKING)))
222
			printf("callid: %d\n");
223
	}
224
	printf("New new phoneid: %d\n", ipc_connect_me_to(PHONE_NS, 10, 20));
225
}
226
 
227
static void test_slam(void)
228
{
229
	int i;
230
	ipc_call_t data;
231
	ipc_callid_t callid;
232
 
233
	printf("ping");
234
	ipc_call_async_2(PHONE_NS, NS_PING, 1, 0xbeefbee2,
235
			 "Pong1", got_answer);
236
	printf("slam");
237
	ipc_call_async_2(PHONE_NS, NS_HANGUP, 1, 0xbeefbee2,
238
			 "Hang", got_answer);
239
	printf("ping2\n");
240
	ipc_call_async_2(PHONE_NS, NS_PING, 1, 0xbeefbee2,
241
			 "Ping2", got_answer);
242
 
243
	for (i=0; i < 1000; i++) {
244
		if ((callid=ipc_wait_for_call(&data, IPC_WAIT_NONBLOCKING)))
245
			printf("callid: %d\n");
246
	}
247
	ipc_call_async_2(PHONE_NS, NS_PING, 1, 0xbeefbee2,
248
			 "Pong1", got_answer);
249
	printf("Closing file\n");
250
	ipc_hangup(PHONE_NS);
251
	ipc_call_async_2(PHONE_NS, NS_PING, 1, 0xbeefbee2,
252
			 "Pong1", got_answer);
253
	ipc_wait_for_call(&data, 0);
254
}
255
 
1113 palkovsky 256
static int ptest(void *arg)
257
{
258
	printf("Pseudo thread-1\n");
259
	ps_preempt();
260
	printf("Pseudo thread-2\n");
261
	ps_preempt();
262
	printf("Pseudo thread-3\n");
263
	ps_preempt();
264
	printf("Pseudo thread-4\n");
265
	ps_preempt();
266
	printf("Pseudo finish\n");
267
	return 0;	
268
}
1111 jermar 269
 
447 decky 270
int main(int argc, char *argv[])
271
{
1113 palkovsky 272
	pstid_t ptid;
1065 jermar 273
	int tid;
1113 palkovsky 274
 
713 decky 275
	version_print();
954 palkovsky 276
 
1125 jermar 277
//	test_printf();
1028 palkovsky 278
//	test_ping();
279
//	test_async_ipc();
1061 palkovsky 280
//	test_advanced_ipc();
1089 palkovsky 281
//	test_connection_ipc();
282
//	test_hangup();
1091 palkovsky 283
//	test_slam();
1111 jermar 284
 
285
	futex_initialize(&ftx, 1);
286
	if (futex_down(&ftx) < 0)
287
		printf("Futex failed.\n");
288
	if (futex_up(&ftx) < 0)
289
		printf("Futex failed.\n");
1089 palkovsky 290
 
1111 jermar 291
	if (futex_down(&ftx) < 0)
292
		printf("Futex failed.\n");
293
 
1125 jermar 294
	if ((tid = thread_create(utest, NULL, "utest")) != -1) {
1091 palkovsky 295
		printf("Created thread tid=%d\n", tid);
296
	}
1111 jermar 297
 
1125 jermar 298
	if ((tid = thread_create(utest, NULL, "utest")) != -1) {
299
		printf("Created thread tid=%d\n", tid);
300
	}
301
 
1111 jermar 302
	int i;
303
 
1125 jermar 304
	for (i = 0; i < 50000000; i++)
1111 jermar 305
		;
306
 
307
	if (futex_up(&ftx) < 0)
308
		printf("Futex failed.\n");
309
 
1113 palkovsky 310
	ptid = psthread_create(ptest, NULL);
1125 jermar 311
	printf("main thread-1\n");
1113 palkovsky 312
	ps_preempt();
1125 jermar 313
	printf("main thread-2\n");
1113 palkovsky 314
	ps_preempt();
315
	printf("main thread-3\n");
316
 
317
	ps_join(ptid);
318
	printf("Main exiting\n");
319
 
1111 jermar 320
	printf("Main thread exiting.\n");
447 decky 321
	return 0;
322
}