~vkolesnikov/pbxt/pbxt-preload-test-bug

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
/* Copyright (c) 2007 PrimeBase Technologies GmbH
 *
 * PrimeBase XT
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 * 2007-10-30	Paul McCullagh
 *
 * H&G2JCtL
 *
 * The new table cache. Caches all non-index data. This includes the data
 * files and the row pointer files.
 */

#include "xt_config.h"

#ifdef DRIZZLED
#include <bitset>
#endif

#include <signal.h>

#include "pthread_xt.h"
#include "tabcache_xt.h"
#include "table_xt.h"
#include "database_xt.h"
#include "trace_xt.h"
#include "myxt_xt.h"

xtPublic XTTabCacheMemRec	xt_tab_cache;

static void tabc_fr_wait_for_cache(XTThreadPtr self, u_int msecs);

xtPublic void xt_tc_set_cache_size(size_t cache_size)
{
	xt_tab_cache.tcm_cache_size = cache_size;
	/* Multiplying by this number can overflow a 4 byte value! */
	xt_tab_cache.tcm_low_level = (size_t) ((xtWord8) cache_size * (xtWord8) 70 / (xtWord8) 100);	// Current 70%
	xt_tab_cache.tcm_high_level = (size_t) ((xtWord8) cache_size * 95 / (xtWord8) 100);				// Current 95%
	xt_tab_cache.tcm_mid_level = (size_t) ((xtWord8) cache_size * 85 / (xtWord8) 100);				// Current 85%
}

/*
 * Initialize the disk cache.
 */
xtPublic void xt_tc_init(XTThreadPtr self, size_t cache_size)
{
	xt_tc_set_cache_size(cache_size);

	xt_tab_cache.tcm_approx_page_count = cache_size / sizeof(XTTabCachePageRec);
	/* Determine the size of the hash table.
	 * The size is set to 2* the number of pages!
	 */
	xt_tab_cache.tcm_hash_size = (xt_tab_cache.tcm_approx_page_count * 2) / XT_TC_SEGMENT_COUNT;

	try_(a) {
		for (u_int i=0; i<XT_TC_SEGMENT_COUNT; i++) {
			xt_tab_cache.tcm_segment[i].tcs_cache_in_use = 0;
			xt_tab_cache.tcm_segment[i].tcs_hash_table = (XTTabCachePagePtr *) xt_calloc(self, xt_tab_cache.tcm_hash_size * sizeof(XTTabCachePagePtr));
			TAB_CAC_INIT_LOCK(self, &xt_tab_cache.tcm_segment[i].tcs_lock);
		}

		xt_init_mutex_with_autoname(self, &xt_tab_cache.tcm_lock);
		xt_init_cond(self, &xt_tab_cache.tcm_cond);
		xt_init_mutex_with_autoname(self, &xt_tab_cache.tcm_freeer_lock);
		xt_init_cond(self, &xt_tab_cache.tcm_freeer_cond);
	}
	catch_(a) {
		xt_tc_exit(self);
		throw_();
	}
	cont_(a);
}

xtPublic void xt_tc_exit(XTThreadPtr self)
{
	XTTabCacheSegPtr seg;

	for (u_int i=0; i<XT_TC_SEGMENT_COUNT; i++) {
		seg = &xt_tab_cache.tcm_segment[i];
		if (seg->tcs_hash_table) {
			XTTabCachePagePtr page, tmp_page;

			for (size_t j=0; j<xt_tab_cache.tcm_hash_size; j++) {
				page = seg->tcs_hash_table[j];
				while (page) {
					tmp_page = page;
					page = page->tcp_next;
					ASSERT_NS(seg->tcs_cache_in_use >= offsetof(XTTabCachePageRec, tcp_data) + tmp_page->tcp_data_size);
					seg->tcs_cache_in_use -= (offsetof(XTTabCachePageRec, tcp_data) + tmp_page->tcp_data_size);
					ASSERT_NS(seg->tcs_cache_in_use == 0 || seg->tcs_cache_in_use >= 25000);
					xt_free(self, tmp_page);
				}
			}

			xt_free(self, seg->tcs_hash_table);
			seg->tcs_hash_table = NULL;
			TAB_CAC_FREE_LOCK(self, &seg->tcs_lock);
		}
		ASSERT_NS(seg->tcs_cache_in_use == 0);
	}

	xt_free_mutex(&xt_tab_cache.tcm_lock);
	xt_free_cond(&xt_tab_cache.tcm_cond);
	xt_free_mutex(&xt_tab_cache.tcm_freeer_lock);
	xt_free_cond(&xt_tab_cache.tcm_freeer_cond);
}

xtPublic xtInt8 xt_tc_get_usage()
{
	xtInt8 size = 0;

	for (u_int i=0; i<XT_TC_SEGMENT_COUNT; i++) {
		size += xt_tab_cache.tcm_segment[i].tcs_cache_in_use;
	}
	return size;
}

xtPublic xtInt8 xt_tc_get_size()
{
	return (xtInt8) xt_tab_cache.tcm_cache_size;
}

xtPublic xtInt8 xt_tc_get_high()
{
	return (xtInt8) xt_tab_cache.tcm_cache_high;
}

#ifdef DEBUG
xtPublic void xt_check_table_cache(XTTableHPtr tab)
{
	XTTabCachePagePtr page, ppage;

	xt_lock_mutex_ns(&xt_tab_cache.tcm_lock);
	ppage = NULL;
	page = xt_tab_cache.tcm_lru_page;
	while (page) {
		if (tab) {
			if (page->tcp_db_id == tab->tab_db->db_id && page->tcp_tab_id == tab->tab_id) {
				ASSERT_NS(!XTTableSeq::xt_op_is_before(tab->tab_seq.ts_next_seq, page->tcp_op_seq));
			}
		}
		ASSERT_NS(page->tcp_lr_used == ppage);
		ppage = page;
		page = page->tcp_mr_used;
	}
	ASSERT_NS(xt_tab_cache.tcm_mru_page == ppage);
	xt_unlock_mutex_ns(&xt_tab_cache.tcm_lock);
}
#endif

void XTTabCache::xt_tc_setup(XTTableHPtr tab, size_t head_size, size_t rec_size)
{
	tci_table = tab;
	tci_header_size = head_size;
	tci_rec_size = rec_size;
	tci_rows_per_page = (XT_TC_PAGE_SIZE / rec_size) + 1;
	if (tci_rows_per_page < 2)
		tci_rows_per_page = 2;
	tci_page_size = tci_rows_per_page * rec_size;
}

