Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | Download | RSS feed

  1. #include <sys/ksys.h>
  2.  
  3. /* Very simple example of working with futexes in Kolibri OS.
  4.  * Author turbocat (Maxim Logaev).
  5.  * Thanks to Vitaly Krylov for help.
  6.  *
  7.  * The result of the program execution can be seen in the debug board.
  8. */
  9.  
  10. #define TH_STACK_SIZE 1024
  11. #define TH_LOCK       1
  12. #define TH_UNLOCK     0
  13.  
  14. uint8_t th_stack[TH_STACK_SIZE];
  15.  
  16. int glob_var = 0;
  17. int th_lock = TH_UNLOCK;
  18. uint32_t futex = 0;
  19.  
  20. void th_main(void)
  21. {
  22.     _ksys_debug_puts("Child thread start\n");
  23.     _ksys_futex_wait(futex, TH_LOCK, 0);
  24.     glob_var = 99;
  25.     _ksys_debug_puts("Child thread end\n");
  26.     _ksys_exit();
  27. }
  28.  
  29. int main(void)
  30. {
  31.     _ksys_debug_puts("Parrent thread start");
  32.     futex = _ksys_futex_create(&th_lock);
  33.     th_lock = TH_LOCK;
  34.  
  35.     if (_ksys_create_thread(th_main, th_stack) == -1) {
  36.         _ksys_debug_puts("Unable to create a new thread!\n");
  37.         return 1;
  38.     }
  39.  
  40.     glob_var = 88;
  41.     _ksys_thread_yield();
  42.  
  43.     _ksys_delay(100);
  44.  
  45.     if (glob_var == 88) {
  46.         _ksys_debug_puts("Futex test OK :)\n");
  47.     } else {
  48.         _ksys_debug_puts("Futex test FAIL :(\n");
  49.     }
  50.  
  51.     th_lock = TH_UNLOCK;
  52.     _ksys_futex_wake(futex, 1);
  53.  
  54.     _ksys_futex_destroy(futex);
  55.     _ksys_debug_puts("Parrent thread end");
  56.     return 0;
  57. }
  58.