1 /*-------------------------------------------------------------------------
2  *
3  * parallel.h
4  *
5  *	Parallel support header file for the pg_dump archiver
6  *
7  * Portions Copyright (c) 1996-2016, PostgreSQL Global Development Group
8  * Portions Copyright (c) 1994, Regents of the University of California
9  *
10  *	The author is not responsible for loss or damages that may
11  *	result from its use.
12  *
13  * IDENTIFICATION
14  *		src/bin/pg_dump/parallel.h
15  *
16  *-------------------------------------------------------------------------
17  */
18 
19 #ifndef PG_DUMP_PARALLEL_H
20 #define PG_DUMP_PARALLEL_H
21 
22 #include "pg_backup_archiver.h"
23 
24 typedef enum
25 {
26 	WRKR_TERMINATED = 0,
27 	WRKR_IDLE,
28 	WRKR_WORKING,
29 	WRKR_FINISHED
30 } T_WorkerStatus;
31 
32 #define WORKER_IS_RUNNING(workerStatus) \
33 	((workerStatus) != WRKR_TERMINATED)
34 
35 /* Arguments needed for a worker process */
36 typedef struct ParallelArgs
37 {
38 	ArchiveHandle *AH;
39 	TocEntry   *te;
40 } ParallelArgs;
41 
42 /* State for each parallel activity slot */
43 typedef struct ParallelSlot
44 {
45 	ParallelArgs *args;
46 	T_WorkerStatus workerStatus;
47 	int			status;
48 	int			pipeRead;		/* master's end of the pipes */
49 	int			pipeWrite;
50 	int			pipeRevRead;	/* child's end of the pipes */
51 	int			pipeRevWrite;
52 #ifdef WIN32
53 	uintptr_t	hThread;
54 	unsigned int threadId;
55 #else
56 	pid_t		pid;
57 #endif
58 } ParallelSlot;
59 
60 #define NO_SLOT (-1)
61 
62 typedef struct ParallelState
63 {
64 	int			numWorkers;
65 	ParallelSlot *parallelSlot;
66 } ParallelState;
67 
68 #ifdef WIN32
69 extern bool parallel_init_done;
70 extern DWORD mainThreadId;
71 #endif
72 
73 extern void init_parallel_dump_utils(void);
74 
75 extern int	GetIdleWorker(ParallelState *pstate);
76 extern bool IsEveryWorkerIdle(ParallelState *pstate);
77 extern void ListenToWorkers(ArchiveHandle *AH, ParallelState *pstate, bool do_wait);
78 extern int	ReapWorkerStatus(ParallelState *pstate, int *status);
79 extern void EnsureIdleWorker(ArchiveHandle *AH, ParallelState *pstate);
80 extern void EnsureWorkersFinished(ArchiveHandle *AH, ParallelState *pstate);
81 
82 extern ParallelState *ParallelBackupStart(ArchiveHandle *AH);
83 extern void DispatchJobForTocEntry(ArchiveHandle *AH,
84 					   ParallelState *pstate,
85 					   TocEntry *te, T_Action act);
86 extern void ParallelBackupEnd(ArchiveHandle *AH, ParallelState *pstate);
87 
88 extern void set_archive_cancel_info(ArchiveHandle *AH, PGconn *conn);
89 
90 #endif   /* PG_DUMP_PARALLEL_H */
91