static void test_atomic_flag(void) { pg_atomic_flag flag; pg_atomic_init_flag(&flag); if (!pg_atomic_unlocked_test_flag(&flag)) elog(ERROR, "flag: unexpectedly set"); if (!pg_atomic_test_set_flag(&flag)) elog(ERROR, "flag: couldn't set"); if (pg_atomic_unlocked_test_flag(&flag)) elog(ERROR, "flag: unexpectedly unset"); if (pg_atomic_test_set_flag(&flag)) elog(ERROR, "flag: set spuriously #2"); pg_atomic_clear_flag(&flag); if (!pg_atomic_unlocked_test_flag(&flag)) elog(ERROR, "flag: unexpectedly set #2"); if (!pg_atomic_test_set_flag(&flag)) elog(ERROR, "flag: couldn't set"); pg_atomic_clear_flag(&flag); }
Datum cfs_start_gc(PG_FUNCTION_ARGS) { int i = 0; if (cfs_gc_workers == 0 && pg_atomic_test_set_flag(&cfs_state->gc_started)) { int j; BackgroundWorkerHandle** handles; cfs_stop = true; /* do just one iteration */ cfs_state->max_iterations = 1; cfs_state->n_workers = PG_GETARG_INT32(0); handles = (BackgroundWorkerHandle**)palloc(cfs_state->n_workers*sizeof(BackgroundWorkerHandle*)); for (i = 0; i < cfs_state->n_workers; i++) { BackgroundWorker worker; MemSet(&worker, 0, sizeof(worker)); sprintf(worker.bgw_name, "cfs-worker-%d", i); worker.bgw_flags = BGWORKER_SHMEM_ACCESS; worker.bgw_start_time = BgWorkerStart_ConsistentState; worker.bgw_restart_time = 1; worker.bgw_main = cfs_bgworker_main; worker.bgw_main_arg = Int32GetDatum(i); if (!RegisterDynamicBackgroundWorker(&worker, &handles[i])) { break; } } for (j = 0; j < i; j++) { WaitForBackgroundWorkerShutdown(handles[j]); } pfree(handles); pg_atomic_clear_flag(&cfs_state->gc_started); } PG_RETURN_INT32(i); }