<commit_before>
#include "process.h"

#include <stdio.h>

struct thorium_script process_script = {
    .identifier = SCRIPT_PROCESS,
    .init = process_init,
    .destroy = process_destroy,
    .receive = process_receive,
    .size = sizeof(struct process),
    .name = "process"
};

void process_init(struct thorium_actor *actor)
{
    struct process *process1;

    process1 = (struct process *)thorium_actor_concrete_actor(actor);
    process1->clone = -1;
    process1->value = 42;
    process1->ready = 0;
    process1->cloned = 0;
    thorium_actor_send_to_self_empty(actor, ACTION_PACK_ENABLE);
}

void process_destroy(struct thorium_actor *actor)
{
    struct process *process1;

    process1 = (struct process *)thorium_actor_concrete_actor(actor);
    process1->clone = -1;
}

void process_receive(struct thorium_actor *actor, struct thorium_message *message)
{
    int tag;
    int name;
    void *buffer;
    struct process *process1;
    int i;
    int other;
    struct thorium_message new_message;

    process1 = (struct process *)thorium_actor_concrete_actor(actor);
    tag = thorium_message_tag(message);
    name = thorium_actor_name(actor);
    buffer = thorium_message_buffer(message);

    if (tag == ACTION_START) {

        bsal_vector_init(&process1->initial_processes, 0);
        bsal_vector_unpack(&process1->initial_processes, buffer);

        for (i = 0; i < bsal_vector_size(&process1->initial_processes); i++) {

            other = bsal_vector_at_as_int(&process1->initial_processes, i);

            if (other != name) {
                break;
            }
        }

        process1->value = 89 * thorium_actor_name(actor);
        printf("Hi, I am actor:%d and my value is %d. I will clone myself using %d as the spawner\n",
                        name, process1->value, other);

        thorium_message_init(&new_message, ACTION_CLONE, sizeof(other), &other);

        /* create 2 clones
         * to test the capacity of the runtime to queue messages
         * during cloning
         */
        thorium_actor_send_to_self(actor, &new_message);
        thorium_actor_send_to_self(actor, &new_message);

    } else if (tag == ACTION_CLONE_REPLY) {

        process1->clone = *(int *)buffer;

        printf("New clone is actor:%d\n", process1->clone);

        thorium_actor_send_empty(actor, process1->clone, ACTION_ASK_TO_STOP);

        process1->cloned++;

        /* wait for the second clone
         */
        if (process1->cloned != 2) {
            return;
        }

        if (bsal_vector_at_as_int(&process1->initial_processes, 0) == name) {
            thorium_actor_synchronize(actor, &process1->initial_processes);
        }

        if (process1->ready) {<commit_msg>ACTION_CLONE messages can not currently be nested<commit_after>
#include "process.h"

#include <stdio.h>

struct thorium_script process_script = {
    .identifier = SCRIPT_PROCESS,
    .init = process_init,
    .destroy = process_destroy,
    .receive = process_receive,
    .size = sizeof(struct process),
    .name = "process"
};

void process_init(struct thorium_actor *actor)
{
    struct process *process1;

    process1 = (struct process *)thorium_actor_concrete_actor(actor);
    process1->clone = -1;
    process1->value = 42;
    process1->ready = 0;
    process1->cloned = 0;
    thorium_actor_send_to_self_empty(actor, ACTION_PACK_ENABLE);
}

void process_destroy(struct thorium_actor *actor)
{
    struct process *process1;

    process1 = (struct process *)thorium_actor_concrete_actor(actor);
    process1->clone = -1;
}

void process_receive(struct thorium_actor *actor, struct thorium_message *message)
{
    int tag;
    int name;
    void *buffer;
    struct process *process1;
    int i;
    int other;
    struct thorium_message new_message;

    process1 = (struct process *)thorium_actor_concrete_actor(actor);
    tag = thorium_message_tag(message);
    name = thorium_actor_name(actor);
    buffer = thorium_message_buffer(message);

    if (tag == ACTION_START) {

        bsal_vector_init(&process1->initial_processes, 0);
        bsal_vector_unpack(&process1->initial_processes, buffer);

        for (i = 0; i < bsal_vector_size(&process1->initial_processes); i++) {

            other = bsal_vector_at_as_int(&process1->initial_processes, i);

            if (other != name) {
                break;
            }
        }

        process1->value = 89 * thorium_actor_name(actor);
        printf("Hi, I am actor:%d and my value is %d. I will clone myself using %d as the spawner\n",
                        name, process1->value, other);

        /*
         * Apparently, the code does not work when nesting ACTION_CLONE messages.
         */
        thorium_message_init(&new_message, ACTION_CLONE, sizeof(other), &other);

        /* create 2 clones
         * to test the capacity of the runtime to queue messages
         * during cloning
         */
        thorium_actor_send_to_self(actor, &new_message);
        /*thorium_actor_send_to_self(actor, &new_message);*/

    } else if (tag == ACTION_CLONE_REPLY) {

        process1->clone = *(int *)buffer;

        printf("New clone is actor:%d\n", process1->clone);

        thorium_actor_send_empty(actor, process1->clone, ACTION_ASK_TO_STOP);

        process1->cloned++;

        /* wait for the second clone
         */
        if (process1->cloned != 1) {
            return;
        }

        if (bsal_vector_at_as_int(&process1->initial_processes, 0) == name) {
            thorium_actor_synchronize(actor, &process1->initial_processes);
        }

        if (process1->ready) {
{'fraction_non_alphanumeric': 0.1079393398751115, 'fraction_numerical': 0.013559322033898305, 'mean_word_length': 2.7955314827352744, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 43, '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': '4477914', 'n_tokens_mistral': 1841, 'n_tokens_neox': 1723, 'n_words': 392}