I am creating a simple kdb+ shared library which generates values on a seperate thread and executes a callback function when data is ready. The application is writing data to a file descriptor in the new thread and reading from this in the main event loop. The application appears to be segfaulting when trying to lock and unlock the mutex.
If I introduce a small sleep into the loop, the segfault seems to disappear. This would suggest that the pthread_mutex_lock
call is not blocking the thread until a lock is obtained as I would have 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);
}