/*
 * This function assumes that we never write past the boundary of a page.
 * This should be the case, because we should never write more than
 * a row, and there are only whole rows on a page.
 */
xtBool XTTabCache::xt_tc_write(XT_ROW_REC_FILE_PTR file, xtRefID ref_id, size_t inc, size_t size, xtWord1 *data, xtOpSeqNo *op_seq, xtBool read, XTThreadPtr thread)
{
	size_t				offset;
	XTTabCachePagePtr	page;
	XTTabCacheSegPtr	seg;

	/*
	retry:
	*/
	if (!tc_fetch(file, ref_id, &seg, &page, &offset, read, thread))
		return FAILED;
	/* Don't write while there is a read lock on the page,
	 * which can happen during a sequential scan...
	 *
	 * This will have to be OK.
	 * I cannot wait for the lock because a thread locks
	 * itself out when updating during a sequential scan.
	 *
	 * However, I don't think this is a problem, because
	 * the only records that are changed, are records
	 * containing uncommitted data. Such records should
	 * be ignored by a sequential scan. As long as
	 * we don't crash due to reading half written
	 * data!
	 *
	if (page->tcp_lock_count) {
		if (!xt_timed_wait_cond_ns(&seg->tcs_cond, &seg->tcs_lock, 100)) {
			xt_rwmutex_unlock(&seg->tcs_lock, thread->t_id);
			return FAILED;
		}
		xt_rwmutex_unlock(&seg->tcs_lock, thread->t_id);
		// The page may have dissappeared from the cache, while we were sleeping!
		goto retry;
	}
	*/
	
	ASSERT_NS(offset + inc + 4 <= tci_page_size);
	memcpy(page->tcp_data + offset + inc, data, size);
	/* GOTCHA, this was "op_seq > page->tcp_op_seq", however
	 * this does not handle overflow!
	if (XTTableSeq::xt_op_is_before(page->tcp_op_seq, op_seq))
		page->tcp_op_seq = op_seq;
	 */

	page->tcp_dirty = TRUE;
	ASSERT_NS(page->tcp_db_id == tci_table->tab_db->db_id && page->tcp_tab_id == tci_table->tab_id);
	*op_seq = tci_table->tab_seq.ts_set_op_seq(page);
	TAB_CAC_UNLOCK(&seg->tcs_lock, thread->t_id);
	return OK;
}

/*
 * This is a special version of write which is used to set the "clean" bit.
 * The alternative would be to read the record first, but this
 * is much quicker!
 *
 * This function also checks if xn_id, row_id and other data match (the checks 
 * are similar to xn_sw_cleanup_done) before modifying the record, otherwise it 
 * assumes that the record was already updated earlier and we must not set it to 
 * clean.
 *
 * If the record was not modified the function returns FALSE.
 *
 * The function has a self pointer and can throw an exception.
 */
xtBool XTTabCache::xt_tc_write_cond(XTThreadPtr self, XT_ROW_REC_FILE_PTR file, xtRefID ref_id, xtWord1 new_type, xtOpSeqNo *op_seq, 
	xtXactID xn_id, xtRowID row_id, u_int stat_id, u_int rec_type)
{
	size_t				offset;
	XTTabCachePagePtr	page;
	XTTabCacheSegPtr	seg;
	XTTabRecHeadDPtr	rec_head;

	if (!tc_fetch(file, ref_id, &seg, &page, &offset, TRUE, self))
		xt_throw(self);

	ASSERT(offset + 1 <= tci_page_size);

	rec_head = (XTTabRecHeadDPtr)(page->tcp_data + offset);

	/* Transaction must match: */
	if (XT_GET_DISK_4(rec_head->tr_xact_id_4) != xn_id)
		goto no_change;

	/* Record header must match expected value from
	 * log or clean has been done, or is not required.
	 *
	 * For example, it is not required if a record
	 * has been overwritten in a transaction.
	 */
	if (rec_head->tr_rec_type_1 != rec_type ||
		rec_head->tr_stat_id_1 != stat_id)
		goto no_change;

	/* Row must match: */
	if (XT_GET_DISK_4(rec_head->tr_row_id_4) != row_id)
		goto no_change;

	*(page->tcp_data + offset) = new_type;

	page->tcp_dirty = TRUE;
	ASSERT(page->tcp_db_id == tci_table->tab_db->db_id && page->tcp_tab_id == tci_table->tab_id);
	*op_seq = tci_table->tab_seq.ts_set_op_seq(page);
	TAB_CAC_UNLOCK(&seg->tcs_lock, self->t_id);
	return TRUE;

	no_change:
	TAB_CAC_UNLOCK(&seg->tcs_lock, self->t_id);
	return FALSE;
}

xtBool XTTabCache::xt_tc_read(XT_ROW_REC_FILE_PTR file, xtRefID ref_id, size_t size, xtWord1 *data, XTThreadPtr thread)
{
#ifdef XT_USE_ROW_REC_MMAP_FILES
	return tc_read_direct(file, ref_id, size, data, thread);
#else
	size_t				offset;
	XTTabCachePagePtr	page;
	XTTabCacheSegPtr	seg;

	if (!tc_fetch(file, ref_id, &seg, &page, &offset, TRUE, thread))
		return FAILED;
	/* A read must be completely on a page: */
	ASSERT_NS(offset + size <= tci_page_size);
	memcpy(data, page->tcp_data + offset, size);
	TAB_CAC_UNLOCK(&seg->tcs_lock, thread->t_id);
	return OK;
#endif
}

