Semaphore.c 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #include "api.h"
  2. #include "pal.h"
  3. #include "pal_debug.h"
  4. void helper_timeout(PAL_NUM timeout) {
  5. /* Create a binary semaphore */
  6. PAL_HANDLE sem1 = DkMutexCreate(1);
  7. if (!sem1) {
  8. pal_printf("Failed to create a binary semaphore\n");
  9. return;
  10. }
  11. /* Wait on the binary semaphore with a timeout */
  12. PAL_BOL rv = DkSynchronizationObjectWait(sem1, timeout);
  13. if (rv == PAL_FALSE)
  14. pal_printf("Locked binary semaphore timed out (%ld).\n", timeout);
  15. else
  16. pal_printf("Acquired locked binary semaphore!?! sem1 is %p (%ld)\n", sem1, timeout);
  17. DkObjectClose(sem1);
  18. }
  19. void helper_success(PAL_NUM timeout) {
  20. /* Create a binary semaphore */
  21. PAL_HANDLE sem1 = DkMutexCreate(0);
  22. if (!sem1) {
  23. pal_printf("Failed to create a binary semaphore\n");
  24. return;
  25. }
  26. /* Wait on the binary semaphore with a timeout */
  27. PAL_BOL rv = DkSynchronizationObjectWait(sem1, timeout);
  28. if (rv == PAL_TRUE)
  29. pal_printf("Locked binary semaphore successfully (%ld).\n", timeout);
  30. else
  31. pal_printf("Failed to lock binary semaphore: sem1 is %p\n", sem1);
  32. DkObjectClose(sem1);
  33. }
  34. int main(int argc, char** argv, char** envp) {
  35. helper_timeout(1000);
  36. /* Try again with timeout 0 (trylock) */
  37. helper_timeout(0);
  38. /* Try cases that should succeed */
  39. helper_success(NO_TIMEOUT);
  40. helper_success(0);
  41. return 0;
  42. }