package tools.concurrency;

import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.ForkJoinWorkerThread;
import java.util.concurrent.atomic.AtomicInteger;

import som.vm.VmSettings;
import tools.TraceData;


public abstract class TracingActivityThread extends ForkJoinWorkerThread {
  private static AtomicInteger threadIdGen = new AtomicInteger(1);
  protected final long threadId;

  protected long nextActivityId = 1;

  protected long nextMessageId;
  protected long nextPromiseId;

  // Used for tracing, accessed by the ExecAllMessages classes
  public long createdMessages;
  public long resolvedPromises;

  protected final TraceBuffer traceBuffer;

  public TracingActivityThread(final ForkJoinPool pool) {
    super(pool);
    if (VmSettings.ACTOR_TRACING) {
      traceBuffer = TraceBuffer.create();
      threadId = threadIdGen.getAndIncrement();
      nextMessageId = (threadId << TraceData.ACTIVITY_ID_BITS);
      nextPromiseId = (threadId << TraceData.ACTIVITY_ID_BITS);
    } else {
      threadId = 0;
      traceBuffer = null;
    }
    setName(getClass().getSimpleName() + "-" + threadId);
  }

  public long generateActivityId() {
    long result = (threadId << TraceData.ACTIVITY_ID_BITS) | nextActivityId;
    nextActivityId++;
    assert TraceData.isWithinJSIntValueRange(result);
    return result;
  }

  public long generatePromiseId() {
    return nextPromiseId++;
  }

  public final TraceBuffer getBuffer() {
    return traceBuffer;
  }

  public abstract long getCurrentMessageId();

  @Override
  protected void onStart() {
    super.onStart();
    if (VmSettings.ACTOR_TRACING) {
      traceBuffer.init(ActorExecutionTrace.getEmptyBuffer(), threadId);
      ActorExecutionTrace.registerThread(this);
    }
  }

  @Override
  protected void onTermination(final Throwable exception) {
    if (VmSettings.ACTOR_TRACING) {
      traceBuffer.returnBuffer();
      ActorExecutionTrace.unregisterThread(this);
    }
    super.onTermination(exception);
  }
}

@@ -391,17 +391,16 @@
 readId;%0A
-%0A
   protec
@@ -428,17 +428,16 @@
 Id = 1;%0A
-%0A
   protec
@@ -861,24 +861,93 @@
 ncrement();%0A
+      nextActivityId = 1 + (threadId %3C%3C TraceData.ACTIVITY_ID_BITS);%0A
       nextMe
@@ -1248,51 +1248,8 @@
 lt =
- (threadId %3C%3C TraceData.ACTIVITY_ID_BITS) %7C
  nex

Fix activity IDs