Files @ d0a14f973771
Branch filter:

Location: vmkdrivers/BLD/build/HEADERS/vmkapi-current-all-public/vmkernel64/release/base/vmkapi_lock.h

unknown
ESXi-5.0-U1
   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
/* **********************************************************
 * Copyright 2007 - 2009 VMware, Inc.  All rights reserved.
 * **********************************************************/

/*
 * @VMKAPIMOD_LICENSE@
 */

/*
 ***********************************************************************
 * Spinlocks                                                      */ /**
 * \defgroup Spinlocks Spin Locks
 *
 * \par Lock and Timer Ranks:
 * All locks are associated with a numeric major rank \em R and a
 * numeric  minor rank \em r. While holding lock \em L1 (ranks: \em R1,
 * \em r1) a lock \em L2 (ranks: \em R2, \em r2) can be acquired if one
 * of the following hold true:\n
 * - \em R2 > \em R1 \n
 * - \em R2 == \em R1, \em L2 and \em  L1 belong to the same class,
 *    and \em r2 > \em  r1.\n
 * \n
 * The only exception to the above rule is for "unranked locks", which
 * are applicable only for timers.  An unranked timer does not participate
 * in the ranking rules.  Standalone unranked spinlocks are not supported.\n
 * \n
 * Two types of spinlocks, with respect to lock ranks are supported:\n
 * \n
 * \li <b>Simple Ranked Spinlock:</b> \n
 *    A stand-alone spinlock which has an explicit major rank other 
 *    than VMK_SP_RANK_UNRANKED. The minor rank (which is implicitly set) 
 *    is always equal to '0'.
 * \li <b>Class Based Ranked Spinlock:</b>\n
 *    Belongs to a class of spinlocks all of which share the same 
 *    explicit major rank other than VMK_SP_RANK_UNRANKED. Members of 
 *    the same class differ only with respect to their minor rank, which is 
 *    explicitly set to a desired value, other than VMK_SP_RANK_UNRANKED, 
 *    during lock initialization.
 * 
 * \par Timers and Ranks:
 * Timers also have ranks within the lock ranking system.  While a
 * timer callback is firing, the callback is treated as holding a lock
 * of the timer's rank, so it is not allowed to acquire locks of lower
 * rank.  A call to vmk_TimerRemoveSync is treated as acquiring (and
 * releasing) a lock of the timer's rank, and thus is not allowed when
 * holding locks of higher or equal rank.\n
 * \n
 * Timers can only be unranked or simple, not class-based.  It is
 * common to give a timer the rank VMK_SP_RANK_LOWEST_MAJOR_LEGACY; this 
 * allows the callback to acquire any higher-ranked lock, but requires
 * that code calling vmk_TimerRemoveSync on the timer hold no locks.
 *
 * @{
 ***********************************************************************
 */

#ifndef _VMKAPI_SPLOCK_H_
#define _VMKAPI_SPLOCK_H_

/** \cond never */
#ifndef VMK_HEADER_INCLUDED_FROM_VMKAPI_H
#error This vmkapi file should never be included directly but only via vmkapi.h
#endif
/** \endcond never */

/**
 * \brief Recursive flag
 *
 * Recursive flag indicates that an instace of lock \em L can be held while
 * acquiring another instance of lock \em L. It is assumed the caller knows
 * what they're doing.
 */
#define VMK_SP_RANK_RECURSIVE_FLAG_LEGACY  _VMK_SP_RANK_RECURSIVE_FLAG_LEGACY

/**
 * \brief Mask for major rank value.
 */
#define VMK_SP_RANK_NUMERIC_MASK_MAJOR_LEGACY  _VMK_SP_RANK_NUMERIC_MASK_MAJOR_LEGACY

/**
 * \brief Mask for minor rank value.
 */
#define VMK_SP_RANK_NUMERIC_MASK_MINOR_LEGACY _VMK_SP_RANK_NUMERIC_MASK_MINOR_LEGACY

/**
 * \brief Lowest major rank for locks
 */
#define VMK_SP_RANK_LOWEST_MAJOR_LEGACY  _VMK_SP_RANK_LOWEST_MAJOR_LEGACY

/**
 * \brief highest major rank for non-IRQ locks
 */
#define VMK_SP_RANK_HIGHEST_MAJOR_LEGACY  _VMK_SP_RANK_HIGHEST_MAJOR_LEGACY

/**
 * \brief Maximum minor rank for locks.
 */
