jungerl /lib/proc/src/proc.erl

Language Erlang Lines 1652
MD5 Hash cdec37ea41d52bc5d8d6804bc5039128 Estimated Cost $21,430 (why?)
Repository https://github.com/bmizerany/jungerl.git View Raw File View Project SPDX
   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
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
%%%
%%% The contents of this file are subject to the Erlang Public License,
%%% Version 1.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.erlang.org/license/EPL1_0.txt
%%%
%%% Software distributed under the License is distributed on an "AS IS"
%%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
%%% the License for the specific language governing rights and limitations
%%% under the License.
%%%
%%% The Original Code is proc-1.0.
%%%
%%% The Initial Developer of the Original Code is Ericsson AB
%%% Portions created by Ericsson are Copyright (C), 2006, Ericsson AB
%%% All Rights Reserved.
%%%
%%% Contributor(s): ______________________________________.

%%%----------------------------------------------------------------------
%%% #0.    BASIC INFORMATION
%%%----------------------------------------------------------------------
%%% File:	proc.erl
%%% Author       : Ulf Wiger <ulf.wiger@ericsson.com>
%%% Description  : Flexible Local Process Registry
%%% 
%%%----------------------------------------------------------------------

%%% @doc Flexible Local Process Registry 
%%%
%%%  <p>This application supports local registration of processes using any
%%%  erlang term. Furthermore, each process can register by several
%%%  different names at the same time.</p>
%%%
%%%  <h2>Semantics:</h2> <p>`proc' works with unique `names' and
%%%  non-unique `properties'.  </p><p>A name can be any erlang term
%%%  (except a pid or '_'), and each process can register itself under many
%%%  different unique names. 
%%%  A `property' can be any term (except '_'), and must be unique 
%%%  within a process, but several processes can register the same
%%%   property.</p>
%%%  <p>Furthermore, there are "fold" and "select"
%%%  functions to allow for efficient iteration over names or properties in the
%%%  process registry.</p>
%%%  <p>Many of the operations to access names and properties rely on
%%%  the semantics given by {@link //stdlib/ets}, and this is often 
%%%  mentioned in the interface documentation. This means that it is 
%%%  safe to assume that the functions conform to the search semantics
%%%  of ordered_set ets tables. However, it is <i>not</i> safe to assume
%%%  anything about whether the data in fact resides in ets tables, and
%%%  if so, what those tables are called, or indeed how the data is 
%%%  represented internally.</p>
%%%  @end


-module(proc).
-id('102/190 55-CNA 113 33 Ux').
-vsn('/main/R1A/16').
-date('2005-12-09').
-behaviour(gen_server).

%%%----------------------------------------------------------------------
%%% Template Id: ETX/B 00201 - 19055/1 Rev C
%%% 
%%% #Copyright (C) 1996
%%% by ERICSSON TELECOM AB
%%% S - 125 26  STOCKHOLM
%%% SWEDEN, tel int + 46 8 719 0000
%%% 
%%% The program may be used and/or copied only with the written permission from
%%% ERICSSON TELECOM AB, or in accordance with the terms and conditions
%%% stipulated in the agreement/contract under which the program has been
%%% supplied.
%%% 
%%% All rights reserved
%%% 
%%%----------------------------------------------------------------------
%%% #1.    REVISION LOG
%%%----------------------------------------------------------------------
%%% Rev      Date       Name        What
%%% -----    -------    --------    -------------------------------------
%%% R1A/1    20050630   ebcfsva     Checked Ulf Wiger's code into CC
%%% R1A/2    20050704   etxuwig     Renamed tables, use proc_tabs, etc.
%%% R1A/3    20050816   etxuwig     Fixed fold_keys, added await_reg()
%%% R1A/4    20050901   etxuwig     Added load regulation support, etc.
%%% R1A/5    20050902   etxuwig     Changed semantics of non-unique keys
%%% R1A/6    20050902   etxuwig     Changed names (API)
%%% R1A/7    20050905   etxuwig     update_property -> replace_property
%%%----------------------------------------------------------------------
%%% #2.    EXPORT LISTS
%%%----------------------------------------------------------------------
%%% #2.1   EXPORTED INTERFACE FUNCTIONS
%%%----------------------------------------------------------------------
-export([
	 %% Functions for registering, unregistering, sending,
	 reg/1,
	 unreg/1,
	 where/1,
	 send/2,
	 %% New functions for setting properies (what used to be (Key,Value))
	 add_property/1, add_property/2,
	 is_property/1, is_property/2,
	 replace_property/2, replace_property/3,
	 del_property/1, del_property/2,
	 set_access/1,
	 %% Counters
	 add_counter/2, add_counter/3,
	 read_counter/1, read_counter/2,
	 del_counter/1, del_counter/2,
	 update_counter/2, update_counter/3,
	 is_counter/1, is_counter/2,
	 guards/1,
	 %% Utility function allowing for notifications when someone registers
	 await_reg/1, clear_await_reg/1,
	 %% 
	 pids/1, pids/2,
	 select_pattern/1, select_pattern/2,
	 %% Functions for searching properties
	 select_properties/1, select_properties/2,
	 fold_properties/3, fold_properties/4, fold_properties/5, 
	 select_fold_properties/3, select_fold_properties/4,
	 select_fold_properties/5,
	 properties_by_pid/1, properties_by_pid/2, properties_by_pid/3,
	 %% Functions for searching unique names
	 select_names/1, select_names/2,
	 select/1,
	 fold_names/3, fold_names/4, fold_names/5,
	 first/1,
	 next/2,
	 previous/2,
	 last/1
	]).
-export([info/1,
	 info/2]).

-export([enable_i/0]).

%%% gen_server start function
-export([start_link/0]).

%%% utility functions
-export([i/1]).

%%% gen_server callbacks
-export([init/1,
	 handle_call/3,
	 handle_cast/2,
	 handle_info/2,
	 terminate/2,
	 code_change/3]).

%%-export([start/0]).

-include_lib("stdlib/include/ms_transform.hrl").
-define(REG, proc).
-define(REG_REV, procRev).
-define(PROPS, procProps).
-define(PROPS_REV, procPropsRev).
-define(PRIVS, procPrivs).
-define(MONITORS, procMonitors).

-define(BADARG, throw(badarg)).
-define(EACCESS, throw({badarg,access})).

%% start() ->
%%     application:start(proc).


%%% ===================== (unique) Names =========================



%%% @spec reg(Name) -> true
%%% @doc Register a unique `Name'. Returns `true' or exits.
%%% <p>This function differs from `erlang:register(Name,Pid)' in 
%%% the following ways:</p>
%%% <ul>
%%%  <li>Pid is implicit: always the current process.</li>
%%%  <li>Name can be any term (except a pid or '_'), not just an atom.</li>
%%%  <li>A process can be registered under several unique names.</li>
%%% </ul>
%%% @end
%%%
reg(Pid) when is_pid(Pid) ->
    erlang:error(badarg);
reg('_') ->
    erlang:error(badarg);
reg(Name) ->
    call({reg, Name}).

%%% @spec unreg(Name) -> true
%%% @doc Unregister `Name'
%%% <p>This function exits if `Name' isn't registered to the current
%%% process.</p>
%%% @end
%%%
unreg(Name) ->
    call({unreg, Name}).

%%% @spec where(Name) -> pid() | undefined
%%% @doc Analogous to `erlang:whereis(Name)'
%%%
where(Name) ->
    case ets:lookup(?REG, Name) of
	[{_, Pid}] ->
	    case erlang:is_process_alive(Pid) of
		true ->
		    Pid;
		false ->
		    undefined
	    end;
	[] ->
	    undefined
    end.

%%% @spec send(Name, Msg) -> Msg
%%% @doc Analogous to `Name ! Msg', except that send({Node,Dest}, Msg)
%%% will be interpreted as 'send to the local process registered as 
%%% {Node, Dest}'. If Name is a pid, this function will send the message
%%% to the process with process identifier Name (recall that it is not
%%% possible to register a pid as a unique name in proc.)
%%% @end
%%%
send(Pid, Msg) when is_pid(Pid) ->
    Pid ! Msg;
send(Name, Msg) ->
    case where(Name) of
	undefined ->
	    erlang:error(badarg);
	Pid when is_pid(Pid) ->
	    Pid ! Msg
    end.


%%% @spec await_reg(Name) -> {already_registered,pid()} | Reg
%%% @doc Request to be notified when someone registers as `Name'.
%%% <p>If `Name' is already registered, the function returns 
%%% with the value `{already_registered, Pid}'. Otherwise, it returns
%%% a "reference" (not necessarily of type `reference()') that can be used
%%% to recognize a future message.</p>
%%% <p>When some process `P' registers as `Name', a message on the form
%%% `{sysProgReg, Ref, reg, Name, Pid}' is sent to each waiting process
%%% (note that Ref will be different for each waiting process.)</p>
%%% @end
%%%
await_reg(Pid) when is_pid(Pid) ->
    erlang:error(badarg);
await_reg('_') ->
    erlang:error(badarg);
await_reg(Name) ->
    call({await_reg, Name}).

%%% @spec clear_await_reg(Ref) -> true
%%% @doc Cancel waiting for notification of process registration of a 
%%% given name.
%%% <p>This function exits with `badarg' if Ref does not appear to be 
%%% a valid reference returned by {@link await_reg/1}; otherwise returns
%%% `true'.</p>
%%% @end
%%%
clear_await_reg(Ref) ->
    call({clear_await_reg, Ref}).


%%% ================ (non-unique) Properties =====================

%%% @spec add_property(Property::term()) -> true
%%% @doc Publish a property of the current process.
%%% <p>If `Property' is already published for the current process,
%%% this function exits with badarg.</p>
%%%
%%% <p>This operation can be viewed as publishing the meta-data `Property'
%%% as part of the interface of the process. Several processes may publish
%%% the same property. The process can be found (as part of a group)
%%% through this property, using one of the functions {@link
%%% fold_properties/3}, {@link pids/1}, et al.</p>
%%% @end
%%%
add_property('_') ->
    erlang:error(badarg);
add_property(Property) ->
    call({add_property, self(), Property}).

%%% @spec add_property(Process, Property) -> true
%%% @doc Publish a property on behalf of another process.
%%% <p>Adding properties on behalf of another process is only 
%%% allowed if the other process has called {@link set_access/1},
%%% giving this process the proper rights to act as proxy.</p>
%%% <p>`Process' can be either a `pid()' or a registered name.</p>
%%% @end
%%%
add_property(_Process, '_') ->
    erlang:error(badarg);
add_property(Process, Property) ->
    call({add_property, Process, Property}).



%%% @spec set_access(Access::[{Action,Processes,Ops}]) -> true
%%%      Action = grant | revoke
%%%         Ops = [Op]
%%%         Op  = add_property | replace_property | del_property | properties |
%%%               add_counter | update_counter | del_counter | counters
%%% @doc Control the ability of Processes to perform certain functions
%%%  on behalf of the current process. 
%%% <p>`Op' must be one of</p>
%%% <ul>
%%%  <li>`add_property'</li>
%%%  <li>`replace_property'</li>
%%%  <li>`del_property'</li>
%%%  <li>`properties' (perform all operations on properties)</li>
%%%  <li>`add_counter'</li>
%%%  <li>`update_counter'</li>
%%%  <li>`del_counter'</li>
%%%  <li>`counters' (perform all operations on counters)</li>
%%% </ul>
%%% <p>`Processes' is a list of registered names and/or pids.</p>
%%% <p>Example:</p><pre>
%%% set_access([{grant, ["foo","bar"], [update_counter]},
%%%             {revoke, ["baz"], [properties]}])</pre>
%%% <p>Allows the processes registered as `"foo"' and `"bar"' to update
%%% counters, and makes sure that `"baz"' is no longer able to add, delete
%%% or replace properties.</p>
%%% @end
%%%
set_access(Access) when is_list(Access) ->
    call({set_access, Access}).

%%% @spec is_property(Property) -> true | false
%%% @doc Returns `true' if `Property' is a published property of the current
%%% process.
%%% @end
%%%
is_property(Property) ->
    is_property(self(), Property).

%%% @spec is_property(Process, Property) -> true | false
%%% @doc Check whether Property is in fact a property registered with Process.
%%% <p>`Process' can be either a pid or a registered name.</p>
%%% @end
%%%
is_property(Pid, Property) when is_pid(Pid) ->
    ets:member(?PROPS_REV, {Pid, Property});
is_property(Process, Property) ->
    Pid = on_pid(Process),
    ets:member(?PROPS_REV, {Pid, Property}).

%%% @spec is_counter(Property) -> true | false
%%% @doc Check whether `Property' is a counter property.
%%% <p>Counter properties are created through {@link add_counter/2}, and
%%% behave as normal properties with the following exceptions:</p>
%%% <ul>
%%% <li>A counter property has an integer value attached, which can be 
%%%     updated using {@link update_counter/2}</li>
%%% <li>Counter properties cannot be replaced using
%%%     {@link replace_property/2}</li>
%%% </ul>
%%% @end
%%%
is_counter(Property) ->
    is_counter(self(), Property).

%%% @spec is_counter(Process, Property) -> true | false
%%% @doc Check whether `Propery' registered under `Process' is a counter
%%% property.
%%% <p>`Process' can be either a pid or a registered name.</p>
%%% @end
%%%
is_counter(Pid, Property) when is_pid(Pid) ->
    (ets:select_count(?PROPS_REV, [{{{Pid,Property},counter},[],[true]}]) > 0);
is_counter(Process, Property) ->
    Pid = on_pid(Process),
    (ets:select_count(?PROPS_REV, [{{{Pid,Property},counter},[],[true]}]) > 0).


%%% @spec replace_property(OldProperty, NewProperty) -> true
%%% @doc Equivalent to del_property(OldProperty), add_property(NewProperty),
%%% but much more efficient.
%%% <p>If `OldProperty' is not a published property of the current process
%%% (see {@link add_property/1}), the function exits with `badarg'.</p>
%%% <p>One could use properties as a "published process dictionary",
%%% e.g. by replacing `put(Tag,Value)' with 
%%% `proc:add_property({Tag,Value})', and `get(Tag)' with (roughly)
%%% `proc:properties_by_pid(self(),Tag)'.
%%% While this would be somewhat less efficient than the built-in process
%%% dictionary, it has the advantage of allowing other processes to key
%%% on process meta-data in a much more efficient and disciplined way than
%%% `{_,Dict} = process_info(Pid,dictionary), lists:keysearch(Tag,1,Dict)'
%%% (Which makes no distinction between public and private data, and therefore
%%% is rightfully frowned upon, and hopefully never attempted.)"</p>
%%% <p><i>Note:</i> This function does not work on counter properties.</p>
%%% @end
%%%
replace_property(_OldProp, '_') ->
    erlang:error(badarg);
replace_property(OldProperty, NewProperty) ->
    call({replace_property, self(), OldProperty, NewProperty}).

%%% @spec replace_property(Process, OldProperty, NewProperty) -> true
%%% @doc Replace `OldProperty' with `NewProperty' on behalf of `Process'.
%%% <p>This function is allowed only if permission to replace properties
%%% has been given through `Process' calling {@link grant_access/2}.</p>
%%% <p>If permission to replace properties has not been given, this function
%%% exits with `access'.</p>
%%% @end
%%%
replace_property(_Process, _OldProperty, '_') ->
    erlang:error(badarg);
replace_property(Process, OldProperty, NewProperty) ->
    call({replace_property, Process, OldProperty, NewProperty}).


%%% @spec del_property(Property) -> true
%%% @doc Un-publishes the property `Property' for the current process.
%%% <p>If there is no published property `Property'
%%% (see {@link add_property/1}), for the current process, the function
%%%  exits with `badarg'.</p>
%%% @end
%%%
del_property(Property) ->
    call({del_property, self(), Property}).

%%% @spec del_property(Process, Property) -> true
%%% @doc Un-publish the property `Property' on behalf of `Process'.
%%% <p>This function is allowed only if permission to delete properties
%%% has been given through `Process' calling {@link grant_access/2}.</p>
%%% <p>If permission to delete properties has not been given, this function
%%% exits with `access'.</p>
%%% @end
%%%
del_property(Process, Property) ->
    call({del_property, Process, Property}).

%%% @spec add_counter(Name, InitialValue::integer()) -> true
%%% @doc Publish a counter property for the current process.
%%% <p>Counter properties behave as normal properties with
%%%    the following exceptions:</p>
%%% <ul>
%%% <li>A counter property has an integer value attached, which can be 
%%%     updated using {@link update_counter/2}</li>
%%% <li>Counter properties cannot be replaced using
%%%     {@link replace_property/2}</li>
%%% </ul>
%%% @end
%%%
add_counter('_', _Initial) ->
    erlang:error(badarg);
add_counter(Name, InitialValue) when is_integer(InitialValue) ->
    call({add_counter, self(), Name, InitialValue}).

%%% @spec add_counter(Process, Name, InitialValue::integer()) -> true
%%% @doc Publish a counter property on behalf of `Process'.
%%% <p>This function is allowed only if permission to add counters
%%% has been given through `Process' calling {@link grant_access/2}.</p>
%%% <p>If permission to add counters has not been given, this function
%%% exits with `access'.</p>
%%% @end
%%%
add_counter(_Process, '_', _Initial) ->
    erlang:error(badarg);
add_counter(Process, Name, InitialValue) when is_integer(InitialValue) ->
    call({add_counter, Process, Name, InitialValue}).

%%% @spec del_counter(Name) -> true
%%% @doc Un-publish a counter property for the current process.
%%%
del_counter(Name) ->
    call({del_counter, self(), Name}).

%%% @spec del_counter(Process, Name) -> true
%%% @doc Un-publish a counter property on behalf of `Process'.
%%% <p>This function is allowed only if permission to delete counters
%%% has been given through `Process' calling {@link grant_access/2}.</p>
%%% <p>If permission to delete counters has not been given, this function
%%% exits with `access'.</p>
%%% @end
%%%
del_counter(Process, Name) ->
    call({del_counter, Process, Name}).

%%% @spec read_counter(Name) -> integer()
%%% @doc Read the value of a counter property for the current process.
%%%
read_counter(Name) ->
    do_read_counter(self(), Name).

%%% @spec read_counter(Process, Name) -> integer()
%%% @doc Read the value of a counter property published by `Process'.
%%%
read_counter(Pid, Name) when is_pid(Pid) ->
    do_read_counter(Pid, Name);
read_counter(Process, Name) ->
    case where(Process) of
	Pid when is_pid(Pid) -> do_read_counter(Pid, Name);
	_ ->
	    erlang:error(badarg)
    end.

do_read_counter(Pid, Name) ->
    case ets:lookup_element(?PROPS, {Name, Pid}, 2) of
	I when is_integer(I) ->
	    I;
	_ ->
	    erlang:error(badarg)
    end.
   
%%% @spec update_counter(Name, Incr) -> integer()
%%% @doc Update the counter attribute of a counter property.
%%% <p>This function only works on counter properties. The `Incr' argument
%%% is passed as-is to {@link //stlib/ets:update_counter/3}.
%%% If a complex option, such as `{Pos, Incr}' is used,
%%% `Pos' must be equal to 2.</p>
%%% @end
%%%
update_counter(Name, Incr) ->
    ets:update_counter(?PROPS, {Name, self()}, Incr).

%%% @spec update_counter(Process, Name, Incr) -> integer()
%%% @doc Update counter attribute on behalf of `Process'.
%%% <p>This function is allowed only if permission to update counters
%%% has been given through `Process' calling {@link grant_access/2}.</p>
%%% <p>If permission to update counters has not been given, this function
%%% exits with `access'.</p>
%%% @end
%%%
update_counter(Process, Name, Value) ->
    try begin
	    Pid = on_pid(Process),
	    verify_access(Pid, update_counter, self()),
	    ets:update_counter(?PROPS, {Name, Pid}, Value)
	end
    catch 
	throw:Error ->
	    erlang:error(Error)
    end.

%%% @spec pids(Property) -> [pid()]
%%% @doc Returns all pids for which a property `Property' is published.
%%% <p>To be more precise, `Property' can be any pattern that would be
%%% accepted by {@link //stdlib/ets:select/2}, e.g. &apos;_&apos;</p>
%%% <p>If one imagines an `ordered_set ets' table `Tab' where all properties
%%% are stored on the form `{{Property,Pid},1}', then calling 
%%% this function is equivalent to calling
%%% <pre>ets:select(Tab, [{{{Property,'$1'},'_'}, [], ['$1']}]).</pre></p>
%%% <p>Note that this is also true as regards performance. If the `Key'
%%% pattern is unbound, the operation will be a linear search.</p>
%%% @end
%%%
pids(Property) ->
    pids(Property, []).

%%% @spec pids(Property, Guards::list()) -> [pid()]
%%% @doc Similar to {@link pids/1}, but also allowing select guards.
%%% <p>If one imagines an `ordered_set ets' table `Tab' where all non-unique
%%% keys are stored on the form `{{Property,Pid},1}', then calling 
%%% this function is equivalent to calling
%%% <pre>ets:select(Tab, [{{{Property,'$1'},'_'}, Guards, ['$1']}]).</pre>
%%% </p>
%%% <p>Note that this is also true as regards performance. If the `Property'
%%% pattern is unbound, the operation will be a linear search.</p>
%%% @end
%%%
pids(Property, Guards) ->
    ets:select(?PROPS, [{{{Property,'_'},'_'}, Guards, 
			[{element,2,{element,1,'$_'}}]}]).

%%% --

%%% @spec fold_properties(Fun::function(), Acc, Property) -> NewAcc
%%% @doc Similar to {@link //stdlib/lists:foldl/3}, but based on a
%%% select pattern on `Property', identifying a sub-set of
%%% published properties.
%%% <p>For each matching key, a call is made to 
%%% <pre>Fun({Property1,Pid1}, Acc1)</pre>, which is expected to 
%%% return `NewAcc'. Note that, as in {@link pids/1}, `Property'
%%% can be select a pattern. The following expression would return a 
%%% list of all published instances of `Property' together with the processes
%%% that registered them:</p>
%%% <pre>
%%% proc:fold_properties(fun(X,Acc) -> [X|Acc] end, [], '_')
%%% </pre>
%%% <p>This function is equivalent to `fold_properties(Fun,Acc,Property,1)',
%%% (see {@link fold_properties/4}.) See also {@link select_pattern/1}.</p>
%%% @end
%%%
fold_properties(Fun, Acc, Property) ->
    fold_properties(Fun, Acc, Property, 1, fun(_) -> true end).

%%% @spec fold_properties(Fun::function(), Acc, Key, Limit::integer()) -> 
%%% NewAcc
%%% @doc Like {@link fold_properties/3}, but works in batches of `Limit'
%%% objects at a time, in order to improve raw performance.
%%% <p>`fold_properties/3' uses a default limit of 1 object at a time.</p>
%%% <p>This function is equivalent to 
%%% `fold_properties(Fun,Acc,Property,Limit,fun(_) -> true end)',
%%% (see {@link fold_properties/5}.) See also {@link select_pattern/1}.</p>
%%% @end
%%%
fold_properties(Fun, Acc, Properties, Limit) ->
    fold_properties(Fun, Acc, Properties, Limit, fun(_) -> true end).

%%% @spec fold_properties(Fun::function(), Acc, Property,
%%%     Limit::integer(), Regulator::function()) -> NewAcc
%%% @doc Like {@link fold_properties/4}, but applies a "Regulator function"
%%% after each batch of objects before proceeding with the next one.
%%% <p>The `Regulator' function is expected to take the current accumulator
%%% as an argument and return either `true' (in which case processing 
%%% continues), or `false' (in which case the `fold_properties/5' function
%%% breaks and returns the current accumulator. See also 
%%% {@link select_pattern/1}.</p>
%%% @end
fold_properties(Fun, Acc, Property, Limit, Regulator) ->
    select_fold_properties(
      Fun, Acc, [{Property,[],[true]}], Limit, Regulator).


%%% @spec select_pattern(What) -> Patterns
%%% @doc Helper function to generate select patterns from the more
%%% intuitive patterns `Property' (or `Name', as it works equally for names).
%%% <p>The return value from this function can be used in e.g. 
%%% {@link select_properties/1}, {@link select_properties/2},
%%% {@link select_fold_properties/3},
%%% {@link select_fold_properties/4} or {@link select_fold_properties/5},
%%% or similarly, {@link select_names/1}, {@link select_names/2},
%%% {@link select_fold_names/3}, {@link select_fold_names/4}, or 
%%% {@link select_fold_names/5}.</p>
%%% <p>`Patterns' is a list of match specifications on the same form as 
%%% that used by {@link //stdlib/ets:select_delete/2} and
%%% {@link //stdlib/ets:select_count/2},
%%% that is:</p>
%%% <pre>
%%%   * Patterns = [MatchFunction] | '_'
%%%   * MatchFunction = {MatchHead, [Guard], [Result]}
%%%   * MatchHead = "Pattern as in ets:match"
%%%   * Guard = {"Guardtest name", ...}
%%%   * Result = true (if object is to be included)
%%% </pre>
%%% @end
select_pattern(What) ->
    [{What, [], [true]}].

%%% @spec select_pattern(What, Guards) -> Patterns
%%% @doc As {@link select_pattern/1}, but with the option to add Guard
%%% expressions. 
%%%
select_pattern(What, Guards) ->
    [{What, Guards, [true]}].


%%% @spec guards(List) -> NewList
%%% @doc Expand a list of `select' guards, allowing for the pseudo guard
%%% `is_counter'.
%%% <p>How counter properties can be distinguished from regular properties
%%% internally is not defined in the proc interface. Using this function,
%%% it is however possible to specify a guard test, `is_counter', which will
%%% expand to a legal guard. The `is_counter' test can be combined with the
%%% standard logical operators, `not', `and', `or', `orelse', `andalso',
%%% and `xor'.</p>
%%% <p>Example:</p>
%%% <pre>
%%% proc:select_properties(
%%%     [{'_', proc:guards([{'not', is_counter}]), [true]}]).
%%% </pre>
%%% <p>will list all published properties that are not counters.</p>
%%% @end
%%%
%%% Note that this function must work for both PROPS and PROPS_REV.
%%% For now, this affects counters. The second element of a counter 
%%% must be an integer, in both PROPS and PROPS_REV.
%%%
guards([is_counter|Gs]) ->
    [{is_integer, {element, 2, '$_'}} | guards(Gs)];
guards([{'not', G}|Gs]) ->
    [G1] = guards([G]),
    [{'not', G1} | guards(Gs)];
guards([G|Gs]) when is_tuple(G), size(G) > 1 ->
    case element(1,G) of
	Op when Op == 'and';
		Op == 'or';
		Op == 'andalso';
		Op == 'xor';
		Op == 'orelse' ->
	    Gs1 = tl(tuple_to_list(G)),
	    Gs2 = guards(Gs1),
	    [list_to_tuple([Op|Gs2]) | guards(Gs)];
	_ ->
	    [G | guards(Gs)]
    end;
guards([G|Gs]) ->
    [G | guards(Gs)];
guards([]) ->
    [].
	     
     

%%% @spec select_properties(Patterns) -> {Objs, Continuation} | '$end_of_table'
%%% @doc Returns `{Property,Pid}' objects one at a time based on 
%%% a given selection of all registered instances of `Key'.
%%% <p>`Patterns' can be written as follows:</p>
%%% <pre>
%%%   * Patterns = [MatchFunction] | '_'
%%%   * MatchFunction = {MatchHead, [Guard], [Result]}
%%%   * MatchHead = "Pattern as in ets:match"
%%%   * Guard = {"Guardtest name", ...}
%%%   * Result = true (if object is to be included)
%%% </pre>
%%% <p>or generated using {@link select_pattern/1} or 
%%% {@link select_pattern/2}.</p>
%%% <p>For convenience, the special pattern '_' is also accepted.
%%% It expands to <code>[{'_', [], [true]}]</code>.</p>
%%% <p>The `Continuation' returned can be used when calling {@link select/1}
%%% in order to continue processing.</p>
%%% @end
%%%
select_properties(Patterns) ->
    select_properties(Patterns, 1).



%%% @spec select_properties(Patterns, Limit::integer()) -> 
%%%   {Objs, Continuation} | '$end_of_table'
%%% @doc Like {@link select_properties/1} but works in batches of `Limit'
%%% objects at a time in order to improve raw performance.
%%%
select_properties(Patterns0, Limit) ->
    Patterns = if Patterns0 == '_' ->
		       [{'_',[],[true]}];
		  true ->
		       Patterns0
	       end,
    Patterns1 = [{{{P,'_'},'_'},G,[{{{element,1,{element,1,'$_'}},
 				     {element,2,{element,1,'$_'}}}}]} ||
 		    {P,G,[true]} <- Patterns],
    ets:select(?PROPS, Patterns1, Limit).



%%% @spec select_fold_properties(Fun, Acc, Patterns) -> NewAcc
%%% @doc Like {@link fold_properties/3} but more flexible as it allows
%%% for combining several patterns into one query.
%%% <p>`Patterns' is composed as for {@link select_properties/1}.</p>
%%% @end
%%%
select_fold_properties(Fun, Acc, Patterns) ->
    select_fold_properties(Fun, Acc, Patterns, 1).


%%% @spec select_fold_properties(Fun, Acc, Patterns, Limit) -> NewAcc
%%% @doc Like {@link select_fold_properties/3}, but works in batches of `Limit'
%%% objects at a time, in order to improve raw performance.
%%% <p>`select_fold_properties/3' uses a default limit of 1 object at a
%%% time.</p>
%%% <p>This function is equivalent to 
%%% `select_fold_properties(Fun,Acc,Patterns,Limit,fun(_) -> true end)',
%%% (see {@link select_fold_properties/5}.)</p>
%%% @end
select_fold_properties(Fun, Acc, Patterns, Limit) ->
    select_fold_properties(Fun, Acc, Patterns, Limit, fun(_) -> true end).


%%% @spec select_fold_properties(Fun::function(), Acc, Property,
%%%     Limit::integer(), Regulator::function()) -> NewAcc
%%% @doc Like {@link select_fold_properties/4}, but applies a
%%% "Regulator function"
%%% after each batch of objects before proceeding with the next one.
%%% <p>The `Regulator' function is expected to take the current accumulator
%%% as an argument and return either `true' (in which case processing 
%%% continues), or `false' (in which case the `select_fold_properties/5'
%%% function breaks and returns the current accumulator.</p>
%%% @end
select_fold_properties(Fun, Acc, Patterns, Limit, Regulator) ->
    Res = select_properties(Patterns, Limit),
    select_each_property(Res, Fun, Acc, Regulator).

select_each_property({Objs, Continuation}, Fun, Acc, Regulator) ->
    NewAcc = lists:foldl(
	       fun({_Prop,Pid} =X, Acc1) when is_pid(Pid) ->
		       Fun(X, Acc1)
	       end, Acc, Objs),
    case Regulator(NewAcc) of
	true ->
	    select_each_property(ets:select(Continuation),
				 Fun, NewAcc, Regulator);
	false ->
	    NewAcc
    end;
select_each_property('$end_of_table', _, Acc, _Regulator) ->
    Acc.


%%% @spec select_names(Patterns) -> {Objs, Continuation} | '$end_of_table'
%%% @doc Returns `{Name,Pid}' objects one at a time based on 
%%% a given selection of all registered unique names.
%%% <p>`Patterns' can be written as follows:</p>
%%% <pre>
%%%   * Patterns = [MatchFunction] | '_'
%%%   * MatchFunction = {MatchHead, [Guard], [Result]}
%%%   * MatchHead = "Pattern as in ets:match"
%%%   * Guard = {"Guardtest name", ...}
%%%   * Result = true (if object is to be included)
%%% </pre>
%%% <p>or generated using {@link select_pattern/1} or 
%%% {@link select_pattern/2}.</p>
%%% <p>The function is <i>almost</i> equivalent to
%%% `ets:select(Tab,Patterns, 100)' on an `ordered_set' table `Tab'
%%%  with the following representation:</p>
%%% <pre>{{Key, Value}, Pid}</pre>
%%% <p>The difference compared to `ets:select/3' is the `Result' expression.
%%% In this function, `Result' is expected to be &apos;true&apos; for those
%%% objects that are to be included.</p>
%%% <p>For convenience, the special pattern '_' is accepted, 
%%% and expanded to <code>[{'_', [], [true]}]</code>.</p>
%%% <p>Any other patterns are ignored.</p>
%%% <p>The `Continuation' returned can be used when calling {@link select/1}
%%% in order to continue processing.</p>
%%% @end
%%%
select_names(Patterns) ->    
    select_names(Patterns, 100).

%%% @spec select_names(Patterns, Limit) -> 
%%%    {Objs, Continuation} | '$end_of_table'
%%% @doc Like {@link select_names/1}, but works in batches of `Limit'
%%% objects at a time, in order to improve raw performance.
%%%
select_names(Patterns0, Limit) ->
    Patterns = if Patterns0 == '_' ->
		       [{'_',[],[true]}];
		   true ->
		       Patterns0
	       end,
    Patterns1 =
	[{P,[{is_pid,{element,2,'$_'}}|G],
	  [{{{element,1,'$_'},
	     {element,2,'$_'}}}]} ||
	    {P,G,[true]} <- Patterns],
    ets:select(?REG, Patterns1, Limit).


%%% @spec fold_names(Fun::function(), Acc, Patterns) -> NewAcc
%%% @doc Similar to {@link lists:foldl/3}, but based on a list of select
%%% patterns, identifying a sub-set of unique names.
%%% <p>For each matching name, a call is made to 
%%% <pre>Fun({Name1,Pid1}, Acc1)</pre>, which is expected to 
%%% return `NewAcc'.</p>
%%% <p>`Patterns' is a list of match specifications on the same form as 
%%% that used by {@link ets:select_delete/2} and {@link ets:select_count/2},
%%% that is:</p>
%%% <pre>
%%%   * Patterns = [MatchFunction] | '_'
%%%   * MatchFunction = {MatchHead, [Guard], [Result]}
%%%   * MatchHead = "Pattern as in ets:match"
%%%   * Guard = {"Guardtest name", ...}
%%%   * Result = true (if object is to be included)
%%% </pre>
%%% <p>The following expression would return a 
%%% list of all registered unique names together with the processes
%%% that registered them:</p>
%%% <pre>
%%% proc:fold_names(fun(X,Acc) -> [X|Acc] end, [], [{'_',[],[true]}])
%%% </pre>
%%% <p>For convenience, the special pattern '_' is also allowed. It is
%%% expanded to <code>[{'_',[],[true]}]</code>.</p>
%%% <p>This function is equivalent to `fold_names(Fun,Acc,Patterns,100)',
%%% (see {@link fold_names/5}.)</p>
%%% @end
%%%
fold_names(Fun, Acc, Patterns) ->
    fold_names(Fun, Acc, Patterns, 100).


%%% @spec fold_names(Fun, Acc, Patterns, Limit::integer()) -> NewAcc
%%% @doc Like {@link fold_names/3}, but works in batches of `Limit' objects
%%% at a time, in order to improve memory characteristics.
%%% <p>`fold_names/3' uses a default limit of 1 object at a time.</p>
%%% <p>This function is equivalent to 
%%% `fold_names(Fun,Acc,Patterns,Limit,fun(_) -> true end)',
%%% (see {@link fold_names/5}.)</p>
%%% @end
%%%
fold_names(Fun, Acc, Patterns, Limit) ->
    fold_names(Fun, Acc, Patterns, Limit, fun(_) -> true end).


%%% @spec fold_names(Fun::function(), Acc, Patterns,
%%%     Limit::integer(), Regulator::function()) -> 
%%% NewAcc
%%% @doc Like {@link fold_names/4}, but applies a "Regulator function"
%%% after each batch of objects before proceeding with the next one.
%%% <p>The `Regulator' function is expected to take the current accumulator
%%% as an argument and return either `true' (in which case processing 
%%% continues), or `false' (in which case the `fold_names/5' function breaks
%%% and returns the current accumulator.</p>
%%% @end
%%%
fold_names(Fun, Acc, Patterns, Limit, Regulator) ->
    Res = select_names(Patterns, Limit),
    select_each_name(Res, Fun, Acc, Regulator).
    
select_each_name({Objs, Continuation}, Fun, Acc, Regulator) ->
    NewAcc = lists:foldl(
	       fun(Key, Acc1) ->
		       Fun(Key, Acc1)
	       end, Acc, Objs),
    case Regulator(NewAcc) of
	true ->
	    select_each_name(ets:select(Continuation), Fun, NewAcc, Regulator);
	false ->
	    NewAcc
    end;
select_each_name('$end_of_table', _, Acc, _Regulator) ->
    Acc.


%%% @spec select(Continuation) -> {[Obj], NewContinuation} | '$end_of_table'
%%% @doc Analogous to {@link ets:select/1}.
%%% <p>This function is intended to be called with a `Continuation' 
%%% returned from {@link select_names/1} or {@link select_properties/2}.</p>
%%% @end
%%%
select(Continuation) ->
    ets:select(Continuation).


%%% @spec first(Type:: names | properties) -> Key | '$end_of_table'
%%% @doc Analogous to {@link ets:first/1}.
%%% <p>The tables corresponding to `names' and `properties' both have 
%%% `ordered_set' semantics.</p>
%%% @end
first(properties) ->
    ets:first(?PROPS);
first(names) ->
    ets:first(?REG).

%%% @spec next(Type:: names | properties, Key) -> Key | '$end_of_table'
%%% @doc Analogous to {@link ets:next/2}.
%%% <p>The tables corresponding to `names' and `properties' both have 
%%% `ordered_set' semantics. The key format of the `properties' table is
%%% `{Property, Pid}', while the key format of the `names' table is
%%% simply the name. Note that `names' and `properties' are not likely
%%% the physical names of the actual tables.</p>
%%% @end
next(properties, Prev) ->
    ets:next(?PROPS, Prev);
next(names, Prev) ->
    ets:next(?REG, Prev).

%%% @spec previous(Type:: names | properties, Key) -> Key | '$end_of_table'
%%% @doc Analogous to {@link ets:previous/2}.
%%% <p>The tables corresponding to `names' and `properties' both have 
%%% `ordered_set' semantics. The key format of the `properties' table is
%%% `{Property, Pid}', while the key format of the `names' table is
%%% simply the name. Note that `names' and `properties' are not likely
%%% the physical names of the actual tables.</p>
%%% @end
previous(properties, {_Prop, Pid} = Next) when is_pid(Pid) ->
    ets:prev(?PROPS, Next);
previous(names, Next) ->
    ets:prev(?REG, Next).


%%% @spec last(Type:: names | properties) -> Key | '$end_of_table'
%%% @doc Analogous to {@link ets:last/1}.
%%% <p>The tables corresponding to `names' and `properties' both have 
%%% `ordered_set' semantics.</p>
%%% @end
last(properties) ->
    ets:last(?PROPS);
last(names) ->
    ets:last(?REG).
     
    
    


%%% --



%%% @spec properties_by_pid(pid()) -> [Property]
%%% @doc Lists all properties for a given process.
%%%
properties_by_pid(P) when is_pid(P) ->
    properties_by_pid(P, '_', []).

%%% @spec properties_by_pid(P::pid(), KeyPat) ->
%%%   [Property]
%%% @doc Lists all properties for a given process that match the pattern
%%%  KeyPat.
%%% <p>Equivalend to `properties_by_pid(P, KeyPat, [])'.</p>
%%% @end
%%%
properties_by_pid(P, KeyPat) ->
    properties_by_pid(P, KeyPat, []).

%%% @spec properties_by_pid(P::pid(), KeyPat, Guards) -> [Property]
%%% @doc Like {@link properties_by_pid/2}, but with an added list of guards.
%%% <p>The Guards list may be one returned from the function {@link guards/1}.
%%% </p>
%%% @end
%%%
properties_by_pid(P, KeyPat, Guards) ->
    ets:select(?PROPS_REV, [{{{P,KeyPat},'_'}, Guards,
			    [{element,2,{element,1,'$_'}}]}]).



%%% @spec info(Process) -> [{Tag, Info}] | undefined
%%% @doc Like `process_info(PidOfProcess)', but with added
%%%      proc-related attributes.
%%% <p>This function calls `process_info(PidOfProcess)', and adds to the 
%%% result the following info items:</p>
%%% <ul>
%%%   <li>`{pid, pid()}' -- the pid of the process</li>
%%%   <li>`{names, Names}' -- all unique proc names
%%%                            registered by the process</li>
%%%   <li>`{properties, Props}' -- all non-unique properties</li>
%%% </ul>
%%% @end
%%%
info(NameOrPid) ->
    info(NameOrPid, all).



%%% @spec info(Process, Attr) -> {Attr, Info}
%%% @doc Like `process_info(PidOfProcess, Attr)' but accepts some 
%%%      additional attributes.
%%% <p>All attributes supported by {@link erlang:process_info/2} are 
%%% supported by this function. In addition, the following attributes are 
%%% handled:</p>
%%% <ul>
%%%   <li>`pid' -- the pid of the process (mainly here for symmetry)</li>
%%%   <li>`names' -- all unique names registered in proc by `Process'</li>
%%%   <li>`properties' -- all non-unique properties of `Process'</li>
%%% </ul>
%%% @end
info(NameOrPid, Attr) ->
    case NameOrPid of
        P when is_pid(P) -> pid_info(P, Attr);
        N -> case where(N) of
                 undefined ->
                     undefined;
                 P ->
                     pid_info(P, Attr)
             end
    end.

pid_info(P, all) when is_pid(P) ->
    Names = case pid_info(P, names) of
		undefined -> [];
		{names, Ns} -> Ns
	    end,
    Keys = properties_by_pid(P),
    [{pid, P},
     {names, Names},
     {properties, Keys} | process_info(P)];
pid_info(P, pid) when is_pid(P) ->
    {pid, P};
pid_info(P, names) when is_pid(P) ->
    {names,
     ets:select(?REG_REV, [{{{P,'$1'},'_'}, [], ['$1']}])};
pid_info(P, properties) when is_pid(P) ->
    {properties, properties_by_pid(P)};
pid_info(P, Attr) when is_pid(P), is_atom(Attr) ->
    process_info(P, Attr).




%%% @spec i(Option) -> ok
%%%   Option = help | [{Op, Type, Patterns}]
%%%       Op = i | x
%%%     Type = names | properties
%%% @doc Like {@link c:i/0}, but allows for filtering output with 
%%% `proc' names and properties.
%%% <p>Usage: `i([{i | x, names | properties, Patterns}])'.<br/>
%%% calls `c:i(Processes)' for a subset of all running processes.
%%% `{i, Type, Pattern}' specifies by name or property which processes
%%% to include. `{x, Type, Pattern}' specifies which processes to
%%% exclude from the listing. If no `{i,T,P}' tuples are specified,
%%% all processes are included per default, then reduced by the
%%% `{x,T,P}' patterns. Multiple tuples are allowed.
%%% `Pattern' is a match specification where the result head is `true'.</p>
%%% <p>Note that currently, using {@link ets:fun2ms/1} may have severe impact
%%% on performance if there is a large number of registered names or
%%% keys.</p>
%%% @end
%%%
i(help) ->	    
    io:format(
      "~s~n",
      ["------------------------------------------------------------------\n"
       "Usage: i([{i | x, names | properties, Patterns}]).\n\n"
       "calls c:i(Processes) for a subset of all running processes.\n"
       "{i, Type, Pattern} specifies by name or property which processes\n"
       "to include. {x, Type, Pattern} specifies which processes to\n"
       "exclude from the listing. If no {i,T,P} tuples are specified,\n"
       "all processes are included per default, then reduced by the\n"
       "{x,T,P} patterns. Multiple tuples are allowed.\n\n"
       "Pattern is a match specification where the result head is true.\n"
       "Note that currently, using ets:fun2ms/1 may have severe impact\n"
       "on performance if there is a large number of registered names or\n"
       "keys.\n\n"
       "(Note: 'name' uniquely identifies one process; 'property' is non-unique.)\n"
       "-------------------------------------------------------------------\n"]);
i(Options) when is_list(Options) ->
    Temp = ets:new(temp, [ordered_set]),
    case lists:keymember(i,1,Options) of
	true ->
	    i_names(Options, Temp),
	    i_props(Options, Temp);
	false ->
	    lists:foreach(
	      fun(Pid) -> 
		      ets:insert(Temp, {Pid, 1})
	      end, processes())
    end,
    x_names(Options, Temp),
    x_props(Options, Temp),
    Pids = ets:select(Temp, ets:fun2ms(fun({P,_}) ->
					       P
				       end)),
    ets:delete(Temp),
    c:i(Pids).
    

i_names(Opts, Temp) ->
    Pats = 
	case lists:member({i,names,'_'}, Opts) of
	    true ->
		[{'_',[],[true]}];
	    false ->
		lists:concat([P || {i, names, P} <- Opts])
	end,
    case Pats of
	[] ->
	    true;
	_ ->
	    fold_names(
	      fun({_Name, Pid}, _) ->
		      ets:insert(Temp, {Pid,1})
	      end, [], Pats)
    end.


i_props(Opts, Temp) ->
    Pats = 
	case lists:member({i,properties,'_'}, Opts) of
	    true ->
		[{'_',[],[true]}];
	    false ->
		lists:concat([P || {i, properties, P} <- Opts])
	end,
    case Pats of
	[] ->
	    true;
	_ ->
	    select_fold_properties(
	      fun({_Prop, Pid}, _) ->
		      ets:insert(Temp, {Pid, 1})
	      end, [], Pats)
    end.
    
    
x_names(Opts, Temp) ->
    case lists:concat([P || {x, names, P} <- Opts]) of
	[] ->
	    true;
	Pats ->
	    fold_names(
	      fun({_Name, Pid}, _) ->
		      ets:delete(Temp, Pid)
	      end, [], Pats)
    end.

x_props(Opts, Temp) ->
    case lists:concat([P || {x, properties, P} <- Opts]) of
	[] ->
	    true;
	Pats ->
	    select_fold_properties(
	      fun({_Prop, Pid}, _) ->
		      ets:delete(Temp, Pid)
	      end, [], Pats)
    end.



%%% Gen_server callbacks

%%% @spec start_link() -> {ok, pid()}
%%% @doc Starts the proc server.
%%% <p>`proc' assumes that the `proc_tabs' module is available, and that
%%% the `proc_tabs' process is running.</p>
%%% @end
%%%
start_link() ->
    gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).


%%% @hidden
init([]) ->
    ensure_tabs_created(),
    set_monitors(),
    {ok, []}.

%%% @hidden
handle_call(Req, From, S) ->
    try handle_call1(Req, From, S)
    catch
	throw:Return ->
	    {reply, Return, S}
    end.


%%% @hidden
handle_cast(Msg, S) ->
    {stop, {unknown_cast, Msg}, S}.

%%% @hidden
handle_info({'DOWN', _MRef, process, Pid, _}, S) ->
    process_is_down(Pid),
    {noreply, S};
handle_info(_, S) ->
    {noreply, S}.


process_is_down(Pid) ->
    Pattern = {{Pid, '_'}, '_'},
    Regs = ets:match_object(?REG_REV, Pattern),
    lists:foreach(
      fun({{_, I}, 1}) ->
	      ets:delete(?REG, I);
	 ({{_, I}, w}) ->
	      [{_Name, {awaiting_reg, Ps}}] = ets:lookup(?REG, I),
	      case lists:keydelete(Pid, 1, Ps) of
		  [] ->
		      ets:delete(?REG, I);
		  [_|_] = PsLeft ->
		      ets:insert(?REG, {I, {awaiting_reg, PsLeft}})
	      end
      end, Regs),
    ets:match_delete(?REG_REV, Pattern),
    PPattern = {{Pid,'_'},'_'},
    Properties = ets:match_object(?PROPS_REV, PPattern),
    lists:foreach(
      fun({{_, Prop}, _}) ->
	      ets:delete(?PROPS, {Prop,Pid})
      end, Properties),
    ets:match_delete(?PROPS_REV, PPattern),
    ets:match_delete(?PRIVS, {{Pid,'_','_'}}),
    ets:delete(?MONITORS, Pid).


%%% @hidden
terminate(_Reason, _S) ->
    ok.

%%% @hidden
code_change(_FromVsn, State, _Extra) ->
    {ok, State}.


%%% broken out from handle_call/3
handle_call1({reg, Name}, {Pid,_} = From, S) ->
    CompleteReg = fun() ->
			  set_monitor(Pid),
			  ets:insert(?REG_REV, {{Pid, Name}, 1})
		  end,
    case ets:insert_new(?REG, {Name, Pid}) of
	false ->
	    case ets:lookup(?REG, Name) of
		[{_, {awaiting_reg, Pids}}] ->
		    ets:insert(?REG, {Name, Pid}),
		    CompleteReg(),
		    gen:reply(From, true),
		    lists:foreach(
		      fun({WaitingPid, Ref}) ->
			      clear_monitor(WaitingPid),
			      ets:delete(?REG_REV, {WaitingPid,Name}),
			      WaitingPid ! {?MODULE, Ref, reg, Name, Pid}
		      end, Pids),
		    {noreply, S};
		[{_,OtherPid}] ->
		    case is_process_alive(OtherPid) of
			true ->
			    ?BADARG;
			false ->
			    process_is_down(OtherPid),
			    ets:insert(?REG, {Name, Pid}),
			    CompleteReg(),
			    {reply, true, S}
		    end;
		[] ->
		    io:format("what the f...?~n", []),
		    ?BADARG
	    end;
	true ->
	    CompleteReg(),
	    {reply, true, S}
    end;
handle_call1({unreg, Name}, {Pid,_}, S) ->
    case ets:lookup(?REG, Name) of
	[{_, Pid}] ->
	    clear_monitor(Pid),
	    ets:delete(?REG_REV, {Pid, Name}),
	    ets:delete(?REG, Name),
	    {reply, true, S};
	_ ->
	    ?BADARG
    end;
handle_call1({await_reg, Name}, {Pid,_}, S) ->
    case ets:lookup(?REG, Name) of
	[] ->
	    set_monitor(Pid),
	    Ref = make_ref(),
	    ets:insert(?REG, {Name, {awaiting_reg, [{Pid, Ref}]}}),
	    ets:insert(?REG_REV, {{Pid, Name}, w}),
	    {reply, {Ref,Name}, S};
	[{_, RegPid}] when is_pid(RegPid) ->
	    {reply, {already_registered, RegPid}, S};
	[{_, {awaiting_reg, OtherPids}}] ->
	    set_monitor(Pid),
	    Ref = make_ref(),
	    ets:insert(?REG, {Name, {awaiting_reg, [{Pid, Ref}|OtherPids]}}),
	    ets:insert(?REG_REV, {{Pid, Name}, w}),
	    {reply, {Ref,Name}, S}
    end;
handle_call1({clear_await_reg, {Ref,Name}}, _From, S) when is_reference(Ref) ->
    case ets:lookup(?REG, Name) of
	[{Name, Pid}] when is_pid(Pid) ->
	    ignore;
	[{Name, {awaiting_reg, Ps}}] ->
	    case lists:keysearch(Ref, 2, Ps) of
		{value, {WPid, _}} when is_pid(WPid) ->
		    clear_monitor(WPid),
		    ets:delete(?REG_REV, {WPid, Name}),
		    case lists:keydelete(Ref, 2, Ps) of
			[] ->
			    ets:delete(?REG, Name);
			[_|_] = PsLeft ->
			    ets:insert(?REG, {Name, {awaiting_reg, PsLeft}})
		    end;
		false ->
		    ignore
	    end
    end,
    %% due to possible race conditions, we reply true as long as it's 
    %% reasonable that the reference given was valid once. It would be 
    %% unreasonable to require the client to verify that Name is not 
    %% registered just before the registry server handles this request.
    %% Therefore, whether the request would exit or return true could be 
    %% timing dependent, which would suck greatly. Therefore, don't worry,
    %% be happy, and return 'true' each time. If the argument is not 
    %% on the form {reference(), Name}, there _will_ be a BADARG.
    {reply, true, S};
handle_call1({add_property, Pid, Property}, {Pid,_}, S) ->
    do_add_property(Pid, Property),
    {reply, true, S};
handle_call1({add_property, Process, Property}, {Pid, _}, S) ->
    OnPid = on_pid(Process),
    verify_access(OnPid, add_property, Pid),
    do_add_property(OnPid, Property),
    {reply, true, S};
handle_call1({replace_property, Pid, OldProp, NewProp}, {Pid,_}, S) ->
    do_replace_property(Pid, OldProp, NewProp),
    {reply, true, S};
handle_call1({replace_property, Process, OldProp, NewProp}, {Pid,_}, S) ->
    OnPid = on_pid(Process),
    verify_access(OnPid, replace_property, Pid),
    do_replace_property(OnPid, OldProp, NewProp),
    {reply, true, S};
handle_call1({del_property, Pid, Property}, {Pid,_}, S) ->
    do_del_property(Pid, Property),
    {reply, true, S};
handle_call1({del_property, Process, Property}, {Pid, _}, S) ->
    OnPid = on_pid(Process),
    verify_access(OnPid, del_property, Pid),
    do_del_property(OnPid, Property),
    {reply, true, S};
handle_call1({add_counter, Pid, Name, Initial}, {Pid,_}, S) ->
    do_add_counter(Pid, Name, Initial),
    {reply, true, S};
handle_call1({add_counter, Process, Name, Initial}, {Pid,_}, S) ->
    OnPid = on_pid(Process),
    verify_access(OnPid, add_counter, Pid),
    do_add_counter(OnPid, Name, Initial),
    {reply, true, S};
handle_call1({del_counter, Pid, Name}, {Pid,_}, S) ->
    do_del_counter(Pid, Name),
    {reply, true, S};
handle_call1({del_counter, Process, Name}, {Pid,_}, S) ->
    OnPid = on_pid(Process),
    verify_access(OnPid, del_counter, Pid),
    do_del_counter(Process, Name),
    {reply, true, S};
handle_call1({set_access, Access}, {Pid,_}, S) ->
    Objs = 
	lists:foldr(
	  fun({Action, Procs, Ops}, Acc) ->
		  Pids = [on_pid(P) || P <- Procs],
		  Ops1 = expand_ops(Ops),
		  [[{Action, {Pid, Op, P}} || P <- Pids,
					      Op <- Ops1] | Acc]
	  end, [], Access),
    lists:foreach(
      fun({grant, Key}) ->
	      ets:insert(?PRIVS, {Key});
	 ({revoke, Key}) ->
	      ets:delete(?PRIVS, Key)
      end, lists:concat(Objs)),
    {reply, true, S};
	      
handle_call1(_Req, _From, _S) ->
    ?BADARG.


expand_ops(Ops) ->
    lists:foldl(
      fun(properties, Acc) ->
	      [add_property, del_property, replace_property] ++ Acc;
	 (counters, Acc) ->
	      [add_counter, del_counter, update_counter] ++ Acc;
	 (Op, Acc) when Op == add_property;
			Op == replace_property;
			Op == del_property;
			Op == add_counter;
			Op == del_counter;
			Op == update_counter ->
	      [Op|Acc]
      end, [], Ops).



verify_access(OnPid, Op, Pid) ->
    case ets:member(?PRIVS, {OnPid, Op, Pid}) of
	true ->
	    true;
	false ->
	    ?EACCESS
    end.



do_add_property(Pid, Property) ->
    case ets:member(?PROPS_REV, {Pid,Property}) of
	true ->
	    ?BADARG;
	false ->
	    set_monitor(Pid),
	    insert_property(Property, Pid)
    end.

do_replace_property(Pid, OldProp, NewProp) ->
    case ets:lookup(?PROPS_REV, {Pid, OldProp}) of
	[{_, property}] ->
	    delete_property(OldProp, Pid),
	    insert_property(NewProp, Pid);
	_ ->
	    ?BADARG
    end.

do_del_property(Pid, Property) ->
    case ets:lookup(?PROPS_REV, {Pid,Property}) of
	[{_, property}] ->
	    clear_monitor(Pid),
	    delete_property(Property, Pid);
	_ ->
	    ?BADARG
    end.


do_add_counter(Pid, Name, Initial) ->
    case ets:member(?PROPS_REV, {Pid, Name}) of
	true ->
	    ?BADARG;
	false ->
	    set_monitor(Pid),
	    insert_counter(Name, Initial, Pid)
    end.


do_del_counter(Pid, Counter) ->
    case ets:lookup(?PROPS_REV, {Pid,Counter}) of
	[{_, counter}] ->
	    clear_monitor(Pid),
	    delete_counter(Counter, Pid);
	Other ->
	    io:format(user, "ets:lookup(~p, ~p) -> ~p~n",
		      [?PROPS_REV, {Pid,Counter}, Other]),
	    ?BADARG
    end.

    
on_pid(Process) ->
    if is_pid(Process) -> Process;
       true -> 
	    case ets:lookup(?REG, Process) of
		[{_, P}] ->
		    P;
		_ ->
		    ?BADARG
	    end
    end.



%%% Helper functions

call(Req) ->
    case gen_server:call(?MODULE, Req) of
	badarg ->
	    erlang:error(badarg);
	{badarg, Error} ->
	    erlang:error(Error);
	Reply ->
	    Reply
    end.

ensure_tabs_created() ->
    lists:foreach(
      fun({T, Opts}) ->
	      case ets:info(T, size) of
		  undefined ->
		      proc_tabs:ets_new(T, Opts);
		  N when is_integer(N) ->
		      ok
	      end
      end, tabs()).

tabs() ->
    T = [public, named_table],
    [{?REG, [ordered_set|T]},
     {?REG_REV, [ordered_set|T]},
     {?PROPS, [ordered_set|T]},
     {?PROPS_REV, [ordered_set|T]},
     {?MONITORS, [set|T]},
     {?PRIVS, [ordered_set|T]}].

set_monitors() -> 
    ets:delete_all_objects(?REG_REV),
    ets:delete_all_objects(?PROPS_REV),
    ets:delete_all_objects(?MONITORS),
    ets:foldl(fun({Id, Pid}, Acc) -> 
		      set_monitor(Pid),
		      ets:insert(?REG_REV, {{Pid, Id}}),
                      Acc
              end, ok, ?REG),
    ets:foldl(fun({{Property,Pid}, _}, Acc) ->
		      set_monitor(Pid),
		      ets:insert(?PROPS_REV, {{Pid,Property},1}),
		      Acc
	      end, ok, ?PROPS).


			      
			  
set_monitor(Pid) ->
    case ets:member(?MONITORS, Pid) of
	false ->
	    MRef = erlang:monitor(process, Pid),
	    ets:insert(?MONITORS, {Pid, MRef, 1});
	true ->
	    ets:update_counter(?MONITORS, Pid, {3,1})
    end.

clear_monitor(Pid) ->
    case ets:update_counter(?MONITORS, Pid, {3,-1}) of
	0 ->
	    MRef = ets:lookup_element(?MONITORS, Pid, 2),
	    erlang:demonitor(MRef),
	    ets:delete(?MONITORS, Pid);
	_ ->
	    true
    end.


insert_property(Property, Pid) ->
    ets:insert(?PROPS, {{Property,Pid},property}),
    ets:insert(?PROPS_REV, {{Pid,Property},property}).

delete_property(Property, Pid) ->
    ets:delete(?PROPS, {Property,Pid}),
    ets:delete(?PROPS_REV, {Pid,Property}).

insert_counter(Name, Initial, Pid) ->
    ets:insert(?PROPS, {{Name, Pid}, Initial}),
    ets:insert(?PROPS_REV, {{Pid, Name}, counter}).

delete_counter(Name, Pid) ->
    delete_property(Name, Pid).



%%% dirty hack to get proc:i/1 to work:
%%% While waiting for a patch that makes c:i/1 exported,
%%% we force the export and recompile on the fly.
%%% Most of this code was copied from cover.erl.

enable_i() ->
    case lists:member({i,1},c:module_info(exports)) of
	true ->
	    already_enabled;
	false ->
	    Beam = code:which(c),
	    ok = code:unstick_dir(filename:dirname(Beam)),
	    case get_abstract_code(c, Beam) of
		no_abstract_code=E ->
		    {error, E};
		encrypted_abstract_code=E ->
		    {error, E};
		{Vsn,Code} ->
		    Forms = transform_c(Vsn, Code),
		    {ok, Module, Binary} = compile:forms(Forms, []),
		    case code:load_binary(Module, Beam, Binary) of
			{module, Module} ->
			    {ok, Module};
			_Error ->
			    error
		    end
	    end
    end.

get_abstract_code(Module, Beam) ->
    case beam_lib:chunks(Beam, [abstract_code]) of
	{ok, {Module, [{abstract_code, AbstractCode}]}} ->
	    AbstractCode;
	{error,beam_lib,{key_missing_or_invalid,_,_}} ->
	    encrypted_abstract_code;
	Error -> Error
    end.

transform_c(Vsn, Code) 
  when Vsn==abstract_v1; Vsn==raw_abstract_v1; Vsn==abstract_v2 ->
    Code1 = lists:filter(
	      fun({function,_,module_info,_,_}) ->
		      false;
		 (_) -> true
	      end, Code),
    lists:map(
      fun({attribute,L,export,Es} = Form) ->
	      case lists:member({i,0},Es) of
		  true ->
		      {attribute,L,export,[{i,1}|Es]};
		  false ->
		      Form
	      end;
	 (Form) ->
	      Form
      end, Code1).
Back to Top