Subversion Repositories Kolibri OS

Rev

Rev 9137 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. /*
  2.  * An example of using threads to create a copy of a window.
  3.  * Built on top of the /programs/develop/examples/thread/trunk/thread.asm example.
  4.  *
  5.  * Created by turbocat (Maxim Logaev) 2021.
  6.  */
  7.  
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <sys/ksys.h>
  11.  
  12. #define TH_STACK_SIZE 1024
  13.  
  14. enum BUTTONS {
  15.     BTN_QUIT = 1,
  16.     BTN_CREATE_TH = 2,
  17. };
  18.  
  19. ksys_colors_table_t sys_colors;
  20.  
  21. extern int main();
  22.  
  23. void redraw_window(void)
  24. {
  25.     ksys_pos_t mouse_pos = _ksys_get_mouse_pos(KSYS_MOUSE_SCREEN_POS);
  26.     _ksys_start_draw();
  27.     _ksys_create_window(mouse_pos.x, mouse_pos.y, 140, 60, "Threads", sys_colors.work_area, 0x14);
  28.     _ksys_define_button(10, 30, 120, 20, BTN_CREATE_TH, sys_colors.work_button);
  29.     _ksys_draw_text("Create thread!", 15, 34, 0, 0x90000000 | sys_colors.work_button_text);
  30.     _ksys_end_draw();
  31. }
  32.  
  33. void create_thread(void)
  34. {
  35.     unsigned tid;                           // New thread ID
  36.     void* th_stack = malloc(TH_STACK_SIZE); // Allocate memory for thread stack
  37.     if (!th_stack) {
  38.         _ksys_debug_puts("Memory allocation error for thread!");
  39.         return;
  40.     }
  41.     tid = _ksys_create_thread(main, th_stack + TH_STACK_SIZE); // Create new thread with entry "main"
  42.     if (tid == -1) {
  43.         _ksys_debug_puts("Unable to create a new thread!");
  44.         return;
  45.     }
  46.     debug_printf("New thread created (TID=%u)\n", tid);
  47. }
  48.  
  49. int main()
  50. {
  51.     _ksys_get_system_colors(&sys_colors);
  52.     int gui_event;
  53.     redraw_window();
  54.  
  55.     while (1) {
  56.         gui_event = _ksys_get_event();
  57.         switch (gui_event) {
  58.         case KSYS_EVENT_REDRAW:
  59.             redraw_window();
  60.             break;
  61.         case KSYS_EVENT_BUTTON:
  62.             switch (_ksys_get_button()) {
  63.             case BTN_CREATE_TH:
  64.                 create_thread();
  65.                 break;
  66.             case BTN_QUIT:
  67.                 _ksys_exit();
  68.             }
  69.             break;
  70.         }
  71.     }
  72. }
  73.