#define VMK_SP_RANK_MAX_MINOR_LEGACY (_VMK_SP_RANK_NUMERIC_MASK_MINOR_LEGACY)

/**
 * \brief Minimum minor rank for locks.
 */
#define VMK_SP_RANK_MIN_MINOR_LEGACY (0)

/**
 * \brief Maximum major rank for locks
 */
#define VMK_SP_RANK_MAX_MAJOR_LEGACY  (VMK_SP_RANK_NUMERIC_MASK_MAJOR_LEGACY - 1)

/**
 * \brief Minimum major rank for locks
 */
#define VMK_SP_RANK_MIN_MAJOR_LEGACY  (0)

/**
 * \brief Leaf rank for IRQ locks
 *
 * To be used for IRQ locks that leafs, except for log/warning
 *
 */
#define VMK_SP_RANK_IRQ_LEAF_LEGACY  (0x3ff9)

/**
 * \brief Rank used by calls potentially invoked by timers that need
 *        to manage memory. For instance: an allocator callable by
 *        timers.
 */
#define VMK_SP_RANK_IRQ_MEMTIMER_LEGACY (0x3000)

/**
 * \brief Block rank for IRQ locks
 *
 * To be used for IRQ locks that depend on eventqueue/cpusched locks
 */
#define VMK_SP_RANK_IRQ_BLOCK_LEGACY  (0x2000)

/**
 * \brief Lowest major rank for IRQ locks
 */
#define VMK_SP_RANK_IRQ_LOWEST_MAJOR_LEGACY  _VMK_SP_RANK_IRQ_LOWEST_MAJOR_LEGACY

/**
 * \brief Highest major rank for IRQ locks.
 */
#define VMK_SP_RANK_IRQ_HIGHEST_MAJOR_LEGACY _VMK_SP_RANK_IRQ_HIGHEST_MAJOR_LEGACY

/**
 * \brief Leaf rank for locks
 *
 * Leaf locks are ranked lower than spin locks protecting semaphores,
 * so that one can grab a semaphore, grab a leaf lock and then call
 * vmk_SemaIsLocked() on the semaphore.
 */
#define VMK_SP_RANK_LEAF_LEGACY  (0xffd)

/**
 * \brief Rank for all SCSI locks
 *
 */
#define VMK_SP_RANK_SCSI_LEGACY _VMK_SP_RANK_SCSI_LEGACY

/**
 * \brief Rank for all SCSI plugin locks
 *
 */
#define VMK_SP_RANK_SCSI_PLUGIN2 _VMK_SP_RANK_SCSI_PLUGIN2
#define VMK_SP_RANK_SCSI_PLUGIN1 _VMK_SP_RANK_SCSI_PLUGIN1

/**
 * \brief Lowest rank for all SCSI locks
 *
 */
#define VMK_SP_RANK_SCSI_LOWEST_LEGACY _VMK_SP_RANK_SCSI_LOWEST_LEGACY

/**
 * \brief Lowest rank for locks in FS Device Switch component
 *
 * Lowest possible rank for locks used by FS Device Switch. Modules
 * operating above the device switch, and calling into the device switch
 * should use locks ranked lower than this.
 */
#define VMK_SP_RANK_FDS_LOWEST_LEGACY  _VMK_SP_RANK_FDS_LOWEST_LEGACY

/**
 * \brief Lowest rank for locks in VMK FS drivers
 *
 * Lowest possible rank for locks used by VMK FS drivers. FSS and everyone
 * above should use locks ranked lower than this.
 */
#define VMK_SP_RANK_FSDRIVER_LOWEST_LEGACY  _VMK_SP_RANK_FSDRIVER_LOWEST_LEGACY

/**
 * \brief Rank for Network locks
 *
 * Usual lock rank used for module internal usage which doesn't imply
 * vmkapi call.
 */
#define VMK_SP_RANK_NETWORK_LEGACY _VMK_SP_RANK_NETWORK_LEGACY

/**
 * \brief Lowest Rank for Network locks
 *
 * Should be used if the module is intended to hold a lock while calling 
 * a vmkapi function.
 */
#define VMK_SP_RANK_NETWORK_LOWEST_LEGACY _VMK_SP_RANK_NETWORK_LOWEST_LEGACY

/**
 * \brief Highest Rank for Network locks
 *
 * Should be used if the module is intended to hold a lock while calling 
 * a vmkapi function.
 */
#define VMK_SP_RANK_NETWORK_HIGHEST_LEGACY _VMK_SP_RANK_NETWORK_HIGHEST_LEGACY

