<commit_before>%%%----------------------------------------------------------------------
%%% File        : rep.erl
%%% Authors     : <NAME> <<EMAIL>>
%%%               <NAME> <<EMAIL>>
%%% Description : Replacement BIFs
%%% Created     : 11 Jan 2010
%%%----------------------------------------------------------------------

-module(rep).

-export([rep_after_notify/0, rep_demonitor/1, rep_demonitor/2,
	 rep_exit/2, rep_halt/0, rep_halt/1, rep_link/1,
	 rep_monitor/2, rep_process_flag/2, rep_receive/1,
         rep_receive_block/0, rep_receive_notify/1,
         rep_receive_notify/2, rep_register/2, rep_send/2,
	 rep_send/3, rep_spawn/1, rep_spawn/3, rep_spawn_link/1,
	 rep_spawn_link/3, rep_spawn_monitor/1, rep_spawn_monitor/3,
	 rep_spawn_opt/2, rep_spawn_opt/4, rep_unlink/1,
	 rep_unregister/1, rep_whereis/1]).

-include("gen.hrl").

%% The destination of a `send' operation.
-type dest() :: pid() | port() | atom() | {atom(), node()}.

%% @spec: rep_demonitor(reference()) -> 'true'
%% @doc: Replacement for `demonitor/1'.
%%
%% Just yield after demonitoring.
-spec rep_demonitor(reference()) -> 'true'.

rep_demonitor(Ref) ->
    Result = demonitor(Ref),
    sched:notify(demonitor, Ref),
    Result.

%% @spec: rep_demonitor(reference(), ['flush' | 'info']) -> 'true'
%% @doc: Replacement for `demonitor/2'.
%%
%% Just yield after demonitoring.
-spec rep_demonitor(reference(), ['flush' | 'info']) -> 'true'.

rep_demonitor(Ref, Opts) ->
    Result = demonitor(Ref, Opts),
    sched:notify(demonitor, Ref),
    Result.

%% @spec: rep_exit(pid(), term()) -> true
%% @doc: Replacement for `exit/2'.
%%
%% Just send exit signal and yield.
-spec rep_exit(pid(), term()) -> 'true'.

rep_exit(Pid, Reason) ->
    Result = exit(Pid, Reason),
    sched:notify(fun_exit, {Pid, Reason}),
    Result.

%% @spec: rep_halt() -> no_return()
%% @doc: Replacement for `halt/0'.
%%
%% Just send halt message and yield.
-spec rep_halt() -> no_return().

rep_halt() ->
    sched:notify(halt, empty).

%% @spec: rep_halt() -> no_return()
%% @doc: Replacement for `halt/1'.
%%
%% Just send halt message and yield.
-spec rep_halt(non_neg_integer() | string()) -> no_return().

rep_halt(Status) ->
    sched:notify(halt, Status).

%% @spec: rep_link(pid() | port()) -> 'true'
%% @doc: Replacement for `link/1'.
%%
%% Just yield after linking.
-spec rep_link(pid() | port()) -> 'true'.

rep_link(Pid) ->
    Result = link(Pid),
    sched:notify(link, Pid),
    Result.

%% @spec: rep_monitor('process', pid() | {atom(), node()} | atom()) ->
%%                           reference()
%% @doc: Replacement for `monitor/2'.
%%
%% Just yield after monitoring.
-spec rep_monitor('process', pid() | {atom(), node()} | atom()) ->
                         reference().

rep_monitor(Type, Item) ->
    Ref = monitor(Type, Item),
    NewItem = find_pid(Item),
    sched:notify(monitor, {NewItem, Ref}),
    Ref.

