Wait.c 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #include "pal.h"
  2. #include "pal_debug.h"
  3. PAL_HANDLE event1;
  4. PAL_HANDLE event2;
  5. int thread1_func(void* args) {
  6. pal_printf("Enter thread 1\n");
  7. DkThreadDelayExecution(3000);
  8. DkEventSet(event1);
  9. pal_printf("Leave thread 1\n");
  10. return 0;
  11. }
  12. int thread2_func(void* args) {
  13. pal_printf("Enter thread 2\n");
  14. DkThreadDelayExecution(5000);
  15. DkEventSet(event2);
  16. pal_printf("Leave thread 2\n");
  17. return 0;
  18. }
  19. int main(int argc, char** argv) {
  20. pal_printf("Enter Main Thread\n");
  21. PAL_HANDLE thd1;
  22. PAL_HANDLE thd2;
  23. event1 = DkNotificationEventCreate(0);
  24. event2 = DkNotificationEventCreate(0);
  25. thd1 = DkThreadCreate(&thread1_func, 0);
  26. if (!thd1) {
  27. pal_printf("DkThreadCreate failed\n");
  28. return -1;
  29. }
  30. thd2 = DkThreadCreate(&thread2_func, 0);
  31. if (!thd2) {
  32. pal_printf("DkThreadCreate failed\n");
  33. return -1;
  34. }
  35. PAL_HANDLE hdls[2] = {event1, event2};
  36. PAL_FLG events[2] = {PAL_WAIT_READ, PAL_WAIT_READ};
  37. PAL_FLG revents[2] = {0, 0};
  38. PAL_BOL polled = DkStreamsWaitEvents(2, hdls, events, revents, NO_TIMEOUT);
  39. if (!polled) {
  40. pal_printf("DkStreamsWaitEvents did not return any events\n");
  41. return -1;
  42. }
  43. for (int i = 0; i < 2; i++) {
  44. if (revents[i])
  45. pal_printf("event%d is set\n", i + 1);
  46. }
  47. pal_printf("Leave main thread\n");
  48. return 0;
  49. }