/**
 * \brief Tcpip stack highest rank.
 *
 */
#define VMK_SP_RANK_TCPIP_HIGHEST_LEGACY _VMK_SP_RANK_TCPIP_HIGHEST_LEGACY

/**
 * \brief Unranked timers.
 */
#define VMK_SP_RANK_UNRANKED_TIMER_LEGACY VMK_SPINLOCK_UNRANKED

/**
 * \brief Handle for lock classes
 */
typedef struct vmk_SpinlockClass_LEGACYInt *vmk_SpinlockClass_LEGACY;

/**
 * \brief Handle for non-IRQ locks
 */
typedef struct vmk_SpinlockInt *vmk_Spinlock;

/**
 * \brief Handle for IRQ locks
 */
typedef struct vmk_SpinlockIRQInt *vmk_SpinlockIRQ;

/**
 * \brief Handle for non-IRQ rw locks
 */
typedef struct vmk_SpinlockRWInt *vmk_SpinlockRW;

/**
 * \brief Rank for locks
 */
typedef vmk_uint32 vmk_SpinlockRank;


/*
 ***********************************************************************
 * vmk_SPClassCreate_LEGACY --                                    */ /**
 *
 * \ingroup Spinlocks
 * \brief Allocate and initialize a spinlock class
 *
 * Creates a new spinlock class and initializes it.
 *
 * The spinlock class's rank serves as the major rank for the
 * spinlocks that are members of the class.
 *
 * \note Requires that the module heap be initialized.
 *
 * \param[out] lockClass   Pointer to the new spinlock class.
 * \param[in]  moduleID    Module ID who manages the created
 *                         spinlock class.
 * \param[in]  name        Name of the spinlock class.
 * \param[in]  rank        Rank of the spinlock class.
 *                         The specified rank must be greater than or
 *                         equal to VMK_SP_RANK_LOWEST_MAJOR_LEGACY
 *                         and less than or equal to
 *                         VMK_SP_RANK_HIGHEST_MAJOR_LEGACY.
 *
 * \retval VMK_OK The spinlock  class was successfully initialized
 * \retval VMK_NO_MEMORY The spinlock class could not be allocated
 * \retval VMK_NO_MODULE_HEAP The module has no heap to allocate from
 * \retval VMK_BAD_PARAM Rank was invalid.
 *
 ***********************************************************************
 */
VMK_ReturnStatus vmk_SPClassCreate_LEGACY(
   vmk_SpinlockClass_LEGACY *lockClass,
   vmk_ModuleID moduleID,
   const char *name,
   vmk_SpinlockRank rank);

/*
 ***********************************************************************
 * vmk_SPClassDestroy_LEGACY --                                   */ /**
 *
 * \ingroup Spinlocks
 * \brief Destroy a spinlock class
 *
 * Revert all side effects of vmk_SPClassCreate_LEGACY
 *
 * \param[in] lockClass    Pointer to the spinlock class to be deleted.
 *
 ***********************************************************************
 */
void vmk_SPClassDestroy_LEGACY(vmk_SpinlockClass_LEGACY *lockClass);

/*
 ***********************************************************************
 * vmk_SPCreate_LEGACY --                                         */ /**
 *
 * \ingroup Spinlocks
 * \brief Allocate and initialize a non-IRQ spinlock
 *
 * Creates a new non-IRQ spinlock and initializes it.
 *
 * \note Requires that the module heap be initialized.
 *
 * \param[out] lock        Pointer to the new spinlock class.
 * \param[in]  moduleID    Module that owns the new spinlock.
 * \param[in]  name        Name of the spinlock,
 * \param[in]  lockClass   Pointer to a spinlock class, or NULL if the
 *                         new spinlock should be a simple (non-classed)
 *                         spinlock.
 * \param[in]  rank        The spinlock's rank.\n
 *                         If lockClass is NULL then this parameter
 *                         specifies the lock's major rank.
 *                         The specified rank must be greater than or
 *                         equal to VMK_SP_RANK_LOWEST_MAJOR_LEGACY and
 *                         less than or equal to 
 *                         VMK_SP_RANK_HIGHEST_MAJOR_LEGACY.\n
 *                         If lockClass is set to a valid spinlock
 *                         class then this parameter specifies the
 *                         new spinlock's minor rank with the lock's
 *                         major rank being inherited from the
 *                         spinlock class.
 *                         The specified rank must be greater than or
 *                         equal to VMK_SP_RANK_MIN and less than or
 *                         equal to VMK_SP_RANK_MAX.\n
 *
 * \retval VMK_OK The spinlock was successfully initialized
 * \retval VMK_NO_MEMORY The spinlock could not be allocated
 * \retval VMK_NO_MODULE_HEAP The module has no heap to allocate from
 * \retval VMK_BAD_PARAM Rank was invalid.
 *
 ***********************************************************************
 */