xtBool XTTabCache::xt_tc_read_4(XT_ROW_REC_FILE_PTR file, xtRefID ref_id, xtWord4 *value, XTThreadPtr thread)
{
#ifdef XT_USE_ROW_REC_MMAP_FILES
	register u_int				page_idx;
	register XTTabCachePagePtr	page;
	register XTTabCacheSegPtr	seg;
	register u_int				hash_idx;
	register XTTabCacheMemPtr	dcg = &xt_tab_cache;
	off_t						address;

	ASSERT_NS(ref_id);
	ref_id--;
	page_idx = ref_id / this->tci_rows_per_page;
	address = (off_t) ref_id * (off_t) this->tci_rec_size + (off_t) this->tci_header_size;

	hash_idx = page_idx + (file->fr_id * 223);
	seg = &dcg->tcm_segment[hash_idx & XT_TC_SEGMENT_MASK];
	hash_idx = (hash_idx >> XT_TC_SEGMENT_SHIFTS) % dcg->tcm_hash_size;

	TAB_CAC_READ_LOCK(&seg->tcs_lock, thread->t_id);
	page = seg->tcs_hash_table[hash_idx];
	while (page) {
		if (page->tcp_page_idx == page_idx && page->tcp_file_id == file->fr_id) {
			size_t	offset;
			xtWord1	*buffer;

			offset = (ref_id % this->tci_rows_per_page) * this->tci_rec_size;
			ASSERT_NS(offset + 4 <= this->tci_page_size);
			buffer = page->tcp_data + offset;
			*value = XT_GET_DISK_4(buffer);
			TAB_CAC_UNLOCK(&seg->tcs_lock, thread->t_id);
			return OK;
		}
		page = page->tcp_next;
	}
	TAB_CAC_UNLOCK(&seg->tcs_lock, thread->t_id);

	return xt_pread_fmap_4(file, address, value, &thread->st_statistics.st_rec, thread);
#else
	size_t				offset;
	XTTabCachePagePtr	page;
	XTTabCacheSegPtr	seg;
	xtWord1				*data;

	if (!tc_fetch(file, ref_id, &seg, &page, &offset, TRUE, thread))
		return FAILED;
	/* A read must be completely on a page: */
	ASSERT_NS(offset + 4 <= tci_page_size);
	data = page->tcp_data + offset;
	*value = XT_GET_DISK_4(data);
	TAB_CAC_UNLOCK(&seg->tcs_lock, thread->t_id);
	return OK;
#endif
}

xtBool XTTabCache::xt_tc_get_page(XT_ROW_REC_FILE_PTR file, xtRefID ref_id, xtBool load, XTTabCachePagePtr *ret_page, size_t *offset, XTThreadPtr thread)
{
	XTTabCachePagePtr	page;
	XTTabCacheSegPtr	seg;

	if (load) {
		if (!tc_fetch(file, ref_id, &seg, &page, offset, TRUE, thread))
			return FAILED;
	}
	else {
		if (!tc_fetch_direct(file, ref_id, &seg, &page, offset, thread))
			return FAILED;
		if (!seg) {
			*ret_page = NULL;
			return OK;
		}
	}
	page->tcp_lock_count++;
	TAB_CAC_UNLOCK(&seg->tcs_lock, thread->t_id);
	*ret_page = page;
	return OK;
}

void XTTabCache::xt_tc_release_page(XT_ROW_REC_FILE_PTR XT_UNUSED(file), XTTabCachePagePtr page, XTThreadPtr thread)
{
	XTTabCacheSegPtr	seg;

	seg = &xt_tab_cache.tcm_segment[page->tcp_seg];
	TAB_CAC_WRITE_LOCK(&seg->tcs_lock, thread->t_id);

#ifdef DEBUG
	XTTabCachePagePtr lpage, ppage;

	ppage = NULL;
	lpage = seg->tcs_hash_table[page->tcp_hash_idx];
	while (lpage) {
		if (lpage->tcp_page_idx == page->tcp_page_idx &&
			lpage->tcp_file_id == page->tcp_file_id)
			break;
		ppage = lpage;
		lpage = lpage->tcp_next;
	}

	ASSERT_NS(page == lpage);
	ASSERT_NS(page->tcp_lock_count > 0);
#endif

	if (page->tcp_lock_count > 0)
		page->tcp_lock_count--;

	TAB_CAC_UNLOCK(&seg->tcs_lock, thread->t_id);
}

xtBool XTTabCache::xt_tc_read_page(XT_ROW_REC_FILE_PTR file, xtRefID ref_id, xtWord1 *data, XTThreadPtr thread)
{
	return tc_read_direct(file, ref_id, this->tci_page_size, data, thread);
}

/* Read row and record files directly.
 * This by-passed the cache when reading, which mean
 * we rely in the OS for caching.
 * This probably only makes sense when these files
 * are memory mapped.
 */
xtBool XTTabCache::tc_read_direct(XT_ROW_REC_FILE_PTR file, xtRefID ref_id, size_t size, xtWord1 *data, XTThreadPtr thread)
{
	register u_int				page_idx;
	register XTTabCachePagePtr	page;
	register XTTabCacheSegPtr	seg;
	register u_int				hash_idx;
	register XTTabCacheMemPtr	dcg = &xt_tab_cache;
	size_t						red_size;
	off_t						address;

	ASSERT_NS(ref_id);
	ref_id--;
	page_idx = ref_id / this->tci_rows_per_page;
	address = (off_t) ref_id * (off_t) this->tci_rec_size + (off_t) this->tci_header_size;

	hash_idx = page_idx + (file->fr_id * 223);
	seg = &dcg->tcm_segment[hash_idx & XT_TC_SEGMENT_MASK];
	hash_idx = (hash_idx >> XT_TC_SEGMENT_SHIFTS) % dcg->tcm_hash_size;

	TAB_CAC_READ_LOCK(&seg->tcs_lock, thread->t_id);
	page = seg->tcs_hash_table[hash_idx];
	while (page) {
		if (page->tcp_page_idx == page_idx && page->tcp_file_id == file->fr_id) {
			size_t offset;

			offset = (ref_id % this->tci_rows_per_page) * this->tci_rec_size;
			ASSERT_NS(offset + size <= this->tci_page_size);
			memcpy(data, page->tcp_data + offset, size);
			TAB_CAC_UNLOCK(&seg->tcs_lock, thread->t_id);
			return OK;
		}
		page = page->tcp_next;
	}
	TAB_CAC_UNLOCK(&seg->tcs_lock, thread->t_id);
	if (!XT_PREAD_RR_FILE(file, address, size, 0, data, &red_size, &thread->st_statistics.st_rec, thread))
		return FAILED;
	memset(data + red_size, 0, size - red_size);
	return OK;
}

xtBool XTTabCache::tc_fetch_direct(XT_ROW_REC_FILE_PTR file, xtRefID ref_id, XTTabCacheSegPtr *ret_seg, XTTabCachePagePtr *ret_page, size_t *offset, XTThreadPtr thread)
{
	register u_int				page_idx;
	register XTTabCachePagePtr	page;
	register XTTabCacheSegPtr	seg;
	register u_int				hash_idx;
	register XTTabCacheMemPtr	dcg = &xt_tab_cache;

	ASSERT_NS(ref_id);
	ref_id--;
	page_idx = ref_id / this->tci_rows_per_page;
	*offset = (ref_id % this->tci_rows_per_page) * this->tci_rec_size;

	hash_idx = page_idx + (file->fr_id * 223);
	seg = &dcg->tcm_segment[hash_idx & XT_TC_SEGMENT_MASK];
	hash_idx = (hash_idx >> XT_TC_SEGMENT_SHIFTS) % dcg->tcm_hash_size;

	TAB_CAC_WRITE_LOCK(&seg->tcs_lock, thread->t_id);
	page = seg->tcs_hash_table[hash_idx];
	while (page) {
		if (page->tcp_page_idx == page_idx && page->tcp_file_id == file->fr_id) {
			*ret_seg = seg;
			*ret_page = page;
			return OK;
		}
		page = page->tcp_next;
	}
	TAB_CAC_UNLOCK(&seg->tcs_lock, thread->t_id);
	*ret_seg = NULL;
	*ret_page = NULL;
	return OK;
}

