<!DOCTYPE html><html><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  </head>
  <body>
    <p>Hi Roberto,<br>
      by "singleton limitation" in JNA, I assume you refer to the fact
      that, in JNA (but also JNI) you can only load a native library
      once, by only one classloader. Correct?</p>
    <p>If that's the limitation you are trying to workaround, then yes,
      FFM allows you to load the same native library multiple times
      using its SymbolLookup abstraction. Make sure to use the correct
      factory -- the one that does not rely on an association with the
      current classloader:</p>
    <p><a class="moz-txt-link-freetext" href="https://docs.oracle.com/en/java/javase/23/docs//api/java.base/java/lang/foreign/SymbolLookup.html#libraryLookup(java.lang.String,java.lang.foreign.Arena)">https://docs.oracle.com/en/java/javase/23/docs//api/java.base/java/lang/foreign/SymbolLookup.html#libraryLookup(java.lang.String,java.lang.foreign.Arena)</a></p>
    <p>This is just a very thin wrapper around dlopen, so it will take
      whatever library name dlopen can resolve, and will also allow you
      to open the library multiple times. Note that the library will be
      unloaded when the provided arena is closed -- so ideally you could
      use one arena per client.</p>
    <p>Examples of Arena.ofConfined, or custom arenas can be found in
      the javadoc:</p>
    <p>* confined arena -
<a class="moz-txt-link-freetext" href="https://docs.oracle.com/en/java/javase/23/docs//api/java.base/java/lang/foreign/package-summary.html">https://docs.oracle.com/en/java/javase/23/docs//api/java.base/java/lang/foreign/package-summary.html</a><br>
    </p>
    <p>* custom arena -
