forked from yandex/odyssey
-
Notifications
You must be signed in to change notification settings - Fork 0
/
worker_pool.h
95 lines (81 loc) · 2.01 KB
/
worker_pool.h
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#ifndef ODYSSEY_WORKER_POOL_H
#define ODYSSEY_WORKER_POOL_H
/*
* Odyssey.
*
* Scalable PostgreSQL connection pooler.
*/
#include "machinarium.h"
typedef struct od_worker_pool od_worker_pool_t;
struct od_worker_pool {
od_worker_t *pool;
od_atomic_u32_t round_robin;
uint32_t count;
};
static inline void od_worker_pool_init(od_worker_pool_t *pool)
{
pool->count = 0;
pool->round_robin = 0;
pool->pool = NULL;
}
static inline od_retcode_t od_worker_pool_start(od_worker_pool_t *pool,
od_global_t *global,
uint32_t count)
{
pool->pool = malloc(sizeof(od_worker_t) * count);
if (pool->pool == NULL)
return -1;
pool->count = count;
uint32_t i;
for (i = 0; i < count; i++) {
od_worker_t *worker = &pool->pool[i];
od_worker_init(worker, global, i);
int rc;
rc = od_worker_start(worker);
if (rc == -1)
return NOT_OK_RESPONSE;
}
return 0;
}
static inline void od_worker_pool_stop(od_worker_pool_t *pool)
{
for (uint32_t i = 0; i < pool->count; i++) {
od_worker_t *worker = &pool->pool[i];
machine_stop(worker->machine);
}
}
static inline void od_worker_pool_wait()
{
machine_sleep(1);
}
static inline void
od_worker_pool_wait_gracefully_shutdown(od_worker_pool_t *pool)
{
// // In fact we cannot wait anything here - machines may be in epoll
// waiting
// // No new TLS handshakes should be initiated, so, just wait a bit.
// machine_sleep(1);
for (uint32_t i = 0; i < pool->count; i++) {
od_worker_t *worker = &pool->pool[i];
int rc = machine_wait(worker->machine);
if (rc != MM_OK_RETCODE)
return;
}
}
static inline void od_worker_pool_feed(od_worker_pool_t *pool,
machine_msg_t *msg)
{
uint32_t next;
uint32_t oldValue;
for (;;) {
oldValue = od_atomic_u32_of(&pool->round_robin);
next = oldValue + 1 == pool->count ? 0 : oldValue + 1;
if (od_atomic_u32_cas(&pool->round_robin, oldValue, next) ==
oldValue)
break;
}
od_worker_t *worker;
worker = &pool->pool[next];
machine_channel_write(worker->task_channel, msg);
}
#endif /* ODYSSEY_WORKER_POOL_H */