"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.adjustRead = void 0;
exports.adjustRead_ = adjustRead_;

var tsplus_module_1 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/stm/TRef/operations/unsafeGet"));

var tsplus_module_2 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/stm/TReentrantLock/operations/ReadLock/adjust"));

var tsplus_module_3 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/stm/TRef/operations/unsafeSet"));

var tsplus_module_4 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@tsplus/stdlib/structure/Equals"));

var tsplus_module_5 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/stm/TReentrantLock/definition"));

var tsplus_module_6 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/stm/TReentrantLock/operations/WriteLock/make"));

var tsplus_module_7 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/stm/STM/operations/effect"));

var _STM = /*#__PURE__*/require("@effect/core/stm/STM");

var _InternalReadLock = /*#__PURE__*/require("@effect/core/stm/TReentrantLock/operations/_internal/InternalReadLock");

var _InternalTReentrantLock = /*#__PURE__*/require("@effect/core/stm/TReentrantLock/operations/_internal/InternalTReentrantLock");

var _InternalWriteLock = /*#__PURE__*/require("@effect/core/stm/TReentrantLock/operations/_internal/InternalWriteLock");

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

/**
 * @tsplus fluent ets/TReentrantLock adjustRead
 */
function adjustRead_(self, delta) {
  void 0;
  return tsplus_module_7.effect((journal, fiberId) => {
    const lock = tsplus_module_1.unsafeGet_(self.data, journal);

    if (lock instanceof _InternalReadLock.InternalReadLock) {
      const res = tsplus_module_2.adjust_(lock, fiberId, delta);
      tsplus_module_3.unsafeSet_(self.data, res, journal);
      return res.readLocksHeld(fiberId);
    }

    if (lock instanceof _InternalWriteLock.InternalWriteLock && tsplus_module_4.equals(lock.fiberId, fiberId)) {
      const newTotal = lock.readLocks + delta;

      if (newTotal < 0) {
        throw new Error(`Defect: Fiber ${fiberId} releasing read locks it does not hold, newTotal: ${newTotal}`);
      }

      tsplus_module_3.unsafeSet_(self.data, tsplus_module_6.make(lock.writeLocks, newTotal, fiberId), journal);
      return newTotal;
    }

    throw new _STM.STMRetryException();
  });
}
/**
 * @tsplus static ets/TReentrantLock/Aspects adjustRead
 */


const adjustRead = delta => self => adjustRead_(self, delta);

exports.adjustRead = adjustRead;
//# sourceMappingURL=adjustRead.js.map