<commit_before>package net.ihiroky.niotty;

import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author <NAME>
 */
public class ThreadPipelineElementExecutor extends TaskLoop<ThreadPipelineElementExecutor> implements PipelineElementExecutor {

    private final Lock lock_;
    private final Condition condition_;
    private final AtomicBoolean signaled_;
    private final Set<PipelineElement<?, ?>> contextSet_;
    private final ThreadPipelineElementExecutorPool pool_;

    public ThreadPipelineElementExecutor(ThreadPipelineElementExecutorPool pool) {
        lock_ = new ReentrantLock();
        condition_ = lock_.newCondition();
        signaled_ = new AtomicBoolean();
        contextSet_ = new HashSet<>();
        pool_ = pool;
    }

    @Override
    protected void onOpen() {
    }

    @Override
    protected void onClose() {
    }

    @Override
    protected void process(int timeout) throws Exception {
        lock_.lock();
        try {
            long timeoutNanos = TimeUnit.MILLISECONDS.toNanos(timeout);
            while (signaled_.getAndSet(false) || (timeoutNanos > 0 && hasNoTask())) {
                timeoutNanos = condition_.awaitNanos(timeoutNanos);
            }
        } finally {
            lock_.unlock();
        }
    }

    @Override
    protected void wakeUp() {
        lock_.lock();
        try {
            signaled_.set(true);
            condition_.signal();
        } finally {
            lock_.unlock();
        }
    }

    @Override
    public <I> void execute(final PipelineElement<I, ?> context, final I input) {
        offerTask(new Task<ThreadPipelineElementExecutor>() {
            @Override
            public int execute(ThreadPipelineElementExecutor eventLoop) throws Exception {
                context.fire(input);
                return TIMEOUT_NO_LIMIT;
            }
        });<commit_msg>Fix timeout handling<commit_after>package net.ihiroky.niotty;

import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author <NAME>
 */
public class ThreadPipelineElementExecutor extends TaskLoop<ThreadPipelineElementExecutor> implements PipelineElementExecutor {

    private final Lock lock_;
    private final Condition condition_;
    private boolean signaled_;
    private final Set<PipelineElement<?, ?>> contextSet_;
    private final ThreadPipelineElementExecutorPool pool_;

    public ThreadPipelineElementExecutor(ThreadPipelineElementExecutorPool pool) {
        lock_ = new ReentrantLock();
        condition_ = lock_.newCondition();
        signaled_ = false;
        contextSet_ = new HashSet<>();
        pool_ = pool;
    }

    @Override
    protected void onOpen() {
    }

    @Override
    protected void onClose() {
    }

    @Override
    protected void process(int timeout) throws Exception {
        lock_.lock();
        try {
            if (timeout > 0) {
                long timeoutNanos = TimeUnit.MILLISECONDS.toNanos(timeout);
                while (!signaled_) {
                    timeoutNanos = condition_.awaitNanos(timeoutNanos);
                }
            } else if (timeout < 0) {
                while (!signaled_) {
                    condition_.await();
                }
            }
        } finally {
            lock_.unlock();
        }
    }

    @Override
    protected void wakeUp() {
        lock_.lock();
        try {
            signaled_ = true;
            condition_.signal();
        } finally {
            lock_.unlock();
        }
    }

    @Override
    public <I> void execute(final PipelineElement<I, ?> context, final I input) {
        offerTask(new Task<ThreadPipelineElementExecutor>() {
            @Override
            public int execute(ThreadPipelineElementExecutor eventLoop) throws Exception {
                context.fire(input);
                return TIMEOUT_NO_LIMIT;
            }
        });
{'fraction_non_alphanumeric': 0.09120905020032996, 'fraction_numerical': 0.0007070469007777516, 'mean_word_length': 2.315625, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '829541', 'n_tokens_mistral': 1107, 'n_tokens_neox': 1074, 'n_words': 250}