/*
 * Note, this function may return an exclusive, or a shared lock.
 * If the page is in cache it will return a shared lock of the segment.
 * If the page was just added to the cache it will return an
 * exclusive lock.
 */
xtBool XTTabCache::tc_fetch(XT_ROW_REC_FILE_PTR file, xtRefID ref_id, XTTabCacheSegPtr *ret_seg, XTTabCachePagePtr *ret_page, size_t *offset, xtBool read, XTThreadPtr thread)
{
	register u_int				page_idx;
	register XTTabCachePagePtr	page, new_page;
	register XTTabCacheSegPtr	seg;
	register u_int				hash_idx;
	register XTTabCacheMemPtr	dcg = &xt_tab_cache;
	size_t						red_size;
	off_t						address;

	ASSERT_NS(ref_id);
	ref_id--;
	page_idx = ref_id / this->tci_rows_per_page;
	address = (off_t) page_idx * (off_t) this->tci_page_size + (off_t) this->tci_header_size;
	*offset = (ref_id % this->tci_rows_per_page) * this->tci_rec_size;

	hash_idx = page_idx + (file->fr_id * 223);
	seg = &dcg->tcm_segment[hash_idx & XT_TC_SEGMENT_MASK];
	hash_idx = (hash_idx >> XT_TC_SEGMENT_SHIFTS) % dcg->tcm_hash_size;

	TAB_CAC_READ_LOCK(&seg->tcs_lock, thread->t_id);
	page = seg->tcs_hash_table[hash_idx];
	while (page) {
		if (page->tcp_page_idx == page_idx && page->tcp_file_id == file->fr_id) {
			/* This page has been most recently used: */
			if (XT_TIME_DIFF(page->tcp_ru_time, dcg->tcm_ru_now) > (dcg->tcm_approx_page_count >> 1)) {
				/* Move to the front of the MRU list: */
				xt_lock_mutex_ns(&dcg->tcm_lock);

				page->tcp_ru_time = ++dcg->tcm_ru_now;
				if (dcg->tcm_mru_page != page) {
					/* Remove from the MRU list: */
					if (dcg->tcm_lru_page == page)
						dcg->tcm_lru_page = page->tcp_mr_used;
					if (page->tcp_lr_used)
						page->tcp_lr_used->tcp_mr_used = page->tcp_mr_used;
					if (page->tcp_mr_used)
						page->tcp_mr_used->tcp_lr_used = page->tcp_lr_used;
	
					/* Make the page the most recently used: */
					if ((page->tcp_lr_used = dcg->tcm_mru_page))
						dcg->tcm_mru_page->tcp_mr_used = page;
					page->tcp_mr_used = NULL;
					dcg->tcm_mru_page = page;
					if (!dcg->tcm_lru_page)
						dcg->tcm_lru_page = page;
				}
				xt_unlock_mutex_ns(&dcg->tcm_lock);
			}
			*ret_seg = seg;
			*ret_page = page;
			thread->st_statistics.st_rec_cache_hit++;
			return OK;
		}
		page = page->tcp_next;
	}
	
	size_t page_size = offsetof(XTTabCachePageRec, tcp_data) + this->tci_page_size;

	TAB_CAC_UNLOCK(&seg->tcs_lock, thread->t_id);
	
	/* Page not found, allocate a new page: */
	if (!(new_page = (XTTabCachePagePtr) xt_malloc_ns(page_size)))
		return FAILED;

	/* Check the level of the cache: */
	size_t cache_used = 0;
	for (int i=0; i<XT_TC_SEGMENT_COUNT; i++)
		cache_used += dcg->tcm_segment[i].tcs_cache_in_use;

	if (cache_used + page_size > dcg->tcm_cache_high)
		dcg->tcm_cache_high = cache_used;

	if (cache_used + page_size > dcg->tcm_cache_size) {
		XTThreadPtr self;
		time_t		now;

		/* Wait for the cache level to go down.
		 * If this happens, then the freeer is not working fast
		 * enough!
		 */

		/* But before I do this, I must flush my own log because:
		 * - The freeer might be waiting for a page to be cleaned.
		 * - The page can only be cleaned once it has been written to
		 *   the database.
		 * - The writer cannot write the page data until it has been
		 *   flushed to the log.
		 * - The log won't be flushed, unless this thread does it.
		 * So there could be a deadlock if I don't flush the log!
		 */
		if ((self = xt_get_self())) {
			if (!xt_xlog_flush_log(tci_table->tab_db, self))
				goto failed;
		}

		/* Wait for the free'er thread: */
		xt_lock_mutex_ns(&dcg->tcm_freeer_lock);
		now = time(NULL);
		do {
			/* I have set the timeout to 2 here because of the following situation:
			 * 1. Transaction allocates an op seq
			 * 2. Transaction goes to update cache, but must wait for
			 *    cache to be freed (after this, the op would be written to
			 *    the log).
			 * 3. The free'er wants to free cache, but is waiting for the writter.
			 * 4. The writer cannot continue because an op seq is missing!
			 *    So the writer is waiting for the transaction thread to write
			 *    the op seq.
			 * - So we have a deadlock situation.
			 * - However, this situation can only occur if there is not enougn
			 *   cache.
			 * The timeout helps, but will not solve the problem, unless we
			 * ignore cache level here, after a while, and just continue.
			 */

			/* Wake freeer before we go to sleep: */
			if (!dcg->tcm_freeer_busy) {
				if (!xt_broadcast_cond_ns(&dcg->tcm_freeer_cond))
					xt_log_and_clear_exception_ns();
			}

			dcg->tcm_threads_waiting++;
#ifdef DEBUG
			if (!xt_timed_wait_cond_ns(&dcg->tcm_freeer_cond, &dcg->tcm_freeer_lock, 30000)) {
				dcg->tcm_threads_waiting--;
				break;
			}
#else
			if (!xt_timed_wait_cond_ns(&dcg->tcm_freeer_cond, &dcg->tcm_freeer_lock, 1000)) {
				dcg->tcm_threads_waiting--;
				break;
			}
#endif
			dcg->tcm_threads_waiting--;

			cache_used = 0;
			for (int i=0; i<XT_TC_SEGMENT_COUNT; i++)
				cache_used += dcg->tcm_segment[i].tcs_cache_in_use;

			if (cache_used + page_size <= dcg->tcm_high_level)
				break;
			/*
			 * If there is too little cache we can get stuck here.
			 * The problem is that seg numbers are allocated before fetching a
			 * record to be updated.
			 *
			 * It can happen that we end up waiting for that seq number
			 * to be written to the log before we can continue here.
			 *
			 * This happens as follows:
			 * 1. This thread waits for the freeer.
			 * 2. The freeer cannot free a page because it has not been
			 *    written by the writter.
			 * 3. The writter cannot continue because it is waiting
			 *    for a missing sequence number.
			 * 4. The missing sequence number is the one allocated
			 *    before we entered this function!
			 * 
			 * So don't wait for more than 5 seconds here!
			 */
		}
		while (time(NULL) < now + 5);
		xt_unlock_mutex_ns(&dcg->tcm_freeer_lock);
	}
	else if (cache_used + page_size > dcg->tcm_high_level) {
		/* Wake up the freeer because the cache level,
		 * is higher than the high level.
		 */
		if (!dcg->tcm_freeer_busy) {
			xt_lock_mutex_ns(&xt_tab_cache.tcm_freeer_lock);
			if (!xt_broadcast_cond_ns(&xt_tab_cache.tcm_freeer_cond))
				xt_log_and_clear_exception_ns();
			xt_unlock_mutex_ns(&xt_tab_cache.tcm_freeer_lock);
		}
	}

	/* Read the page into memory.... */
	new_page->tcp_dirty = FALSE;
	new_page->tcp_seg = (xtWord1) ((page_idx + (file->fr_id * 223)) & XT_TC_SEGMENT_MASK);
	new_page->tcp_lock_count = 0;
	new_page->tcp_hash_idx = hash_idx;
	new_page->tcp_page_idx = page_idx;
	new_page->tcp_file_id = file->fr_id;
	new_page->tcp_db_id = this->tci_table->tab_db->db_id;
	new_page->tcp_tab_id = this->tci_table->tab_id;
	new_page->tcp_data_size = this->tci_page_size;
	new_page->tcp_op_seq = 0; // Value not used because not dirty

	if (read) {
		if (!XT_PREAD_RR_FILE(file, address, this->tci_page_size, 0, new_page->tcp_data, &red_size, &thread->st_statistics.st_rec, thread))
			goto failed;
	}
	
#ifdef XT_MEMSET_UNUSED_SPACE
	else
		red_size = 0;

	/* Removing this is an optimization. It should not be required
	 * to clear the unused space in the page.
	 */
	memset(new_page->tcp_data + red_size, 0, this->tci_page_size - red_size);
#endif

	/* Add the page to the cache! */
	TAB_CAC_WRITE_LOCK(&seg->tcs_lock, thread->t_id);
	page = seg->tcs_hash_table[hash_idx];
	while (page) {
		if (page->tcp_page_idx == page_idx && page->tcp_file_id == file->fr_id) {
			/* Oops, someone else was faster! */
			xt_free_ns(new_page);
			goto done_ok;
		}
		page = page->tcp_next;
	}
	page = new_page;

	/* Make the page the most recently used: */
	xt_lock_mutex_ns(&dcg->tcm_lock);
	page->tcp_ru_time = ++dcg->tcm_ru_now;
	if ((page->tcp_lr_used = dcg->tcm_mru_page))
		dcg->tcm_mru_page->tcp_mr_used = page;
	page->tcp_mr_used = NULL;
	dcg->tcm_mru_page = page;
	if (!dcg->tcm_lru_page)
		dcg->tcm_lru_page = page;
	xt_unlock_mutex_ns(&dcg->tcm_lock);

	/* Add the page to the hash table: */
	page->tcp_next = seg->tcs_hash_table[hash_idx];
	seg->tcs_hash_table[hash_idx] = page;

	/* GOTCHA! This increment was done just after the malloc!
	 * So it was not protected by the segment lock!
	 * The result was that this count was no longer reliable,
	 * This resulted in the amount of cache being used becoming less, and\
	 * less, because increments were lost over time!
	 */
	/* Increment cache used. */
	seg->tcs_cache_in_use += page_size;

	done_ok:
	*ret_seg = seg;
	*ret_page = page;
#ifdef DEBUG_CHECK_CACHE
	//XT_TC_check_cache();
#endif
	thread->st_statistics.st_rec_cache_miss++;
	return OK;

	failed:
	xt_free_ns(new_page);
	return FAILED;
}