VMK_ReturnStatus vmk_SPCreate_LEGACY(
   vmk_Spinlock *lock,
   vmk_ModuleID moduleID,
   const char *name,
   vmk_SpinlockClass_LEGACY *lockClass,
   vmk_SpinlockRank rank);

/*
 ***********************************************************************
 * vmk_SPAssertHasState --
 *
 * This is used by vmk_SPAssertIsLocked() and vmk_SPAssertIsUnlocked().
 * VMKAPI clients should not call this function directly.
 *
 ***********************************************************************
 */

/** \cond nodoc */
void vmk_SPAssertHasState(
   vmk_Spinlock *lock,
   vmk_Bool locked);
/** \endcond */

/*
 ***********************************************************************
 * vmk_SPAssertIsLocked --                                        */ /**
 *
 * \ingroup Spinlocks
 * \brief Assert that a spinlock is currently locked.
 *
 * \note This call only performs the check on a debug build.
 *       Other builds always succeed.
 * \note This call will not block.
 *
 * \param[in] lock   Non-IRQ spinlock to check
 *
 ***********************************************************************
 */

static VMK_ALWAYS_INLINE void
vmk_SPAssertIsLocked(
   vmk_Spinlock *lock)
{
#ifdef VMX86_DEBUG
   vmk_SPAssertHasState(lock, VMK_TRUE);
#endif
}


/*
 ***********************************************************************
 * vmk_SPAssertNotUnlocked --                                     */ /**
 *
 * \ingroup Spinlocks
 * \brief Assert that a spinlock is currently unlocked.
 *
 * \note This call only performs the check on a debug build.
 *       Other builds always succeed.
 * \note This call will not block.
 *
 * \param[in] lock   Non-IRQ spinlock to check
 *
 ***********************************************************************
 */

static VMK_ALWAYS_INLINE void
vmk_SPAssertIsUnlocked(
   vmk_Spinlock *lock)
{
#ifdef VMX86_DEBUG
   vmk_SPAssertHasState(lock, VMK_FALSE);
#endif
}


/*
 ***********************************************************************
 * vmk_SPAssertNoLocksHeldInt --                                  */ /**
 *
 * This is used by vmk_SPAssertNoLocksHeld().  VMKAPI clients should
 * not call this function directly.
 *
 ***********************************************************************
 */

/** \cond nodoc */
void
vmk_SPAssertNoLocksHeldInt(
   void);
/** \endcond */


/*
 ***********************************************************************
 * vmk_SPAssertNoLocksHeld --                                     */ /**
 *
 * \ingroup Spinlocks
 * \brief Assert that no non-IRQ spinlocks are held by this CPU.
 *
 * \note This call only performs the check on a debug build.
 *       Other builds always succeed.
 * \note This call will not block.
 *
 ***********************************************************************
 */

static VMK_ALWAYS_INLINE void
vmk_SPAssertNoLocksHeld(
   void)
{
#ifdef VMX86_DEBUG
   vmk_SPAssertNoLocksHeldInt();
#endif
}


/*
 ***********************************************************************
 * vmk_SPAssertNoLocksHeldIRQInt --
 *
 * This is used by vmk_SPAssertNoLocksHeldIRQ.  VMKAPI clients should not
 * call this function directly.
 *
 ***********************************************************************
 */

/** \cond nodoc */
void
vmk_SPAssertNoLocksHeldIRQInt(
   void);
/** \endcond */

/*
 ***********************************************************************
 * vmk_SPAssertNoLocksHeldIRQ --                                  */ /**
 *
 * \ingroup Spinlocks
 * \brief Assert that no IRQ spinlocks are held by this CPU.
 *
 * \note This call only performs the check on a debug build.
 *       Other builds always succeed.
 * \note This call will not block.
 *
 ***********************************************************************
 */

static VMK_ALWAYS_INLINE void
vmk_SPAssertNoLocksHeldIRQ(
   void) 
{
#ifdef VMX86_DEBUG
   vmk_SPAssertNoLocksHeldIRQInt();
#endif
}


