<commit_before>#include "common.h"

#include <assert.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>


struct state* state;


int init_socket(int* mem)
{
    fputs("Connecting to socket...\n", stdout);

    int s = socket(AF_UNIX, SOCK_SEQPACKET, 0);
    struct sockaddr_un sa;
    sa.sun_family = AF_UNIX;
    strcpy(sa.sun_path, "socket");

    if (connect(s, (struct sockaddr*)&sa, sizeof(sa)) == -1) {
        warn("Can't connect to socket");
        return -1;
    };

    fputs("Receiving shared memory object...\n", stdout);

    char control[CMSG_SPACE(sizeof(int)) * 1];  // one cmsg with one int
    struct msghdr mh = {
        .msg_name = NULL,
        .msg_namelen = 0,
        .msg_iov = NULL,
        .msg_iovlen = 0,
        .msg_control = control,
        .msg_controllen = CMSG_SPACE(sizeof(int)) * 1,
        .msg_flags = 0
    };
    if (recvmsg(s, &mh, 0) == -1) {
        warn("Can't recvmsg() shared memory object");
        return -1;
    }
    struct cmsghdr* cmh = CMSG_FIRSTHDR(&mh);
    memcpy(mem, CMSG_DATA(cmh), sizeof(int));

    return s;
}


int main()
{
    int mem;
    int s = init_socket(&mem);
    if (s == -1)
        return 1;

    state = mmap(NULL, sizeof(struct state),
        PROT_READ | PROT_WRITE, MAP_SHARED, mem, 0);
    if (state == NULL) {
        warn("Can't mmap() shared memory object");
        return 1;
    }

    state->thruster_data.ls = 20;
    state->thruster_data.rs = 20;
    state->thruster_data.fl = 800;
    state->thruster_data.fr = 300;
    state->thruster_data.bl = 800;
    state->thruster_data.br = 300;

    int i = 0;
    while (1) {
        fputs("worker <-- manager ...\n", stdout);

        if (pthread_mutex_lock(&state->worker_mutexes[0]) == -1) {
            warn("Can't lock worker mutex");
            return 1;
        }
        while (state->worker_misses[0] == 0) {
            alarm(5);
            pthread_cond_wait(&state->worker_conds[0],
                &state->worker_mutexes[0]);
            alarm(0);
        }
        state->worker_misses[0] = 0;
        if (pthread_mutex_unlock(&state->worker_mutexes[0]) == -1) {
            warn("Can't unlock worker mutex");
            return 1;
        }

        fputs("    ", stdout);
        print_sensor_data(&state->sensor_data);

        if (i++ == 4) {
            fputs("Purposely spinning...\n", stdout);
            while (1) { __asm(""); }
        }

        ++state->thruster_data.ls;
        ++state->thruster_data.rs;
        ++state->thruster_data.fl;
        ++state->thruster_data.fr;
        ++state->thruster_data.bl;
        ++state->thruster_data.br;
    }
}<commit_msg>Don't print useless errors (pthread functions don't use errno)<commit_after>#include "common.h"

#include <assert.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>


struct state* state;


int init_socket(int* mem)
{
    fputs("Connecting to socket...\n", stdout);

    int s = socket(AF_UNIX, SOCK_SEQPACKET, 0);
    struct sockaddr_un sa;
    sa.sun_family = AF_UNIX;
    strcpy(sa.sun_path, "socket");

    if (connect(s, (struct sockaddr*)&sa, sizeof(sa)) == -1) {
        warn("Can't connect to socket");
        return -1;
    };

    fputs("Receiving shared memory object...\n", stdout);

    char control[CMSG_SPACE(sizeof(int)) * 1];  // one cmsg with one int
    struct msghdr mh = {
        .msg_name = NULL,
        .msg_namelen = 0,
        .msg_iov = NULL,
        .msg_iovlen = 0,
        .msg_control = control,
        .msg_controllen = CMSG_SPACE(sizeof(int)) * 1,
        .msg_flags = 0
    };
    if (recvmsg(s, &mh, 0) == -1) {
        warn("Can't recvmsg() shared memory object");
        return -1;
    }
    struct cmsghdr* cmh = CMSG_FIRSTHDR(&mh);
    memcpy(mem, CMSG_DATA(cmh), sizeof(int));

    return s;
}


int main()
{
    int mem;
    int s = init_socket(&mem);
    if (s == -1)
        return 1;

    state = mmap(NULL, sizeof(struct state),
        PROT_READ | PROT_WRITE, MAP_SHARED, mem, 0);
    if (state == NULL) {
        warn("Can't mmap() shared memory object");
        return 1;
    }

    state->thruster_data.ls = 20;
    state->thruster_data.rs = 20;
    state->thruster_data.fl = 800;
    state->thruster_data.fr = 300;
    state->thruster_data.bl = 800;
    state->thruster_data.br = 300;

    int i = 0;
    while (1) {
        fputs("worker <-- manager ...\n", stdout);

        if (pthread_mutex_lock(&state->worker_mutexes[0]) == -1) {
            warnx("Can't lock worker mutex");
            return 1;
        }
        while (state->worker_misses[0] == 0) {
            alarm(5);
            pthread_cond_wait(&state->worker_conds[0],
                &state->worker_mutexes[0]);
            alarm(0);
        }
        state->worker_misses[0] = 0;
        if (pthread_mutex_unlock(&state->worker_mutexes[0]) == -1) {
            warnx("Can't unlock worker mutex");
            return 1;
        }

        fputs("    ", stdout);
        print_sensor_data(&state->sensor_data);

        if (i++ == 4) {
            fputs("Purposely spinning...\n", stdout);
            while (1) { __asm(""); }
        }

        ++state->thruster_data.ls;
        ++state->thruster_data.rs;
        ++state->thruster_data.fl;
        ++state->thruster_data.fr;
        ++state->thruster_data.bl;
        ++state->thruster_data.br;
    }
}
{'fraction_non_alphanumeric': 0.15939687668282176, 'fraction_numerical': 0.01759109675103213, 'mean_word_length': 2.1533672891907187, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 61, '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': '7340813', 'n_tokens_mistral': 2129, 'n_tokens_neox': 2005, 'n_words': 418}