3 Replies Latest reply: May 28, 2008 5:10 AM by 807557 RSS

    RealtimeThread can't enter Scoped Memory

    807557
      Hello everybody,
      i making some benchmarks for the RTJ and have to figure out if there are differences between RealtimeThreads in normal heap memory and RT Threads in Scoped or Immortal Memory.
      When i try to access (enter, executeInArea) with my RT Thread he returns this ugly error.
      So i tried to use a simple example code from a book. This didn't help either.
      Does any one have a clue what im doing wrong? do i have to initialize the size of the default scoped memory?

      Thank you in advance for your help
      Best regards
      Ch. Duchamps
      public class Simple {
           static final int TAILLE_INITIALE = 16*1024, TAILLE_MAXIMALE = 256*1024;
           
           static class ThreadTempsReel extends RealtimeThread {
                public void run(){
                     String str = new String();
                     System.out.println(
                     getCurrentMemoryArea() + " " + getMemoryAreaStackDepth() + " " +
                     getCurrentMemoryArea().memoryConsumed() + " " +
                     getOuterMemoryArea(getMemoryAreaStackDepth()-1));
                }
           }
           
           static void itererAlgorithme(){
                LTMemory domaineMemory = new LTMemory(TAILLE_INITIALE, TAILLE_MAXIMALE);
                ThreadTempsReel codeAlgo = new ThreadTempsReel();
                while(true){
                     System.out.println(MemoryArea.getMemoryArea(codeAlgo));
                     domaineMemory.enter(codeAlgo);
                }
           }
           
           public static void main(String[] args){
                itererAlgorithme();
           }
           
      }
      Exception in thread "main" java.lang.IllegalThreadStateException: Only RealtimeThread thread can call this method
              at javax.realtime.MemoryArea.enter0(Native Method)
              at javax.realtime.MemoryArea.enter(MemoryArea.java:190)
              at javax.realtime.ScopedMemory.enter(ScopedMemory.java:191)
              at Simple.itererAlgorithme(Simple.java:30)
              at Simple.main(Simple.java:35)
        • 1. Re: RealtimeThread can't enter Scoped Memory
          807557
          As the exception states:

          java.lang.IllegalThreadStateException: Only RealtimeThread thread can call this method

          Only RealtimeThreads can use ScopedMemory areas. The main() method of an application is not executed by a RealtimeThread but by a normal java.lang.Thread (or JLT as we call them). MemoryArea.enter() is just a normal method call and for ScopedMemory the current thread has to be a RealtimeThread. The Runnable you pass to it is just a Runnable - not a Thread. You've created a subclass of RealtimeThread but you are using it as a Runnable by passing it to MemoryArea.enter. For this example, as far as I understand it, you will need both a RealtimeThread and a Runnable:

          - the RealtimeThread's run() method will call itereAlgorithme()
          - the Runnable will contain the println's currently in ThreadTempsReel.run()

          Or to make it very clear (but not compiled/tested):
          public class Simple {
               static final int TAILLE_INITIALE = 16*1024, TAILLE_MAXIMALE = 256*1024;
               
               static class Runner implements Runnable {
                    public void run(){
                         String str = new String();
                         System.out.println(
                         getCurrentMemoryArea() + " " + getMemoryAreaStackDepth() + " " +
                         getCurrentMemoryArea().memoryConsumed() + " " +
                         getOuterMemoryArea(getMemoryAreaStackDepth()-1));
                    }
               }
               
               static void itererAlgorithme(){
                    LTMemory domaineMemory = new LTMemory(TAILLE_INITIALE, TAILLE_MAXIMALE);
                    Runnable codeAlgo = new Runner();
                    while(true){
                         System.out.println(MemoryArea.getMemoryArea(codeAlgo));
                         domaineMemory.enter(codeAlgo);
                    }
               }
               
               public static void main(String[] args){
                  (new RealtimeThread() {
                    public void run() {
                    itererAlgorithme();
                    }
                  }).start();
               }
               
          }
          David Holmes

          Edited by: davidholmes on May 28, 2008 8:36 AM
          • 2. Re: RealtimeThread can't enter Scoped Memory
            807557
            Right, seems to work this way.
            First of all Thank You for your help. Was realy greatly appreciated. It was also realy quick too.

            Now my problem lies in the understanding of why it has to be this way up. Seems to me for the Memory manager of the non Heap section. Can it be that the RTT, does something more than the normal thread when exciting a scope. So that the memory manager can know that it can free up the space? that a scope area isn't in use anymore.

            Seems to me so, what is more interesting how he handles the variables that are allready instantiatet. Like im creating a RTT and initialize a string. Then in the run i'm creating a runnable, which is executed in the NonHeap section of the memory. Does this variable stay in the Heap? If yes maybe it will be wise to make the whole RTT object in the NonHeap.

            Overall speaked im quite positively astonished, about the result that i receive. With little program design changes, i can have some considerable improvement.
            Thank you all and good luck with RTJ
            • 3. Re: RealtimeThread can't enter Scoped Memory
              807557
              Now my problem lies in the understanding of why it has to be this way up. Seems to me for the Memory manager of the non Heap section. Can it be that the RTT, does something more than the normal thread when exciting a scope. So that the memory manager can know that it can free up the space? that a scope area isn't in use anymore.
              The use of scopes and the associated rules are very complex. The VM has to do a lot of work to enforce those rules and make scopes work correctly. Plain threads are not allowed to use scopes because it would require them to be burdened with a lot of additional runtime overhead - and because it simplifies the programming model somewhat.

              Seems to me so, what is more interesting how he handles the variables that are allready instantiatet. Like im creating a RTT and initialize a string. Then in the run i'm creating a runnable, which is executed in the NonHeap section of the memory. Does this variable stay in the Heap? If yes maybe it will be wise to make the whole RTT object in the NonHeap.
              Whenever a piece of code is executing there is a current allocation context, which is represented by an instance of MemoryArea (HeapMemory.instance(), ImmortalMemory.instance(), or an instance of ScopedMemory, or one of the physical memory areas). Any "new" allocates in that context. You change contexts by using the MemoryArea.enter or executeInArea methods passing in a Runnable that contains the code to be executed with that memoryArea as the allocation context. You also implicitly change to ImmortalMemory when performing static initialization, and to create certain objects eg Class object, interned Strings, string literals. So yes your Runnable is created in the heap, but it is then executed in the scope by the current real-time thread.

              If you try to create everything in non-heap you'll have to start with ImmortalMemory for the initial RTT as that's the only non-heap memory that the Java thread executing main() can access.

              Hope this clarifies things.

              David Holmes