/*
 ***********************************************************************
 * vmk_SPLock --                                                  */ /**
 *
 * \ingroup Spinlocks
 * \brief Acquire the non-IRQ spinlock
 *
 * \pre The caller shall not already hold a spinlock of equal or
 *      higher rank.
 *
 * \param[in,out] lock  Non-IRQ spinlock to acquire.
 *
 * \return None.
 *
 ***********************************************************************
 */
void vmk_SPLock(
   vmk_Spinlock *lock);

/*
 ***********************************************************************
 * vmk_SPTryLock --                                               */ /**
 *
 * \ingroup Spinlocks
 * \brief Try acquire a non-IRQ spinlock
 *
 * This function tries to acquire the given spinlock:
 * - If the given spinlock has not been locked, this function locks it.
 * - If the given spinlock has been locked already, this function does
 *   nothing and returns immediately.
 *
 * \pre This caller shall not already hold a spinlock of lower or
 *      equal rank.
 *
 * \param[in,out] lock   Non-IRQ spinlock to attempt to acquire.
 *
 * \retval VMK_OK    The given spinlock is successfully locked by this
 *                   call.
 * \retval VMK_BUSY  The given spinlock has been locked already.
 *
 ***********************************************************************
 */
VMK_ReturnStatus vmk_SPTryLock(
   vmk_Spinlock *lock);

/*
 ***********************************************************************
 * vmk_SPUnlock --                                                */ /**
 *
 * \ingroup Spinlocks
 * \brief Release a non-IRQ spinlock
 *
 * \pre The caller currently owns the spinlock.
 *
 * \param[in,out] lock  Non-IRQ spinlock to release.
 *
 ***********************************************************************
 */
void vmk_SPUnlock(
   vmk_Spinlock *lock);

/*
 ***********************************************************************
 * vmk_SPDestroy --                                               */ /**
 *
 * \ingroup Spinlocks
 * \brief Destroy a non-IRQ spinlock
 *
 * Revert all side effects of vmk_SPCreate_LEGACY
 *
 * \param[in] lock   Non-IRQ spinlock to be destroyed.
 *
 ***********************************************************************
 */
void vmk_SPDestroy(vmk_Spinlock *lock);

/*
 ***********************************************************************
 * vmk_SPCreateIRQ_LEGACY --                                      */ /**
 *
 * \ingroup Spinlocks
 * \brief Allocate and initialize an IRQ spinlock
 *
 * Creates a new IRQ spinlock and initializes it.
 *
 * \note Requires that the module heap be initialized.
 *
 * \param[out] lock        Pointer to the new spinlock class.
 * \param[in]  moduleID    Module that owns the new spinlock.
 * \param[in]  name        Name of the spinlock,
 * \param[in]  lockClass   Pointer to a spinlock class, or NULL if the
 *                         new spinlock should be a simple (non-classed)
 *                         spinlock.
 * \param[in]  rank        The spinlock's rank.\n
 *                         If lockClass is NULL then this parameter
 *                         specifies the lock's major rank.
 *                         The specified rank must be greater than or
 *                         equal to VMK_SP_RANK_IRQ_LOWEST and less
 *                         than or equal to VMK_SP_RANK_IRQ_HIGHEST.\n
 *                         The specified rank must be greater than or
 *                         equal to VMK_SP_RANK_MIN_MAJOR_LEGACY and
 *                         less than or equal to 
 *                         VMK_SP_RANK_MAX_MAJOR_LEGACY.\n
 *                         If lockClass is set to a valid spinlock
 *                         class then this parameter specifies the
 *                         new spinlock's minor rank with the lock's
 *                         major rank being inherited from the
 *                         spinlock class.  Minor spin ranks must be
 *                         greater than or equal to
 *                         VMK_SP_RANK_MIN_MINOR_LEGACY and less than or
 *                         equal to VMK_SP_RANK_MAX_MINOR_LEGACY.
 *
 * \retval VMK_OK The spinlock was successfully initialized
 * \retval VMK_NO_MEMORY The spinlock could not be allocated
 * \retval VMK_NO_MODULE_HEAP The module has no heap to allocate from
 *
 ***********************************************************************
 */
VMK_ReturnStatus vmk_SPCreateIRQ_LEGACY(
   vmk_SpinlockIRQ *lock,
   vmk_ModuleID moduleID,
   const char *name,
   vmk_SpinlockClass_LEGACY *lockClass,
   vmk_SpinlockRank rank);

