1 /*-
2  * Copyright (c) 2006, 2020 Oracle and/or its affiliates.  All rights reserved.
3  *
4  * See the file EXAMPLES-LICENSE for license information.
5  *
6  * $Id$
7  */
8 
9 /* User-specified role an environment should play in the replication group. */
10 typedef enum { MASTER, CLIENT, UNKNOWN } ENV_ROLE;
11 
12 /* User-specified information about a replication site. */
13 typedef struct {
14 	char *host;		/* Host name. */
15 	u_int32_t port;		/* Port on which to connect to this site. */
16 	int peer;		/* Whether remote site is repmgr peer. */
17 	int creator;		/* Whether local site is group creator. */
18 } repsite_t;
19 
20 /* Data used for common replication setup. */
21 typedef struct {
22 	const char *progname;
23 	char *home;
24 	int nsites;
25 	int remotesites;
26 	ENV_ROLE role;
27 	repsite_t self;
28 	repsite_t *site_list;
29 } SETUP_DATA;
30 
31 /* Data shared by both repmgr and base versions of this program. */
32 typedef struct {
33 	int is_master;
34 	int app_finished;
35 	int in_client_sync;
36 	int is_repmgr;
37 } SHARED_DATA;
38 
39 /* Arguments for support threads. */
40 typedef struct {
41 	DB_ENV *dbenv;
42 	SHARED_DATA *shared;
43 } supthr_args;
44 
45 /*
46  * Per-thread Replication Manager structure to associate a PERM_FAILED event
47  * with its originating transaction.
48  */
49 typedef struct {
50 	char *thread_name;
51 	int flag;
52 } permfail_t;
53 
54 /* Portability macros for basic threading & timing */
55 #ifdef _WIN32
56 #define	WIN32_LEAN_AND_MEAN
57 #include <windows.h>
58 #include <winsock2.h>
59 #include <ws2tcpip.h>
60 #define	snprintf		_snprintf
61 #define	sleep(s)		Sleep(1000 * (s))
62 
63 extern int getopt(int, char * const *, const char *);
64 
65 typedef HANDLE thread_t;
66 #define	thread_create(thrp, attr, func, arg)				   \
67     (((*(thrp) = CreateThread(NULL, 0,					   \
68 	(LPTHREAD_START_ROUTINE)(func), (arg), 0, NULL)) == NULL) ? -1 : 0)
69 #define	thread_join(thr, statusp)					   \
70     ((WaitForSingleObject((thr), INFINITE) == WAIT_OBJECT_0) &&		   \
71     GetExitCodeThread((thr), (LPDWORD)(statusp)) ? 0 : -1)
72 
73 /* Thread-specific data key for PERM_FAILED structure for Windows. */
74 extern DWORD permfail_key;
75 /* Thread local storage routine definitions for Windows. */
76 #define thread_key_create(keyp) ((*keyp = TlsAlloc()) ==			\
77     TLS_OUT_OF_INDEXES ? (int)GetLastError() : 0)
78 #define thread_key_delete(key) (TlsFree(key) ? 0 : (int)GetLastError())
79 #define thread_setspecific(key, value) (TlsSetValue(key, value) ? 0 :	\
80     (int)GetLastError())
81 #define thread_getspecific(key) TlsGetValue(key)
82 
83 #else /* !_WIN32 */
84 #include <sys/time.h>
85 #include <pthread.h>
86 
87 typedef pthread_t thread_t;
88 #define	thread_create(thrp, attr, func, arg)				   \
89     pthread_create((thrp), (attr), (func), (arg))
90 #define	thread_join(thr, statusp) pthread_join((thr), (statusp))
91 
92 /* Thread-specific data key for PERM_FAILED structure for Posix. */
93 extern pthread_key_t permfail_key;
94 /* Thread local storage routine definitions for Posix. */
95 #define thread_key_create(keyp) pthread_key_create(keyp, NULL)
96 #define thread_key_delete(key) pthread_key_delete(key)
97 #define thread_setspecific(key, value) pthread_setspecific(key, value)
98 #define thread_getspecific(key) pthread_getspecific(key)
99 
100 #endif
101 
102 void *checkpoint_thread __P((void *));
103 int common_rep_setup __P((DB_ENV *, int, char *[], SETUP_DATA *));
104 int create_env __P((const char *, DB_ENV **));
105 int doloop __P((DB_ENV *, SHARED_DATA *));
106 int env_init __P((DB_ENV *, const char *));
107 int finish_support_threads __P((thread_t *, thread_t *));
108 void *log_archive_thread __P((void *));
109 int start_support_threads __P((DB_ENV *, supthr_args *, thread_t *,
110     thread_t *));
111 void usage __P((const int, const char *));
112