gxsync.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /* Copyright (C) 1998 Aladdin Enterprises. All rights reserved.
  2. This software is provided AS-IS with no warranty, either express or
  3. implied.
  4. This software is distributed under license and may not be copied,
  5. modified or distributed except as expressly authorized under the terms
  6. of the license contained in the file LICENSE in this distribution.
  7. For more information about licensing, please refer to
  8. http://www.ghostscript.com/licensing/. For information on
  9. commercial licensing, go to http://www.artifex.com/licensing/ or
  10. contact Artifex Software, Inc., 101 Lucas Valley Road #110,
  11. San Rafael, CA 94903, U.S.A., +1(415)492-9861.
  12. */
  13. /* $Id: gxsync.c,v 1.4 2002/02/21 22:24:53 giles Exp $ */
  14. /* Interface to platform-based synchronization primitives */
  15. /* Initial version 2/1/98 by John Desrosiers (soho@crl.com) */
  16. #include "memory_.h"
  17. #include "gx.h"
  18. #include "gserrors.h"
  19. #include "gsmemory.h"
  20. #include "gxsync.h"
  21. /* This module abstracts the platform-specific synchronization primitives. */
  22. /* Since these routines will see heavy use, performance is important. */
  23. /* ----- Semaphore interface ----- */
  24. /* These have the usual queued, counting semaphore semantics: at init time, */
  25. /* the event count is set to 0 ('wait' will wait until 1st signal). */
  26. /* Allocate & initialize a semaphore */
  27. gx_semaphore_t * /* returns a new semaphore, 0 if error */
  28. gx_semaphore_alloc(
  29. gs_memory_t * memory /* memory allocator to use */
  30. )
  31. {
  32. gx_semaphore_t *sema;
  33. /* sizeof decl'd sema struct, minus semaphore placeholder's size, + actual semaphore size */
  34. unsigned semaSizeof
  35. = sizeof(*sema) - sizeof(sema->native) + gp_semaphore_sizeof();
  36. if (gp_semaphore_open(0) == 0) /* see if gp_semaphores are movable */
  37. /* movable */
  38. sema = (gx_semaphore_t *) gs_alloc_bytes(memory, semaSizeof,
  39. "gx_semaphore (create)");
  40. else
  41. /* unmovable */
  42. sema = (gx_semaphore_t *) gs_alloc_bytes_immovable(memory, semaSizeof,
  43. "gx_semaphore (create)");
  44. if (sema == 0)
  45. return 0;
  46. /* Make sema remember which allocator was used to allocate it */
  47. sema->memory = memory;
  48. if (gp_semaphore_open(&sema->native) < 0) {
  49. gs_free_object(memory, sema, "gx_semaphore (alloc)");
  50. return 0;
  51. }
  52. return sema;
  53. }
  54. /* Deinit & free a semaphore */
  55. void
  56. gx_semaphore_free(
  57. gx_semaphore_t * sema /* semaphore to delete */
  58. )
  59. {
  60. if (sema) {
  61. gp_semaphore_close(&sema->native);
  62. gs_free_object(sema->memory, sema, "gx_semaphore (free)");
  63. }
  64. }
  65. /* Macros defined in gxsync.h, but redefined here so compiler chex consistency */
  66. #define gx_semaphore_wait(sema) gp_semaphore_wait(&(sema)->native)
  67. #define gx_semaphore_signal(sema) gp_semaphore_signal(&(sema)->native)
  68. /* ----- Monitor interface ----- */
  69. /* These have the usual monitor semantics: at init time, */
  70. /* the event count is set to 1 (1st 'enter' succeeds immediately). */
  71. /* Allocate & Init a monitor */
  72. gx_monitor_t * /* returns a new monitor, 0 if error */
  73. gx_monitor_alloc(
  74. gs_memory_t * memory /* memory allocator to use */
  75. )
  76. {
  77. gx_monitor_t *mon;
  78. /* sizeof decl'd mon struct, minus monitor placeholder's size, + actual monitor size */
  79. unsigned monSizeof
  80. = sizeof(*mon) - sizeof(mon->native) + gp_monitor_sizeof();
  81. if (gp_monitor_open(0) == 0) /* see if gp_monitors are movable */
  82. /* movable */
  83. mon = (gx_monitor_t *) gs_alloc_bytes(memory, monSizeof,
  84. "gx_monitor (create)");
  85. else
  86. /* unmovable */
  87. mon = (gx_monitor_t *) gs_alloc_bytes_immovable(memory, monSizeof,
  88. "gx_monitor (create)");
  89. if (mon == 0)
  90. return 0;
  91. /* Make monitor remember which allocator was used to allocate it */
  92. mon->memory = memory;
  93. if (gp_monitor_open(&mon->native) < 0) {
  94. gs_free_object(memory, mon, "gx_monitor (alloc)");
  95. return 0;
  96. }
  97. return mon;
  98. }
  99. /* Dnit & free a monitor */
  100. void
  101. gx_monitor_free(
  102. gx_monitor_t * mon /* monitor to delete */
  103. )
  104. {
  105. if (mon) {
  106. gp_monitor_close(&mon->native);
  107. gs_free_object(mon->memory, mon, "gx_monitor (free)");
  108. }
  109. }
  110. /* Macros defined in gxsync.h, but redefined here so compiler chex consistency */
  111. #define gx_monitor_enter(sema) gp_monitor_enter(&(sema)->native)
  112. #define gx_monitor_leave(sema) gp_monitor_leave(&(sema)->native)