/* ----------------------------------------------------------------------
 * OPERATION SEQUENCE
 */

xtBool XTTableSeq::ts_log_no_op(XTThreadPtr thread, xtTableID tab_id, xtOpSeqNo op_seq)
{
	XTactNoOpEntryDRec	ent_rec;
	xtWord4				sum = (xtWord4) tab_id ^ (xtWord4) op_seq;

	ent_rec.no_status_1 = XT_LOG_ENT_NO_OP;
	ent_rec.no_checksum_1 = XT_CHECKSUM_1(sum);
	XT_SET_DISK_4(ent_rec.no_tab_id_4, tab_id);
	XT_SET_DISK_4(ent_rec.no_op_seq_4, op_seq);
	/* TODO - If this also fails we have a problem.
	 * From this point on we should actually not generate
	 * any more op IDs. The problem is that the
	 * some will be missing, so the writer will not
	 * be able to contniue.
	 */
	return xt_xlog_log_data(thread, sizeof(XTactNoOpEntryDRec), (XTXactLogBufferDPtr) &ent_rec, XT_XLOG_NO_WRITE_NO_FLUSH);
}

#ifdef XT_NOT_INLINE
xtOpSeqNo XTTableSeq::ts_set_op_seq(XTTabCachePagePtr page)
{
	xtOpSeqNo seq;

	xt_lock_mutex_ns(&ts_ns_lock);
	page->tcp_op_seq = seq = ts_next_seq++;
	xt_unlock_mutex_ns(&ts_ns_lock);
	return seq;
}

xtOpSeqNo XTTableSeq::ts_get_op_seq()
{
	xtOpSeqNo seq;

	xt_lock_mutex_ns(&ts_ns_lock);
	seq = ts_next_seq++;
	xt_unlock_mutex_ns(&ts_ns_lock);
	return seq;
}
#endif

