Event.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #include <stdatomic.h>
  2. #include "pal.h"
  3. #include "pal_debug.h"
  4. #include "pal_error.h"
  5. static PAL_HANDLE event1;
  6. atomic_int timeouts = 0;
  7. int thread2_run(void* args) {
  8. pal_printf("Second thread started.\n");
  9. DkThreadDelayExecution(3000000);
  10. pal_printf("Sending event...\n");
  11. DkEventSet(event1);
  12. pal_printf("End of second thread.\n");
  13. DkThreadExit(/*clear_child_tid=*/NULL);
  14. return 0;
  15. }
  16. void pal_failure_handler(PAL_PTR event, PAL_NUM error, PAL_CONTEXT* context) {
  17. pal_printf("pal_failure_handler called\n");
  18. if (error == PAL_ERROR_TRYAGAIN) {
  19. pal_printf("Timeout event received.\n");
  20. timeouts += 1;
  21. }
  22. DkExceptionReturn(event);
  23. }
  24. int main() {
  25. pal_printf("Started main thread.\n");
  26. DkSetExceptionHandler(pal_failure_handler, PAL_EVENT_FAILURE);
  27. event1 = DkNotificationEventCreate(0);
  28. if (event1 == NULL) {
  29. pal_printf("DkNotificationEventCreate failed\n");
  30. return 1;
  31. }
  32. PAL_HANDLE thread2 = DkThreadCreate(thread2_run, NULL);
  33. if (thread2 == NULL) {
  34. pal_printf("DkThreadCreate failed\n");
  35. return 1;
  36. }
  37. unsigned long t_start = DkSystemTimeQuery();
  38. pal_printf("Testing wait with too short timeout...\n");
  39. DkSynchronizationObjectWait(event1, 1000000);
  40. unsigned long t_wait1 = DkSystemTimeQuery();
  41. unsigned long dt_wait1 = t_wait1 - t_start;
  42. pal_printf("Wait returned after %lu us.\n", dt_wait1);
  43. pal_printf("Timeout count: %d\n", timeouts);
  44. if (dt_wait1 > 1000000 && dt_wait1 < 1100000 && timeouts == 1) {
  45. pal_printf("Wait with too short timeout ok.\n");
  46. }
  47. pal_printf("Testing wait with long enough timeout...\n");
  48. DkSynchronizationObjectWait(event1, 5000000);
  49. unsigned long t_wait2 = DkSystemTimeQuery();
  50. unsigned long dt_wait2 = t_wait2 - t_start;
  51. pal_printf("Wait returned after %lu us since start.\n", dt_wait2);
  52. pal_printf("Timeout count: %d\n", timeouts);
  53. if (dt_wait2 > 3000000 && dt_wait2 < 3100000 && timeouts == 1) {
  54. pal_printf("Wait with long enough timeout ok.\n");
  55. }
  56. pal_printf("End of main thread.\n");
  57. return 0;
  58. }