Interleaved ReentrantLocks (for 3D camera movement, LWJGL)

138 Views Asked by At

My question is if is rather a valid choice to interleave multiple ReentrantLocks or not for a specific case: camera movement in 3D space (LWJGL, openGL) - with graphical representation of course.

My camera controller object holds a private method to manipulate to modelview matrix (lookThrough()), so I can "walk" and "rotate" the users view in 3D space. It contains public methods (i.e. walkForward()) which let other classes/objects change the movement related variables (pitch, yaw, position). Now, an object/thread for user controls constantly change the position as well as yaw and pitch. Concurrently, the main window (graphics) applies the position and rotational factors to the modelview matrix by using the lookThrough()-method. All methods of camer movement and the manipulation of the modelview matrix use one or all spatial variables which were mentioned before.

Here are two of the mentioned methods of the camera controller object before limiting access by concurrent threads. They clearly show the problem of accessing the same variables while the camera movement and the manipulation of the modelview matrix can happen at the same time:

// moves camera forward rel. to its current rotation (yaw)
public void walkForward(float distance){
    position.x -= distance * (float)Math.sin(Math.toRadians(yaw));
    position.z += distance * (float)Math.cos(Math.toRadians(yaw));
    position.y += distance * (float)Math.sin(Math.toRadians(pitch));
}

// translate and rotate to look through the camera
public void lookThrough(){
    // rotate the pitch around the X-axis
    GL11.glRotatef(pitch, 1.0f, 0.0f, 0.0f);
    // rotate the yaw around the Y-axis
    GL11.glRotatef(yaw, 0.0f, 1.0f, 0.0f);
    // translate to the location defined int he position vector
    GL11.glTranslatef(position.x, position.y, position.z);
}

To limit the thre access of threads to these variables for the given threads I assume, the standard Lock- or ReentrantLock-Classes of Java would do the job. I can either create locks for each spatial variable or one to rule them all. Assuming that I am going for the multiple ReentrantLock approach, I am having trouble how to lock and unlock all lock objects properly. Should I do it like in the following example:

lock_pitch.lock();
lock_yaw.lock();
lock_position.lock();
try{
    [...]
}
finally{
    lock_pitch.unlock();
    lock_yaw.unlock();
    lock_position.unlock();
}

Or is it more advisable to interleave the locks in multiple try-finally-blocks:

lock_pitch.lock();
try{
    lock_yaw.lock();
    try{
        lock_position.lock();
        try{
            // put executed code here
        }
        finally{
            lock_position.unlock();
        }
    }
    finally{
        lock_yaw.unlock();
    }
}
finally{
    lock_pitch.unlock();
}

The second example looks quite awkward to me. Thus, I would choose the first approach which locks and unlocks in a less interleaved manner. Maybe it is better to use only a single lock to manage the thread access. This way, the locking and unlocking would be straightforward. In more than half of the cases all spatial variables would be accessed at the same time anyway. Maybe I can simply neglect the lock(s) when using the lookThrough()-method. The user's view through the "camera" wouldn't be thread-safe, however, the actual camera position is. As a result, the user's view can not be completely corrupted by it.

Enough mass confusion - Here are my main questions summarized for simplicity:

  1. Would you interleave Locks or ReentrantLocks? In this specific case?
  2. How would you actually design the code when using multiple locks "at the same time" (I know it's not exactly the same time)?
  3. Is it maybe advisable to summarize the signle lock objects into one lock to lock them all?
  4. Can I simply neglect using locks when manipulating the modelmatrix in openGL/LWJGL if I keep track of the actual "camera" position and rotation which is applied to this matrix in each rendering loop?

Thank you for your advise!

0

There are 0 best solutions below