monref.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. /*
  6. * This test program demonstrates that PR_ExitMonitor needs to add a
  7. * reference to the PRMonitor object before unlocking the internal
  8. * mutex.
  9. */
  10. #include "prlog.h"
  11. #include "prmon.h"
  12. #include "prthread.h"
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. /* Protected by the PRMonitor 'mon' in the main function. */
  16. static PRBool done = PR_FALSE;
  17. static void ThreadFunc(void *arg)
  18. {
  19. PRMonitor *mon = (PRMonitor *)arg;
  20. PRStatus rv;
  21. PR_EnterMonitor(mon);
  22. done = PR_TRUE;
  23. rv = PR_Notify(mon);
  24. PR_ASSERT(rv == PR_SUCCESS);
  25. rv = PR_ExitMonitor(mon);
  26. PR_ASSERT(rv == PR_SUCCESS);
  27. }
  28. int main()
  29. {
  30. PRMonitor *mon;
  31. PRThread *thread;
  32. PRStatus rv;
  33. mon = PR_NewMonitor();
  34. if (!mon) {
  35. fprintf(stderr, "PR_NewMonitor failed\n");
  36. exit(1);
  37. }
  38. thread = PR_CreateThread(PR_USER_THREAD, ThreadFunc, mon,
  39. PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD,
  40. PR_JOINABLE_THREAD, 0);
  41. if (!thread) {
  42. fprintf(stderr, "PR_CreateThread failed\n");
  43. exit(1);
  44. }
  45. PR_EnterMonitor(mon);
  46. while (!done) {
  47. rv = PR_Wait(mon, PR_INTERVAL_NO_TIMEOUT);
  48. PR_ASSERT(rv == PR_SUCCESS);
  49. }
  50. rv = PR_ExitMonitor(mon);
  51. PR_ASSERT(rv == PR_SUCCESS);
  52. /*
  53. * Do you agree it should be safe to destroy 'mon' now?
  54. * See bug 844784 comment 27.
  55. */
  56. PR_DestroyMonitor(mon);
  57. rv = PR_JoinThread(thread);
  58. PR_ASSERT(rv == PR_SUCCESS);
  59. printf("PASS\n");
  60. return 0;
  61. }