#ifdef XT_NOT_INLINE
/*
 * Return TRUE if the current sequence is before the
 * target (then) sequence number. This function
 * takes into account overflow. Overflow is detected
 * by checking the difference between the 2 values.
 * If the difference is very large, then we
 * assume overflow.
 */
xtBool XTTableSeq::xt_op_is_before(register xtOpSeqNo now, register xtOpSeqNo then)
{
	ASSERT_NS(sizeof(xtOpSeqNo) == 4);
	/* The now time is being incremented.
	 * If it is after the then time (which is static, then
	 * it is not before!
	 */
	if (now >= then) {
		if ((now - then) > (xtOpSeqNo) 0xFFFFFFFF/2)
			return TRUE;
		return FALSE;
	}

	/* If it appears to be before, we still have to check
	 * for overflow. If the gap is bigger then half of
	 * the MAX value, then we can assume it has wrapped around
	 * because we know that no then can be so far in the
	 * future!
	 */
	if ((then - now) > (xtOpSeqNo) 0xFFFFFFFF/2)
		return FALSE;
	return TRUE;
}
#endif


/* ----------------------------------------------------------------------
 * F R E E E R    P R O C E S S
 */

/*
 * Used by the writer to wake the freeer.
 */
xtPublic void xt_wr_wake_freeer(XTThreadPtr self, XTDatabaseHPtr db)
{
	/* BUG FIX: Was using tcm_freeer_cond.
	 * This is incorrect. When the freeer waits for the
	 * writter, it uses the writer's condition!
	 */
	xt_lock_mutex_ns(&db->db_wr_lock);
	if (!xt_broadcast_cond_ns(&db->db_wr_cond))
		xt_log_and_clear_exception_ns();
	xt_unlock_mutex_ns(&db->db_wr_lock);
/*
	xt_lock_mutex(self, &xt_tab_cache.tcm_freeer_lock);
	pushr_(xt_unlock_mutex, &xt_tab_cache.tcm_freeer_lock);
	if (!xt_broadcast_cond_ns(&xt_tab_cache.tcm_freeer_cond))
		xt_log_and_clear_exception_ns();
	freer_(); // xt_unlock_mutex(&xt_tab_cache.tcm_freeer_lock)
*/
}

/* Wait for a transaction to quit: */
static void tabc_fr_wait_for_cache(XTThreadPtr self, u_int msecs)
{
	if (!self->t_quit)
		xt_timed_wait_cond(NULL, &xt_tab_cache.tcm_freeer_cond, &xt_tab_cache.tcm_freeer_lock, msecs);
}

typedef struct TCResource {
	XTOpenTablePtr		tc_ot;
} TCResourceRec, *TCResourcePtr;

static void tabc_free_fr_resources(XTThreadPtr self, TCResourcePtr tc)
{
	if (tc->tc_ot) {
		xt_db_return_table_to_pool(self, tc->tc_ot);
		tc->tc_ot = NULL;
	}
}

static XTTableHPtr tabc_get_table(XTThreadPtr self, TCResourcePtr tc, xtDatabaseID db_id, xtTableID tab_id)
{
	XTTableHPtr	tab;
	XTDatabaseHPtr	db;

	if (tc->tc_ot) {
		tab = tc->tc_ot->ot_table;
		if (tab->tab_id == tab_id && tab->tab_db->db_id == db_id)
			return tab;

		xt_db_return_table_to_pool(self, tc->tc_ot);
		tc->tc_ot = NULL;
	}

	if (!tc->tc_ot) {
		if (!(db = xt_get_database_by_id(self, db_id)))
			return NULL;

		pushr_(xt_heap_release, db);
		tc->tc_ot = xt_db_open_pool_table(self, db, tab_id, NULL, TRUE);
		freer_(); // xt_heap_release(db);
		if (!tc->tc_ot)
			return NULL;
	}

	return tc->tc_ot->ot_table;
}

/*
 * Free the given page, or the least recently used page.
 * Return the amount of bytes freed.
 */
