martinweiler commented on code in PR #3875:
URL: 
https://github.com/apache/incubator-kie-kogito-runtimes/pull/3875#discussion_r2031978894


##########
jbpm/jbpm-flow/src/main/java/org/kie/kogito/process/impl/lock/ProcessInstanceAtomicLockStrategy.java:
##########
@@ -0,0 +1,126 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.kie.kogito.process.impl.lock;
+
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.locks.ReentrantLock;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class ProcessInstanceAtomicLockStrategy implements 
ProcessInstanceLockStrategy {
+
+    private class ProcessInstanceLockHolder {
+        Integer counter;
+        ReentrantLock lock;
+
+        public ProcessInstanceLockHolder() {
+            counter = 0;
+            lock = new ReentrantLock();
+        }
+
+        void lock() {
+            lock.lock();
+        }
+
+        void unlock() {
+            lock.unlock();
+        }
+
+        boolean isReferenced() {
+            return counter <= 0;
+        }
+
+        boolean isHeldByCurrentThread() {
+            return lock.isHeldByCurrentThread();
+        }
+
+        public void addReference() {
+            counter++;
+        }
+
+        public void removeReference() {
+            counter--;
+        }
+    }
+
+    private static final Logger LOG = 
LoggerFactory.getLogger(ProcessInstanceAtomicLockStrategy.class);
+
+    private static ProcessInstanceAtomicLockStrategy INSTANCE;
+
+    private Map<String, ProcessInstanceLockHolder> locks = new 
ConcurrentHashMap<>();
+
+    @Override
+    public <T> T executeOperation(String processInstanceId, 
WorkflowAtomicExecutor<T> executor) {
+        // This is a bit tricky. To avoid resource memory leak of the 
reentrant lock and proper reuse we need to compute how many times
+        // the lock has being referenced. We avoid that way to compute 
incorrectly when to release it.
+        // compute and compute if present are thread safe and atomic to the 
bucket being computed meaning that the creation and obtaining this will rise
+        // the proper counter
+
+        ProcessInstanceLockHolder processInstanceLockHolder = 
locks.compute(processInstanceId, (pid, holder) -> {
+            ProcessInstanceLockHolder newHolder = holder;
+            if (newHolder == null) {
+                newHolder = new ProcessInstanceLockHolder();
+            }
+            newHolder.addReference();
+            LOG.trace("Creating lock {} from list as none is waiting for it by 
{}", newHolder.lock, pid);
+            return newHolder;
+        });
+
+        // at this points this is a safe ask as if we invoked prior to this 
point the hold it will always return
+        // properly
+        boolean alreadyAdquired = 
processInstanceLockHolder.isHeldByCurrentThread();

Review Comment:
   regarding this flag:
   1. `s/adquire/acquire`
   2. would the logging not be interesting in any case, even if the current 
thread had to wait until it successfully obtained the lock? I'd remove the if 
clause around the logging, and instead add the `alreadyAcquired` flag to the 
log message itself. wdyt?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscr...@kie.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@kie.apache.org
For additional commands, e-mail: commits-h...@kie.apache.org

Reply via email to