summaryrefslogtreecommitdiff
path: root/ot_worker.h
blob: ea4f3791e41972034a3bfda3adfad387e0050a05 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/* This software was written by Dirk Engling <erdgeist@erdgeist.org>
   It is considered beerware. Prost. Skol. Cheers or whatever.

   $id$ */

#ifndef OT_WORKER_H__
#define OT_WORKER_H__

#include <pthread.h>
#include <stdatomic.h>
#include <stdint.h>

#include "trackerlogic.h"

struct ot_worker;

/* What a worker does between being started and being told to stop. It reads
   worker->sock and works out of worker->ws; everything around it, from the
   buffers to the counters to saying goodbye, is handled here. */
typedef void (*ot_worker_run)(struct ot_worker *worker);

/* A thread that serves requests on a socket of its own, with the buffers and
   counters it needs. Each is allocated on its own, so taking on another one
   never moves the record a running worker holds. */
struct ot_worker {
  pthread_t            thread;
  _Atomic int          left;
  int64_t              sock;
  ot_worker_run        run;
  struct ot_workstruct ws;
};

/* Start one. Returns -1 if it could not be started. */
int  worker_start(ot_worker_run run, int64_t sock);

/* Wake every worker until it has left, then join it */
void worker_stop_all(void);

#endif