static size_t tabc_free_page(XTThreadPtr self, TCResourcePtr tc)
{
	register XTTabCacheMemPtr	dcg = &xt_tab_cache;
	XTTableHPtr					tab = NULL;
	XTTabCachePagePtr			page, lpage, ppage;
	XTTabCacheSegPtr			seg;
	u_int						page_cnt;
	xtBool						was_dirty;

#ifdef DEBUG_CHECK_CACHE
	//XT_TC_check_cache();
#endif
	dcg->tcm_free_try_count = 0;

	retry:
	/* Note, handling the page is safe because
	 * there is only one free'er thread which
	 * can remove pages from the cache!
	 */
	page_cnt = 0;
	if (!(page = dcg->tcm_lru_page)) {
		dcg->tcm_free_try_count = 0;
		return 0;
	}

	retry_2:
	if ((was_dirty = page->tcp_dirty)) {
		/* Do all this stuff without a lock, because to
		 * have a lock while doing this is too expensive!
		 */
	
		/* Wait for the page to be cleaned. */
		tab = tabc_get_table(self, tc, page->tcp_db_id, page->tcp_tab_id);
	}

	seg = &dcg->tcm_segment[page->tcp_seg];
	TAB_CAC_WRITE_LOCK(&seg->tcs_lock, self->t_id);

	if (page->tcp_dirty) {
		if (!was_dirty) {
			TAB_CAC_UNLOCK(&seg->tcs_lock, self->t_id);
			goto retry_2;
		}

		if (tab) {
			ASSERT(!XTTableSeq::xt_op_is_before(tab->tab_seq.ts_next_seq, page->tcp_op_seq+1));
			/* This should never happen. However, is has been occuring,
			 * during multi_update test on Windows.
			 * In particular it occurs after rename of a table, during ALTER.
			 * As if the table was not flushed before the rename!?
			 * To guard against an infinite loop below, I will just continue here.
			 */
			if (XTTableSeq::xt_op_is_before(tab->tab_seq.ts_next_seq, page->tcp_op_seq+1))
				goto go_on;
			/* OK, we have the table, now we check where the current
			 * sequence number is.
			 */
			if (XTTableSeq::xt_op_is_before(tab->tab_head_op_seq, page->tcp_op_seq)) {
				XTDatabaseHPtr db = tab->tab_db;

				rewait:
				TAB_CAC_UNLOCK(&seg->tcs_lock, self->t_id);

				/* Flush the log, in case this is holding up the
				 * writer!
				 */
				if (!db->db_xlog.xlog_flush(self)) {
					dcg->tcm_free_try_count = 0;
					xt_throw(self);
				}

				xt_lock_mutex(self, &db->db_wr_lock);
				pushr_(xt_unlock_mutex, &db->db_wr_lock);

				/* The freeer is now waiting: */
				db->db_wr_freeer_waiting = TRUE;

				/* If the writer is idle, wake it up. 
				 * The writer will commit the changes to the database
				 * which will allow the freeer to free up the cache.
				 */
				if (db->db_wr_idle) {
					if (!xt_broadcast_cond_ns(&db->db_wr_cond))
						xt_log_and_clear_exception_ns();
				}

				/* Go to sleep on the writer's condition.
				 * The writer will wake the free'er before it goes to
				 * sleep!
				 */
				tab->tab_wake_freeer_op = page->tcp_op_seq;
				tab->tab_wr_wake_freeer = TRUE;
				if (!xt_timed_wait_cond_ns(&db->db_wr_cond, &db->db_wr_lock, 30000)) {
					tab->tab_wr_wake_freeer = FALSE;
					db->db_wr_freeer_waiting = FALSE;
					xt_throw(self);
				}
				tab->tab_wr_wake_freeer = FALSE;
				db->db_wr_freeer_waiting = FALSE;
				freer_(); // xt_unlock_mutex(&db->db_wr_lock)

				TAB_CAC_WRITE_LOCK(&seg->tcs_lock, self->t_id);
				if (XTTableSeq::xt_op_is_before(tab->tab_head_op_seq, page->tcp_op_seq))
					goto rewait;
			}
			go_on:;
		}
	}

	/* Wait if the page is being read or locked. */
	if (page->tcp_lock_count) {
		/* (1) If the page is being read, then we should not free
		 *     it immediately.
		 * (2) If a page is locked, the locker may be waiting
		 *     for the freeer to free some cache - this
		 *     causes a deadlock.
		 *
		 * Therefore, we move on, and try to free another page...
		 */
		if (page_cnt < (dcg->tcm_approx_page_count >> 1)) {
			/* Page has not changed MRU position, and we
			 * have looked at less than half of the pages.
			 * Go to the next page...
			 */
			if ((page = page->tcp_mr_used)) {
				page_cnt++;
				TAB_CAC_UNLOCK(&seg->tcs_lock, self->t_id);
				goto retry_2;
			}
		}
		TAB_CAC_UNLOCK(&seg->tcs_lock, self->t_id);
		dcg->tcm_free_try_count++;				

		/* Starting to spin, free the threads: */
		if (dcg->tcm_threads_waiting) {
			if (!xt_broadcast_cond_ns(&dcg->tcm_freeer_cond))
				xt_log_and_clear_exception_ns();
		}
		goto retry;
	}

	/* Page is clean, remove from the hash table: */

	/* Find the page on the list: */
	u_int page_idx = page->tcp_page_idx;
	u_int file_id = page->tcp_file_id;

	ppage = NULL;
	lpage = seg->tcs_hash_table[page->tcp_hash_idx];
	while (lpage) {
		if (lpage->tcp_page_idx == page_idx && lpage->tcp_file_id == file_id)
			break;
		ppage = lpage;
		lpage = lpage->tcp_next;
	}

	if (page == lpage) {
		/* Should be the case! */
		if (ppage)
			ppage->tcp_next = page->tcp_next;
		else
			seg->tcs_hash_table[page->tcp_hash_idx] = page->tcp_next;
	}
#ifdef DEBUG
	else
		ASSERT_NS(FALSE);
#endif

	/* Remove from the MRU list: */
	xt_lock_mutex_ns(&dcg->tcm_lock);
	if (dcg->tcm_lru_page == page)
		dcg->tcm_lru_page = page->tcp_mr_used;
	if (dcg->tcm_mru_page == page)
		dcg->tcm_mru_page = page->tcp_lr_used;
	if (page->tcp_lr_used)
		page->tcp_lr_used->tcp_mr_used = page->tcp_mr_used;
	if (page->tcp_mr_used)
		page->tcp_mr_used->tcp_lr_used = page->tcp_lr_used;
	xt_unlock_mutex_ns(&dcg->tcm_lock);

	/* Free the page: */
	size_t freed_space = offsetof(XTTabCachePageRec, tcp_data) + page->tcp_data_size;
	ASSERT_NS(seg->tcs_cache_in_use >= freed_space);
	seg->tcs_cache_in_use -= freed_space;
	ASSERT_NS(seg->tcs_cache_in_use == 0 || seg->tcs_cache_in_use >= 25000);
	xt_free_ns(page);

	TAB_CAC_UNLOCK(&seg->tcs_lock, self->t_id);
	self->st_statistics.st_rec_cache_frees++;
	dcg->tcm_free_try_count = 0;
	return freed_space;
}

static void tabc_fr_main(XTThreadPtr self)
{
	register XTTabCacheMemPtr	dcg = &xt_tab_cache;
	TCResourceRec				tc = { 0 };
	int							i;

	xt_set_low_priority(self);
	dcg->tcm_freeer_busy = TRUE;

	while (!self->t_quit) {		
		size_t cache_used, freed;

		pushr_(tabc_free_fr_resources, &tc);

		while (!self->t_quit) {
			/* Total up the cache memory used: */
			cache_used = 0;
			for (i=0; i<XT_TC_SEGMENT_COUNT; i++)
				cache_used += dcg->tcm_segment[i].tcs_cache_in_use;

			if (cache_used > dcg->tcm_cache_high)
				dcg->tcm_cache_high = cache_used;

			/* Check if the cache usage is over 95%: */
			if (self->t_quit)
				break;

			/* If threads are waiting then we are more aggressive about freeing
			 * cache.
			 */ 
			if (cache_used < (dcg->tcm_threads_waiting ? dcg->tcm_mid_level : dcg->tcm_high_level))
				break;

			/* Reduce cache to the 75% level: */
			while (!self->t_quit && cache_used > dcg->tcm_low_level) {
				freed = tabc_free_page(self, &tc);
				cache_used -= freed;
				if (cache_used <= dcg->tcm_high_level) {
					/* Wakeup any threads that are waiting for some cache to be
					 * freed.
					 */
					if (dcg->tcm_threads_waiting) {
						if (!xt_broadcast_cond_ns(&dcg->tcm_freeer_cond))
							xt_log_and_clear_exception_ns();
					}
				}
			}
		}

		freer_(); // tabc_free_fr_resources(&tc)

		xt_lock_mutex(self, &dcg->tcm_freeer_lock);
		pushr_(xt_unlock_mutex, &dcg->tcm_freeer_lock);

		if (dcg->tcm_threads_waiting) {
			/* Wake threads before we go to sleep: */
			if (!xt_broadcast_cond_ns(&dcg->tcm_freeer_cond))
				xt_log_and_clear_exception_ns();
		}
			
		/* Wait for a thread that allocates data to signal
		 * that the cache level has exceeeded the upper limit:
		 */
		xt_db_approximate_time = time(NULL);
		dcg->tcm_freeer_busy = FALSE;
		/* No idea, why, but I am getting an uneccesarry pause here.
		 * I run DBT2 with low record cache.
		 *
		 * Every now and then there is a pause where the freeer is here,
		 * and all user threads are waiting for the freeer.
		 *
		 * So adding the tcm_threads_waiting condition.
		 */
		if (dcg->tcm_threads_waiting) {
			cache_used = 0;
			for (i=0; i<XT_TC_SEGMENT_COUNT; i++)
				cache_used += dcg->tcm_segment[i].tcs_cache_in_use;
			if (cache_used < dcg->tcm_mid_level)
				tabc_fr_wait_for_cache(self, 500);
		}
		else
			tabc_fr_wait_for_cache(self, 500);
		//tabc_fr_wait_for_cache(self, 30*1000);
		dcg->tcm_freeer_busy = TRUE;
		xt_db_approximate_time = time(NULL);
		freer_(); // xt_unlock_mutex(&dcg->tcm_freeer_lock)
	}
}

