Thread.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /* -*- mode:c; c-file-style:"k&r"; c-basic-offset: 4; tab-width:4; indent-tabs-mode:nil; mode:auto-fill; fill-column:78; -*- */
  2. /* vim: set ts=4 sw=4 et tw=78 fo=cqt wm=0: */
  3. /* This Hello World demostrate a simple multithread program */
  4. #include "pal.h"
  5. #include "pal_debug.h"
  6. void * private1 = "Hello World 1";
  7. void * private2 = "Hello World 2";
  8. static volatile int count1 = 0;
  9. int callback1 (void * args)
  10. {
  11. pal_printf("Run in Child Thread: %s\n", (char *) args);
  12. while (count1 < 10) {
  13. while (!(count1 % 2))
  14. DkThreadYieldExecution();
  15. count1++;
  16. __asm__ volatile("nop" ::: "memory");
  17. }
  18. pal_printf("Threads Run in Parallel OK\n");
  19. DkSegmentRegister(PAL_SEGMENT_FS, &private2);
  20. const char * ptr2;
  21. __asm__ volatile("mov %%fs:0, %0" : "=r"(ptr2) :: "memory");
  22. pal_printf("Private Message (FS Segment) 2: %s\n", ptr2);
  23. count1 = 100;
  24. __asm__ volatile("nop" ::: "memory");
  25. DkThreadExit();
  26. count1 = 101;
  27. __asm__ volatile("nop" ::: "memory");
  28. return 0;
  29. }
  30. int main (int argc, const char ** argv, const char ** envp)
  31. {
  32. DkSegmentRegister(PAL_SEGMENT_FS, &private1);
  33. const char * ptr1;
  34. __asm__ volatile("mov %%fs:0, %0" : "=r"(ptr1) :: "memory");
  35. pal_printf("Private Message (FS Segment) 1: %s\n", ptr1);
  36. PAL_HANDLE thread1 = DkThreadCreate(callback1, "Hello World");
  37. if (thread1) {
  38. pal_printf("Child Thread Created\n");
  39. while (count1 < 10) {
  40. while (!!(count1 % 2))
  41. DkThreadYieldExecution();
  42. count1++;
  43. __asm__ volatile("nop" ::: "memory");
  44. }
  45. while (count1 < 100)
  46. DkThreadYieldExecution();
  47. for (int i = 0 ; i < 300 ; i++)
  48. DkThreadYieldExecution();
  49. if (count1 == 100)
  50. pal_printf("Child Thread Exited\n");
  51. }
  52. return 0;
  53. }