%% @spec: rep_process_flag('trap_exit', boolean()) -> boolean();
%%                        ('error_handler', atom()) -> atom();
%%                        ('min_heap_size', non_neg_integer()) ->
%%                                non_neg_integer();
%%                        ('min_bin_vheap_size', non_neg_integer()) ->
%%                                non_neg_integer();
%%                        ('priority', process_priority_level()) ->
%%                                process_priority_level();
%%                        ('save_calls', non_neg_integer()) ->
%%                                non_neg_integer();
%%                        ('sensitive', boolean()) -> boolean()
%% @doc: Replacement for `process_flag/2'.
%%
%% Just yield after altering the process flag.
-type process_priority_level() :: 'max' | 'high' | 'normal' | 'low'.
-spec rep_process_flag('trap_exit', boolean()) -> boolean();
                      ('error_handler', atom()) -> atom();
                      ('min_heap_size', non_neg_integer()) -> non_neg_integer();
                      ('min_bin_vheap_size', non_neg_integer()) ->
                              non_neg_integer();
                      ('priority', process_priority_level()) ->
                              process_priority_level();
                      ('save_calls', non_neg_integer()) -> non_neg_integer();
                      ('sensitive', boolean()) -> boolean().

rep_process_flag(trap_exit = Flag, Value) ->
    Result = process_flag(Flag, Value),
    sched:notify(process_flag, {Flag, Value}),
    Result;
rep_process_flag(Flag, Value) ->
    process_flag(Flag, Value).

%% @spec rep_receive(fun((function()) -> term())) -> term()
%% @doc: Function called right before a receive statement.
%%
%% If a matching message is found in the process' message queue, continue
%% to actual receive statement, else block and when unblocked do the same.
-spec rep_receive(fun((term()) -> 'block' | 'continue')) -> 'ok'.

rep_receive(Fun) ->
    {messages, Mailbox} = process_info(self(), messages),
    case rep_receive_match(Fun, Mailbox) of
	block ->
	    sched:block(),
	    rep_receive_loop(Fun);
	continue -> ok
    end.

rep_receive_loop(Fun) ->
    {messages, Mailbox} = process_info(self(), messages),
    case rep_receive_match(Fun, Mailbox) of
	block -> rep_receive_loop(Fun);
	continue ->
	    sched:wakeup(),
	    sched:wait(),
	    ok
    end.

%% Blocks forever (used for 'receive after infinity -> ...' expressions).
-spec rep_receive_block() -> no_return().

rep_receive_block() ->
    sched:block(),
    rep_receive_block().

rep_receive_match(_Fun, []) ->
    block;<commit_msg>Ignore rep_receive for unknown processes See #66<commit_after>%%%----------------------------------------------------------------------
%%% File        : rep.erl
%%% Authors     : <NAME> <<EMAIL>>
%%%               <NAME> <<EMAIL>>
%%% Description : Replacement BIFs
%%% Created     : 11 Jan 2010
%%%----------------------------------------------------------------------

-module(rep).

-export([rep_after_notify/0, rep_demonitor/1, rep_demonitor/2,
	 rep_exit/2, rep_halt/0, rep_halt/1, rep_link/1,
	 rep_monitor/2, rep_process_flag/2, rep_receive/1,
         rep_receive_block/0, rep_receive_notify/1,
         rep_receive_notify/2, rep_register/2, rep_send/2,
	 rep_send/3, rep_spawn/1, rep_spawn/3, rep_spawn_link/1,
	 rep_spawn_link/3, rep_spawn_monitor/1, rep_spawn_monitor/3,
	 rep_spawn_opt/2, rep_spawn_opt/4, rep_unlink/1,
	 rep_unregister/1, rep_whereis/1]).

-include("gen.hrl").

%% The destination of a `send' operation.
-type dest() :: pid() | port() | atom() | {atom(), node()}.

%% @spec: rep_demonitor(reference()) -> 'true'
%% @doc: Replacement for `demonitor/1'.
%%
%% Just yield after demonitoring.
-spec rep_demonitor(reference()) -> 'true'.

rep_demonitor(Ref) ->
    Result = demonitor(Ref),
    sched:notify(demonitor, Ref),
    Result.

%% @spec: rep_demonitor(reference(), ['flush' | 'info']) -> 'true'
%% @doc: Replacement for `demonitor/2'.
%%
%% Just yield after demonitoring.
-spec rep_demonitor(reference(), ['flush' | 'info']) -> 'true'.

rep_demonitor(Ref, Opts) ->
    Result = demonitor(Ref, Opts),
    sched:notify(demonitor, Ref),
    Result.

%% @spec: rep_exit(pid(), term()) -> true
%% @doc: Replacement for `exit/2'.
%%
%% Just send exit signal and yield.
-spec rep_exit(pid(), term()) -> 'true'.

rep_exit(Pid, Reason) ->
    Result = exit(Pid, Reason),
    sched:notify(fun_exit, {Pid, Reason}),
    Result.

%% @spec: rep_halt() -> no_return()
%% @doc: Replacement for `halt/0'.
%%
%% Just send halt message and yield.
-spec rep_halt() -> no_return().