/*
 ***********************************************************************
 * vmk_SPAssertHasStateIRQ --
 *
 * This is use by vmk_SPAssertIsLockedIRQ and 
 * vmk_SPAssertIsUnlockedIRQ().  VMKAPI clients should not call this
 * function directly.
 *
 ***********************************************************************
 */

/** \cond nodoc */
void vmk_SPAssertHasStateIRQ(
   vmk_SpinlockIRQ *lock,
   vmk_Bool         locked);
/** \endcond */


/*
 ***********************************************************************
 * vmk_SPAssertIsLockedIRQ --                                     */ /**
 *
 * \ingroup Spinlocks
 * \brief Assert that an IRQ spinlock is currently locked.
 *
 * \note This call only performs the check on a debug build.
 * \note This call will not block.
 *
 * \param[in] lock   IRQ spinlock to check
 *
 ***********************************************************************
 */

static VMK_ALWAYS_INLINE void
vmk_SPAssertIsLockedIRQ(
   vmk_SpinlockIRQ *lock)
{
#ifdef VMX86_DEBUG
   vmk_SPAssertHasStateIRQ(lock, VMK_TRUE);
#endif
}


/*
 ***********************************************************************
 * vmk_SPAssertIsUnlockedIRQ --                                   */ /**
 *
 * \ingroup Spinlocks
 * \brief Assert that an IRQ spinlock is currently unlocked.
 *
 * \note This call only performs the check on a debug build.
 * \note This call will not block.
 *
 * \param[in] lock   IRQ spinlock to check
 *
 ***********************************************************************
 */

static VMK_ALWAYS_INLINE void
vmk_SPAssertIsUnlockedIRQ(
   vmk_SpinlockIRQ *lock)
{
#ifdef VMX86_DEBUG
   vmk_SPAssertHasStateIRQ(lock, VMK_FALSE);
#endif
}


/*
 ***********************************************************************
 * vmk_SPLockIRQ --                                               */ /**
 *
 * \ingroup Spinlocks
 * \brief Acquire the IRQ spinlock
 *
 * Acquires an IRQ spinlock and disable interrupts on the current CPU
 * if they are not already disabled.
 *
 * \pre The caller shall not already hold a spinlock of equal or
 *      higher rank.
 *
 * \param[in,out] lock  IRQ spinlock to acquire.
 *
 * \return Previous IRQ state.
 * \retval 0 interrupts were enabled.
 * \retval 1 interrupts were disabled.
 *
 ***********************************************************************
 */
unsigned long vmk_SPLockIRQ(
   vmk_SpinlockIRQ *lock);

/*
 ***********************************************************************
 * vmk_SPTryLockIRQ --                                            */ /**
 *
 * \ingroup Spinlocks
 * \brief Try acquire an IRQ spinlock
 *
 * This function tries to acquire the given spinlock:
 * - If the given IRQ spinlock has not been locked, this function locks it
 *   and disables interrupts on the current CPU if interrupts were not
 *   already disabled.
 * - If the given IRQ spinlock has been locked already, this function does
 *   nothing and returns immediately.
 *
 * \pre This caller shall not already hold a spinlock of lower or
 *      equal rank.
 *
 * \param[in,out] lock   IRQ spinlock to attempt to acquire.
 * \param[out]    flags  Previous IRQ level. 0 if interrupts were enabled.
 *                       1 if interrupts were disabled.
 *
 * \retval VMK_OK    The given spinlock is successfully locked by this
 *                   call.
 * \retval VMK_BUSY  The given spinlock has been locked already.
 *
 ***********************************************************************
 */
VMK_ReturnStatus vmk_SPTryLockIRQ(
   vmk_SpinlockIRQ *lock,
   unsigned long *flags);

/*
 ***********************************************************************
 * vmk_SPUnlockIRQ --                                             */ /**
 *
 * \ingroup Spinlocks
 * \brief Release an IRQ spinlock
 *
 * \pre The caller currently owns the spinlock.
 *
 * \param[in,out] lock    IRQ spinlock to unlock.
 * \param[in]     prevIRQ IRQ level from returned from vmk_SPLockIRQ
 *                        or vmk_SPTryLockIRQ.
 *
 ***********************************************************************
 */
void vmk_SPUnlockIRQ(
   vmk_SpinlockIRQ *lock,
   unsigned long prevIRQ);

