Thread.c 1.7 KB

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