Rev 1129 | Rev 1175 | 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 | |
1152 | jermar | 42 | int __thread stage; |
1129 | palkovsky | 43 | |
1065 | jermar | 44 | extern void utest(void *arg); |
45 | void utest(void *arg) |
||
46 | { |
||
1081 | jermar | 47 | printf("Uspace thread started.\n"); |
1111 | jermar | 48 | if (futex_down(&ftx) < 0) |
49 | printf("Futex failed.\n"); |
||
50 | if (futex_up(&ftx) < 0) |
||
51 | printf("Futex failed.\n"); |
||
52 | |||
53 | printf("%s in good condition.\n", __FUNCTION__); |
||
54 | |||
1065 | jermar | 55 | for (;;) |
56 | ; |
||
57 | } |
||
1049 | cejka | 58 | |
995 | cejka | 59 | static void test_printf(void) |
60 | { |
||
61 | printf("Simple text.\n"); |
||
62 | printf("Now insert '%s' string.\n","this"); |
||
1073 | cejka | 63 | printf("Signed formats on uns. numbers: '%d', '%+d', '% d', '%u' (,+, ,u)\n", 321, 321, 321, 321); |
64 | printf("Signed formats on sig. numbers: '%d', '%+d', '% d', '%u' (,+, ,u)\n", -321, -321, -321, -321); |
||
65 | printf("Signed with different sized: '%hhd', '%hd', '%d', '%ld', %lld;\n", -3, -32, -321, -32101l, -3210123ll); |
||
66 | printf("And now... '%hhd' byte! '%hd' word! '%d' int! \n", 11, 11111, 1111111111); |
||
67 | printf("Different bases: %#hx, %#hu, %#ho and %#hb\n", 123, 123, 123, 123); |
||
68 | printf("Different bases signed: %#hx, %#hu, %#ho and %#hb\n", -123, -123, -123, -123); |
||
69 | printf("'%llX' llX! Another '%llx' llx! \n", 0x1234567887654321ll, 0x1234567887654321ll); |
||
70 | printf("'%llX' with 64bit value and '%x' with 32 bit value. \n", 0x1234567887654321ll, 0x12345678 ); |
||
71 | printf("'%llx' 64bit, '%x' 32bit, '%hhx' 8bit, '%hx' 16bit, '%llX' 64bit and '%s' string.\n", 0x1234567887654321ll, 0x12345678, 0x12, 0x1234, 0x1234567887654321ull, "Lovely string" ); |
||
1049 | cejka | 72 | |
995 | cejka | 73 | printf("Thats all, folks!\n"); |
74 | } |
||
999 | palkovsky | 75 | |
1049 | cejka | 76 | |
1028 | palkovsky | 77 | extern char _heap; |
994 | jermar | 78 | static void test_mremap(void) |
988 | palkovsky | 79 | { |
80 | printf("Writing to good memory\n"); |
||
81 | mremap(&_heap, 120000, 0); |
||
82 | printf("%P\n", ((char *)&_heap)); |
||
83 | printf("%P\n", ((char *)&_heap) + 80000); |
||
84 | *(((char *)&_heap) + 80000) = 10; |
||
85 | printf("Making small\n"); |
||
86 | mremap(&_heap, 16000, 0); |
||
87 | printf("Failing..\n"); |
||
88 | *((&_heap) + 80000) = 10; |
||
89 | |||
90 | printf("memory done\n"); |
||
91 | } |
||
92 | /* |
||
93 | static void test_sbrk(void) |
||
94 | { |
||
95 | printf("Writing to good memory\n"); |
||
96 | printf("Got: %P\n", sbrk(120000)); |
||
97 | printf("%P\n", ((char *)&_heap)); |
||
98 | printf("%P\n", ((char *)&_heap) + 80000); |
||
99 | *(((char *)&_heap) + 80000) = 10; |
||
100 | printf("Making small, got: %P\n",sbrk(-120000)); |
||
101 | printf("Testing access to heap\n"); |
||
102 | _heap = 10; |
||
103 | printf("Failing..\n"); |
||
104 | *((&_heap) + 80000) = 10; |
||
105 | |||
106 | printf("memory done\n"); |
||
107 | } |
||
108 | */ |
||
109 | /* |
||
110 | static void test_malloc(void) |
||
111 | { |
||
112 | char *data; |
||
113 | |||
114 | data = malloc(10); |
||
115 | printf("Heap: %P, data: %P\n", &_heap, data); |
||
116 | data[0] = 'a'; |
||
117 | free(data); |
||
118 | } |
||
119 | */ |
||
120 | |||
1028 | palkovsky | 121 | |
122 | static void test_ping(void) |
||
123 | { |
||
124 | ipcarg_t result; |
||
125 | int retval; |
||
126 | |||
1091 | palkovsky | 127 | printf("Pinging\n"); |
1028 | palkovsky | 128 | retval = ipc_call_sync(PHONE_NS, NS_PING, 0xbeef,&result); |
129 | printf("Retval: %d - received: %P\n", retval, result); |
||
130 | } |
||
131 | |||
1091 | palkovsky | 132 | static void got_answer(void *private, int retval, ipc_call_t *data) |
999 | palkovsky | 133 | { |
1073 | cejka | 134 | printf("Retval: %d...%s...%zX, %zX\n", retval, private, |
999 | palkovsky | 135 | IPC_GET_ARG1(*data), IPC_GET_ARG2(*data)); |
136 | } |
||
137 | static void test_async_ipc(void) |
||
138 | { |
||
1028 | palkovsky | 139 | ipc_call_t data; |
999 | palkovsky | 140 | int i; |
141 | |||
142 | printf("Sending ping\n"); |
||
143 | ipc_call_async_2(PHONE_NS, NS_PING, 1, 0xbeefbee2, |
||
144 | "Pong1", got_answer); |
||
145 | ipc_call_async_2(PHONE_NS, NS_PING, 2, 0xbeefbee4, |
||
146 | "Pong2", got_answer); |
||
147 | ipc_call_async_2(PHONE_NS, NS_PING, 3, 0xbeefbee4, |
||
148 | "Pong3", got_answer); |
||
149 | ipc_call_async_2(PHONE_NS, NS_PING, 4, 0xbeefbee4, |
||
150 | "Pong4", got_answer); |
||
151 | ipc_call_async_2(PHONE_NS, NS_PING, 5, 0xbeefbee4, |
||
152 | "Pong5", got_answer); |
||
153 | ipc_call_async_2(PHONE_NS, NS_PING, 6, 0xbeefbee4, |
||
154 | "Pong6", got_answer); |
||
155 | printf("Waiting forever...\n"); |
||
156 | for (i=0; i<100;i++) |
||
157 | printf("."); |
||
158 | printf("\n"); |
||
159 | ipc_wait_for_call(&data, NULL); |
||
160 | printf("Received call???\n"); |
||
161 | } |
||
162 | |||
1028 | palkovsky | 163 | |
1091 | palkovsky | 164 | static void got_answer_2(void *private, int retval, ipc_call_t *data) |
1028 | palkovsky | 165 | { |
166 | printf("Pong\n"); |
||
167 | } |
||
168 | static void test_advanced_ipc(void) |
||
169 | { |
||
170 | int res; |
||
1091 | palkovsky | 171 | ipcarg_t phonead; |
1028 | palkovsky | 172 | ipc_callid_t callid; |
173 | ipc_call_t data; |
||
1061 | palkovsky | 174 | int i; |
1028 | palkovsky | 175 | |
176 | printf("Asking 0 to connect to me...\n"); |
||
1091 | palkovsky | 177 | res = ipc_connect_to_me(0, 1, 2, &phonead); |
178 | printf("Result: %d - phonead: %llu\n", res, phonead); |
||
1061 | palkovsky | 179 | for (i=0; i < 100; i++) { |
1028 | palkovsky | 180 | printf("----------------\n"); |
181 | ipc_call_async(PHONE_NS, NS_PING_SVC, 0, "prov", |
||
182 | got_answer_2); |
||
183 | callid = ipc_wait_for_call(&data, NULL); |
||
184 | printf("Received ping\n"); |
||
185 | ipc_answer(callid, 0, 0, 0); |
||
1061 | palkovsky | 186 | } |
1089 | palkovsky | 187 | // callid = ipc_wait_for_call(&data, NULL); |
1028 | palkovsky | 188 | } |
189 | |||
1061 | palkovsky | 190 | static void test_connection_ipc(void) |
191 | { |
||
192 | int res; |
||
193 | ipcarg_t result; |
||
1091 | palkovsky | 194 | int phoneid; |
1061 | palkovsky | 195 | |
196 | printf("Starting connect...\n"); |
||
197 | res = ipc_connect_me_to(PHONE_NS, 10, 20); |
||
198 | printf("Connected: %d\n", res); |
||
199 | printf("pinging.\n"); |
||
200 | res = ipc_call_sync(res, NS_PING, 0xbeef,&result); |
||
1089 | palkovsky | 201 | printf("Retval: %d - received: %X\n", res, result); |
1061 | palkovsky | 202 | |
203 | } |
||
204 | |||
1089 | palkovsky | 205 | static void test_hangup(void) |
206 | { |
||
207 | int phoneid; |
||
208 | ipc_call_t data; |
||
209 | ipc_callid_t callid; |
||
210 | int i; |
||
211 | |||
212 | printf("Starting connect...\n"); |
||
213 | phoneid = ipc_connect_me_to(PHONE_NS, 10, 20); |
||
214 | printf("Phoneid: %d, pinging\n", phoneid); |
||
215 | ipc_call_async_2(PHONE_NS, NS_PING, 1, 0xbeefbee2, |
||
216 | "Pong1", got_answer); |
||
217 | printf("Hangin up\n"); |
||
218 | ipc_hangup(phoneid); |
||
219 | printf("Connecting\n"); |
||
220 | phoneid = ipc_connect_me_to(PHONE_NS, 10, 20); |
||
221 | printf("Newphid: %d\n", phoneid); |
||
222 | for (i=0; i < 1000; i++) { |
||
223 | if ((callid=ipc_wait_for_call(&data, IPC_WAIT_NONBLOCKING))) |
||
224 | printf("callid: %d\n"); |
||
225 | } |
||
226 | printf("New new phoneid: %d\n", ipc_connect_me_to(PHONE_NS, 10, 20)); |
||
227 | } |
||
228 | |||
229 | static void test_slam(void) |
||
230 | { |
||
231 | int i; |
||
232 | ipc_call_t data; |
||
233 | ipc_callid_t callid; |
||
234 | |||
235 | printf("ping"); |
||
236 | ipc_call_async_2(PHONE_NS, NS_PING, 1, 0xbeefbee2, |
||
237 | "Pong1", got_answer); |
||
238 | printf("slam"); |
||
239 | ipc_call_async_2(PHONE_NS, NS_HANGUP, 1, 0xbeefbee2, |
||
240 | "Hang", got_answer); |
||
241 | printf("ping2\n"); |
||
242 | ipc_call_async_2(PHONE_NS, NS_PING, 1, 0xbeefbee2, |
||
243 | "Ping2", got_answer); |
||
244 | |||
245 | for (i=0; i < 1000; i++) { |
||
246 | if ((callid=ipc_wait_for_call(&data, IPC_WAIT_NONBLOCKING))) |
||
247 | printf("callid: %d\n"); |
||
248 | } |
||
249 | ipc_call_async_2(PHONE_NS, NS_PING, 1, 0xbeefbee2, |
||
250 | "Pong1", got_answer); |
||
251 | printf("Closing file\n"); |
||
252 | ipc_hangup(PHONE_NS); |
||
253 | ipc_call_async_2(PHONE_NS, NS_PING, 1, 0xbeefbee2, |
||
254 | "Pong1", got_answer); |
||
255 | ipc_wait_for_call(&data, 0); |
||
256 | } |
||
257 | |||
1113 | palkovsky | 258 | static int ptest(void *arg) |
259 | { |
||
1152 | jermar | 260 | stage = 1; |
261 | printf("Pseudo thread stage%d.\n", stage); |
||
262 | stage++; |
||
1128 | jermar | 263 | psthread_schedule_next(); |
1152 | jermar | 264 | printf("Pseudo thread stage%d.\n", stage); |
265 | stage++; |
||
1128 | jermar | 266 | psthread_schedule_next(); |
1152 | jermar | 267 | printf("Pseudo thread stage%d.\n", stage); |
1128 | jermar | 268 | psthread_schedule_next(); |
1152 | jermar | 269 | stage++; |
270 | printf("Pseudo thread stage%d.\n", stage); |
||
1128 | jermar | 271 | psthread_schedule_next(); |
272 | printf("Pseudo thread exiting.\n"); |
||
1113 | palkovsky | 273 | return 0; |
274 | } |
||
1111 | jermar | 275 | |
447 | decky | 276 | int main(int argc, char *argv[]) |
277 | { |
||
1113 | palkovsky | 278 | pstid_t ptid; |
1065 | jermar | 279 | int tid; |
1113 | palkovsky | 280 | |
713 | decky | 281 | version_print(); |
954 | palkovsky | 282 | |
1125 | jermar | 283 | // test_printf(); |
1028 | palkovsky | 284 | // test_ping(); |
285 | // test_async_ipc(); |
||
1061 | palkovsky | 286 | // test_advanced_ipc(); |
1089 | palkovsky | 287 | // test_connection_ipc(); |
288 | // test_hangup(); |
||
1091 | palkovsky | 289 | // test_slam(); |
1111 | jermar | 290 | |
291 | futex_initialize(&ftx, 1); |
||
292 | if (futex_down(&ftx) < 0) |
||
293 | printf("Futex failed.\n"); |
||
294 | if (futex_up(&ftx) < 0) |
||
295 | printf("Futex failed.\n"); |
||
1089 | palkovsky | 296 | |
1111 | jermar | 297 | if (futex_down(&ftx) < 0) |
298 | printf("Futex failed.\n"); |
||
1152 | jermar | 299 | |
1125 | jermar | 300 | if ((tid = thread_create(utest, NULL, "utest")) != -1) { |
1091 | palkovsky | 301 | printf("Created thread tid=%d\n", tid); |
302 | } |
||
1111 | jermar | 303 | |
1125 | jermar | 304 | if ((tid = thread_create(utest, NULL, "utest")) != -1) { |
305 | printf("Created thread tid=%d\n", tid); |
||
306 | } |
||
1152 | jermar | 307 | |
1111 | jermar | 308 | int i; |
309 | |||
1125 | jermar | 310 | for (i = 0; i < 50000000; i++) |
1111 | jermar | 311 | ; |
312 | |||
313 | if (futex_up(&ftx) < 0) |
||
314 | printf("Futex failed.\n"); |
||
315 | |||
1129 | palkovsky | 316 | |
1152 | jermar | 317 | printf("Creating pseudo thread.\n"); |
318 | stage = 1; |
||
1113 | palkovsky | 319 | ptid = psthread_create(ptest, NULL); |
1152 | jermar | 320 | printf("Main thread stage%d.\n", stage); |
321 | stage++; |
||
1128 | jermar | 322 | psthread_schedule_next();; |
1152 | jermar | 323 | printf("Main thread stage%d.\n", stage); |
324 | stage++; |
||
1128 | jermar | 325 | psthread_schedule_next();; |
1152 | jermar | 326 | printf("Main thread stage%d.\n", stage); |
1113 | palkovsky | 327 | |
1128 | jermar | 328 | psthread_join(ptid); |
1113 | palkovsky | 329 | |
1111 | jermar | 330 | printf("Main thread exiting.\n"); |
447 | decky | 331 | return 0; |
332 | } |