/*
 ***********************************************************************
 * vmk_SPDestroyIRQ --                                            */ /**
 *
 * \ingroup Spinlocks
 * \brief Destroy an IRQ spinlock
 *
 * Revert all side effects of vmk_SPCreateIRQ_LEGACY
 *
 * \param lock    IRQ spinlock to be detroyed.
 *
 ***********************************************************************
 */
void vmk_SPDestroyIRQ(vmk_SpinlockIRQ *lock);

/*
 ***********************************************************************
 * vmk_RWCreate_LEGACY --                                         */ /**
 *
 * \ingroup Spinlocks
 * \brief Allocate and initialize a reader/writer spinlock
 *
 * Creates a new reader/writer spinlock and initializes it as
 * ranked spinlock.
 *
 * \note Requires that the module heap be initialized.
 *
 * \param[out] lock        Pointer to the new spinlock class.
 * \param[in]  moduleID    Module that owns the new spinlock.
 * \param[in]  name        Name of the spinlock,
 * \param[in]  lockClass   Pointer to a spinlock class, or NULL if the
 *                         new spinlock should be a simple (non-classed)
 *                         spinlock.
 * \param[in]  rank        The spinlock's rank.\n
 *                         If lockClass is NULL then this parameter
 *                         specifies the lock's major rank.
 *                         The specified rank must be greater than or
 *                         equal to VMK_SP_RANK_LOWEST and less than or
 *                         equal to VMK_SP_RANK_HIGHEST.\n
 *                         If lockClass is set to a valid spinlock
 *                         class then this parameter specifies the
 *                         new spinlock's minor rank with the lock's
 *                         major rank being inherited from the
 *                         spinlock class.
 *                         The specified rank must be greater than or
 *                         equal to VMK_SP_RANK_MIN_MAJOR_LEGACY and 
 *                         less than or equal to 
 *                         VMK_SP_RANK_MAX_MAJOR_LEGACY.\n
 *
 * \retval VMK_OK The spinlock was successfully initialized
 * \retval VMK_NO_MEMORY The spinlock could not be allocated
 * \retval VMK_NO_MODULE_HEAP The module has no heap to allocate from
 *
 ***********************************************************************
 */
VMK_ReturnStatus vmk_RWCreate_LEGACY(
   vmk_SpinlockRW *lock,
   vmk_ModuleID moduleID,
   const char *name,
   vmk_SpinlockClass_LEGACY *lockClass,
   vmk_SpinlockRank rank);

/*
 ***********************************************************************
 * vmk_RWReadLock --                                              */ /**
 *
 * \ingroup Spinlocks
 * \brief Acquire a reader/writer spinlock for reading.
 *
 * \pre The caller shall not already hold a spinlock of lower or
 *      equal rank.
 *
 * \param[in,out] lock  Reader/writer spinlock to acquire.
 *
 ***********************************************************************
 */
void vmk_RWReadLock(
   vmk_SpinlockRW *lock);

/*
 ***********************************************************************
 * vmk_RWTryReadLock --                                           */ /**
 *
 * \ingroup Spinlocks
 * \brief Try acquire the reader/writer spinlock for reading
 *
 * This function tries to acquire the given spinlock:
 * - If the given r/w spinlock has not been locked, this function locks it.
 * - If the given r/w spinlock has been locked already, this function does
 *   nothing and returns immediately.
 *
 * \pre This caller shall not already hold a spinlock of lower or
 *      equal rank.
 *
 * \param[in,out] lock   Reader/writer spinlock to attempt to acquire.
 *
 * \retval VMK_OK    The given spinlock is successfully locked by
 *                   this call.
 * \retval VMK_BUSY  The given spinlock has been locked already.
 *
 ***********************************************************************
 */
VMK_ReturnStatus vmk_RWTryReadLock(
   vmk_SpinlockRW *lock);

/*
 ***********************************************************************
 * vmk_RWIsRLocked --                                             */ /**
 *
 * \ingroup Spinlocks
 * \brief Tests a reader/writer spinlock to see if it is currently
 *        read-locked.
 *
 * \param[in]  lock  Reader/writer spinlock to check
 *
 * \retval VMK_OK          Lock is unlocked.
 * \retval VMK_BUSY        Lock is locked.
 * \retval VMK_BAD_PARAM   Not a valid lock.
 *
 ***********************************************************************
 */
VMK_ReturnStatus vmk_RWIsRLocked(
   vmk_SpinlockRW *lock);