<a class="moz-txt-link-freetext" href="https://docs.oracle.com/en/java/javase/22/docs//api/java.base/java/lang/foreign/SymbolLookup.html#libraryLookup(java.lang.String,java.lang.foreign.Arena)">https://docs.oracle.com/en/java/javase/22/docs//api/java.base/java/lang/foreign/SymbolLookup.html#libraryLookup(java.lang.String,java.lang.foreign.Arena)</a></p>
    <p>We also have some narrative documents on how to use memory
      segment and linker API here:</p>
    <p><a class="moz-txt-link-freetext" href="https://github.com/openjdk/panama-foreign/blob/foreign-memaccess%2Babi/doc/panama_memaccess.md">https://github.com/openjdk/panama-foreign/blob/foreign-memaccess%2Babi/doc/panama_memaccess.md</a></p>
    <p><a class="moz-txt-link-freetext" href="https://github.com/openjdk/panama-foreign/blob/foreign-memaccess%2Babi/doc/panama_ffi.md">https://github.com/openjdk/panama-foreign/blob/foreign-memaccess%2Babi/doc/panama_ffi.md</a></p>
    <p>And, if you want to know more on jextract, we have a
      comprehensive guide here:</p>
    <p><a class="moz-txt-link-freetext" href="https://github.com/openjdk/jextract/blob/master/doc/GUIDE.md">https://github.com/openjdk/jextract/blob/master/doc/GUIDE.md</a></p>
    <p><br>
      Cheers<br>
      Maurizio<br>
    </p>
    <div class="moz-cite-prefix">On 05/02/2025 00:03,
      <a class="moz-txt-link-abbreviated" href="mailto:roberto.minoletti@trusthub.cloud">roberto.minoletti@trusthub.cloud</a> wrote:<br>
    </div>
    <blockquote type="cite" cite="mid:!&!AAAAAAAAAAAuAAAAAAAAAExtYa3gpX5HoQdE6pcyVYEBAMO2jhD3dRHOtM0AqgC7tuYAAAAAAA4AABAAAADd+TEeumaRTZk1zPeQBgVaAQAAAAA=@trusthub.cloud">
      
      <meta name="Generator" content="Microsoft Word 15 (filtered medium)">
      <style>@font-face
        {font-family:"Cambria Math";
        panose-1:2 4 5 3 5 4 6 3 2 4;}@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}@font-face
        {font-family:Aptos;}p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0cm;
        font-size:11.0pt;
        font-family:"Calibri",sans-serif;
        mso-ligatures:standardcontextual;
        mso-fareast-language:EN-US;}span.EstiloCorreo18
        {mso-style-type:personal-compose;
        font-family:"Calibri",sans-serif;
        color:windowtext;}.MsoChpDefault
        {mso-style-type:export-only;
        font-size:11.0pt;
        mso-ligatures:none;
        mso-fareast-language:EN-US;}div.WordSection1
        {page:WordSection1;}</style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]-->
      <div class="WordSection1">
        <p class="MsoNormal">Hello Maurizio,<o:p></o:p></p>
        <p class="MsoNormal" style="text-indent:35.4pt">Thank you to
          provide the right contact of the Panama project (I reached to
          you in LinkedIn some days ago)<o:p></o:p></p>
        <p class="MsoNormal">I am writing to you on behalf of a backend
          development team that is trying to accomplish our
          requirements.<o:p></o:p></p>
        <p class="MsoNormal"><o:p> </o:p></p>
        <p class="MsoNormal">We need to interact from our java code
          (JDK21 that we can easely update to JDK23 if required and
          Spring Web-Flux) to a C++ library that allow to create Slots,
          generate Keypairs store Certificates simulating a real HSM
          (Hardware Security Module). This is named libsofthsm2.so
          (SoftHSM2).<o:p></o:p></p>
        <p class="MsoNormal"><span lang="IT">As I mentioned we
            customized this library as we need to store certificates and
            keys in a different path for each user. One important
            requirement is that we need to be able to process many
            requests coming from many different users to the same
            library that should operate in different paths at the same
            time.<o:p></o:p></span></p>
        <p class="MsoNormal"><span lang="IT">We preliminary resolved a
            POC using JNA but when tried to execute many parallel treads
            we faced with his singleton limitation and after a deep
            reasearch we founded Panama project.<o:p></o:p></span></p>
        <p class="MsoNormal"><span lang="IT"><o:p> </o:p></span></p>
        <p class="MsoNormal"><span lang="IT">Does Panama FFI support
            multiple parallel treads? Does it have any Singleton
            limitations?<o:p></o:p></span></p>
        <p class="MsoNormal"><span lang="IT">What we need is to get many
            objects/treads at the same time (paralley) loading its own
            different instance of the same library, getting its own
            memory context, variables and states, all completely
            isolated but working at the same time. This was not possible
            for us because JNA uses the Singleton pattern to load the
            library directly into the process, making it only possible
            to have a single instance of the library for the entire
            process.<o:p></o:p></span></p>
        <p class="MsoNormal"><span lang="IT"><o:p> </o:p></span></p>
        <p class="MsoNormal"><span lang="IT">The goal is that each
            object/tread parallely instantiate the same library but
            having isolated states, configurations and variables stored
            in a dedicated memory slot for each tread, so even if the
            C++ library maybe does not completely support cuncurrency
            itself it should attend multiple treads as it is virtually
            responding at once having one specific configuration per
            user.<o:p></o:p></span></p>
        <p class="MsoNormal"><span lang="IT"><o:p> </o:p></span></p>
        <p class="MsoNormal"><span lang="IT">What we are looking for is
            that each ARENA has access to libsofthsm2.so and its
            internal variables using that dedicated memory space to each
            tread to calculate the operations it must perform.<o:p></o:p></span></p>
        <p class="MsoNormal"><span lang="IT"><o:p> </o:p></span></p>
        <p class="MsoNormal"><span lang="IT">Does Panama FFI allow all
            this? This is the first basic question we have. After this
            we will be able to perform more specific questions.<o:p></o:p></span></p>
        <p class="MsoNormal"><span lang="IT">Also if you have any sample
            of code that use Arena.ofConfined() or any Custom Arena it
            would be really fantastic.<o:p></o:p></span></p>
        <p class="MsoNormal"><span lang="IT"><o:p> </o:p></span></p>
        <p class="MsoNormal"><span lang="IT">Thank you in advance for
            any information and/or support impelmenting Panama in our
            Project.<o:p></o:p></span></p>
        <p class="MsoNormal"><span lang="IT"><o:p> </o:p></span></p>
        <p class="MsoNormal"><span lang="IT">Roberto Minoletti<o:p></o:p></span></p>
        <p class="MsoNormal"><span lang="IT">Trusthub LLC<o:p></o:p></span></p>
      </div>
    </blockquote>
  </body>
</html>