static void *tabc_fr_run_thread(XTThreadPtr self)
{
	int		count;
	void	*mysql_thread;

	myxt_wait_pbxt_plugin_slot_assigned(self);

	mysql_thread = myxt_create_thread();

	while (!self->t_quit) {
		try_(a) {
			tabc_fr_main(self);
		}
		catch_(a) {
			/* This error is "normal"! */
			if (!(self->t_exception.e_xt_err == XT_SIGNAL_CAUGHT &&
				self->t_exception.e_sys_err == SIGTERM))
				xt_log_and_clear_exception(self);
		}
		cont_(a);

		/* After an exception, pause before trying again... */
		/* Number of seconds */
#ifdef DEBUG
		count = 10;
#else
		count = 2*60;
#endif
		while (!self->t_quit && count > 0) {
			xt_db_approximate_time = time(NULL);
			sleep(1);
			count--;
		}
	}

   /*
	* {MYSQL-THREAD-KILL}
	myxt_destroy_thread(mysql_thread, TRUE);
	*/
	return NULL;
}

static void tabc_fr_free_thread(XTThreadPtr self, void *XT_UNUSED(data))
{
	if (xt_tab_cache.tcm_freeer_thread) {
		xt_lock_mutex(self, &xt_tab_cache.tcm_freeer_lock);
		pushr_(xt_unlock_mutex, &xt_tab_cache.tcm_freeer_lock);
		xt_tab_cache.tcm_freeer_thread = NULL;
		freer_(); // xt_unlock_mutex(&xt_tab_cache.tcm_freeer_lock)
	}
}

xtPublic void xt_start_freeer(XTThreadPtr self)
{
	xt_tab_cache.tcm_freeer_thread = xt_create_daemon(self, "free-er");
	xt_set_thread_data(xt_tab_cache.tcm_freeer_thread, NULL, tabc_fr_free_thread);
	xt_run_thread(self, xt_tab_cache.tcm_freeer_thread, tabc_fr_run_thread);
}

xtPublic void xt_quit_freeer(XTThreadPtr self)
{
	if (xt_tab_cache.tcm_freeer_thread) {
		xt_lock_mutex(self, &xt_tab_cache.tcm_freeer_lock);
		pushr_(xt_unlock_mutex, &xt_tab_cache.tcm_freeer_lock);
		xt_terminate_thread(self, xt_tab_cache.tcm_freeer_thread);
		freer_(); // xt_unlock_mutex(&xt_tab_cache.tcm_freeer_lock)
	}
}

xtPublic void xt_stop_freeer(XTThreadPtr self)
{
	XTThreadPtr thr_fr;

	if (xt_tab_cache.tcm_freeer_thread) {
		xt_lock_mutex(self, &xt_tab_cache.tcm_freeer_lock);
		pushr_(xt_unlock_mutex, &xt_tab_cache.tcm_freeer_lock);

		/* This pointer is safe as long as you have the transaction lock. */
		if ((thr_fr = xt_tab_cache.tcm_freeer_thread)) {
			xtThreadID tid = thr_fr->t_id;

			/* Make sure the thread quits when woken up. */
			xt_terminate_thread(self, thr_fr);

			/* Wake the freeer to get it to quit: */
			if (!xt_broadcast_cond_ns(&xt_tab_cache.tcm_freeer_cond))
				xt_log_and_clear_exception_ns();
	
			freer_(); // xt_unlock_mutex(&xt_tab_cache.tcm_freeer_lock)

			/*
			 * GOTCHA: This is a wierd thing but the SIGTERM directed
			 * at a particular thread (in this case the sweeper) was
			 * being caught by a different thread and killing the server
			 * sometimes. Disconcerting.
			 * (this may only be a problem on Mac OS X)
			xt_kill_thread(thread);
			 */
			xt_wait_for_thread(tid, FALSE);
	
			/* PMC - This should not be necessary to set the signal here, but in the
			 * debugger the handler is not called!!?
			thr_fr->t_delayed_signal = SIGTERM;
			xt_kill_thread(thread);
			 */
			xt_tab_cache.tcm_freeer_thread = NULL;
		}
		else
			freer_(); // xt_unlock_mutex(&xt_tab_cache.tcm_freeer_lock)
	}
}

xtPublic void xt_load_pages(XTThreadPtr self, XTOpenTablePtr ot)
{
	XTTableHPtr			tab = ot->ot_table;
	xtRecordID			rec_id;
	XTTabCachePagePtr	page;
	XTTabCacheSegPtr	seg;
	size_t				poffset;

	rec_id = 1;
	while (rec_id<tab->tab_row_eof_id) {
		if (!tab->tab_rows.tc_fetch(ot->ot_row_file, rec_id, &seg, &page, &poffset, TRUE, self))
			xt_throw(self);
		TAB_CAC_UNLOCK(&seg->tcs_lock, self->t_id);
		rec_id += tab->tab_rows.tci_rows_per_page;
	}

	rec_id = 1;
	while (rec_id<tab->tab_rec_eof_id) {
		if (!tab->tab_recs.tc_fetch(ot->ot_rec_file, rec_id, &seg, &page, &poffset, TRUE, self))
			xt_throw(self);
		TAB_CAC_UNLOCK(&seg->tcs_lock, self->t_id);
		rec_id += tab->tab_recs.tci_rows_per_page;
	}
}