package org.perl6.nqp.sixmodel.reprs;

import java.util.concurrent.locks.ReentrantLock;

import org.perl6.nqp.runtime.ExceptionHandling;
import org.perl6.nqp.runtime.ThreadContext;
import org.perl6.nqp.sixmodel.REPR;
import org.perl6.nqp.sixmodel.STable;
import org.perl6.nqp.sixmodel.SerializationReader;
import org.perl6.nqp.sixmodel.SixModelObject;
import org.perl6.nqp.sixmodel.TypeObject;

public class ReentrantMutex extends REPR {
    public SixModelObject type_object_for(ThreadContext tc, SixModelObject HOW) {
        STable st = new STable(this, HOW);
        SixModelObject obj = new TypeObject();
        obj.st = st;
        st.WHAT = obj;
        return st.WHAT;
    }

    public SixModelObject allocate(ThreadContext tc, STable st) {
        ReentrantMutexInstance obj = new ReentrantMutexInstance();
        obj.st = st;
        obj.lock = new ReentrantLock();
        return obj;
    }

    public SixModelObject deserialize_stub(ThreadContext tc, STable st) {
        throw ExceptionHandling.dieInternal(tc, "Cannot deserialize a lock");
    }

    public void deserialize_finish(ThreadContext tc, STable st,
            SerializationReader reader, SixModelObject obj) {
        throw ExceptionHandling.dieInternal(tc, "Cannot deserialize a lock");
    }
}

@@ -299,16 +299,67 @@
 Reader;%0A
+import org.perl6.nqp.sixmodel.SerializationWriter;%0A
 import o
@@ -1036,75 +1036,30 @@
     
-throw ExceptionHandling.dieInternal(tc, %22Cannot deserialize a lock%22
+return allocate(tc, st
 );%0A 
@@ -1202,77 +1202,230 @@
     
-throw ExceptionHandling.dieInternal(tc, %22Cannot deserialize a lock%22);
+/* Already did it all in deserialize_stub. */%0A    %7D%0A%0A    public void serialize(ThreadContext tc, SerializationWriter writer, SixModelObject obj) %7B%0A        /* Nothing to do, we just re-create the lock on deserialization. */
 %0A   

Allow ReentrantMutex REPR to serialize on JVM.