rep_halt() ->
    sched:notify(halt, empty).

%% @spec: rep_halt() -> no_return()
%% @doc: Replacement for `halt/1'.
%%
%% Just send halt message and yield.
-spec rep_halt(non_neg_integer() | string()) -> no_return().

rep_halt(Status) ->
    sched:notify(halt, Status).

%% @spec: rep_link(pid() | port()) -> 'true'
%% @doc: Replacement for `link/1'.
%%
%% Just yield after linking.
-spec rep_link(pid() | port()) -> 'true'.

rep_link(Pid) ->
    Result = link(Pid),
    sched:notify(link, Pid),
    Result.

%% @spec: rep_monitor('process', pid() | {atom(), node()} | atom()) ->
%%                           reference()
%% @doc: Replacement for `monitor/2'.
%%
%% Just yield after monitoring.
-spec rep_monitor('process', pid() | {atom(), node()} | atom()) ->
                         reference().

rep_monitor(Type, Item) ->
    Ref = monitor(Type, Item),
    NewItem = find_pid(Item),
    sched:notify(monitor, {NewItem, Ref}),
    Ref.

%% @spec: rep_process_flag('trap_exit', boolean()) -> boolean();
%%                        ('error_handler', atom()) -> atom();
%%                        ('min_heap_size', non_neg_integer()) ->
%%                                non_neg_integer();
%%                        ('min_bin_vheap_size', non_neg_integer()) ->
%%                                non_neg_integer();
%%                        ('priority', process_priority_level()) ->
%%                                process_priority_level();
%%                        ('save_calls', non_neg_integer()) ->
%%                                non_neg_integer();
%%                        ('sensitive', boolean()) -> boolean()
%% @doc: Replacement for `process_flag/2'.
%%
%% Just yield after altering the process flag.
-type process_priority_level() :: 'max' | 'high' | 'normal' | 'low'.
-spec rep_process_flag('trap_exit', boolean()) -> boolean();
                      ('error_handler', atom()) -> atom();
                      ('min_heap_size', non_neg_integer()) -> non_neg_integer();
                      ('min_bin_vheap_size', non_neg_integer()) ->
                              non_neg_integer();
                      ('priority', process_priority_level()) ->
                              process_priority_level();
                      ('save_calls', non_neg_integer()) -> non_neg_integer();
                      ('sensitive', boolean()) -> boolean().

rep_process_flag(trap_exit = Flag, Value) ->
    Result = process_flag(Flag, Value),
    sched:notify(process_flag, {Flag, Value}),
    Result;
rep_process_flag(Flag, Value) ->
    process_flag(Flag, Value).

%% @spec rep_receive(fun((function()) -> term())) -> term()
%% @doc: Function called right before a receive statement.
%%
%% If a matching message is found in the process' message queue, continue
%% to actual receive statement, else block and when unblocked do the same.
-spec rep_receive(fun((term()) -> 'block' | 'continue')) -> 'ok'.

rep_receive(Fun) ->
    case lid:from_pid(self()) of
	not_found -> ok;
	_Lid ->
	    {messages, Mailbox} = process_info(self(), messages),
	    case rep_receive_match(Fun, Mailbox) of
		block ->
		    sched:block(),
		    rep_receive_loop(Fun);
		continue -> ok
	    end
    end.

rep_receive_loop(Fun) ->
    {messages, Mailbox} = process_info(self(), messages),
    case rep_receive_match(Fun, Mailbox) of
	block -> rep_receive_loop(Fun);
	continue ->
	    sched:wakeup(),
	    sched:wait(),
	    ok
    end.

%% Blocks forever (used for 'receive after infinity -> ...' expressions).
-spec rep_receive_block() -> no_return().

rep_receive_block() ->
    sched:block(),
    sched:wait(),
    rep_receive_block().

rep_receive_match(_Fun, []) ->
    block;
{'fraction_non_alphanumeric': 0.19555118813650169, 'fraction_numerical': 0.007504020010720028, 'mean_word_length': 2.538242730720607, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 119, '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': '2425180', 'n_tokens_mistral': 3616, 'n_tokens_neox': 3489, 'n_words': 787}