<commit_before>(* Lightweight thread library for Objective Caml
 * http://www.ocsigen.org/lwt
 * Module Lwt
 * Copyright (C) 2005-2008 <NAME>
 * Laboratoire PPS - CNRS Universit Paris Diderot
 *                    2009 <NAME>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation, with linking exceptions;
 * either version 2.1 of the License, or (at your option) any later
 * version. See COPYING file for details.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 *)

(* +-----------------------------------------------------------------+
   | Types                                                           |
   +-----------------------------------------------------------------+ *)

exception Canceled

type +'a t
type -'a u

type cancel_list
  (* Type of a lists of threads, with possibly different types. With
     existential types, it would be:

     {[
       type cancel_list = (exists 'a. 'a t) list
     ]}
  *)

external make_cancel_list : 'a t list -> cancel_list = "%identity"
external cast_cancel_list : cancel_list -> 'a t list = "%identity"

(* Reason for a thread to be a sleeping thread: *)
type sleep_reason =
  | Task
      (* It is a cancealable task *)
  | Wait
      (* It is a thread created with [wait] *)
  | Temp of cancel_list
      (* [Temp threads] is a temporary thread that is meant to be
         later connected to another one. [threads] is the list of
         threads to cancel when this one is cancelled. *)

and 'a thread_state =
  | Return of 'a
      (* [Return v] a terminated thread which has successfully
         terminated with the value [v] *)
  | Fail of exn
      (* [Fail exn] a terminated thread which has failed with the
         exception [exn] *)
  | Sleep of 'a sleeper
      (* [Sleep sleeper] is a sleeping thread *)
  | Repr of 'a thread_repr
      (* [Repr t] a thread which behaves the same as [t] *)

and 'a thread_repr = 'a thread_state ref

and 'a sleeper = {
  reason : sleep_reason;
  (* Reason why the trhead is sleeping *)
  mutable waiters : 'a waiter_set;
  (* All thunk functions *)
  mutable removed : int;
  (* Number of waiter that have been disabled. When this number
     reaches [max_removed], there are effectively removed from
     [waiters]. *)
}

(* A waiter which can be removed from its set: *)
and 'a removable = {
  waiter : 'a t -> unit;
  mutable active : bool;
}

