<commit_before>﻿namespace RemObjects.Elements.System;

interface

uses
  Foundation;

{$G+}

method __ElementsClassLockEnter(var x: intptr_t): Boolean; public;
method __ElementsClassLockExit(var x: intptr_t); public;

method __ElementsReadLine(): String; public;

implementation

method __ElementsClassLockEnter(var x: intptr_t): Boolean;
begin
  if x = -1 then exit false;
  var cid: uint64_t;
  pthread_threadid_np(nil, var cid);

  var lValue := interlockedCompareExchange(var x, cid, 0); // returns old

  if lValue = 0 then exit true; // value was zero, we should run.
  if lValue = cid then exit false; // same thread, but already running, don't go again.
  if lValue = -1 then exit false; // it's done in the mean time, we should exit.

  // At this point it's NOT the same thread, and not done loading yet.
  repeat
    sched_yield;
    usleep(10);
  until interlockedCompareExchange(var x, -1, -1) = -1;
  exit false;<commit_msg>77557: Toffee and island: include info on what was nil in NRE message<commit_after>﻿namespace RemObjects.Elements.System;

interface

uses
  Foundation;

{$G+}

method __ElementsClassLockEnter(var x: intptr_t): Boolean; public;
method __ElementsClassLockExit(var x: intptr_t); public;

method __ElementsReadLine(): String; public;
method __ElementsRaiseException(s: String); public;

implementation

method __ElementsRaiseException(s: String);
begin 
  if s = nil then 
    NSException.&raise('NullReferenceException') format('Null Reference Exception');
  NSException.&raise('NullReferenceException') format('Null Reference Exception for expression: %@', s);
end;

method __ElementsClassLockEnter(var x: intptr_t): Boolean;
begin
  if x = -1 then exit false;
  var cid: uint64_t;
  pthread_threadid_np(nil, var cid);

  var lValue := interlockedCompareExchange(var x, cid, 0); // returns old

  if lValue = 0 then exit true; // value was zero, we should run.
  if lValue = cid then exit false; // same thread, but already running, don't go again.
  if lValue = -1 then exit false; // it's done in the mean time, we should exit.

  // At this point it's NOT the same thread, and not done loading yet.
  repeat
    sched_yield;
    usleep(10);
  until interlockedCompareExchange(var x, -1, -1) = -1;
  exit false;
{'fraction_non_alphanumeric': 0.09911111111111111, 'fraction_numerical': 0.012, 'mean_word_length': 4.308962264150943, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '1501808', 'n_tokens_mistral': 732, 'n_tokens_neox': 692, 'n_words': 261}