Subversion Repositories HelenOS-historic

Rev

Rev 1129 | Rev 1197 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  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.  
  29. #include "version.h"
  30. #include <ipc.h>
  31. #include <stdio.h>
  32. #include <unistd.h>
  33. #include <stdlib.h>
  34. #include <ns.h>
  35. #include <thread.h>
  36. #include <psthread.h>
  37. #include <futex.h>
  38.  
  39. int a;
  40. atomic_t ftx;
  41.  
  42. int __thread stage;
  43.  
  44. extern void utest(void *arg);
  45. void utest(void *arg)
  46. {
  47.     printf("Uspace thread started.\n");
  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.    
  55.     for (;;)
  56.         ;
  57. }
  58.  
  59. static void test_printf(void)
  60. {
  61.     printf("Simple text.\n");
  62.     printf("Now insert '%s' string.\n","this");
  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" );
  72.    
  73.     printf("Thats all, folks!\n");
  74. }
  75.  
  76.  
  77. extern char _heap;
  78. static void test_mremap(void)
  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.  
  121.  
  122. static void test_ping(void)
  123. {
  124.     ipcarg_t result;
  125.     int retval;
  126.  
  127.     printf("Pinging\n");
  128.     retval = ipc_call_sync(PHONE_NS, NS_PING, 0xbeef,&result);
  129.     printf("Retval: %d - received: %P\n", retval, result);
  130. }
  131.  
  132. static void got_answer(void *private, int retval, ipc_call_t *data)
  133. {
  134.     printf("Retval: %d...%s...%zX, %zX\n", retval, private,
  135.            IPC_GET_ARG1(*data), IPC_GET_ARG2(*data));
  136. }
  137. static void test_async_ipc(void)
  138. {
  139.     ipc_call_t data;
  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.  
  163.  
  164. static void got_answer_2(void *private, int retval, ipc_call_t *data)
  165. {
  166.     printf("Pong\n");
  167. }
  168. static void test_advanced_ipc(void)
  169. {
  170.     int res;
  171.     ipcarg_t phonead;
  172.     ipc_callid_t callid;
  173.     ipc_call_t data;
  174.     int i;
  175.  
  176.     printf("Asking 0 to connect to me...\n");
  177.     res = ipc_connect_to_me(0, 1, 2, &phonead);
  178.     printf("Result: %d - phonead: %llu\n", res, phonead);
  179.     for (i=0; i < 100; i++) {
  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);
  186.     }
  187. //  callid = ipc_wait_for_call(&data, NULL);
  188. }
  189.  
  190. static void test_connection_ipc(void)
  191. {
  192.     int res;
  193.     ipcarg_t result;
  194.     int phoneid;
  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);
  201.     printf("Retval: %d - received: %X\n", res, result);
  202.    
  203. }
  204.  
  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.  
  258. static int ptest(void *arg)
  259. {
  260.     stage = 1;
  261.     printf("Pseudo thread stage%d.\n", stage);
  262.     stage++;
  263.     psthread_schedule_next();
  264.     printf("Pseudo thread stage%d.\n", stage);
  265.     stage++;
  266.     psthread_schedule_next();
  267.     printf("Pseudo thread stage%d.\n", stage);
  268.     psthread_schedule_next();
  269.     stage++;
  270.     printf("Pseudo thread stage%d.\n", stage);
  271.     psthread_schedule_next();
  272.     printf("Pseudo thread exiting.\n");
  273.     return 0;  
  274. }
  275.  
  276. int main(int argc, char *argv[])
  277. {
  278.     pstid_t ptid;
  279.     int tid;
  280.  
  281.     version_print();
  282.  
  283. //  test_printf();
  284. //  test_ping();
  285. //  test_async_ipc();
  286. //  test_advanced_ipc();
  287. //  test_connection_ipc();
  288. //  test_hangup();
  289. //  test_slam();
  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");
  296.  
  297.     if (futex_down(&ftx) < 0)
  298.         printf("Futex failed.\n");
  299.  
  300.     if ((tid = thread_create(utest, NULL, "utest")) != -1) {
  301.         printf("Created thread tid=%d\n", tid);
  302.     }
  303.  
  304.     if ((tid = thread_create(utest, NULL, "utest")) != -1) {
  305.         printf("Created thread tid=%d\n", tid);
  306.     }
  307.  
  308.     int i;
  309.    
  310.     for (i = 0; i < 50000000; i++)
  311.         ;
  312.        
  313.     if (futex_up(&ftx) < 0)
  314.         printf("Futex failed.\n");
  315.  
  316.  
  317.     printf("Creating pseudo thread.\n");
  318.     stage = 1;
  319.     ptid = psthread_create(ptest, NULL);
  320.     printf("Main thread stage%d.\n", stage);
  321.     stage++;
  322.     psthread_schedule_next();;
  323.     printf("Main thread stage%d.\n", stage);
  324.     stage++;
  325.     psthread_schedule_next();;
  326.     printf("Main thread stage%d.\n", stage);
  327.  
  328.     psthread_join(ptid);
  329.  
  330.     printf("Main thread exiting.\n");
  331.     return 0;
  332. }
  333.