(* Type of set of waiters: *)
and 'a waiter_set =
  | Empty
  | Removable of 'a removable
  | Immutable of ('a t -> unit)
  | Append of 'a waiter_set * 'a waiter_set

external thread_repr : 'a t -> 'a thread_repr = "%identity"
external thread : 'a thread_repr -> 'a t = "%identity"
external wakener : 'a thread_repr -> 'a u = "%identity"
external wakener_repr : 'a u -> 'a thread_repr = "%identity"

(* Maximum number of disabled waiters a waiter set may contains before
   it get cleaned: *)
let max_removed = 42

(* +-----------------------------------------------------------------+
   | Restarting/connecting threads                                   |
   +-----------------------------------------------------------------+ *)

(* Returns the represent of a thread, updating non-direct references: *)
let rec repr_rec t =
  match !t with
    | Repr t' -> let t'' = repr_rec t' in if t'' != t' then t := Repr t''; t''
    | _       -> t
let repr t = repr_rec (thread_repr t)

let rec run_waiters_rec t ws rem =
  match ws, rem with
    | Empty, [] ->
        ()
    | Empty, ws :: rem ->
        run_waiters_rec t ws rem
    | Immutable f, [] ->
        f t
    | Immutable f, ws :: rem ->
        f t;
        run_waiters_rec t ws rem
    | Removable w, [] ->
        if w.active then w.waiter t
    | Removable w, ws :: rem ->
        if w.active then w.waiter t;
        run_waiters_rec t ws rem
    | Append(ws1, ws2), _ ->
        run_waiters_rec t ws1 (ws2 :: rem)

(* Run all waiters waiting on [t]: *)
let run_waiters waiters t =
  run_waiters_rec (thread t) waiters []

(* Restarts a sleeping thread [t]:<commit_msg>typos in comments<commit_after>(* Lightweight thread library for Objective Caml
 * http://www.ocsigen.org/lwt
 * Module Lwt
 * Copyright (C) 2005-2008 <NAME>
 * Laboratoire PPS - CNRS Universit Paris Diderot
 *                    2009 <NAME>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation, with linking exceptions;
 * either version 2.1 of the License, or (at your option) any later
 * version. See COPYING file for details.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 *)

(* +-----------------------------------------------------------------+
   | Types                                                           |
   +-----------------------------------------------------------------+ *)

exception Canceled

type +'a t
type -'a u

type cancel_list
  (* Type of a lists of threads, with possibly different types. With
     existential types, it would be:

     {[
       type cancel_list = (exists 'a. 'a t) list
     ]}
  *)

external make_cancel_list : 'a t list -> cancel_list = "%identity"
external cast_cancel_list : cancel_list -> 'a t list = "%identity"

(* Reason for a thread to be a sleeping thread: *)
type sleep_reason =
  | Task
      (* It is a cancealable task *)
  | Wait
      (* It is a thread created with [wait] *)
  | Temp of cancel_list
      (* [Temp threads] is a temporary thread that is meant to be
         later connected to another one. [threads] is the list of
         threads to cancel when this one is cancelled. *)

and 'a thread_state =
  | Return of 'a
      (* [Return v] a terminated thread which has successfully
         terminated with the value [v] *)
  | Fail of exn
      (* [Fail exn] a terminated thread which has failed with the
         exception [exn] *)
  | Sleep of 'a sleeper
      (* [Sleep sleeper] is a sleeping thread *)
  | Repr of 'a thread_repr
      (* [Repr t] a thread which behaves the same as [t] *)

and 'a thread_repr = 'a thread_state ref

and 'a sleeper = {
  reason : sleep_reason;
  (* Reason why the thread is sleeping *)
  mutable waiters : 'a waiter_set;
  (* All thunk functions *)
  mutable removed : int;
  (* Number of waiter that have been disabled. When this number
     reaches [max_removed], they are effectively removed from
     [waiters]. *)
}

(* A waiter which can be removed from its set: *)
and 'a removable = {
  waiter : 'a t -> unit;
  mutable active : bool;
}

(* Type of set of waiters: *)
and 'a waiter_set =
  | Empty
  | Removable of 'a removable
  | Immutable of ('a t -> unit)
  | Append of 'a waiter_set * 'a waiter_set

external thread_repr : 'a t -> 'a thread_repr = "%identity"
external thread : 'a thread_repr -> 'a t = "%identity"
external wakener : 'a thread_repr -> 'a u = "%identity"
external wakener_repr : 'a u -> 'a thread_repr = "%identity"

(* Maximum number of disabled waiters a waiter set can contains before
   being cleaned: *)
let max_removed = 42

(* +-----------------------------------------------------------------+
   | Restarting/connecting threads                                   |
   +-----------------------------------------------------------------+ *)

(* Returns the representative of a thread, updating non-direct references: *)
let rec repr_rec t =
  match !t with
    | Repr t' -> let t'' = repr_rec t' in if t'' != t' then t := Repr t''; t''
    | _       -> t
let repr t = repr_rec (thread_repr t)

let rec run_waiters_rec t ws rem =
  match ws, rem with
    | Empty, [] ->
        ()
    | Empty, ws :: rem ->
        run_waiters_rec t ws rem
    | Immutable f, [] ->
        f t
    | Immutable f, ws :: rem ->
        f t;
        run_waiters_rec t ws rem
    | Removable w, [] ->
        if w.active then w.waiter t
    | Removable w, ws :: rem ->
        if w.active then w.waiter t;
        run_waiters_rec t ws rem
    | Append(ws1, ws2), _ ->
        run_waiters_rec t ws1 (ws2 :: rem)

(* Run all waiters waiting on [t]: *)
let run_waiters waiters t =
  run_waiters_rec (thread t) waiters []

(* Restarts a sleeping thread [t]:
{'fraction_non_alphanumeric': 0.14827851721482785, 'fraction_numerical': 0.007479925200747992, 'mean_word_length': 2.103071672354949, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '701020', 'n_tokens_mistral': 2710, 'n_tokens_neox': 2581, 'n_words': 1120}