Semaphore.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /* -*- mode:c; c-file-style:"k&r"; c-basic-offset: 4; tab-width:4; indent-tabs-mode:nil; mode:auto-fill; fill-column:78; -*- */
  2. /* vim: set ts=4 sw=4 et tw=78 fo=cqt wm=0: */
  3. #include "pal.h"
  4. #include "pal_debug.h"
  5. #include "api.h"
  6. void helper_timeout(PAL_NUM timeout) {
  7. /* Create a binary semaphore */
  8. PAL_HANDLE sem1 = DkSemaphoreCreate(1, 1);
  9. if(!sem1) {
  10. pal_printf("Failed to create a binary semaphore\n");
  11. return;
  12. }
  13. /* Wait on the binary semaphore with a timeout */
  14. PAL_HANDLE rv = DkObjectsWaitAny(1, &sem1, timeout);
  15. if (rv == NULL)
  16. pal_printf("Locked binary semaphore timed out (%d).\n", timeout);
  17. else
  18. pal_printf("Acquired locked binary semaphore!?! Got back %p; sem1 is %p (%d)\n", rv, sem1, timeout);
  19. PAL_HANDLE sem2 = DkSemaphoreCreate(2, 2);
  20. if(!sem2) {
  21. pal_printf("Failed to create a non-binary semaphore\n");
  22. return;
  23. }
  24. /* Wait on the non-binary semaphore with a timeout */
  25. rv = DkObjectsWaitAny(1, &sem2, timeout);
  26. if (rv == NULL)
  27. pal_printf("Locked non-binary semaphore timed out (%d).\n", timeout);
  28. else
  29. pal_printf("Acquired locked non-binary semaphore!?! Got back %p; sem2 is %p (%d)\n", rv, sem2, timeout);
  30. /* Try waiting on both */
  31. PAL_HANDLE hdls[2];
  32. hdls[0] = sem1;
  33. hdls[1] = sem2;
  34. rv = DkObjectsWaitAny(2, hdls, timeout);
  35. if (rv == NULL)
  36. pal_printf("Two locked semaphores timed out (%d).\n", timeout);
  37. else
  38. pal_printf("Somehow locked one of two locked semaphore handles? %p (%d)\n", rv, timeout);
  39. DkObjectClose(sem1);
  40. DkObjectClose(sem2);
  41. }
  42. void helper_success(PAL_NUM timeout) {
  43. /* Create a binary semaphore */
  44. PAL_HANDLE sem1 = DkSemaphoreCreate(0, 1);
  45. if(!sem1) {
  46. pal_printf("Failed to create a binary semaphore\n");
  47. return;
  48. }
  49. /* Wait on the binary semaphore with a timeout */
  50. PAL_HANDLE rv = DkObjectsWaitAny(1, &sem1, timeout);
  51. if (rv == sem1)
  52. pal_printf("Locked binary semaphore successfully (%d).\n", timeout);
  53. else
  54. pal_printf("Failed to lock binary semaphore: Got back %p; sem1 is %p\n", rv, sem1);
  55. PAL_HANDLE sem2 = DkSemaphoreCreate(0, 2);
  56. if(!sem2) {
  57. pal_printf("Failed to create a non-binary semaphore\n");
  58. return;
  59. }
  60. /* Wait on the non-binary semaphore with a timeout */
  61. rv = DkObjectsWaitAny(1, &sem2, timeout);
  62. if (rv == sem2)
  63. pal_printf("Locked non-binary semaphore successfully (%d).\n", timeout);
  64. else
  65. pal_printf("Failed to lock non-binary semaphore Got back %p; sem2 is %p\n", rv, sem2);
  66. DkObjectClose(sem1);
  67. DkObjectClose(sem2);
  68. }
  69. int main (int argc, char ** argv, char ** envp)
  70. {
  71. helper_timeout(1000);
  72. /* Try again with timeout 0 (trylock) */
  73. helper_timeout(0);
  74. /* Try cases that should succeed */
  75. helper_success(NO_TIMEOUT);
  76. helper_success(0);
  77. return 0;
  78. }