1
0
mirror of git://erdgeist.org/opentracker synced 2025-01-26 06:36:26 +00:00
opentracker/ot_mutex.c

275 lines
7.2 KiB
C
Raw Normal View History

/* This software was written by Dirk Engling <erdgeist@erdgeist.org>
2007-12-20 05:59:34 +00:00
It is considered beerware. Prost. Skol. Cheers or whatever.
2008-10-28 01:27:22 +00:00
2007-12-20 05:59:34 +00:00
$id$ */
/* System */
#include <pthread.h>
#include <stdio.h>
2007-11-14 12:59:58 +00:00
#include <stdlib.h>
#include <sys/mman.h>
2007-12-03 00:52:05 +00:00
#include <sys/uio.h>
/* Libowfat */
#include "byte.h"
2007-12-03 00:52:05 +00:00
#include "io.h"
#include "uint32.h"
/* Opentracker */
2024-04-13 00:47:29 +02:00
#include "ot_iovec.h"
#include "ot_mutex.h"
#include "ot_stats.h"
2024-04-15 00:39:02 +02:00
#include "trackerlogic.h"
2009-01-03 03:25:37 +00:00
/* #define MTX_DBG( STRING ) fprintf( stderr, STRING ) */
2024-04-15 00:39:02 +02:00
#define MTX_DBG(STRING)
/* Our global all torrents list */
2024-04-15 00:39:02 +02:00
static ot_vector all_torrents[OT_BUCKET_COUNT];
2021-04-19 03:25:18 +02:00
static pthread_mutex_t bucket_mutex[OT_BUCKET_COUNT];
2024-04-15 00:39:02 +02:00
static size_t g_torrent_count;
/* Self pipe from opentracker.c */
2024-04-15 00:39:02 +02:00
extern int g_self_pipe[2];
2024-04-15 00:39:02 +02:00
ot_vector *mutex_bucket_lock(int bucket) {
pthread_mutex_lock(bucket_mutex + bucket);
return all_torrents + bucket;
}
2024-04-15 00:39:02 +02:00
ot_vector *mutex_bucket_lock_by_hash(ot_hash const hash) { return mutex_bucket_lock(uint32_read_big((const char *)hash) >> OT_BUCKET_COUNT_SHIFT); }
2024-04-15 00:39:02 +02:00
void mutex_bucket_unlock(int bucket, int delta_torrentcount) {
2021-04-19 03:25:18 +02:00
pthread_mutex_unlock(bucket_mutex + bucket);
g_torrent_count += delta_torrentcount;
2008-10-28 01:27:22 +00:00
}
2024-04-15 00:39:02 +02:00
void mutex_bucket_unlock_by_hash(ot_hash const hash, int delta_torrentcount) {
mutex_bucket_unlock(uint32_read_big((char *)hash) >> OT_BUCKET_COUNT_SHIFT, delta_torrentcount);
}
2024-04-15 00:39:02 +02:00
size_t mutex_get_torrent_count() { return g_torrent_count; }
2007-11-14 12:59:58 +00:00
/* TaskQueue Magic */
struct ot_task {
ot_taskid taskid;
ot_tasktype tasktype;
int64 sock;
2007-11-14 12:59:58 +00:00
int iovec_entries;
struct iovec *iovec;
struct ot_task *next;
};
2024-04-15 00:39:02 +02:00
static ot_taskid next_free_taskid = 1;
static struct ot_task *tasklist;
2007-11-14 12:59:58 +00:00
static pthread_mutex_t tasklist_mutex;
2024-04-15 00:39:02 +02:00
static pthread_cond_t tasklist_being_filled;
2007-11-14 12:59:58 +00:00
2024-04-15 00:39:02 +02:00
int mutex_workqueue_pushtask(int64 sock, ot_tasktype tasktype) {
struct ot_task **tmptask, *task;
2007-11-14 12:59:58 +00:00
2024-04-15 00:39:02 +02:00
task = malloc(sizeof(struct ot_task));
if (!task)
2007-11-14 12:59:58 +00:00
return -1;
task->taskid = 0;
task->tasktype = tasktype;
task->sock = sock;
2007-11-14 12:59:58 +00:00
task->iovec_entries = 0;
task->iovec = NULL;
task->next = 0;
2024-03-08 16:59:26 +01:00
/* Want exclusive access to tasklist */
2024-04-15 00:39:02 +02:00
pthread_mutex_lock(&tasklist_mutex);
2024-03-08 16:59:26 +01:00
/* Skip to end of list */
tmptask = &tasklist;
2024-04-15 00:39:02 +02:00
while (*tmptask)
2024-03-08 16:59:26 +01:00
tmptask = &(*tmptask)->next;
*tmptask = task;
2007-11-14 12:59:58 +00:00
/* Inform waiting workers and release lock */
2024-04-15 00:39:02 +02:00
pthread_cond_broadcast(&tasklist_being_filled);
pthread_mutex_unlock(&tasklist_mutex);
2007-11-14 12:59:58 +00:00
return 0;
}
2024-04-15 00:39:02 +02:00
void mutex_workqueue_canceltask(int64 sock) {
struct ot_task **task;
2007-11-14 12:59:58 +00:00
/* Want exclusive access to tasklist */
2024-04-15 00:39:02 +02:00
pthread_mutex_lock(&tasklist_mutex);
2007-11-14 12:59:58 +00:00
2024-03-08 16:59:26 +01:00
for (task = &tasklist; *task; task = &((*task)->next))
if ((*task)->sock == sock) {
2024-04-15 00:39:02 +02:00
struct iovec *iovec = (*task)->iovec;
2024-03-08 16:59:26 +01:00
struct ot_task *ptask = *task;
2024-04-15 00:39:02 +02:00
int i;
2007-11-14 12:59:58 +00:00
2024-03-08 16:59:26 +01:00
/* Free task's iovec */
2024-04-15 00:39:02 +02:00
for (i = 0; i < (*task)->iovec_entries; ++i)
free(iovec[i].iov_base);
2007-11-14 12:59:58 +00:00
2024-03-08 16:59:26 +01:00
*task = (*task)->next;
2024-04-15 00:39:02 +02:00
free(ptask);
2024-03-08 16:59:26 +01:00
break;
}
2007-11-14 12:59:58 +00:00
/* Release lock */
2024-04-15 00:39:02 +02:00
pthread_mutex_unlock(&tasklist_mutex);
2007-11-14 12:59:58 +00:00
}
2024-04-15 00:39:02 +02:00
ot_taskid mutex_workqueue_poptask(ot_tasktype *tasktype) {
struct ot_task *task;
ot_taskid taskid = 0;
2007-11-14 12:59:58 +00:00
/* Want exclusive access to tasklist */
2024-04-15 00:39:02 +02:00
pthread_mutex_lock(&tasklist_mutex);
2007-11-14 12:59:58 +00:00
2024-04-15 00:39:02 +02:00
while (!taskid) {
2007-11-14 12:59:58 +00:00
/* Skip to the first unassigned task this worker wants to do */
2024-03-08 16:59:26 +01:00
for (task = tasklist; task; task = task->next)
2024-04-15 00:39:02 +02:00
if (!task->taskid && (TASK_CLASS_MASK & task->tasktype) == *tasktype) {
2024-03-08 16:59:26 +01:00
/* If we found an outstanding task, assign a taskid to it
and leave the loop */
task->taskid = taskid = ++next_free_taskid;
2024-04-15 00:39:02 +02:00
*tasktype = task->tasktype;
2024-03-08 16:59:26 +01:00
break;
}
/* Wait until the next task is being fed */
if (!taskid)
2024-04-15 00:39:02 +02:00
pthread_cond_wait(&tasklist_being_filled, &tasklist_mutex);
2007-11-14 12:59:58 +00:00
}
/* Release lock */
2024-04-15 00:39:02 +02:00
pthread_mutex_unlock(&tasklist_mutex);
2007-11-14 12:59:58 +00:00
return taskid;
}
2024-04-15 00:39:02 +02:00
void mutex_workqueue_pushsuccess(ot_taskid taskid) {
struct ot_task **task;
/* Want exclusive access to tasklist */
2024-04-15 00:39:02 +02:00
pthread_mutex_lock(&tasklist_mutex);
2024-03-08 16:59:26 +01:00
for (task = &tasklist; *task; task = &((*task)->next))
if ((*task)->taskid == taskid) {
struct ot_task *ptask = *task;
*task = (*task)->next;
2024-04-15 00:39:02 +02:00
free(ptask);
2024-03-08 16:59:26 +01:00
break;
}
/* Release lock */
2024-04-15 00:39:02 +02:00
pthread_mutex_unlock(&tasklist_mutex);
}
2024-04-15 00:39:02 +02:00
int mutex_workqueue_pushresult(ot_taskid taskid, int iovec_entries, struct iovec *iovec) {
struct ot_task *task;
const char byte = 'o';
2007-11-14 12:59:58 +00:00
/* Want exclusive access to tasklist */
2024-04-15 00:39:02 +02:00
pthread_mutex_lock(&tasklist_mutex);
2007-11-14 12:59:58 +00:00
2024-03-08 16:59:26 +01:00
for (task = tasklist; task; task = task->next)
if (task->taskid == taskid) {
task->iovec_entries = iovec_entries;
task->iovec = iovec;
task->tasktype = TASK_DONE;
break;
}
2007-11-14 12:59:58 +00:00
/* Release lock */
2024-04-15 00:39:02 +02:00
pthread_mutex_unlock(&tasklist_mutex);
2008-10-28 01:27:22 +00:00
2024-04-15 00:39:02 +02:00
io_trywrite(g_self_pipe[1], &byte, 1);
2007-11-14 12:59:58 +00:00
/* Indicate whether the worker has to throw away results */
return task ? 0 : -1;
}
2024-04-13 00:47:29 +02:00
int mutex_workqueue_pushchunked(ot_taskid taskid, struct iovec *iovec) {
2024-04-15 00:39:02 +02:00
struct ot_task *task;
const char byte = 'o';
2024-04-13 00:47:29 +02:00
/* Want exclusive access to tasklist */
2024-04-15 00:39:02 +02:00
pthread_mutex_lock(&tasklist_mutex);
2024-04-13 00:47:29 +02:00
for (task = tasklist; task; task = task->next)
if (task->taskid == taskid) {
2024-04-15 00:39:02 +02:00
if (iovec) {
if (iovec_append(&task->iovec_entries, &task->iovec, iovec))
task->tasktype = TASK_DONE_PARTIAL;
else
task = NULL;
} else
2024-04-13 00:47:29 +02:00
task->tasktype = TASK_DONE;
break;
}
/* Release lock */
2024-04-15 00:39:02 +02:00
pthread_mutex_unlock(&tasklist_mutex);
2024-04-13 00:47:29 +02:00
2024-04-15 00:39:02 +02:00
io_trywrite(g_self_pipe[1], &byte, 1);
2024-04-13 00:47:29 +02:00
/* Indicate whether the worker has to throw away results */
return task ? 0 : -1;
}
2024-04-15 00:39:02 +02:00
int64 mutex_workqueue_popresult(int *iovec_entries, struct iovec **iovec, int *is_partial) {
struct ot_task **task;
int64 sock = -1;
2007-11-14 12:59:58 +00:00
2024-04-13 00:47:29 +02:00
*is_partial = 0;
2007-11-14 12:59:58 +00:00
/* Want exclusive access to tasklist */
2024-04-15 00:39:02 +02:00
pthread_mutex_lock(&tasklist_mutex);
2007-11-14 12:59:58 +00:00
2024-03-08 16:59:26 +01:00
for (task = &tasklist; *task; task = &((*task)->next))
2024-04-15 00:39:02 +02:00
if (((*task)->tasktype & TASK_CLASS_MASK) == TASK_DONE) {
2024-03-08 16:59:26 +01:00
struct ot_task *ptask = *task;
2024-04-15 00:39:02 +02:00
*iovec_entries = ptask->iovec_entries;
*iovec = ptask->iovec;
sock = ptask->sock;
2008-10-28 01:27:22 +00:00
2024-04-13 00:47:29 +02:00
if ((*task)->tasktype == TASK_DONE) {
*task = ptask->next;
2024-04-15 00:39:02 +02:00
free(ptask);
2024-04-13 00:47:29 +02:00
} else {
ptask->iovec_entries = 0;
2024-04-15 00:39:02 +02:00
ptask->iovec = NULL;
*is_partial = 1;
2024-04-13 00:47:29 +02:00
/* Prevent task from showing up immediately again unless new data was added */
2024-04-15 00:39:02 +02:00
(*task)->tasktype = TASK_FULLSCRAPE;
2024-04-13 00:47:29 +02:00
}
2024-03-08 16:59:26 +01:00
break;
}
2007-11-14 12:59:58 +00:00
/* Release lock */
2024-04-15 00:39:02 +02:00
pthread_mutex_unlock(&tasklist_mutex);
return sock;
2007-11-14 12:59:58 +00:00
}
2024-04-15 00:39:02 +02:00
void mutex_init() {
2021-04-19 03:25:18 +02:00
int i;
pthread_mutex_init(&tasklist_mutex, NULL);
2024-04-15 00:39:02 +02:00
pthread_cond_init(&tasklist_being_filled, NULL);
for (i = 0; i < OT_BUCKET_COUNT; ++i)
pthread_mutex_init(bucket_mutex + i, NULL);
byte_zero(all_torrents, sizeof(all_torrents));
}
2024-04-15 00:39:02 +02:00
void mutex_deinit() {
2021-04-19 03:25:18 +02:00
int i;
2024-04-15 00:39:02 +02:00
for (i = 0; i < OT_BUCKET_COUNT; ++i)
pthread_mutex_destroy(bucket_mutex + i);
pthread_mutex_destroy(&tasklist_mutex);
pthread_cond_destroy(&tasklist_being_filled);
2024-04-15 00:39:02 +02:00
byte_zero(all_torrents, sizeof(all_torrents));
}
2007-12-20 05:59:34 +00:00
const char *g_version_mutex_c = "$Source$: $Revision$\n";