-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy patherl_db.c
5771 lines (5000 loc) · 158 KB
/
erl_db.c
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
/*
* %CopyrightBegin%
*
* Copyright Ericsson AB 1996-2024. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* %CopyrightEnd%
*/
/*
* This file contains the 'ets' bif interface functions.
*/
/*
#ifdef DEBUG
#define HARDDEBUG 1
#endif
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include "sys.h"
#include "erl_vm.h"
#include "global.h"
#include "erl_process.h"
#include "error.h"
#define ERTS_WANT_DB_INTERNAL__
#include "erl_db.h"
#include "bif.h"
#include "big.h"
#include "erl_binary.h"
#include "bif.h"
/*
* Extended error information for ETS functions.
*/
#define EXI_TYPE am_type /* The type is wrong (or TRAP). */
#define EXI_ID am_id /* The table identifier is invalid. */
#define EXI_ACCESS am_access /* Insufficient access rights for ETS table. */
#define EXI_TAB_TYPE am_table_type /* Unsupported table type for this operation. */
#define EXI_BAD_KEY am_badkey /* No such key exists in the table. */
#define EXI_KEY_POS am_keypos /* The element to update is also the key. */
#define EXI_POSITION am_position /* The position is out of range. */
#define EXI_OWNER am_owner /* The receiving process is already the owner. */
#define EXI_NOT_OWNER am_not_owner /* The current process is not the owner. */
#define EXI_ALREADY_EXISTS am_already_exists /* The table identifier already exists. */
#define DB_WRITE_CONCURRENCY_MIN_LOCKS 1
#define DB_WRITE_CONCURRENCY_MAX_LOCKS 32768
erts_atomic_t erts_ets_misc_mem_size;
/*
** Utility macros
*/
#if defined(DEBUG)
# define DBG_RANDOM_REDS(REDS, SEED) \
((REDS) * 0.1 * erts_sched_local_random_float(SEED))
#else
# define DBG_RANDOM_REDS(REDS, SEED) (REDS)
#endif
#define DB_BIF_GET_TABLE(TB, WHAT, KIND, BIF_IX) \
DB_GET_TABLE(TB, BIF_ARG_1, WHAT, KIND, BIF_IX, NULL, BIF_P)
#define DB_TRAP_GET_TABLE(TB, TID, WHAT, KIND, BIF_EXP) \
DB_GET_TABLE(TB, TID, WHAT, KIND, 0, BIF_EXP, BIF_P)
#define DB_GET_TABLE(TB, TID, WHAT, KIND, BIF_IX, BIF_EXP, PROC) \
do { \
Uint freason__; \
if (!(TB = db_get_table(PROC, TID, WHAT, KIND, &freason__))) { \
return db_bif_fail(PROC, freason__, BIF_IX, BIF_EXP); \
} \
}while(0)
#define DB_GET_APPROX_NITEMS(DB) \
erts_flxctr_read_approx(&(DB)->common.counters, ERTS_DB_TABLE_NITEMS_COUNTER_ID)
#define DB_GET_APPROX_MEM_CONSUMED(DB) \
erts_flxctr_read_approx(&(DB)->common.counters, ERTS_DB_TABLE_MEM_COUNTER_ID)
static BIF_RETTYPE db_bif_fail(Process* p, Uint freason,
Uint bif_ix, Export* bif_exp)
{
if (freason == TRAP) {
if (!bif_exp) {
bif_exp = BIF_TRAP_EXPORT(bif_ix);
}
ERTS_BIF_PREP_TRAP(bif_exp, p, bif_exp->info.mfa.arity);
}
p->freason = freason;
return THE_NON_VALUE;
}
/* Get a key from any table structure and a tagged object */
#define TERM_GETKEY(tb, obj) db_getkey((tb)->common.keypos, (obj))
# define ITERATION_SAFETY(Proc,Tab) \
((IS_TREE_TABLE((Tab)->common.status) || IS_CATREE_TABLE((Tab)->common.status) \
|| ONLY_WRITER(Proc,Tab)) ? ITER_SAFE \
: (((Tab)->common.status & DB_FINE_LOCKED) ? ITER_UNSAFE : ITER_SAFE_LOCKED))
#define DID_TRAP(P,Ret) (!is_value(Ret) && ((P)->freason == TRAP))
/*
* "fixed_tabs": list of all fixed tables for a process
*/
#ifdef DEBUG
static bool fixed_tabs_find(DbFixation* first, DbFixation* fix);
#endif
static void fixed_tabs_insert(Process* p, DbFixation* fix)
{
DbFixation* first = erts_psd_get(p, ERTS_PSD_ETS_FIXED_TABLES);
if (!first) {
fix->tabs.next = fix->tabs.prev = fix;
erts_psd_set(p, ERTS_PSD_ETS_FIXED_TABLES, fix);
}
else {
ASSERT(!fixed_tabs_find(first, fix));
fix->tabs.prev = first->tabs.prev;
fix->tabs.next = first;
fix->tabs.prev->tabs.next = fix;
first->tabs.prev = fix;
}
}
static void fixed_tabs_delete(Process *p, DbFixation* fix)
{
if (fix->tabs.next == fix) {
DbFixation* old;
ASSERT(fix->tabs.prev == fix);
old = erts_psd_set(p, ERTS_PSD_ETS_FIXED_TABLES, NULL);
ASSERT(old == fix); (void)old;
}
else {
DbFixation *first = (DbFixation*) erts_psd_get(p, ERTS_PSD_ETS_FIXED_TABLES);
ASSERT(fixed_tabs_find(first, fix));
fix->tabs.prev->tabs.next = fix->tabs.next;
fix->tabs.next->tabs.prev = fix->tabs.prev;
if (fix == first)
erts_psd_set(p, ERTS_PSD_ETS_FIXED_TABLES, fix->tabs.next);
}
}
#ifdef DEBUG
static bool fixed_tabs_find(DbFixation* first, DbFixation* fix)
{
DbFixation* p;
if (!first) {
first = (DbFixation*) erts_psd_get(fix->procs.p, ERTS_PSD_ETS_FIXED_TABLES);
}
p = first;
do {
if (p == fix)
return 1;
ASSERT(p->procs.p == fix->procs.p);
ASSERT(p->tabs.next->tabs.prev == p);
p = p->tabs.next;
} while (p != first);
return 0;
}
#endif
/*
* fixing_procs: tree of all processes fixating a table
*/
#define ERTS_RBT_PREFIX fixing_procs
#define ERTS_RBT_T DbFixation
#define ERTS_RBT_KEY_T Process*
#define ERTS_RBT_FLAGS_T bool
#define ERTS_RBT_INIT_EMPTY_TNODE(T) \
do { \
(T)->procs.parent = NULL; \
(T)->procs.right = NULL; \
(T)->procs.left = NULL; \
} while (0)
#define ERTS_RBT_IS_RED(T) ((T)->procs.is_red)
#define ERTS_RBT_SET_RED(T) ((T)->procs.is_red = true)
#define ERTS_RBT_IS_BLACK(T) (!(T)->procs.is_red)
#define ERTS_RBT_SET_BLACK(T) ((T)->procs.is_red = false)
#define ERTS_RBT_GET_FLAGS(T) ((T)->procs.is_red)
#define ERTS_RBT_SET_FLAGS(T, F) ((T)->procs.is_red = (F))
#define ERTS_RBT_GET_PARENT(T) ((T)->procs.parent)
#define ERTS_RBT_SET_PARENT(T, P) ((T)->procs.parent = (P))
#define ERTS_RBT_GET_RIGHT(T) ((T)->procs.right)
#define ERTS_RBT_SET_RIGHT(T, R) ((T)->procs.right = (R))
#define ERTS_RBT_GET_LEFT(T) ((T)->procs.left)
#define ERTS_RBT_SET_LEFT(T, L) ((T)->procs.left = (L))
#define ERTS_RBT_GET_KEY(T) ((T)->procs.p)
#define ERTS_RBT_IS_LT(KX, KY) ((KX) < (KY))
#define ERTS_RBT_IS_EQ(KX, KY) ((KX) == (KY))
#define ERTS_RBT_WANT_INSERT
#define ERTS_RBT_WANT_LOOKUP
#define ERTS_RBT_WANT_DELETE
#define ERTS_RBT_WANT_FOREACH
#define ERTS_RBT_WANT_FOREACH_DESTROY
#define ERTS_RBT_UNDEF
#include "erl_rbtree.h"
#ifdef HARDDEBUG
# error Do something useful with CHECK_TABLES maybe
#else
# define CHECK_TABLES()
#endif
static void
send_ets_transfer_message(Process *c_p, Process *proc,
ErtsProcLocks *locks,
DbTable *tb, Eterm heir_data);
static void schedule_free_dbtable(DbTable* tb);
static void delete_sched_table(Process *c_p, DbTable *tb);
static void table_dec_refc(DbTable *tb, erts_aint_t min_val)
{
if (erts_refc_dectest(&tb->common.refc, min_val) == 0)
schedule_free_dbtable(tb);
}
static ERTS_INLINE DbTable* btid2tab(Binary* btid)
{
erts_atomic_t *tbref = erts_binary_to_magic_indirection(btid);
return (DbTable *) erts_atomic_read_nob(tbref);
}
static int
db_table_tid_destructor(Binary *btid)
{
ASSERT(btid2tab(btid) == NULL);
return 1;
}
static ERTS_INLINE void
make_btid(DbTable *tb)
{
Binary *btid = erts_create_magic_indirection(db_table_tid_destructor);
erts_atomic_t *tbref = erts_binary_to_magic_indirection(btid);
erts_atomic_init_nob(tbref, (erts_aint_t) tb);
tb->common.btid = btid;
/*
* Table and magic indirection refer each other,
* and table is referred once by being alive...
*/
erts_refc_init(&tb->common.refc, 2);
erts_refc_inc(&btid->intern.refc, 1);
}
static DbTable *
tid2tab(Eterm tid, Eterm *error_info_p)
{
DbTable *tb;
Binary *btid;
erts_atomic_t *tbref;
ASSERT(error_info_p);
if (!is_internal_magic_ref(tid)) {
*error_info_p = EXI_TYPE;
return NULL;
}
btid = erts_magic_ref2bin(tid);
if (ERTS_MAGIC_BIN_DESTRUCTOR(btid) != db_table_tid_destructor) {
*error_info_p = EXI_TYPE;
return NULL;
}
tbref = erts_binary_to_magic_indirection(btid);
tb = (DbTable *) erts_atomic_read_nob(tbref);
ASSERT(!tb || tb->common.btid == btid);
if (tb == NULL) {
*error_info_p = EXI_ID;
}
return tb;
}
static ERTS_INLINE bool
is_table_alive(DbTable *tb)
{
erts_atomic_t *tbref;
DbTable *rtb;
tbref = erts_binary_to_magic_indirection(tb->common.btid);
rtb = (DbTable *) erts_atomic_read_nob(tbref);
ASSERT(!rtb || rtb == tb);
return !!rtb;
}
static ERTS_INLINE bool
is_table_named(DbTable *tb)
{
return tb->common.type & DB_NAMED_TABLE;
}
static ERTS_INLINE void
tid_clear(Process *c_p, DbTable *tb)
{
DbTable *rtb;
Binary *btid = tb->common.btid;
erts_atomic_t *tbref = erts_binary_to_magic_indirection(btid);
rtb = (DbTable *) erts_atomic_xchg_nob(tbref, (erts_aint_t) NULL);
ASSERT(!rtb || tb == rtb);
if (rtb) {
table_dec_refc(tb, 1);
delete_sched_table(c_p, tb);
}
}
static ERTS_INLINE Eterm
make_tid_heap(Eterm **hp, ErlOffHeap *oh, DbTable *tb)
{
return erts_mk_magic_ref(hp, oh, tb->common.btid);
}
static ERTS_INLINE Eterm
make_tid(Process *c_p, DbTable *tb)
{
Eterm *hp = HAlloc(c_p, ERTS_MAGIC_REF_THING_SIZE);
return make_tid_heap(&hp, &c_p->off_heap, tb);
}
static Eterm
make_tid_fact(ErtsHeapFactory *hf, DbTable *tb)
{
Eterm *hp = erts_produce_heap(hf, ERTS_MAGIC_REF_THING_SIZE, 0);
return make_tid_heap(&hp, hf->off_heap, tb);
}
Eterm
erts_db_make_tid(Process *c_p, DbTableCommon *tb)
{
return make_tid(c_p, (DbTable*)tb);
}
/*
** The meta hash table of all NAMED ets tables
*/
# define META_NAME_TAB_LOCK_CNT 256
union {
erts_rwmtx_t lck;
byte align[ERTS_ALC_CACHE_LINE_ALIGN_SIZE(sizeof(erts_rwmtx_t))];
}meta_name_tab_rwlocks[META_NAME_TAB_LOCK_CNT];
static struct meta_name_tab_entry {
union {
Eterm name_atom;
Eterm mcnt; /* Length of mvec in multiple tab entry */
}u;
union {
DbTable *tb;
struct meta_name_tab_entry* mvec;
}pu;
} *meta_name_tab;
static unsigned meta_name_tab_mask;
static ERTS_INLINE
struct meta_name_tab_entry* meta_name_tab_bucket(Eterm name,
erts_rwmtx_t** lockp)
{
unsigned bix = atom_val(name) & meta_name_tab_mask;
struct meta_name_tab_entry* bucket = &meta_name_tab[bix];
/* Only non-dirty schedulers are allowed to access the metatable
The smp 1 optimizations for ETS depend on that */
ASSERT(erts_get_scheduler_data() && !ERTS_SCHEDULER_IS_DIRTY(erts_get_scheduler_data()));
*lockp = &meta_name_tab_rwlocks[bix % META_NAME_TAB_LOCK_CNT].lck;
return bucket;
}
typedef enum {
LCK_READ=1, /* read only access */
LCK_WRITE=2, /* exclusive table write access */
LCK_WRITE_REC=3, /* record write access */
LCK_NOLOCK_ACCESS=4 /* Used to access the table structure
without acquiring the table lock */
} db_lock_kind_t;
extern DbTableMethod db_hash;
extern DbTableMethod db_tree;
extern DbTableMethod db_catree;
int user_requested_db_max_tabs;
bool erts_ets_realloc_always_moves;
bool erts_ets_always_compress;
static int db_max_tabs;
/*
** Forward decls, static functions
*/
static void fix_table_locked(Process* p, DbTable* tb);
static void unfix_table_locked(Process* p, DbTable* tb, db_lock_kind_t* kind);
static void set_heir(Process* me, DbTable* tb, Eterm heir, UWord heir_data);
static void free_heir_data(DbTable*);
static SWord free_fixations_locked(Process* p, DbTable *tb);
static void delete_all_objects_continue(Process* p, DbTable* tb);
static SWord free_table_continue(Process *p, DbTable *tb, SWord reds);
static void print_table(fmtfn_t to, void *to_arg, bool show, DbTable* tb);
static BIF_RETTYPE ets_select_delete_trap_1(BIF_ALIST_1);
static BIF_RETTYPE ets_select_count_1(BIF_ALIST_1);
static BIF_RETTYPE ets_select_replace_1(BIF_ALIST_1);
static BIF_RETTYPE ets_select_trap_1(BIF_ALIST_1);
static BIF_RETTYPE ets_delete_trap(BIF_ALIST_1);
static Eterm table_info(ErtsHeapFactory* hf, DbTable* tb, Eterm What);
static BIF_RETTYPE ets_select1(Process* p, int bif_ix, Eterm arg1);
static BIF_RETTYPE ets_select2(Process* p, DbTable*, Eterm tid, Eterm ms);
static BIF_RETTYPE ets_select3(Process* p, DbTable*, Eterm tid, Eterm ms, Sint chunk_size);
static BIF_RETTYPE ets_insert_2_list_continuation(Process* p,
struct ets_insert_2_list_info* ctx);
/*
* Exported global
*/
Export ets_select_delete_continue_exp;
Export ets_select_count_continue_exp;
Export ets_select_replace_continue_exp;
Export ets_select_continue_exp;
/*
* Static traps
*/
static Export ets_delete_continue_exp;
static Export *ets_info_binary_trap = NULL;
static void
free_dbtable(void *vtb)
{
DbTable *tb = (DbTable *) vtb;
erts_flxctr_add(&tb->common.counters,
ERTS_DB_TABLE_MEM_COUNTER_ID,
-((Sint)erts_flxctr_nr_of_allocated_bytes(&tb->common.counters)));
if (!DB_LOCK_FREE(tb)) {
ERTS_DB_ALC_MEM_UPDATE_(tb, erts_rwmtx_size(&tb->common.rwlock), 0);
erts_rwmtx_destroy(&tb->common.rwlock);
erts_mtx_destroy(&tb->common.fixlock);
}
ASSERT(is_immed(tb->common.heir_data));
ASSERT(erts_flxctr_is_snapshot_ongoing(&tb->common.counters) ||
sizeof(DbTable) == DB_GET_APPROX_MEM_CONSUMED(tb));
ASSERT(tb->common.btid);
erts_bin_release(tb->common.btid);
erts_flxctr_destroy(&tb->common.counters, ERTS_ALC_T_ETS_CTRS);
erts_free(ERTS_ALC_T_DB_TABLE, tb);
}
static void schedule_free_dbtable(DbTable* tb)
{
/* SMP case: Caller is allowed to access the *common* part of the *tb
* structure until the bif has returned (we typically need to
* unlock the table lock after this function has returned).
* Caller is *not* allowed to access the specialized part
* (hash or tree) of *tb after this function has returned.
*/
ASSERT(erts_refc_read(&tb->common.refc, 0) == 0);
ASSERT(erts_refc_read(&tb->common.fix_count, 0) == 0);
erts_schedule_thr_prgr_later_cleanup_op(free_dbtable,
(void *) tb,
&tb->release.data,
sizeof(DbTable));
}
static ERTS_INLINE void
save_sched_table(Process *c_p, DbTable *tb)
{
ErtsSchedulerData *esdp = erts_proc_sched_data(c_p);
DbTable *first;
ASSERT(esdp);
erts_atomic_inc_nob(&esdp->u.ets_tables.count);
erts_refc_inc(&tb->common.refc, 1);
first = esdp->u.ets_tables.clist;
if (!first) {
tb->common.all.next = tb->common.all.prev = tb;
esdp->u.ets_tables.clist = tb;
}
else {
tb->common.all.prev = first->common.all.prev;
tb->common.all.next = first;
tb->common.all.prev->common.all.next = tb;
first->common.all.prev = tb;
}
}
static ERTS_INLINE void
remove_sched_table(ErtsSchedulerData *esdp, DbTable *tb)
{
ErtsEtsAllYieldData *eaydp;
ASSERT(esdp);
ASSERT(erts_get_ref_numbers_thr_id(ERTS_MAGIC_BIN_REFN(tb->common.btid))
== (Uint32) esdp->no);
ASSERT(erts_atomic_read_nob(&esdp->u.ets_tables.count) > 0);
erts_atomic_dec_nob(&esdp->u.ets_tables.count);
eaydp = ERTS_SCHED_AUX_YIELD_DATA(esdp, ets_all);
if (eaydp->ongoing) {
/* ets:all() op process list from last to first... */
if (eaydp->tab == tb) {
if (eaydp->tab == esdp->u.ets_tables.clist)
eaydp->tab = NULL;
else
eaydp->tab = tb->common.all.prev;
}
}
if (tb->common.all.next == tb) {
ASSERT(tb->common.all.prev == tb);
ASSERT(esdp->u.ets_tables.clist == tb);
esdp->u.ets_tables.clist = NULL;
}
else {
#ifdef DEBUG
DbTable *tmp = esdp->u.ets_tables.clist;
do {
if (tmp == tb) break;
tmp = tmp->common.all.next;
} while (tmp != esdp->u.ets_tables.clist);
ASSERT(tmp == tb);
#endif
tb->common.all.prev->common.all.next = tb->common.all.next;
tb->common.all.next->common.all.prev = tb->common.all.prev;
if (esdp->u.ets_tables.clist == tb)
esdp->u.ets_tables.clist = tb->common.all.next;
}
table_dec_refc(tb, 0);
}
static void
scheduled_remove_sched_table(void *vtb)
{
remove_sched_table(erts_get_scheduler_data(), (DbTable *) vtb);
}
static void
delete_sched_table(Process *c_p, DbTable *tb)
{
ErtsSchedulerData *esdp = erts_proc_sched_data(c_p);
Uint32 sid;
ASSERT(esdp);
ASSERT(tb->common.btid);
sid = erts_get_ref_numbers_thr_id(ERTS_MAGIC_BIN_REFN(tb->common.btid));
ASSERT(1 <= sid && sid <= erts_no_schedulers);
if (sid == (Uint32) esdp->no)
remove_sched_table(esdp, tb);
else
erts_schedule_misc_aux_work((int) sid, scheduled_remove_sched_table, tb);
}
static ERTS_INLINE void
save_owned_table(Process *c_p, DbTable *tb)
{
DbTable *first;
erts_proc_lock(c_p, ERTS_PROC_LOCK_STATUS);
first = (DbTable*) erts_psd_get(c_p, ERTS_PSD_ETS_OWNED_TABLES);
erts_refc_inc(&tb->common.refc, 1);
if (!first) {
tb->common.owned.next = tb->common.owned.prev = tb;
erts_psd_set(c_p, ERTS_PSD_ETS_OWNED_TABLES, tb);
}
else {
tb->common.owned.prev = first->common.owned.prev;
tb->common.owned.next = first;
tb->common.owned.prev->common.owned.next = tb;
first->common.owned.prev = tb;
}
erts_proc_unlock(c_p, ERTS_PROC_LOCK_STATUS);
}
static ERTS_INLINE void
delete_owned_table(Process *p, DbTable *tb)
{
erts_proc_lock(p, ERTS_PROC_LOCK_STATUS);
if (tb->common.owned.next == tb) {
DbTable* old;
ASSERT(tb->common.owned.prev == tb);
old = erts_psd_set(p, ERTS_PSD_ETS_OWNED_TABLES, NULL);
ASSERT(old == tb); (void)old;
}
else {
DbTable *first = (DbTable*) erts_psd_get(p, ERTS_PSD_ETS_OWNED_TABLES);
#ifdef DEBUG
DbTable *tmp = first;
do {
if (tmp == tb) break;
tmp = tmp->common.owned.next;
} while (tmp != first);
ASSERT(tmp == tb);
#endif
tb->common.owned.prev->common.owned.next = tb->common.owned.next;
tb->common.owned.next->common.owned.prev = tb->common.owned.prev;
if (tb == first)
erts_psd_set(p, ERTS_PSD_ETS_OWNED_TABLES, tb->common.owned.next);
}
erts_proc_unlock(p, ERTS_PROC_LOCK_STATUS);
table_dec_refc(tb, 1);
}
static ERTS_INLINE void db_init_lock(DbTable* tb, int use_frequent_read_lock)
{
erts_rwmtx_opt_t rwmtx_opt = ERTS_RWMTX_OPT_DEFAULT_INITER;
if (use_frequent_read_lock)
rwmtx_opt.type = ERTS_RWMTX_TYPE_FREQUENT_READ;
if (erts_ets_rwmtx_spin_count >= 0)
rwmtx_opt.main_spincount = erts_ets_rwmtx_spin_count;
if (!DB_LOCK_FREE(tb)) {
erts_rwmtx_init_opt(&tb->common.rwlock, &rwmtx_opt, "db_tab",
tb->common.the_name, ERTS_LOCK_FLAGS_CATEGORY_DB);
ERTS_DB_ALC_MEM_UPDATE_(tb, 0, erts_rwmtx_size(&tb->common.rwlock));
erts_mtx_init(&tb->common.fixlock, "db_tab_fix",
tb->common.the_name, ERTS_LOCK_FLAGS_CATEGORY_DB);
}
tb->common.is_thread_safe = !(tb->common.status & DB_FINE_LOCKED);
ASSERT(!DB_LOCK_FREE(tb) || tb->common.is_thread_safe);
}
static ERTS_INLINE void db_lock(DbTable* tb, db_lock_kind_t kind)
{
ASSERT(kind != LCK_NOLOCK_ACCESS);
if (DB_LOCK_FREE(tb))
return;
if (tb->common.type & DB_FINE_LOCKED) {
if (kind == LCK_WRITE) {
erts_rwmtx_rwlock(&tb->common.rwlock);
tb->common.is_thread_safe = true;
}
else {
erts_rwmtx_rlock(&tb->common.rwlock);
ASSERT(!tb->common.is_thread_safe);
}
}
else
{
switch (kind) {
case LCK_WRITE:
case LCK_WRITE_REC:
erts_rwmtx_rwlock(&tb->common.rwlock);
break;
default:
erts_rwmtx_rlock(&tb->common.rwlock);
}
ASSERT(tb->common.is_thread_safe);
}
}
static ERTS_INLINE void db_unlock(DbTable* tb, db_lock_kind_t kind)
{
if (DB_LOCK_FREE(tb) || kind == LCK_NOLOCK_ACCESS)
return;
if (tb->common.type & DB_FINE_LOCKED) {
if (kind == LCK_WRITE) {
ASSERT(tb->common.is_thread_safe);
tb->common.is_thread_safe = false;
erts_rwmtx_rwunlock(&tb->common.rwlock);
}
else {
ASSERT(!tb->common.is_thread_safe);
erts_rwmtx_runlock(&tb->common.rwlock);
}
}
else {
ASSERT(tb->common.is_thread_safe);
switch (kind) {
case LCK_WRITE:
case LCK_WRITE_REC:
erts_rwmtx_rwunlock(&tb->common.rwlock);
break;
default:
erts_rwmtx_runlock(&tb->common.rwlock);
}
}
}
static ERTS_INLINE int db_is_exclusive(DbTable* tb, db_lock_kind_t kind)
{
if (DB_LOCK_FREE(tb))
return 1;
return
kind != LCK_READ &&
kind != LCK_NOLOCK_ACCESS &&
tb->common.is_thread_safe;
}
static DbTable* handle_lacking_permission(Process* p, DbTable* tb,
db_lock_kind_t kind,
Uint* freason_p)
{
if (tb->common.status & DB_BUSY) {
if (!db_is_exclusive(tb, kind)) {
db_unlock(tb, kind);
db_lock(tb, LCK_WRITE);
}
if (tb->common.continuation_ctx) {
ets_insert_2_list_continuation(p, tb->common.continuation_ctx);
} else {
delete_all_objects_continue(p, tb);
}
db_unlock(tb, LCK_WRITE);
tb = NULL;
*freason_p = TRAP;
p->fvalue = EXI_TYPE;
}
else if (p->common.id != tb->common.owner
&& (!(p->flags & F_ETS_SUPER_USER)
|| (tb->common.status & DB_DELETE))) {
p->fvalue = (tb->common.status & DB_DELETE) ? EXI_ID : EXI_ACCESS;
db_unlock(tb, kind);
tb = NULL;
*freason_p = BADARG | EXF_HAS_EXT_INFO;
}
return tb;
}
static ERTS_INLINE
DbTable* db_get_table_aux(Process *p,
Eterm id,
int what,
db_lock_kind_t kind,
int name_already_locked,
Uint* freason_p)
{
DbTable *tb;
erts_rwmtx_t *name_lck = NULL;
/*
* IMPORTANT: Only non-dirty scheduler threads are allowed
* to access tables. Memory management depend on it.
*/
ASSERT(erts_get_scheduler_data() && !ERTS_SCHEDULER_IS_DIRTY(erts_get_scheduler_data()));
ASSERT((what == DB_READ_TBL_STRUCT) == (kind == LCK_NOLOCK_ACCESS));
if (META_DB_LOCK_FREE())
name_already_locked = 1;
if (is_not_atom(id)) {
tb = tid2tab(id, &p->fvalue);
} else {
struct meta_name_tab_entry* bucket = meta_name_tab_bucket(id,&name_lck);
if (!name_already_locked)
erts_rwmtx_rlock(name_lck);
else {
ERTS_LC_ASSERT(META_DB_LOCK_FREE()
|| erts_lc_rwmtx_is_rlocked(name_lck)
|| erts_lc_rwmtx_is_rwlocked(name_lck));
name_lck = NULL;
}
tb = NULL;
if (bucket->pu.tb != NULL) {
if (is_atom(bucket->u.name_atom)) { /* single */
if (bucket->u.name_atom == id)
tb = bucket->pu.tb;
}
else { /* multi */
Uint cnt = unsigned_val(bucket->u.mcnt);
Uint i;
for (i=0; i<cnt; i++) {
if (bucket->pu.mvec[i].u.name_atom == id) {
tb = bucket->pu.mvec[i].pu.tb;
break;
}
}
}
}
if (tb == NULL) {
if (name_lck)
erts_rwmtx_runlock(name_lck);
p->fvalue = EXI_ID;
}
}
if (tb) {
if (what == DB_READ_TBL_STRUCT) {
if (name_lck)
erts_rwmtx_runlock(name_lck);
return tb;
}
DB_HASH_ADAPT_NUMBER_OF_LOCKS(tb);
db_lock(tb, kind);
if (name_lck)
erts_rwmtx_runlock(name_lck);
#ifdef ETS_DBG_FORCE_TRAP
/*
* The ets_SUITE uses this to verify that all table lookups calls
* can handle a failed TRAP return correctly.
*/
if (tb->common.dbg_force_trap) {
if (!(p->flags & F_DBG_FORCED_TRAP)) {
db_unlock(tb, kind);
tb = NULL;
*freason_p = TRAP;
p->fvalue = EXI_TYPE;
p->flags |= F_DBG_FORCED_TRAP;
return tb;
} else {
/* back from forced trap */
p->flags &= ~F_DBG_FORCED_TRAP;
}
}
#endif
if (ERTS_UNLIKELY(!(tb->common.status & what))) {
tb = handle_lacking_permission(p, tb, kind, freason_p);
}
}
else {
*freason_p = BADARG | EXF_HAS_EXT_INFO;
}
return tb;
}
static ERTS_INLINE
DbTable* db_get_table(Process *p,
Eterm id,
int what,
db_lock_kind_t kind,
Uint* freason_p)
{
return db_get_table_aux(p, id, what, kind, 0, freason_p);
}
static DbTable* db_get_table_or_fail_return(Binary* btid,
Uint32 what,
db_lock_kind_t kind,
Uint bif_ix,
Process* p)
{
DbTable* tb = btid2tab(btid);
if (!tb) {
p->freason = BADARG | EXF_HAS_EXT_INFO;
p->fvalue = EXI_ID;
}
else {
/* The lock has to be taken to complete the operation */
db_lock(tb, LCK_WRITE);
if (!(tb->common.status & what)) {
Uint freason;
tb = handle_lacking_permission(p, tb, kind, &freason);
if (!tb) {
BIF_RETTYPE ret = db_bif_fail(p, freason, bif_ix, NULL);
ASSERT(ret == THE_NON_VALUE); (void)ret;
}
}
}
return tb;
}
static int insert_named_tab(Eterm name_atom, DbTable* tb, int have_lock)
{
int ret = 0;
erts_rwmtx_t* rwlock;
struct meta_name_tab_entry* new_entry;
struct meta_name_tab_entry* bucket = meta_name_tab_bucket(name_atom,
&rwlock);
if (META_DB_LOCK_FREE())
have_lock = 1;
if (!have_lock)
erts_rwmtx_rwlock(rwlock);
if (bucket->pu.tb == NULL) { /* empty */
new_entry = bucket;
}
else {
struct meta_name_tab_entry* entries;
Uint cnt;
if (is_atom(bucket->u.name_atom)) { /* single */
size_t size;
if (bucket->u.name_atom == name_atom) {
goto done;
}
cnt = 2;
size = sizeof(struct meta_name_tab_entry)*cnt;
entries = erts_db_alloc_nt(ERTS_ALC_T_DB_NTAB_ENT, size);
ERTS_ETS_MISC_MEM_ADD(size);
new_entry = &entries[0];
entries[1] = *bucket;
}
else { /* multi */
size_t size, old_size;
Uint i;
cnt = unsigned_val(bucket->u.mcnt);
for (i=0; i<cnt; i++) {
if (bucket->pu.mvec[i].u.name_atom == name_atom) {
goto done;
}
}
old_size = sizeof(struct meta_name_tab_entry)*cnt;
size = sizeof(struct meta_name_tab_entry)*(cnt+1);
entries = erts_db_realloc_nt(ERTS_ALC_T_DB_NTAB_ENT,
bucket->pu.mvec,
old_size,
size);
ERTS_ETS_MISC_MEM_ADD(size-old_size);
new_entry = &entries[cnt];
cnt++;
}
bucket->pu.mvec = entries;
bucket->u.mcnt = make_small(cnt);
}
new_entry->pu.tb = tb;
new_entry->u.name_atom = name_atom;
ret = 1; /* Ok */
done:
if (!have_lock)
erts_rwmtx_rwunlock(rwlock);
return ret;
}
static int remove_named_tab(DbTable *tb, int have_lock)
{
int ret = 0;
erts_rwmtx_t* rwlock;
Eterm name_atom = tb->common.the_name;
struct meta_name_tab_entry* bucket = meta_name_tab_bucket(name_atom,
&rwlock);
ASSERT(is_table_named(tb));
if (META_DB_LOCK_FREE())
have_lock = 1;
if (!have_lock && erts_rwmtx_tryrwlock(rwlock) == EBUSY) {
db_unlock(tb, LCK_WRITE);
erts_rwmtx_rwlock(rwlock);
db_lock(tb, LCK_WRITE);
}
ERTS_LC_ASSERT(META_DB_LOCK_FREE() || erts_lc_rwmtx_is_rwlocked(rwlock));
if (bucket->pu.tb == NULL) {
goto done;
}
else if (is_atom(bucket->u.name_atom)) { /* single */
if (bucket->u.name_atom != name_atom) {
goto done;
}
bucket->pu.tb = NULL;
}
else { /* multi */
Uint cnt = unsigned_val(bucket->u.mcnt);
Uint i = 0;