overview.txt 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. Contents
  2. ========
  3. * Basic MuPDF usage example
  4. * Common function arguments
  5. * Error Handling
  6. * Multi-threading
  7. * Cloning the context
  8. * Bound contexts
  9. Basic MuPDF usage example
  10. =========================
  11. For an example of how to use MuPDF in the most basic way, see
  12. doc/example.c. To limit the complexity and give an easier introduction
  13. this code has no error handling at all, but any serious piece of code
  14. using MuPDF should use the error handling strategies described below.
  15. Common function arguments
  16. =========================
  17. Most functions in MuPDF's interface take a context argument.
  18. A context contains global state used by MuPDF inside functions when
  19. parsing or rendering pages of the document. It contains for example:
  20. an exception stack (see error handling below),
  21. a memory allocator (allowing for custom allocators)
  22. a resource store (for caching of images, fonts, etc.)
  23. a set of locks and (un-)locking functions (for multi-threading)
  24. Without the set of locks and accompanying functions the context and
  25. its proxies may only be used in a single-threaded application.
  26. Error handling
  27. ==============
  28. MuPDF uses a set of exception handling macros to simplify error return
  29. and cleanup. Conceptually, they work a lot like C++'s try/catch
  30. system, but do not require any special compiler support.
  31. The basic formulation is as follows:
  32. fz_try(ctx)
  33. {
  34. // Try to perform a task. Never 'return', 'goto' or
  35. // 'longjmp' out of here. 'break' may be used to
  36. // safely exit (just) the try block scope.
  37. }
  38. fz_always(ctx)
  39. {
  40. // Any code here is always executed, regardless of
  41. // whether an exception was thrown within the try or
  42. // not. Never 'return', 'goto' or longjmp out from
  43. // here. 'break' may be used to safely exit (just) the
  44. // always block scope.
  45. }
  46. fz_catch(ctx)
  47. {
  48. // This code is called (after any always block) only
  49. // if something within the fz_try block (including any
  50. // functions it called) threw an exception. The code
  51. // here is expected to handle the exception (maybe
  52. // record/report the error, cleanup any stray state
  53. // etc) and can then either exit the block, or pass on
  54. // the exception to a higher level (enclosing) fz_try
  55. // block (using fz_throw, or fz_rethrow).
  56. }
  57. The fz_always block is optional, and can safely be omitted.
  58. The macro based nature of this system has 3 main limitations:
  59. 1) Never return from within try (or 'goto' or longjmp out of it).
  60. This upsets the internal housekeeping of the macros and will
  61. cause problems later on. The code will detect such things
  62. happening, but by then it is too late to give a helpful error
  63. report as to where the original infraction occurred.
  64. 2) The fz_try(ctx) { ... } fz_always(ctx) { ... } fz_catch(ctx) { ... }
  65. is not one atomic C statement. That is to say, if you do:
  66. if (condition)
  67. fz_try(ctx) { ... }
  68. fz_catch(ctx) { ... }
  69. then you will not get what you want. Use the following instead:
  70. if (condition) {
  71. fz_try(ctx) { ... }
  72. fz_catch(ctx) { ... }
  73. }
  74. 3) The macros are implemented using setjmp and longjmp, and so
  75. the standard C restrictions on the use of those functions
  76. apply to fz_try/fz_catch too. In particular, any "truly local"
  77. variable that is set between the start of fz_try and something
  78. in fz_try throwing an exception may become undefined as part
  79. of the process of throwing that exception.
  80. As a way of mitigating this problem, we provide a fz_var()
  81. macro that tells the compiler to ensure that that variable is
  82. not unset by the act of throwing the exception.
  83. A model piece of code using these macros then might be:
  84. house build_house(plans *p)
  85. {
  86. material m = NULL;
  87. walls w = NULL;
  88. roof r = NULL;
  89. house h = NULL;
  90. tiles t = make_tiles();
  91. fz_var(w);
  92. fz_var(r);
  93. fz_var(h);
  94. fz_try(ctx)
  95. {
  96. fz_try(ctx)
  97. {
  98. m = make_bricks();
  99. }
  100. fz_catch(ctx)
  101. {
  102. // No bricks available, make do with straw?
  103. m = make_straw();
  104. }
  105. w = make_walls(m, p);
  106. r = make_roof(m, t);
  107. // Note, NOT: return combine(w,r);
  108. h = combine(w, r);
  109. }
  110. fz_always(ctx)
  111. {
  112. drop_walls(w);
  113. drop_roof(r);
  114. drop_material(m);
  115. drop_tiles(t);
  116. }
  117. fz_catch(ctx)
  118. {
  119. fz_throw(ctx, "build_house failed");
  120. }
  121. return h;
  122. }
  123. Things to note about this:
  124. a) If make_tiles throws an exception, this will immediately be
  125. handled by some higher level exception handler. If it
  126. succeeds, t will be set before fz_try starts, so there is no
  127. need to fz_var(t);
  128. b) We try first off to make some bricks as our building material.
  129. If this fails, we fall back to straw. If this fails, we'll end
  130. up in the fz_catch, and the process will fail neatly.
  131. c) We assume in this code that combine takes new reference to
  132. both the walls and the roof it uses, and therefore that w and
  133. r need to be cleaned up in all cases.
  134. d) We assume the standard C convention that it is safe to destroy
  135. NULL things.
  136. Multi-threading
  137. ===============
  138. First off, study the basic usage example in doc/example.c and make
  139. sure you understand how it works as the data structures manipulated
  140. there will be refered to in this section too.
  141. MuPDF can usefully be built into a multi-threaded application without
  142. the library needing to know anything threading at all. If the library
  143. opens a document in one thread, and then sits there as a 'server'
  144. requesting pages and rendering them for other threads that need them,
  145. then the library is only ever being called from this one thread.
  146. Other threads can still be used to handle UI requests etc, but as far
  147. as MuPDF is concerned it is only being used in a single threaded way.
  148. In this instance, there are no threading issues with MuPDF at all,
  149. and it can safely be used without any locking, as described in the
  150. previous sections.
  151. This section will attempt to explain how to use MuPDF in the more
  152. complex case; where we genuinely want to call the MuPDF library
  153. concurrently from multiple threads within a single application.
  154. MuPDF can be invoked with a user supplied set of locking functions.
  155. It uses these to take mutexes around operations that would conflict
  156. if performed concurrently in multiple threads. By leaving the
  157. exact implementation of locks to the caller MuPDF remains threading
  158. library agnostic.
  159. The following simple rules should be followed to ensure that
  160. multi-threaded operations run smoothly:
  161. 1) "No simultaneous calls to MuPDF in different threads are
  162. allowed to use the same context."
  163. Most of the time it is simplest to just use a different
  164. context for every thread; just create a new context at the
  165. same time as you create the thread. For more details see
  166. "Cloning the context" below.
  167. 2) "No simultaneous calls to MuPDF in different threads are
  168. allowed to use the same document."
  169. Only one thread can be accessing a document at a time, but
  170. once display lists are created from that document, multiple
  171. threads at a time can operate on them.
  172. The document can be used from several different threads as
  173. long as there are safeguards in place to prevent the usages
  174. being simultaneous.
  175. 3) "No simultaneous calls to MuPDF in different threads are
  176. allowed to use the same device."
  177. Calling a device simultaneously from different threads will
  178. cause it to get confused and may crash. Calling a device from
  179. several different threads is perfectly acceptable as long as
  180. there are safeguards in place to prevent the calls being
  181. simultaneous.
  182. So, how does a multi-threaded example differ from a non-multithreaded
  183. one?
  184. Firstly, when we create the first context, we call fz_new_context
  185. as before, but the second argument should be a pointer to a set
  186. of locking functions.
  187. The calling code should provide FZ_LOCK_MAX mutexes, which will be
  188. locked/unlocked by MuPDF calling the lock/unlock function pointers
  189. in the supplied structure with the user pointer from the structure
  190. and the lock number, i (0 <= i < FZ_LOCK_MAX). These mutexes can
  191. safely be recursive or non-recursive as MuPDF only calls in a non-
  192. recursive style.
  193. To make subsequent contexts, the user should NOT call fz_new_context
  194. again (as this will fail to share important resources such as the
  195. store and glyphcache), but should rather call fz_clone_context.
  196. Each of these cloned contexts can be freed by fz_free_context as
  197. usual. They will share the important data structures (like store,
  198. glyph cache etc) with the original context, but will have their
  199. own exception stacks.
  200. To open a document, call fz_open_document as usual, passing a context
  201. and a filename. It is important to realise that only one thread at a
  202. time can be accessing the documents itself.
  203. This means that only one thread at a time can perform operations such
  204. as fetching a page, or rendering that page to a display list. Once a
  205. display list has been obtained however, it can be rendered from any
  206. other thread (or even from several threads simultaneously, giving
  207. banded rendering).
  208. This means that an implementer has 2 basic choices when constructing
  209. an application to use MuPDF in multi-threaded mode. Either he can
  210. construct it so that a single nominated thread opens the document
  211. and then acts as a 'server' creating display lists for other threads
  212. to render, or he can add his own mutex around calls to mupdf that
  213. use the document. The former is likely to be far more efficient in
  214. the long run.
  215. For an example of how to do multi-threading see doc/multi-threaded.c
  216. which has a main thread and one rendering thread per page.
  217. Cloning the context
  218. ===================
  219. As described above, every context contains an exception stack which is
  220. manipulated during the course of nested fz_try/fz_catches. For obvious
  221. reasons the same exception stack cannot be used from more than one
  222. thread at a time.
  223. If, however, we simply created a new context (using fz_new_context) for
  224. every thread, we would end up with separate stores/glyph caches etc,
  225. which is not (generally) what is desired. MuPDF therefore provides a
  226. mechanism for "cloning" a context. This creates a new context that
  227. shares everything with the given context, except for the exception
  228. stack.
  229. A commonly used general scheme is therefore to create a 'base' context
  230. at program start up, and to clone this repeatedly to get new contexts
  231. that can be used on new threads.