Segmentation error - pthread mutex lock problem

I am creating a simple kdb + library that generates values ​​in a separate thread and acts as a callback when the data is ready. The application writes data to the file descriptor in a new stream and reads from it in the main event loop. When you try to lock and unlock the mutex, the application turns out to be segfault,

If I put a little sleep into the loop, segfault seems to disappear. This assumes that the call pthread_mutex_lockdoes not block the thread until a block is received, as I expected.

#include <k.h>
#include <pthread.h>
#include <time.h>

#define PIPE_CAPACITY 65536

static int fd;
static pthread_t thread;
static pthread_mutex_t lock;

K callback(int d)
{
    K data;

    // Aquire mutex lock and read from fd
    pthread_mutex_lock(&lock);
    read(d, &data, PIPE_CAPACITY);
    pthread_mutex_unlock(&lock);

    // kdb+ callback
    k(0, (char *)"callback", r1(data), (K)0);

    return (K)0;
}

void* loop()
{
    while (1) {
        struct timespec ts;
        struct tm *time;

        // Get seconds and nanoseconds since epoch
        clock_gettime(CLOCK_REALTIME, &ts);

        // Adjust for kdb+
        time = gmtime(&ts.tv_sec);
        time->tm_sec = 0;
        time->tm_min = 0;
        time->tm_hour = 0;
        ts.tv_sec -= mktime(time); // Subtract seconds between epoch and midnight

        // Create kdb+ timestamp
        K data = ktj(-KN, ts.tv_sec * 1000000000 + ts.tv_nsec);

        // Aquire mutex lock and write to fd
        pthread_mutex_lock(&lock);
        write(fd, &data, sizeof(K));
        pthread_mutex_unlock(&lock);
    }
}

K init()
{
    // Initialize mutex
    pthread_mutex_init(&lock, NULL);

    // Create file descriptor
    fd = eventfd(0, 0);

    // Register callback
    sd1(fd, callback);

    // Launch thread
    pthread_create(&thread, NULL, loop, NULL);
}
+4
source share
1 answer

, K - , k.h :

typedef struct k0{..}*K;

, , , , . , kdb + . .

read(d, &data, PIPE_CAPACITY);

65536 , 8- . , segfault, , , 8 .

, , , eventfd, -. pipe().

:

#include <k.h>
#include <pthread.h>
#include <time.h>
#include <unistd.h>

static int fd[2];
static pthread_t thread;
static pthread_mutex_t lock;

K callback(int d)
{
    K data = ktj(-KN, 0);

    // Aquire mutex lock and read from fd
    pthread_mutex_lock(&lock);
    read(d, (void *)&data->j, sizeof(data->j));
    pthread_mutex_unlock(&lock);

    // kdb+ callback
    k(0, (char *)"callback", data, (K)0);

    return (K)0;
}

void* loop()
{
    while (1) {
        struct timespec ts;
        struct tm *time;

        // Get seconds and nanoseconds since epoch
        clock_gettime(CLOCK_REALTIME, &ts);

        // Adjust for kdb+
        time = gmtime(&ts.tv_sec);
        time->tm_sec = 0;
        time->tm_min = 0;
        time->tm_hour = 0;
        ts.tv_sec -= mktime(time); // Subtract seconds between epoch and midnight

        // Create kdb+ timestamp
        J data = (J)ts.tv_sec * 1000000000 + ts.tv_nsec;

        // Aquire mutex lock and write to fd
        pthread_mutex_lock(&lock);
        write(fd[1], &data, sizeof(data));
        pthread_mutex_unlock(&lock);
    }
}

K1(init)
{
    // Initialize mutex
    pthread_mutex_init(&lock, NULL);

    // Create file descriptor
    pipe(fd);

    // Register callback
    sd1(fd[0], callback);

    // Launch thread
    pthread_create(&thread, NULL, loop, NULL);

    R ktj(0, 0);
}

, x.c,

$ gcc -Wall -shared -fPIC -I $(pwd) -DKXVER=3 x.c -o x.so

q:

callback:0N!
init:`:./x 2:(`init;1)
init[]
+4

Source: https://habr.com/ru/post/1668365/


All Articles