Semaphore.c 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #include "pal.h"
  2. #include "pal_debug.h"
  3. #include "api.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_HANDLE rv = DkObjectsWaitAny(1, &sem1, timeout);
  13. if (rv == NULL)
  14. pal_printf("Locked binary semaphore timed out (%ld).\n", timeout);
  15. else
  16. pal_printf("Acquired locked binary semaphore!?! Got back %p; sem1 is %p (%ld)\n", rv, 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_HANDLE rv = DkObjectsWaitAny(1, &sem1, timeout);
  28. if (rv == sem1)
  29. pal_printf("Locked binary semaphore successfully (%ld).\n", timeout);
  30. else
  31. pal_printf("Failed to lock binary semaphore: Got back %p; sem1 is %p\n", rv, sem1);
  32. DkObjectClose(sem1);
  33. }
  34. int main (int argc, char ** argv, char ** envp)
  35. {
  36. helper_timeout(1000);
  37. /* Try again with timeout 0 (trylock) */
  38. helper_timeout(0);
  39. /* Try cases that should succeed */
  40. helper_success(NO_TIMEOUT);
  41. helper_success(0);
  42. return 0;
  43. }