|
| 1 | +#include "bgwpool.h" |
| 2 | + |
| 3 | +typedef struct |
| 4 | +{ |
| 5 | + BgwPool* pool; |
| 6 | + int id; |
| 7 | +} BgwExecutorCtx; |
| 8 | + |
| 9 | +static void BgwMainLoop(Datum arg) |
| 10 | +{ |
| 11 | + BgwExecutorCtx* ctx = (BgwExecutorCtx*)arg; |
| 12 | + int id = ctx->id; |
| 13 | + BgwPool* pool = ctx->pool; |
| 14 | + int size; |
| 15 | + void* work; |
| 16 | + |
| 17 | + BackgroundWorkerInitializeConnection(pool->dbname, NULL); |
| 18 | + |
| 19 | + while(true) { |
| 20 | + PGSemaphoreLock(&pool->available); |
| 21 | + SpinLockAcquire(&pool->lock); |
| 22 | + Assert(pool->head != pool->tail); |
| 23 | + size = (int*)&pool->buf[pool->head]; |
| 24 | + void* work = palloc(len); |
| 25 | + if (pool->head + size + 4 > pool->size) { |
| 26 | + memcpy(work, pool->buf, size); |
| 27 | + pool->head = (size & 3) & ~3; |
| 28 | + } else { |
| 29 | + memcpy(work, &pool->buf[pool->head+4], size); |
| 30 | + pool->head += 4 + ((size & 3) & ~3); |
| 31 | + } |
| 32 | + if (pool->size == pool->head) { |
| 33 | + pool->head = 0; |
| 34 | + } |
| 35 | + if (pool->producerBlocked) { |
| 36 | + PGSemaphoreUnlock(&pool->overflow); |
| 37 | + pool->producerBlocked = false; |
| 38 | + } |
| 39 | + SpinLockRelease(&pool->lock); |
| 40 | + pool->executor(id, work, size); |
| 41 | + pfree(work); |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +BGWPool* BgwPoolCreate(BgwExecutor executor, char const* dbname, size_t bufSize, size_t nWorkers); |
| 46 | +{ |
| 47 | + int i; |
| 48 | + BackgroundWorker worker; |
| 49 | + BGWPool* pool = (BGWPool*)ShmemAlloc(bufSize + sizeof(BGWPool)); |
| 50 | + pool->executor = executor; |
| 51 | + PGSemaphoreCreate(&pool->available); |
| 52 | + PGSemaphoreCreate(&pool->overflow); |
| 53 | + PGSemaphoreReset(&pool->available); |
| 54 | + PGSemaphoreReset(&pool->overflow); |
| 55 | + SpinLockInit(&pool->lock); |
| 56 | + pool->producerBlocked = false; |
| 57 | + pool->head = 0; |
| 58 | + pool->tail = 0; |
| 59 | + pool->size = bufSize; |
| 60 | + strcpy(pool->dbname, dbname); |
| 61 | + |
| 62 | + MemSet(&worker, 0, sizeof(BackgroundWorker)); |
| 63 | + worker.bgw_flags = BGWORKER_SHMEM_ACCESS | BGWORKER_BACKEND_DATABASE_CONNECTION; |
| 64 | + worker.bgw_start_time = BgWorkerStart_ConsistentState; |
| 65 | + worker.bgw_main = BgwPoolMainLoop; |
| 66 | + worker.bgw_restart_time = 10; /* Wait 10 seconds for restart before crash */ |
| 67 | + |
| 68 | + for (i = 0; i < nWorkers; i++) { |
| 69 | + BgwExecutorCtx* ctx = (BgwExecutorCtx*)malloc(sizeof(BgwExecutorCtx)); |
| 70 | + snprintf(worker.bgw_name, BGW_MAXLEN, "bgw_pool_worker_%d", i+1); |
| 71 | + ctx->id = i; |
| 72 | + ctx->pool = pool; |
| 73 | + worker.bgw_main_arg = (Datum)ctx; |
| 74 | + RegisterBackgroundWorker(&worker); |
| 75 | + } |
| 76 | + return pool; |
| 77 | +} |
| 78 | + |
| 79 | +void BgwPoolExecute(BgwPool* pool, void* work, size_t size); |
| 80 | +{ |
| 81 | + Assert(size+4 <= pool->size); |
| 82 | + |
| 83 | + SpinLockAcquire(&pool->lock); |
| 84 | + while (true) { |
| 85 | + if ((pool->head < pool->tail && pool->size - pool->tail < size + 4 && pool->head < size) |
| 86 | + || (pool->head > pool->tail && pool->head - pool->tail < size + 4)) |
| 87 | + { |
| 88 | + pool->producerBlocked = true; |
| 89 | + SpinLockRelease(&pool->lock); |
| 90 | + PGSemaphoreLock(&pool->overflow); |
| 91 | + SpinLockAcquire(&pool->lock); |
| 92 | + } else { |
| 93 | + *(int*)&pool->buf[pool->tail] = size; |
| 94 | + if (pool->size - pool->tail >= size + 4) { |
| 95 | + memcpy(&pool->buf[pool->tail+4], work, size); |
| 96 | + pool->tail += 4 + (size+3) & ~3; |
| 97 | + } else { |
| 98 | + memcpy(pool->buf, work, size); |
| 99 | + pool->tail = (size+3) & ~3; |
| 100 | + } |
| 101 | + PGSemaphoreUnlock(&pool->available); |
| 102 | + } |
| 103 | + } |
| 104 | +} |
| 105 | + |
0 commit comments