1 Reply Latest reply: Jul 31, 2007 8:11 AM by 807557 RSS

    Using scoped memory

    807557
      A couple of quick questions about using scoped memory. Firstly, it seems like most of the samples I've found in books etc don't run on the Sun RTSJ 2.0 eval. You generally end up with an exception. In order to get scoped memory to work I've had to resort to this clunky bit of stuff:
                      ImmortalMemory.instance().executeInArea(new Runnable() {
                              public void run() {
                                      new RealtimeThread(new PriorityParameters(PriorityScheduler.MAX_PRIORITY), null, null, null, null, new Runnable() {
                                              public void run() {
                                                      new LTMemory(size*128, size*128, new Runnable() {
                                                              public void run() {
                                                                      new RealtimeThread(new PriorityParameters(PriorityScheduler.MAX_PRIORITY), null, null, null, null, new RTQueueTest()).start();
                                                              }
                                                      }).enter();
                                              }
                                      }).start();
                              }
                      });
      I'm kind of hoping that this is just stupidity on my part and that getting to scoped memory from a regular thread (the above is called from main()) is not this hard.

      Secondly, if I create an object in this scope using something like the newInstance() method, do objects created in the constructor go into the scope too? So for example if I'm running using the regular heap as my memory area and I do
      myScope.newInstance( String.class)
      then does the char[] array that's created in the constructor of String end up in the scope too or does it go to the memoryarea in use by the caller?
        • 1. Re: Using scoped memory
          807557
          It doesn't have to be that complicated to use a ScopedMemory object, but the java.lang.Thread that executes main can't enter a scope, so you have to create a RealtimeThread to do it. Regular Java threads can't use scoped memory.
          class MyRTT extends RealtimeThread {
             public void run() {
                VTMemory mem = ...
                Runnable r = ...
                mem.enter(r);
             }
          }
          
          static void main(String[] args) {
             new MyRTT().start();
          }
          Or even simpler just pass the ScopedMemory as the initial memory area via the RealtimeThread constructor.

          Can you post some of the samples that don't run on Sun Java RTS 2.0 - I suspect the samples are making invalid assumptions.

          For your second question, when MemoryArea.newInstance is called the constructor executes in the given MemoryArea. It's a shortcut for doing executeInArea and invoking class.newInstance (or constructor.newInstance).

          Hope this helps.

          David Holmes