<commit_before>package org.basex.core;

import java.util.LinkedList;

/**
 * Management of executing read/write processes. Multiple readers, single
 * writers.
 *
 * @author Workgroup DBIS, University of Konstanz 2005-10, ISC License
 * @author <NAME>
 */
final class Semaphore {
  /** List of waiting processes for writers or reading groups. */
  private final LinkedList<Lock> waiting = new LinkedList<Lock>();

  /** States of locking. */
  private static enum State {
    /** Idle state. */ IDLE,
    /** Read state. */ READ,
    /** Write state. */ WRITE
  }

  /** State of the lock. */
  private State state = State.IDLE;
  /** Number of active readers. */
  private int activeR;

  /**
   * Modifications before executing a process.
   * @param w writing flag
   */
  void before(final boolean w) {
    if(w) {
      // exclusive lock
      final Lock lx = new Lock(true);
      synchronized(lx) {
        synchronized(this) {
          if(state == State.IDLE) {
            state = State.WRITE;
            return;
          }
          waiting.add(lx);
        }
        try {
          lx.wait();
          state = State.WRITE;
        } catch(final InterruptedException ex) {
          ex.printStackTrace();
        }
      }
    } else {
      // shared lock
      Lock ls = null;
      synchronized(this) {
        if(state == State.IDLE) {
          state = State.READ;
          activeR++;
          return;
        } else if(state == State.READ && waiting.size() == 0) {
            activeR++;
            return;
        }
        if(waiting.size() > 0 && !waiting.getLast().writer) {
            ls = waiting.getLast();
            ls.waitingReaders++;<commit_msg>minor<commit_after>package org.basex.core;

import java.util.LinkedList;

/**
 * Management of executing read/write processes. Multiple readers, single
 * writers.
 *
 * @author Workgroup DBIS, University of Konstanz 2005-10, ISC License
 * @author <NAME>
 */
final class Semaphore {
  /** List of waiting processes for writers or reading groups. */
  private final LinkedList<Lock> waiting = new LinkedList<Lock>();

  /** States of locking. */
  private static enum State {
    /** Idle state. */ IDLE,
    /** Read state. */ READ,
    /** Write state. */ WRITE
  }

  /** State of the lock. */
  private State state = State.IDLE;
  /** Number of active readers. */
  private int activeR;

  /**
   * Modifications before executing a process.
   * @param w writing flag
   */
  void before(final boolean w) {
    if(w) {
      // exclusive lock
      final Lock lx = new Lock(true);
      synchronized(lx) {
        synchronized(this) {
          if(state == State.IDLE) {
            state = State.WRITE;
            return;
          }
          waiting.add(lx);
        }
        try {
          lx.wait();
          state = State.WRITE;
        } catch(final InterruptedException ex) {
          ex.printStackTrace();
        }
      }
    } else {
      // shared lock
      Lock ls = null;
      synchronized(this) {
        if(state != State.WRITE && waiting.size() == 0) {
          state = State.READ;
          activeR++;
          return;
        }
        if(waiting.size() > 0 && !waiting.getLast().writer) {
            ls = waiting.getLast();
            ls.waitingReaders++;
{'fraction_non_alphanumeric': 0.12732824427480915, 'fraction_numerical': 0.004885496183206107, 'mean_word_length': 1.8636363636363635, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, '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': '5097395', 'n_tokens_mistral': 973, 'n_tokens_neox': 945, 'n_words': 266}