xref: /titanic_41/usr/src/cmd/sendmail/db/include/txn.h (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1 /*-
2  * See the file LICENSE for redistribution information.
3  *
4  * Copyright (c) 1996, 1997, 1998
5  *	Sleepycat Software.  All rights reserved.
6  *
7  *	@(#)txn.h	10.18 (Sleepycat) 1/3/99
8  */
9 #ifndef	_TXN_H_
10 #define	_TXN_H_
11 
12 #include "xa.h"
13 
14 /*
15  * The name of the transaction shared memory region is DEFAULT_TXN_FILE and
16  * the region is always created group RW of the group owning the directory.
17  */
18 #define	DEFAULT_TXN_FILE	"__db_txn.share"
19 /* TXN_MINIMUM = (DB_LOCK_MAXID + 1) but this makes compilers complain. */
20 #define TXN_MINIMUM		0x80000000
21 #define	TXN_INVALID           	0xffffffff /* Maximum number of txn ids. */
22 
23 /*
24  * Transaction type declarations.
25  */
26 
27 /*
28  * Internal data maintained in shared memory for each transaction.
29  */
30 typedef char DB_XID[XIDDATASIZE];
31 
32 typedef struct __txn_detail {
33 	u_int32_t txnid;		/* current transaction id
34 					   used to link free list also */
35 	DB_LSN	last_lsn;		/* last lsn written for this txn */
36 	DB_LSN	begin_lsn;		/* lsn of begin record */
37 	size_t	last_lock;		/* offset in lock region of last lock
38 					   for this transaction. */
39 	size_t	parent;			/* Offset of transaction's parent. */
40 #define	TXN_UNALLOC	0
41 #define	TXN_RUNNING	1
42 #define	TXN_ABORTED	2
43 #define	TXN_PREPARED	3
44 #define	TXN_COMMITTED	4
45 	u_int32_t status;		/* status of the transaction */
46 	SH_TAILQ_ENTRY	links;		/* free/active list */
47 
48 #define	TXN_XA_ABORTED		1
49 #define	TXN_XA_DEADLOCKED	2
50 #define	TXN_XA_ENDED		3
51 #define	TXN_XA_PREPARED		4
52 #define	TXN_XA_STARTED		5
53 #define	TXN_XA_SUSPENDED	6
54 	u_int32_t xa_status;		/* XA status */
55 
56 	/*
57 	 * XID (xid_t) structure: because these fields are logged, the
58 	 * sizes have to be explicit.
59 	 */
60 	DB_XID xid;			/* XA global transaction id */
61 	u_int32_t bqual;		/* bqual_length from XID */
62 	u_int32_t gtrid;		/* gtrid_length from XID */
63 	int32_t format;			/* XA format */
64 } TXN_DETAIL;
65 
66 /*
67  * The transaction manager encapsulates the transaction system.  It contains
68  * references to the log and lock managers as well as the state that keeps
69  * track of the shared memory region.
70  */
71 struct __db_txnmgr {
72 /* These fields need to be protected for multi-threaded support. */
73 	db_mutex_t	*mutexp;	/* Synchronization. */
74 					/* list of active transactions */
75 	TAILQ_HEAD(_chain, __db_txn)	txn_chain;
76 
77 /* These fields are not protected. */
78 	REGINFO		reginfo;	/* Region information. */
79 	DB_ENV		*dbenv;		/* Environment. */
80 	int (*recover)			/* Recovery dispatch routine */
81 	    __P((DB_LOG *, DBT *, DB_LSN *, int, void *));
82 	u_int32_t	 flags;		/* DB_TXN_NOSYNC, DB_THREAD */
83 	DB_TXNREGION	*region;	/* address of shared memory region */
84 	void		*mem;		/* address of the shalloc space */
85 };
86 
87 /*
88  * Layout of the shared memory region.
89  * The region consists of a DB_TXNREGION structure followed by a large
90  * pool of shalloc'd memory which is used to hold TXN_DETAIL structures
91  * and thread mutexes (which are dynamically allocated).
92  */
93 struct __db_txnregion {
94 	RLAYOUT		hdr;		/* Shared memory region header. */
95 	u_int32_t	magic;		/* transaction magic number */
96 	u_int32_t	version;	/* version number */
97 	u_int32_t	maxtxns;	/* maximum number of active txns */
98 	u_int32_t	last_txnid;	/* last transaction id given out */
99 	DB_LSN		pending_ckp;	/* last checkpoint did not finish */
100 	DB_LSN		last_ckp;	/* lsn of the last checkpoint */
101 	time_t		time_ckp;	/* time of last checkpoint */
102 	u_int32_t	logtype;	/* type of logging */
103 	u_int32_t	locktype;	/* lock type */
104 	u_int32_t	naborts;	/* number of aborted transactions */
105 	u_int32_t	ncommits;	/* number of committed transactions */
106 	u_int32_t	nbegins;	/* number of begun transactions */
107 	SH_TAILQ_HEAD(_active) active_txn;	/* active transaction list */
108 };
109 
110 /*
111  * Make the region large enough to hold N  transaction detail structures
112  * plus some space to hold thread handles and the beginning of the shalloc
113  * region.
114  */
115 #define	TXN_REGION_SIZE(N)						\
116 	(sizeof(DB_TXNREGION) + N * sizeof(TXN_DETAIL) + 1000)
117 
118 /* Macros to lock/unlock the region and threads. */
119 #define	LOCK_TXNTHREAD(tmgrp)						\
120 	if (F_ISSET(tmgrp, DB_THREAD))					\
121 		(void)__db_mutex_lock((tmgrp)->mutexp, -1)
122 #define	UNLOCK_TXNTHREAD(tmgrp)						\
123 	if (F_ISSET(tmgrp, DB_THREAD))					\
124 		(void)__db_mutex_unlock((tmgrp)->mutexp, -1)
125 
126 #define	LOCK_TXNREGION(tmgrp)						\
127 	(void)__db_mutex_lock(&(tmgrp)->region->hdr.lock, (tmgrp)->reginfo.fd)
128 #define	UNLOCK_TXNREGION(tmgrp)						\
129 	(void)__db_mutex_unlock(&(tmgrp)->region->hdr.lock, (tmgrp)->reginfo.fd)
130 
131 /* Check for region catastrophic shutdown. */
132 #define	TXN_PANIC_CHECK(tmgrp) {					\
133 	if ((tmgrp)->region->hdr.panic)					\
134 		return (DB_RUNRECOVERY);				\
135 }
136 
137 /*
138  * Log record types.
139  */
140 #define	TXN_COMMIT	1
141 #define	TXN_PREPARE	2
142 #define	TXN_CHECKPOINT	3
143 
144 #include "txn_auto.h"
145 #include "txn_ext.h"
146 
147 #include "xa_ext.h"
148 #endif /* !_TXN_H_ */
149