/*
 ***********************************************************************
 * VMK_ASSERT_RWLOCK_RLOCKED --                                    */ /**
 *
 * \ingroup Spinlocks
 * \brief Assert that a reader/writer lock is currently read-locked.
 *
 * \note This call only performs the check on a debug build.
 *       Other builds always succeed.
 *
 * \param[in] lock   Reader/writer spinlock to check
 *
 ***********************************************************************
 */
#define VMK_ASSERT_RWLOCK_RLOCKED(lock) \
   VMK_ASSERT(vmk_RWIsRLocked(lock) == VMK_BUSY)


/*
 ***********************************************************************
 * vmk_RWWriteLock --                                              */ /**
 *
 * \ingroup Spinlocks
 * \brief Acquire a reader/writer spinlock for writing.
 *
 * \pre The caller shall not already hold a spinlock of lower or
 *      equal rank.
 *
 * \param[in,out] lock  Reader/writer spinlock to acquire.
 *
 * \return None.
 *
 ***********************************************************************
 */
void vmk_RWWriteLock(
   vmk_SpinlockRW *lock);

/*
 ***********************************************************************
 * vmk_RWTryWriteLock --                                          */ /**
 *
 * \ingroup Spinlocks
 * \brief Try acquire the reader/writer spinlock for writing
 *
 * This function tries to acquire the given spinlock:
 * - If the given r/w spinlock has not been locked, this function locks it.
 * - If the given r/w spinlock has been locked already, this function does
 *   nothing and returns immediately.
 *
 * \pre This caller shall not already hold a spinlock of lower or
 *      equal rank.
 *
 * \param[in,out] lock   Reader/writer spinlock to attempt to acquire.
 *
 * \retval VMK_OK    The given spinlock is successfully locked by this
 *                   call.
 * \retval VMK_BUSY  The given spinlock has been locked already.
 *
 ***********************************************************************
 */
VMK_ReturnStatus vmk_RWTryWriteLock(
   vmk_SpinlockRW *lock);

/*
 ***********************************************************************
 * vmk_RWIsWLocked --                                             */ /**
 *
 * \ingroup Spinlocks
 * \brief Tests a reader/writer spinlock to see if it is currently
 *        write-locked.
 *
 * \param[in]  lock  Reader/writer spinlock to check
 *
 * \retval VMK_OK          Lock is unlocked.
 * \retval VMK_BUSY        Lock is locked.
 * \retval VMK_BAD_PARAM   Not a valid lock.
 *
 ***********************************************************************
 */
VMK_ReturnStatus vmk_RWIsWLocked(
   vmk_SpinlockRW *lock);

/*
 ***********************************************************************
 * VMK_ASSERT_RWLOCK_WLOCKED --                                    */ /**
 *
 * \ingroup Spinlocks
 * \brief Assert that a reader/writer lock is currently write-locked.
 *
 * \note This call only performs the check on a debug build.
 *       Other builds always succeed.
 *
 * \param[in] lock   Reader/writer spinlock to check
 *
 ***********************************************************************
 */
#define VMK_ASSERT_RWLOCK_WLOCKED(lock) \
   VMK_ASSERT(vmk_RWIsWLocked(lock) == VMK_BUSY)

/*
 ***********************************************************************
 * vmk_RWReadUnlock --                                            */ /**
 *
 * \ingroup Spinlocks
 * \brief Release a reader/writer spinlock from a read-lock.
 *
 * \pre The caller currently has a read-lock on the spinlock.
 *
 * \param[in,out] lock  Reader/writer spinlock to release.
 *
 ***********************************************************************
 */
void vmk_RWReadUnlock(
   vmk_SpinlockRW *lock);

/*
 ***********************************************************************
 * vmk_RWWriteUnlock --                                            */ /**
 *
 * \ingroup Spinlocks
 * \brief Release a reader/writer spinlock from a write-lock.
 *
 * \pre The caller currently has a write-lock on the spinlock.
 *
 * \param[in,out] lock  Reader/writer spinlock to release.
 *
 ***********************************************************************
 */
void vmk_RWWriteUnlock(
   vmk_SpinlockRW *lock);

/*
 ***********************************************************************
 * vmk_SPDestroy --                                               */ /**
 *
 * \ingroup Spinlocks
 * \brief Destroy a reader/writer spinlock
 *
 * Revert all side effects of vmk_RWCreate_LEGACY
 *
 * \param[in] lock   Reader/writer spinlock to be destroyed.
 *
 ***********************************************************************
 */
void vmk_RWDestroy(vmk_SpinlockRW *lock);

#endif /* _VMKAPI_SPLOCK_H_ */
/** @} */