Is the library thread safe?

2009-01-10
2013-04-17
  • I'm using multiple thread to concurrently create a thumbnail view of the document, everything works fine with a single thread but rising the number I'm getting the following exception for some pages (randomly, not always the same pages) so I'm wondering if I'm missing something and my code is broken or if the library is not thread safe.
    Thank you
    Andrea

    15:32:22,464 ERROR JPodThumbnailCreator$ThumnailCreator  Unable to generate thumbnail
    de.intarsys.pdf.content.CSError: unexpected exception
        at de.intarsys.pdf.content.CSInterpreter.process(Unknown Source)
        at de.intarsys.pdf.content.CSDeviceBasedInterpreter.process(Unknown Source)
        at org.pdfsam.guiclient.business.thumbnails.creators.JPodThumbnailCreator$ThumnailCreator.run(JPodThumbnailCreator.java:259)
        at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
        at java.lang.Thread.run(Thread.java:619)
    Caused by: java.lang.IndexOutOfBoundsException
        at java.nio.Buffer.checkIndex(Buffer.java:520)
        at java.nio.DirectByteBuffer.getInt(DirectByteBuffer.java:624)
        at de.intarsys.nativec.jna.JnaNativeHandle.getInt(JnaNativeHandle.java:122)
        at de.intarsys.nativec.type.NativeObject.getInt(NativeObject.java:182)
        at de.intarsys.cwt.freetype.Outline.getPointX(Outline.java:65)
        at de.intarsys.pdf.platform.cwt.font.freetype.FreetypeGlyphs.createContour(Unknown Source)
        at de.intarsys.pdf.platform.cwt.font.freetype.FreetypeGlyphs.createShape(Unknown Source)
        at de.intarsys.pdf.platform.cwt.font.freetype.FreetypeGlyphs.getShape(Unknown Source)
        at de.intarsys.pdf.platform.cwt.font.freetype.FreetypeGlyphs.render(Unknown Source)
        at de.intarsys.pdf.platform.cwt.rendering.CSPlatformDevice.basicTextShowGlyphs(Unknown Source)
        at de.intarsys.pdf.content.CSBasicDevice.textShow(Unknown Source)
        at de.intarsys.pdf.platform.cwt.rendering.CSPlatformDevice.textShow(Unknown Source)
        at de.intarsys.pdf.content.CSDeviceBasedInterpreter.render_TJ(Unknown Source)
        at de.intarsys.pdf.content.CSInterpreter.process(Unknown Source)
        ... 6 more

     
    • mtraut
      mtraut
      2009-01-12

      jPod is intended to be thread safe for certain scenarios. For example, read - access to a document is thread safe (which is not understood, as data is read lazy). Changing the document structure needs external synchronization (e.g. writing to COSDictionary).

      Rendering the document *should* be thread safe (as this is a reading scenario), but for sure you have to use different interpreter instances. We have overseen the bottleneck of the unique, lazy implemented Freetype contours, shared by the interpreters. We will review the code with respect to this.

      For your issue at the moment it should be enough to synchronize de.intarsys.pdf.platform.cwt.font.freetype.FreetypeGlyphs.createShape and  de.intarsys.pdf.platform.cwt.font.freetype.FreetypeGlyphs.init

       
    • mtraut
      mtraut
      2009-01-13

      so sorry - one should not answer immediately.

      synchronizing this methods won't help, as the shared resource is the font's native interface, not the lazy glyph. you should apply

          protected Shape createShape() {
              synchronized (font) {
                              .....
              }
          }

      and

          private void init() throws PlatformFontException {
              synchronized (font) {
                              .....
              }
          }

      to make outline access safe.

       
    • Tested and the exception went away :)
      Will you include this in the next release?