Patent Publication Number: US-5832484-A

Title: Database system with methods for parallel lock management

Description:
COPYRIGHT NOTICE 
     A portion of the disclosure of this patent document contains material which is subject to copyright protection. The copyright owner has no objection to the facsimile reproduction by anyone of the patent document or the patent disclosure as it appears in the Patent and Trademark Office patent file or records, but otherwise reserves all copyright rights whatsoever. 
     BACKGROUND OF THE INVENTION 
     The present invention relates generally to information processing environments and, more particularly, to mechanisms for controlling allocation of locks on information stored in a data processing system, such as a Database Management System (DBMS). 
     Computers are very powerful tools for storing and providing access to vast amounts of information. Computer databases are a common mechanism for storing information on computer systems while providing easy access to users. A typical database is an organized collection of related information stored as &#34;records&#34; having &#34;fields&#34; of information. As an example, a database of employees may have a record for each employee where each record contains fields designating specifics about the employee, such as name, home address, salary, and the like. 
     Between the actual physical database itself (i.e., the data actually stored on a storage device) and the users of the system, a database management system or DBMS is typically provided as a software cushion or layer. In essence, the DBMS shields the database user from knowing or even caring about underlying hardware-level details. Typically, all requests from users for access to the data are processed by the DBMS. For example, information may be added or removed from data files, information retrieved from or updated in such files, and so forth, all without user knowledge of underlying system implementation. In this manner, the DBMS provides users with a conceptual view of the database that is removed from the hardware level. The general construction and operation of a database management system is known in the art. See e.g., Date, C., An Introduction to Database Systems, Volume I and II, Addison Wesley, 1990; the disclosure of which is hereby incorporated by reference. 
     DBMS systems have long since moved from a centralized mainframe environment to a de-centralized or distributed environment. One or more PC &#34;client&#34; systems, for instance, may be connected via a network to one or more server-based database systems (SQL database server) acting as a DBMS. Commercial examples of these &#34;client/server&#34; systems include Powersoft™ clients connected to one or more Sybase SQL Server™ database servers. Both Powersoft™ and Sybase SQL Server™ are available from Sybase, Inc. of Emeryville, Calif. 
     As the migration to client/server continues, each day more and more businesses are run from mission-critical systems which store information on server-based SQL database systems, such as Sybase SQL Server™. As a result, increasingly higher demands are being placed on server-based SQL database systems. Accordingly, there is much interest in improving the performance of such systems, particularly in terms of execution speed and reliability. 
     One area where system performance sometimes has been found to be lacking is the server functionality responsible for coordinating access of multiple clients to shared database objects, specifically the aspect of &#34;locking&#34; database objects. Locking mechanisms are employed on DBMSs to prevent multiple tasks from concurrently sharing a resource (e.g., reading or writing a database record) in a manner which leads to inconsistency of the resource. Common concurrency problems include lost updates, uncommitted dependencies, and inconsistent analyses. These problems and some locking mechanisms designed to address them are described in the general database literature; see, e.g., Nath, A., The Guide to SQL Server, Second Edition, Addison-Wesley Publishing Company, 1995, which is hereby incorporated by reference for all purposes. 
     Briefly, certain tasks require &#34;exclusive&#34; access, while other tasks need only &#34;shared&#34; access. When a client accesses a database record for the purpose of updating that record, the client must take out an exclusive &#34;lock&#34; on that record to ensure that while the task is using the record, another task cannot concurrently modify (and in some cases also not read) the record. For example, if a first user updates a data record to reflect a recent transaction, a lock on that record assures the user that no other users can concurrently modify the record while his or her transaction is being processed. 
     In one approach to lock management, such as found in Sybase SQL Server™ (System 10), different locks are provided in shared memory and are made available by a global resource manager commonly referred to as a lock manager. When a task requires access to a record, the lock manager determines whether an appropriate lock can be granted, and, if so, allocates such a lock to the task. If the lock manager finds an existing lock on the record, it either queues a request for the lock or grants a concurrent lock to the task, depending upon the nature of the existing lock and the type of access required (i.e., whether the locks can co-exist). 
     Access to the lock manager in such a system is limited by a single spin lock on the lock manager itself. This spin lock must be taken by any task requiring access to the lock manager for the purpose of obtaining a lock on a database record or for any other purpose. This approach prevents other tasks from simultaneously accessing any other locks through the lock manager. On the one hand, providing a single spin lock for the lock manager simplifies system design: lock-related procedures such as deadlock detection can be conducted easily. On the other hand, the single spin lock also becomes a bottleneck to providing scalability, that is, providing additional server engines on the same network. Specifically, the single spin lock approach hampers system scalability, since any additional engine provided must contend for the same single spin lock. 
     One approach to solving this problem might be to provide multiple instances of the lock manager itself. Such a solution quickly leads to communications problems, however. For example, each request for a new lock would require that the multiple instances of the lock manager communicate the request among themselves to ensure that the requested lock is actually available (i.e., does not conflict with other granted locks). Further, deadlock detection becomes quite complicated: deadlock searches would now have to be conducted over the multiple lock managers, each of which could deallocate or allocate locks while another lock manager is being searched. 
     Thus, what is needed is an improved database system with methods for allocating locks that allows system throughput to scale with the number of database engines on a network, while not requiring an unduly complicated communications protocol. 
     SUMMARY OF THE INVENTION 
     The present invention comprises a Client/Server Database System with methods for providing parallel lock management. In an exemplary embodiment, the system includes one or more Clients (e.g., Terminals or PCs) connected via a Network to a Server. The Server, operating under a server operating system (e.g., Windows NT) includes a Database Server System, such as Sybase SQL Server™. Clients, operating under client applications, store data in and retrieve data from one or more database tables resident on the Server by submitting SQL commands, some of which specify &#34;queries&#34;--criteria for selecting particular records of a table. 
     Operation of a client application requires access to various objects in the system, such as a database table, a data page, an index page, a memory buffer, or the like. Specifically, the Access Methods within the server employ various Resource Governors which controls allocation of such Server resources as buffer caches, locks, and the like. For these functions, the Resource Governors employ separate managers such as a buffer manager for controlling the contents of buffer caches employed by the server to buffer database records. 
     One such resource governor or manager is a Parallel Lock Manager (hereinafter, simply &#34;Lock Manager&#34;) of the present invention. It provides lock management for the Resource Governors&#39; internal clients (e.g., Access Methods)--controlling all client locking. The Lock Manager allocates locks, deallocates/releases locks, searches for deadlocks, and the like. The Lock Manager manages different lock types, each one for controlling access to a different type of system resource. Exemplary lock types include address locks, table locks, pages locks, and semaphore locks. Here, an address lock locks a memory address--a buffer. The semaphore locks control access to a system log table(s) so that tasks can make log entries as necessary. Thus, when a task is ready to log its transaction, it first takes out a semaphore lock. Both address and semaphore locks are system internal locks (i.e., not typically visible to user applications). The table locks control access to tables within the database, and page locks control access to individual pages (e.g., index pages and data record pages) stored in the database. Both the page locks and table locks are user visible locks. Each may be processed indirectly (e.g., through SQL statements) by a user application. Although there are different lock types, the Lock Manager largely views the different lock types generically--applying the same or similar management methodology to each type. 
     The Lock Manager provides an internal application programming interface (&#34;API&#34;) to its internal clients, such as the Access Methods. Because the Lock Manager is a global resource, identical copies are provided in each engine of a database network (as identical pieces of server code). In particular, an SMP (symmetric multi-processor) embodiment of the Database Server System supports multiple database engines (i.e., multiple instances of the engine), up to an arbitrary number of database engines. Each engine includes a Lock Manager instance. Even though there are multiple instances of the Lock Manager, the engines share a single, global set of data structures. 
     Conventionally, such structures were protected by a single spin lock. Such an approach robs a system&#39;s ability to scale in an SMP environment, due to contention among the multiple server engines for the single spin lock. To reduce this contention for these shared data structures, therefore, the present invention provides the Lock Manager (of each engine) with methods for parallel lock management--decomposing the (prior) single spin lock into multiple spin locks, each protecting individual hash buckets or groups of hash buckets. 
     Decomposing the (prior) spin lock into multiple spin locks greatly reduces contention for the Lock Manager&#39;s data structures and thereby improves the system&#39;s scalability. At the same time, however, existing lock management methodology, such as conventional &#34;deadlock&#34; searching, depended on the simplicity of a single spin lock for protecting these interdependent structures--that is, that Lock Manager&#39;s data structures were all frozen by a single spin lock (i.e., would not be changed by another task). Accordingly, implementation of parallel lock management requires improved &#34;deadlock&#34; searching methodology. 
     Access to particular lock types is controlled via hash tables, one table for each lock type. Each hash table has one or more spin locks, each spin lock for controlling a subset or range of one or more hash buckets for the table. In this manner, contention is substantially reduced: locks only block when two tasks each concurrently require access to hash buckets covered by the same spin lock. In previous systems, in contrast, a single spin lock was always used to protect such structures and, thus, always presented a bottleneck. 
     By associating groups of hash buckets with a particular spin lock, spin locks in the system of the present invention are conserved. The number of spin locks employed for each hash table is user (administrator) configurable. The simplest use is to configure the system such that the minimum number of spin locks is employed: one spin lock for each hash table. Even at this minimum, contention is reduced: processing a lock request of one type of lock (e.g., page lock) will not block access to other types of locks (e.g., address locks). At the other extreme, the user can allocate one spin lock for each and every bucket (up to the number of spin locks available in the system). Here, increasing the number of spin locks increases parallelism available with a give lock type--contention for other hash buckets within a given table decreases as the number of spin locks assigned to that table increases. In typical use, the user will configure or tune use of available spin locks to optimize system performance. In this manner, contention for access to the Lock Manager can be reduced while preserving efficient use of system spin locks, thereby allowing the system to appropriately scale up as more engines are added to the system. 
     Decomposing the (prior) spin lock into multiple spin locks greatly reduces contention for the Lock Manager&#39;s data structures and thereby improves the system&#39;s scalability. At the same time, however, existing lock management methodology, such as conventional &#34;deadlock&#34; searching, depended on the simplicity of a single spin lock for protecting these interdependent structures--that is, that Lock Manager&#39;s data structures were all frozen by a single spin lock (i.e., would not be changed by another task). Accordingly, implementation of parallel lock management requires improved &#34;deadlock&#34; searching methodology. 
     &#34;Deadlock&#34; is a well-known scenario when two or more tasks go to sleep waiting (directly or indirectly) for each other to release their respective locks on resources that they are accessing. To detect a deadlock, a database system performs a deadlock search for examining sleeping tasks to identify any &#34;cycles,&#34; i.e., a dependency chain which points back to the sleeping task that initiated the search. The presence of such a cycle indicates a deadlock. When one is identified, the server chooses a &#34;deadlock victim&#34; from among the tasks making up the cycle to break the deadlock. 
     To avoid deadlocks, a system must be able to detect deadlocks among sleeping tasks. Conventionally, each task undertakes deadlock searching immediately after it begins waiting on a lock request. A common approach is to employ a depth-first search of the dependency graph (a tree data structure), looking for cycles. If a cycle is found, the deadlock can be broken by selecting a deadlock victim. In many instances, however, a task&#39;s requested lock is immediately or quickly granted, therefore rendering deadlock searching moot. In many cases, therefore, immediate deadlock searching incurs unnecessary overhead and, thus, hurts system performance. 
     To address this inefficiency, the system of the present invention provides &#34;deferred&#34; deadlock detection. Here, a task simply goes to sleep on a lock; it does not initiate a deadlock search. Routinely, the task will be awakened with the lock being granted. If a deadlock is present, however, the task will not be awakened with a granted lock. To address this second case, the system provides a deferred mode of deadlock detection. For this mode, the system provides a user-configurable, periodic alarm. The user configures the alarm for delaying or &#34;deferring&#34; deadlock detection up to a certain time period. In a preferred embodiment, the delay is set to a default time period of 100 milliseconds. Setting the time period to 0 milliseconds indicates that deadlock detection is not deferred. 
     As before, a task using deferred mode continues to initiates the deadlock searching. However, &#34;when&#34; the task actually initiates deadlock searching changes. If the time period for the alarm is greater than zero, the task will not initiate the search at the time of sleeping on the lock. Instead, a periodic alarm handler will increment a counter until the specified time period is reached. The alarm handler works in conjunction with an &#34;engine scheduler&#34; to make sure that, if a task sleeps on a lock request for more than the configured time period, the task is awakened. This time, however, the task is awakened to initiate deadlock searching. More specifically, the alarm handler checks a &#34;lock sleeptask queue&#34; (described below) to see if there is a sleeping task that has waited for at least the &#34;deadlock checking period.&#34; If such a task is found, the task is awakened so that it can perform its own deadlock search. In a preferred embodiment, only a single deadlock search may be conducted at any given time. Therefore, a semaphore (rdlc --  status) is employed to control the process. If a deadlock search is being conducted by a first task, a second task may have to wait longer than the &#34;deadlock checking period&#34; period before initiating its own deadlock search. 
    
    
     BRIEF DESCRIPTION OF THE DRAWINGS 
     FIG. 1A is a block diagram illustrating a computer system in which the present invention may be embodied. 
     FIG. 1B is a block diagram illustrating a software subsystem for controlling the operation of the computer system of FIG. 1A. 
     FIG. 2A is a block diagram of a client/server system in which the present invention may be embodied. 
     FIG. 2B is a block diagram of an SMP client/server system in which the present invention may be preferably embodied. 
     FIG. 2C is a block diagram illustrating shared Lock Manager structures in the client/server system of FIG. 2B which are, before modification in accordance with the present invention, under control of a single spin lock. 
     FIG. 3A is a block diagram of a Lock Manager illustrating shared Lock Manager structures which have been modified in accordance with the present invention. 
     FIG. 3B is a block diagram illustrating layout and use of hash tables provided by the shared Lock Manager structures. 
     FIG. 4A is a diagram illustrating use of data structures &#34;hanging&#34; off a hash table entry (&#34;hash bucket&#34;) for representing a request for an exclusive lock. 
     FIG. 4B is a diagram illustrating use of data structures for representing multiple requests for exclusive locks. 
     FIG. 4C is a diagram illustrating use of data structures for representing multiple requests for shared locks. 
     FIG. 4D is a diagram illustrating use of data structures for representing multiple requests for both shared and exclusive locks. 
     FIG. 4E is a diagram illustrating addition of a new request for an exclusive lock. 
     FIG. 4F is a diagram illustrating addition of a new request for a shared lock. 
     FIG. 5A is a diagram representing lock record (i.e., lock &#34;request&#34;) and semawait data structures that correspond to a simple deadlock. 
     FIG. 5B is a diagram of a &#34;direct dependency graph&#34; generated during a deadlock search of the data structures shown in FIG. 5A. 
     FIG. 6A is a diagram of a dependency graph of a hypothetical arrangement of tasks as those tasks might be queued at the beginning of a deadlock search. 
     FIGS. 6B-C are diagrams of dependency graphs illustrating use of sequence numbers in the present invention for detecting &#34;spurious deadlocks&#34; for the tasks of FIG. 6A. 
     FIG. 7 is a diagram of a dependency graph for a deadlock search uncovering a cycle in which the initiating task did not form part of the cycle (known as the &#34;innocent bystander deadlock&#34;). 
     FIG. 8 is a diagram of a dependency graph for a deadlock search uncovering multiple cycles. 
     FIG. 9 is a block diagram illustrating a method of the present invention for improved management of global &#34;free&#34; locks. 
    
    
     DESCRIPTION OF THE PREFERRED EMBODIMENTS 
     The following description will focus on the presently preferred embodiment of the present invention, which is operative in a network environment executing client/server database applications. The present invention, however, is not limited to any particular application or environment. Instead, those skilled in the art will find that the present invention may be advantageously applied to any application or environment where contention for resources occurs and some locking mechanism is employed, including non-SQL database management systems and the like. The description of the exemplary embodiments which follows is, therefore, for the purpose of illustration and not limitation. 
     Definitions 
     At the outset, it is helpful to briefly review database terms which will be used in the subsequent discussion of the construction and operation of the system and methods of the present invention. The definitions are provided to assist in understanding the preferred embodiments described herein. 
     A &#34;database object&#34; is an article stored in one or more databases. Examples of such database objects include tables, tuples, logs for databases, statistics pages, views, stored procedures, and the like. Such database objects may have attributes such as sizes, names, types, and so forth. In addition, a database object is a &#34;resource&#34; employed by the database during normal functioning. Examples of such resource-type database objects include buffers and semaphores. In the context of this invention, locks can be placed on some or all of these database object types. 
     A &#34;semaphore&#34; is an indicator, such as a flag, used to govern access to shared system resources, for maintaining order among processes that are competing for use of resources. 
     A &#34;task&#34; is a program in execution together with virtual memory containing instructions, data, and context information, such as descriptors for open files and communication channels. In the context of this invention, tasks and processes will be used interchangeably, although conventionally &#34;process&#34; may refer to only the program in the execution component of a task. 
     A &#34;lock manager&#34; is any database entity or collection of entities that control access to locks on database objects. Typically, tasks requesting locks on particular database objects lodge their requests with a lock manager. 
     A &#34;hash table&#34; is a look-up table associated with particular objects in a collection of resources such as individual locks accessible through a lock manager. It provides pointers to the various objects currently provided. The location of a pointer within the hash table is a function of certain attributes of the object (i.e., &#34;hash function&#34;) associated with the pointer. When access to a particular object is required, the system indexes into the appropriate entry in the hash table for determining whether the object is available and takes action accordingly. 
     &#34;Hash buckets&#34; are provided for holding information about the objects indexed in a hash table. If there are more objects than hash buckets, then the additional objects can be &#34;chained off&#34; at least some hash buckets. This may be desirable when a relatively large number of locks are provided by the system. 
     Transactional locks, or simply &#34;locks,&#34; are employed by the system to coordinate OLTP (on-line transactional processing). Typical transactional locks include, for instance, a table lock and a page lock. These locks can be shared or exclusive. An &#34;exclusive&#34; lock, when held by a given process, completely excludes all other processes from accessing the locked object. A &#34;non-exclusive&#34; or &#34;shared&#34; lock, on the other hand, permits certain types of shared access while the lock is held by a given process. For example, if a first process takes out a non-exclusive lock on a data page, a second process may be permitted to read data records from that page even while the first process holds the lock. However, the non-exclusive lock might prevent the second process from writing to any data records contained in the locked data page. 
     In contrast to transactional locks, &#34;spin&#34; locks operate at a lower level (i.e., at the operating system level) to control system-critical resources. These locks are characterized by the fact that a task will continue to &#34;spin&#34;--that is, continues to execute--if a resource is not immediately available (i.e., if the lock is not immediately granted). Ordinarily, when a task requests a lock, such as a transactional lock, the requesting task will &#34;sleep&#34; on the lock if it is not immediately available (e.g., the lock is currently held by another task). When &#34;sleeping,&#34; the task is in effect placed in a suspended state until some point in time when it is &#34;awakened.&#34; For instance, a task sleeping on a lock will be awakened when the underlying database management system is ready to grant the lock (and, accordingly, grant access to the desired data structure). When a task is put to &#34;sleep,&#34; the underlying system switches tasks, from the now sleeping one to one which is awake. In this manner, processor resources can be conserved: a sleeping task consumes no processor resources while sleeping. 
     A task or context switch itself is not cost-free: system resources are required for swapping out the context of one task for another. For instance, the then-current state of the task to be put to sleep must be stored, while the system switches to another task. Once the task is awakened, the system must restore the state information for the awaking task. For certain system critical resources, the overhead incurred with such a task switch is unacceptable. By spinning instead of sleeping, a task can avoid the overhead and cost of a context switch. To ensure that waiting tasks do not spin too long, spin locks typically contain only very few instructions, so that they are held for only a very short duration. Spin locks are employed within the system of the present invention to control access to low-level system resources, such as important lock management data structures which are shared. 
     Standalone System Hardware 
     The invention may be embodied on a computer system such as the system 100 of FIG. 1A, which comprises a central processor 101, a main memory 102, an input/output controller 103, a keyboard 104, a pointing device 105 (e.g., mouse, track ball, pen device, or the like), a screen display device 106, and a mass storage 107 (e.g., hard or fixed disk, removable disk (e.g., &#34;floppy&#34;), optical disk, magneto-optical disk, or flash memory). Processor 101 includes or is coupled to a cache memory 109 for storing frequently accessed information; memory 109 may be an on-chip cache or external cache (as shown). Additional output device(s) 108, such as a printing device, may be included in the system 100 as desired. As shown, the various components of the system 100 communicate through a system bus 110 or similar architecture. In a preferred embodiment, the system 100 includes an IBM-compatible personal computer system, available from a variety of vendors (including IBM of Armonk, N.Y.). 
     Standalone System Software 
     Illustrated in FIG. 1B, a computer software system 150 is provided for directing the operation of the computer system 100. Software system 150, which is stored in system memory 102 and on mass storage or disk memory 107, includes a kernel or operating system (OS) 140 and a windows shell 145. One or more application programs, such as application software 155, may be &#34;loaded&#34; (i.e., transferred from storage 107 into memory 102) for execution by the system 100. The system also includes a user interface 160 for receiving user commands and data as input and displaying result data as output. 
     Also shown, the software system 150 includes a Relational Database Management System (RDBMS) front-end or &#34;client&#34; 170. The RDBMS client 170 may be any one of a number of database front-ends, including PowerBuilder™, dBASE®, Paradox®, Microsoft® Access, or the like. In an exemplary embodiment, the front-end will include SQL access drivers (e.g., Borland SQL Links, Microsoft ODBC drivers, Intersolv ODBC drivers, and the like) for accessing SQL database server tables in a Client/Server environment. 
     Client/Server Database Management System 
     While the present invention may operate within a single (standalone) computer (e.g., system 100 of FIG. 1A), the present invention is preferably embodied in a multi-user computer system, such as a Client/Server system. FIG. 2A illustrates the general structure of a Client/Server Database System 200. As shown, the system 200 comprises one or more Client(s) 210 connected to a Server 230 via a Network 220. Specifically, the Client(s) 210 comprise one or more standalone Terminals 211 connected to a Database Server System 240 using a conventional network. In an exemplary embodiment, the Terminals 211 may themselves comprise a plurality of standalone workstations, dumb terminals, or the like, or comprise personal computers (PCs) such as the above-described system 100. Typically, such units would operate under a client operating system, such as Microsoft Windows/MS-DOS for PC clients. 
     The Database Server System 240, which comprises Sybase SQL Server™ (available from Sybase, Inc. of Emeryville, Calif.) in an exemplary embodiment, generally operates as an independent process (i.e., independently of the clients), running under a server operating system such as Microsoft Windows NT (Microsoft Corp. of Redmond, Wash.), NetWare (Novell of Provo, Utah), or UNIX (Novell). The Network 220 may be any one of a number of conventional network systems, including a Local Area Network (LAN) or Wide Area Network (WAN), as is known in the art (e.g., using Ethernet, IBM Token Ring, or the like). The Network includes functionality for packaging client calls in the well-known SQL (Structured Query Language) command syntax, together with any parameter information, into a format (of one or more packets) suitable for transmission across a cable or wire, for delivery to the Database Server 240. 
     Client/server environments, database servers, and networks are well documented in the technical, trade, and patent literature. For a discussion of database servers and client/server environments generally and SQL Server™ particularly, see, e.g., Nath, A., The Guide to SQL Server, Second Edition, Addison-Wesley Publishing Company, 1995 (previously incorporated by reference). Additional documentation of SQL Server™ is available from Sybase, Inc. as SQL Server Documentation Set (Catalog No. 49600). For a discussion of a computer network employing Microsoft Networks/OpenNet File Sharing Protocol, see METHOD AND SYSTEM FOR OPPORTUNISTIC LOCKING IN A NETWORKED COMPUTER SYSTEM, Intl. Application No. PCT/US90/04570, Intl. Publication No. WO 91/03024, Intl. Publication Date Mar. 7, 1991. For a general introduction to a Local Area Network operating under NetWare, see Freed, L. et al., PC Magazine Guide to Using NetWare, Ziff-Davis Press, 1991. A more detailed discussion is available in NetWare 3.x and 4.x and accompanying documentation, which is available from Novell of Provo, Utah. The disclosures of each of the foregoing are hereby incorporated by reference. 
     In operation, the Client(s) 210 store data in or retrieve data from one or more database tables 250, shown in FIG. 2. Typically resident on the Server 230, each table itself comprises one or more horizontal rows or &#34;records&#34; (tuples) together with vertical columns or &#34;fields.&#34; A database record includes information which is most conveniently represented as a single unit. A record for an employee, for example, may include information about the employee&#39;s ID Number, Last Name and First Initial, Position, Date Hired, Social Security Number, and Salary. Thus, a typical record includes several categories of information about an individual person, place, or thing. Each of these categories, in turn, represents a database field. In the foregoing employee table, for example, Position is one field, Date Hired is another, and so on. With this format, tables are easy for users to understand and use. Moreover, the flexibility of tables permits a user to define relationships between various items of data, as needed. 
     In operation, the Clients issue one or more SQL commands to the Server. SQL commands may specify, for instance, a query for retrieving particular data (i.e., data records meeting the query condition) from the table 250. The syntax of SQL (Structured Query Language) is well documented; see, e.g., the above mentioned An Introduction to Database Systems. In addition to retrieving the data from Database Server tables, the Clients also include the ability to insert new rows of data records into the table; Clients can also modify and/or delete existing records in the table. 
     For enhancing the speed in which the Database Server stores, retrieves, and presents particular data records, the Server maintains one or more database indexes on the table. A database index, typically maintained as a B-Tree data structure, allows the records of a table to be organized in many different ways, depending on a particular user&#39;s needs. An index may be constructed as a single disk file storing index key values together with unique record numbers. The former is a data quantity composed of one or more fields from a record; the values are used to arrange (logically) the database file records by some desired order (index expression). The latter are unique pointers or identifiers to the actual storage location of each record in the database file. Both are referred to internally by the system for locating and displaying records in a database file. 
     In operation, the SQL statements received from the one or more Clients 210 (via network 220) are processed by Engine 260 of the Database Server System 240. The Engine 260 itself comprises a Parser 261, Normalizer 263, Compiler 265, Execution Unit 269, Access Methods 270, and Resource Governors 272. Specifically, the SQL statements are passed to the Parser 261 which converts the statements into a query tree--a binary tree data structure which represents the components of the query in a format selected for the convenience of the system. In this regard, the Parser 261 employs conventional parsing methodology (e.g., recursive descent parsing). 
     The query tree is normalized by the Normalizer 263. Normalization includes, for example, the elimination of redundant data. Additionally, the Normalizer performs error checking, such as confirming that table names and column names which appear in the query are valid (e.g., are available and belong together). Finally, the Normalizer can also look up any referential integrity constraints which exist and add those to the query. 
     After normalization, the query tree is passed to the Compiler 265, which includes an Optimizer 266 and a Code Generator 267. The Optimizer is responsible for optimizing the query tree. The Optimizer performs a cost-based analysis for formulating a query execution plan. The Optimizer will, for instance, select the join order of tables (e.g., when working with more than one table); it will select relevant indexes (e.g., when indexes arc available). The Optimizer, therefore, performs an analysis of the query and picks the best execution plan, which in turn results in particular ones of the Access Methods being invoked during query execution. 
     The Code Generator 267, on the other hand, converts the query tree into a set of instructions suitable for satisfying the query. These instructions are passed to the Execution Unit 269. Operating under the control of these instructions, the Execution Unit 269 generates calls into lower-level routines, such as the Access Methods 270, for retrieving relevant information (e.g., row 255) from the database table 250. After the plan has been executed by the Execution Unit, the Server returns a query result or answer table back to the Client(s). 
     To retrieve information, the Access Methods 270 must at times make use of resources provided by the Server. To do so, the Access Methods 270 employ Resource Governors 272 which control allocation of such Server resources as buffer caches, locks, and the like. For these functions, Resource Governors 272 employ separate managers such as a buffer manager for controlling the contents of buffer caches employed by the server to buffer database records. 
     Parallel Lock Manager 274 (hereinafter, simply &#34;Lock Manager&#34;) is one such resource governor or manager. It provides lock management for the Resource Governors&#39; internal clients (e.g., Access Methods 270)--controlling all client locking. The Lock Managers allocate locks, deallocate/release locks, searche for deadlocks, and the like. The Lock Manager 274 manages different lock types, each one for controlling access to a different type of system resource. Lock types include address locks, table locks, pages locks, and semaphore locks. The name of each lock suggests its functionality. For instance, an address lock locks a memory address--a buffer. The address locks control access to objects in memory, such as database buffer caches. Specifically, an address lock is a lock on a memory location having a particular specified buffer address. The semaphore locks control access to a system log table(s) so that tasks can enter log entries as necessary. Thus, when a task is ready to log its transaction, it first takes out a semaphore lock. Both address and semaphore locks are system internal locks (i.e., not typically visible to user applications). The table locks control access to tables within the database, and page locks control access to individual pages (e.g., index pages and data record pages) stored in the database. Both the page locks and table locks are user visible locks. Each may be processed indirectly (e.g., through SQL statements) by a user application. Although there are different lock types, the Lock Manager 274 largely views the different lock types generically--applying the same or similar management methodology to each type. 
     As Access Methods 270 must grab locks to access specific records in the database, the Access Methods 270 represent an internal client of the Lock Manager--communicating directly with Lock Manager 274 for obtaining such locks. In this regard, Lock Manager 274 provides an internal application programming interface (&#34;API&#34;) to its internal clients, such as the Access Methods 270. Note that because the Lock Manager 274 is a global resource, identical copies are provided in each engine of a database network (as identical pieces of server code). 
     Of particular interest herein is an embodiment of the present invention in the Client/Server System 200 operating on an symmetric multi-processor (SMP) computer system. Such an embodiment is shown in FIG. 2B, as Client/Server System 200a. The system includes an SMP server 230a providing an SMP Database Server System 240a. Here, the SMP Database Server System supports multiple database engines (i.e., multiple instances of the engine 260), including engines 260a through 260n, up to an arbitrary number of database engines. 
     Ideally, as more processors are added to the SMP server 230a, the Database Server 240a can scale up with additional database engines. Note, however, that certain resources must be shared among the multiple engines, such as global resources require for accessing a common database table. Shared data structures or resource 290, shown in FIG. 2B, is one such resource. So that each engine &#34;sees&#34; the same resource (i.e., has a consistent view), access is protected by a &#34;spin&#34; lock--a well known SMP synchronization mechanism employed for controlling access to lower-level, system critical resources. Note particularly that, as more engines are added to the system, the contention for the shared resource 290 increases. This presents a single bottleneck for all engines. 
     The shared global structures of the Lock Manager 274 is shown in particular detail in FIG. 2C as shared resource 290. These shared structures, which represent the locking status among multiple users running on multiple engines, are preferred over per-engine data structures. In particular, providing multiple instances of lock management data structures would require less-efficient interprocess communication management--an approach to be avoided. Instead, therefore, the preferred approach is a single set of data structures which are shared among the multiple engine instances. 
     The layout of the data structures is designed to facilitate this sharing. In particular, the structures comprise hash tables for processing requests for different lock types in the system, including an &#34;address&#34; hash table 291, &#34;table&#34; hash table 292, and &#34;page&#34; hash table 293. In particular, information about which of the various locks have been &#34;taken out&#34; is stored by the particular hash table associated with each lock type. For instance, the address lock hash table 291 manages address locks; the table lock hash table 292 manages table locks; and the page lock hash table 293 manages page locks. Each hash table itself includes a plurality of &#34;hash buckets&#34; for storing information about locks and lock requests from various tasks. For instance, the table hash table 292 includes 101 available hash buckets. The address hash table and the page hash table, on the other hand, both have 1031 available hash buckets. 
     Given the multi-processing nature of the system of the present invention, access to the hash tables must occur with SMP synchronization. Suppose, for example, that a user task from engine 260a--task 1  --desires to acquire a page lock on the object 294. A page lock is represented by page hash table 293. The task must index into the hash table, for bucket #3. There, the task determines whether the task can be granted the lock or the task must wait for the lock. During this lock operation, task, protects the Lock Manager&#39;s structures 290 from change by another task, by tasking out a spin lock. After task 1  has completed the lock operation, it releases the spin lock. 
     Prior to modification in accordance with the present invention, the Lock Manager&#39;s shared data structures 290 were protected by a single spin lock (LOCK --  SPIN), which serialized access to the address, page, and table hash tables. Whenever a task held this spin lock, it protected the shared Lock Manager data structures entirely (against concurrent changes by other tasks). This approach, to a large extent, simplifies system design. At the same time, however, the approach leads to high contention among multiple engines for the single spin lock. For the example above, for instance, while task, was processing a page lock, another task, say task 2 , cannot process a request for an address lock. 
     To reduce this contention, therefore, the present invention provides the Lock Manager 274 with methods for parallel lock management--decomposing the (prior) single spin lock into multiple spin locks, each protecting individual hash buckets or groups of hash buckets. Decomposing the (prior) spin lock into multiple spin locks greatly reduces contention for the Lock Manager&#39;s data structures and thereby improves the system&#39;s scalability. At the same time, however, existing lock management methodology, such as conventional &#34;deadlock&#34; searching, depended on the simplicity of a single spin lock for protecting these interdependent structures--that is, that Lock Manager&#39;s data structures were all frozen by a single spin lock (i.e., would not be changed by another task). Accordingly, implementation of parallel lock management requires improved &#34;deadlock&#34; searching methodology. Details of the construction and operation of the Lock Manager for achieving this will be described below. 
     Improving system scalability: Parallel Lock Manager 
     A. Increasing scalability by removing single spin lock bottleneck 
     Given the multi-processing nature of the system of the present invention, access to the hash tables occurs using the multi-processor synchronization spin lock technique previously described for FIG. 2C. To improve system scalability, however, the spin lock technique is modified. Specifically, the single spin lock (LOCK --  SPIN) employed for protecting the Lock Manager&#39;s hash table data structures is decomposed into a configurable number of spin locks, employed in conjunction with other spin locks as follows: 
     (1) Configurable number of spin locks (LOCK --  SPIN) for: 
     (a) Address lock hash table; 
     (b) Page lock hash table; 
     (c) Table lock hash table; 
     (2) One spin lock for semaphore locks; 
     (3) One spin lock for a freelock list (LOCK --  FREELIST --  SPIN); and 
     (4) One spin lock for a sleep task queue (LOCK --  SLEEPTASK --  SPIN). 
     The scenario of one engine running is handled as a special case: the Lock Manager will allocate only one spin lock, to avoid wasting memory on spin locks for a single-engine server. 
     FIG. 3A illustrates the modification necessary for implementing this design, with particular emphasis on management of address, page, and table locks. In general, each hash table has one or more spin locks, each spin lock for controlling a subset or range of one or more hash buckets for the table. In the exemplary embodiment depicted in FIG. 3A, for instance, the 1031 hash buckets of address lock hash table 302 are controlled by a plurality of spin locks 312 (numbering 10, by default). In a similar fashion, the 101 hash buckets of table lock hash table 304 are controlled by a plurality of spin locks 314 (numbering 5, by default), and the 1031 hash buckets of page lock hash table 306 are controlled by a plurality of spin locks 316 (numbering 10, by default). Thus, for example, a first one of the page spin locks 316 controls access to hash buckets 1-104 for the page lock hash table 306. In this manner, contention is substantially reduced: locks only block when two tasks each concurrently require access to hash buckets covered by the same spin lock. In previous systems, in contrast, a single spin lock was always used to protect such structures and, thus, always presented a bottleneck. 
     By associating groups of hash buckets with a particular spin lock, spin locks in the system of the present invention are conserved. As described below, the number of spin locks employed for each hash table is user (administrator) configurable. The simplest use is to configure the system such that the minimum number of spin locks is employed: one spin lock for each hash table. Even at this minimum, contention is reduced: processing a lock request of one type of lock (e.g., page lock) will not block access to other types of locks (e.g., address locks). At the other extreme, the user can allocate one spin lock for each and every bucket (up to the number of spin locks available in the system). Here, increasing the number of spin locks increases parallelism available with a given lock type--contention for other hash buckets within a given table decreases as the number of spin locks assigned to that table increases. In typical use, the user will configure or tune use of available spin locks to optimize system performance; the default settings shown in FIG. 3A provide good results for an embodiment scaling up to ten processors. In this manner, contention for access to the Lock Manager can be reduced while preserving efficient use of system spin locks, thereby allowing the system to appropriately scale up as more engines are added to the system. 
     B. Hash bucket storage: chain of lock records/objects 
     Each hash bucket stores locking information which is maintained by the Lock Manager 274. Specifically, each hash bucket stores information about which task holds a particular lock. More particular, each hash bucket is capable of chaining lock data structures--lock records or objects--off of it, for storing detailed locking information. 
     FIG. 3B illustrates this approach. Each object currently &#34;locked&#34; is represented by a &#34;lock object&#34; associated with a particular hash bucket. In the example shown in FIG. 3B, hash bucket 321 of hash table 320 includes two lock objects 330 and 340 &#34;hanging off&#34; it (i.e., linked to it via pointers). Hash bucket 324, on the other hand, contains a single lock object 350; hash bucket 326 contains three lock objects 360, 370, and 380. If no locks associated with a given hash bucket are currently taken, then the hash bucket or entry is empty--no lock objects will be found hanging off such a hash bucket. This is the case with hash buckets 322 and 328 shown in FIG. 3B. This approach of hanging multiple locks off each hash bucket (i.e., the hash bucket overflow structures shown in FIG. 3B) allows a large number of locks to be represented with relatively few hash buckets. The address hash table 302 illustrated in FIG. 3A, for instance, can represent 10,000 or more address locks with only 1031 hash buckets. 
     To actually index into a particular hash bucket, the system employs a &#34;hashing function&#34; which is based on the number of available buckets. Construction of various hashing functions, as well as hash table techniques in general, are well described in the technical and trade literature; see e.g., Bark, R. Hashing: From Good to Perfect, The C Users Journal, pp. 41-52, the disclosure of which is hereby incorporated by reference. For the address hash table, a simple hash function of &#34;modulo 1031&#34; may be employed. Here, indicia for address lock 1 is stored in hash bucket 1, indicia for address lock 2 is stored in hash bucket 2, indicia for address lock 1030 is stored in (&#34;hanging off&#34; of) hash bucket 1030, indicia for address lock 1031 is stored in hash bucket 1031, and so on. Note, however, that address lock 1032, also &#34;hashes&#34; to hash bucket 1: it has a value of 1 when evaluated with the hash function. Thus, hash bucket 1 stores indicia of address lock 1, address lock 1032, address lock 2033, an so forth and so on. Each one of the address locks therefore represents a separate overflow structure which forms an overflow chain hanging off a particular bucket. 
     C. Processing lock requests 
     1. General 
     The Lock Manager processes requests for any type of lock generally as follows. Initially, the Lock Manager receives a task&#39;s request for a lock from Access Methods 270. In response to this request, the Lock Manager goes to the hash table associated with the type of lock requested and hashes into the location specified as the address of the object for which the lock is sought; as described above, the Manager takes out the spin lock associated with the bucket being indexed into. At that bucket, the Lock Manager can determine whether any lock objects are present at that location. If no lock object is found at the indexed-into hash bucket, the desired lock is immediately available and can be provided to the requesting task. If, on the other hand, one or more other tasks have previously requested a lock on the item at that location, the Lock Manager must queue the present request. 
     Regardless of whether an object is already found at the hash bucket of the desired lock, when a task requests that lock, the Lock Manager creates a lock record for the task and queues that request--that is, links it into the chain of lock objects hanging off the hash bucket. After the request is queued, the Lock Manager checks to determine whether that request is at the head of the queue. A lock request (lock record) is at the head of the queue when no other lock object precedes it. In such a case, the lock is immediately available and the request is &#34;granted.&#34; If not, however, the task &#34;sleeps&#34; on the lock request. 
     2. Exclusive lock processing 
     The process is perhaps best illustrated by way of example, using an address lock (an exclusive lock). It should be noted, however, that the Lock Manager handles lock requests for other lock types (and other lock strengths, e.g., &#34;intent&#34; locks) in an analogous manner. As shown in FIG. 4A, a hash bucket provides a lock object 410 for address 1. Such a lock object is generated upon a first request for a lock. Here, a request for address lock 1 comes to the Lock Manager from a first task. The Lock Manager evaluates the hash function to identify the appropriate hash bucket for address lock 1, grabbing the spin lock for the group of hash buckets containing that hash bucket. Now, the Lock Manager can traverse the overflow chain in that hash bucket, looking for any lock objects. If the Lock Manager finds no lock object for the address lock 1 on the overflow chain, the Lock Manager automatically creates lock object 410 for address lock 1 and queues that object to the overflow chain. 
     After the lock object is created, the Lock manager creates a &#34;lock request&#34; 411 for the first task and queues that lock request to a semawait (described below) which is connected to the lock object, as shown in FIG. 4A. The lock record, in this example, is a data structure indicating the task&#39;s request for an exclusive lock and includes various pieces of information about the task including the task&#39;s process ID. After the lock request 411 has been created and queued, the Lock Manager determines whether that lock record is at the head of the queue off of lock object 410. Because it is, in this example, the requesting task is granted the lock on address 1. The spin lock on the hash bucket is then released. 
     Next suppose that another request for access to the address 1 lock arrives. This scenario is illustrated in FIG. 4B. The Lock Manager again evaluates the appropriate hash function, grabs a spin lock for the identified hash bucket, and traverses the structures provided in that hash bucket. In this scenario, the Lock Manager finds lock object 410 associated with address lock 1032. Because a lock object currently exists for address lock 1 (i.e., object 410), the Lock Manager need not create a new lock object. The Lock Manager does, however, create a new lock request 413 and appends it to a queue of lock requests hanging off lock object 410. The Lock Manager then determines whether the newly-created lock request 413 is at the head of the queue. In the example of FIG. 4B, it is not at the head as the task associated with lock request 411 currently holds an exclusive lock on the address lock. Therefore, the second task must wait for the lock; at this point, it can go to sleep. 
     A waiting task&#39;s request is granted only after all tasks in front of it in the queue have released their locks. As various tasks release the lock, the lock requests of other tasks move forward to the head of the queue. In one particular embodiment when a task releases its lock, it first dequeues its lock request and determines which task follows it in the queue. It then wakes up that task. At this point, the newly awakened task resumes execution with the newly-granted lock. 
     In the example of FIG. 4B, when the task associated with lock request 411 releases the lock (address 1), the task dequeues the lock request 411, thereby causing the lock request 413 to move to the head of the queue. The task associated with the request 413 then wakes and continues execution with a lock on address 1. When it no longer requires the lock, it releases the lock, dequeues lock request 413, and wakes any subsequent task, whereupon the lock is passed on to yet another task. 
     3. Shared lock processing 
     The foregoing example illustrates an address lock--an exclusive lock. For an exclusive lock, if the request is at the head of the queue, the request is granted, as no two exclusive lock requests can simultaneously reside at the head of queue. Page and table locks, in contrast, may be either exclusive or shared (non-exclusive). As noted, a non-exclusive lock may permit one or more other non-exclusive locks to be taken out on the same object (e.g., database page). To implement non-exclusive or shared locks, the Lock Manager chains another data structure, a semawait, in addition to the above-described lock object and lock request data structures. The semawait indicates the position in the queue; each position has one or more shared lock requests or a single exclusive lock request. For efficiency in system design, all of these locking data structures share the same size and are allocated from the freelock list (described in detail below). 
     One exemplary process for generating an overflow chain for page locks will now be described with reference to FIG. 4C. Initially, a first task requests a non-exclusive lock on a page 2. In response, the Lock Manager identifies the appropriate hash bucket for the page 2 lock, takes out a spin lock on the associated subset of hash buckets, and hashes into the identified hash bucket. Assuming that the page 2 lock is not currently taken, Lock Manager 274 discovers that no lock object exists for page 2 and creates lock object 420. Because the page lock request is for a non-exclusive lock, Lock Manager 274 then creates a semawait 430 which it appends directly off of page object 420, at the head of queue. Note that semawaits are employed only for non-exclusive locks. After the semawait structure has been created and queued, the Lock Manager creates a first non-exclusive lock request 431, appended directly off of semawait 430, and containing information about the first task. The first task then releases the spin lock on the subset of hash buckets. 
     Next, a second task also requests a non-exclusive lock on page 2. At this point, the Lock Manager grabs the appropriate spin lock, hashes into the hash bucket, and discovers page lock object 420 with semawait 430 and non-exclusive lock request 431. The Lock Manager then produces a second non-exclusive lock request 433, which it queue up (i.e., appends directly to the prior non-exclusive lock record 431). Both the first and second tasks now hold non-exclusive locks on page 2 and continue executing with shared access to page 2. 
     Now, consider a third task which requests an exclusive lock on page 2. This scenario is illustrated in FIG. 4D. Because this exclusive lock is incompatible with the non-exclusive locks currently held on page 2, the third task&#39;s request cannot be granted immediately. Thus, Lock Manager 274 generates a semawait 440 with a lock request 441 for the third task and queues it to the main overflow chain off the lock object for page 2. At this point, the third task will wait for the prior semawait to be dequeued; at this point, it can go to sleep. 
     Next, consider a fourth task which also requests an exclusive lock of page 2. Again, this request cannot be granted because the desired exclusive lock would not be lock compatible with the currently held non-exclusive locks. Further, note that this exclusive lock would not be lock compatible with the prior exclusive lock, as exclusive locks cannot co-exist on the same object. Thus, Lock Manager 274 generates yet another semawait 450 and appends it to the overflow chain with a lock request 451 for another exclusive lock, as illustrated in FIG. 4E. The fourth task now waits on this newly-added lock request; it can sleep at this point. 
     Finally, consider a fifth task which requests a non-exclusive lock on page 2. This non-exclusive lock is lock compatible with the currently held non-exclusive locks of the first and second tasks. As shown in FIG. 4F, therefore, the Lock Manager links the corresponding lock request 435 to the first semawait 430, thereby granting the newly-requested non-exclusive lock to the fifth task. 
     After the first, second, and fifth tasks deallocate their non-exclusive locks, semawait 430 and its non-exclusive lock requests are dequeued. As a result, the second semawait 440 and its exclusive lock request 441 move to the head of the queue. Concurrently therewith, the third task wakes up and continues execution with an exclusive lock on page 2. When this task subsequently deallocates its exclusive lock structures, the third semawait 450 and its exclusive lock request 451 move to the head of the queue. The fourth task thus continues execution with its requested exclusive lock on page 2. Thereafter, the task dequeues the lock request. After all lock requests and semawaits have been dequeued, the lock object itself is de-allocated. 
     Note that the above scenario for handling requests for both exclusive and non-exclusive locks could allow the third and fourth tasks to remain waiting indefinitely while additional requests for non-exclusive locks are received and queued off of the first semawait 430. To address the situation, the Lock Manager is preferably designed such that a queued request for an exclusive lock waits on no more than a certain number of non-exclusive lock requests lodged after it was queued (i.e., &#34;skip&#34; factor). In one particular preferred embodiment, a task requesting an exclusive lock will allow no more than four non-exclusive locks requests to be queued before it, after it has made its request for the exclusive lock. In this case, if an fifth request for a non-exclusive lock comes in, a new semawait is created and appended to the end of the overflow chain (behind any lock requests for exclusive locks). In this manner, subsequent requests for non-exclusive locks are queued through a subsequent semawait, rather than the semawait in front of the exclusive lock record. 
     D. Deadlock Detection in the Parallel Lock Manager 
     1. General 
     &#34;Deadlock&#34; is a well-known scenario when two or more tasks go to sleep waiting (directly or indirectly) for each other to release their respective locks on resources that they are accessing. To detect a deadlock, a database system performs a deadlock search for examining sleeping tasks to identify any &#34;cycles,&#34; i.e., a dependency chain which points back to the sleeping task that initiated the search. The presence of such a cycle indicates a deadlock. When one is identified, the server chooses a &#34;deadlock victim&#34; from among the tasks making up the cycle to break the deadlock. The system chooses a victim by any appropriate scheme. In one embodiment, the system chooses as a victim that task having used the least amount of CPU time. If there is more than one task with the least amount of CPU time, the task last visited among them is chosen. The transaction of the deadlock victim&#39;s task is rolled back. A general description of deadlock can be found in the abovementioned An Introduction to Database Systems (at pages 415-417). 
     A simple deadlock is illustrated in FIG. 5A, with a &#34;directed dependency graph&#34; for the deadlock represented in FIG. 5B. Assume that a first task, T1, requests a page lock on a page 10. As shown in FIG. 5A, a lock object 501 is created for page 10 to which the task appends a semawait 510 having lock request 511. Since no other task has a lock request queued for page 10, the lock is granted to first task. Now, a second task, T2, requests a lock on a page 20. In a corresponding manner, as shown in FIG. 5A, a lock object 541 is created for page 20 to which the second task appends a semawait 550 having lock request 551. Since no other task has a lock request queued for page 20, the lock is granted to first task. 
     Now, consider a request from the second task for a lock on page 10 (with the requested lock not being lock compatible with the existing lock). Because the fist task already holds a lock on page 10, the second task queues up its request (semawait 520 and lock request 521) and goes to sleep waiting for the lock on page 10 to be released. Note particularly that the second task is going to sleep while holding its lock on page 20. The first task, in a corresponding manner, now requests a lock on page 20 (with the requested lock not being lock compatible with the existing lock). The first task queues up its request (semawait 560 and lock request 561); since this is the first request, a lock object 541 is created from the page 20. Because the second task already holds a lock on page 20, the first task goes to sleep waiting for the lock on page 20 to be released. Under these circumstances, a deadlock has resulted. The locks on pages 10 and 20 will remain in perpetuity absent some intervention from the system, thus preventing both the first task and the second task from continuing execution. 
     FIG. 5B illustrates this &#34;cycle&#34; diagrammatically with directed dependency graph 570. As shown, the first task (T1) holds a lock on a first object (obj1) and is waiting for a lock on a second object (obj2). At the same time, the second task (T2) holds a lock on the second object (obj2) while waiting for a lock on the first object (obj1). Here, the locks (L1 and L2) represent &#34;edges&#34; of the graph. The T2 task created the L1 edge between T2 and T1. Then the T1 task created the L2 edge between T1 and T2. 
     2. Improved deadlock detection: &#34;deferred&#34; mode 
     To avoid deadlocks, a system must be able to detect deadlocks among sleeping tasks. Conventionally, each task undertakes deadlock searching immediately after it begins waiting on a lock request. A common approach is to employ a depth-first search of the dependency graph (a tree data structure), looking for cycles. If a cycle is found, the deadlock can be broken by selecting a deadlock victim. In many instances, however, a task&#39;s requested lock is immediately or quickly granted, therefore rendering deadlock searching moot. In many cases, therefore, immediate deadlock searching incurs unnecessary overhead and, thus, hurts system performance. 
     To address this inefficiency, the system of the present invention provides &#34;deferred&#34; deadlock detection. Here, a task simply goes to sleep on a lock; it does not initiate a deadlock search. Routinely, the task will be awakened with the lock being granted. If a deadlock is present, however, the task will not be awakened with a granted lock. To address this second case, the system provides a deferred mode of deadlock detection. For this mode, the system provides a user-configurable, periodic alarm. The user configures the alarm for delaying or &#34;deferring&#34; deadlock detection up to a certain time period. In a preferred embodiment, the delay is set to a default time period of 100 milliseconds. Setting the time period to 0 milliseconds indicates that deadlock detection is not deferred. 
     As before, a task using deferred mode continues to initiates the deadlock searching. However, &#34;when&#34; the task actually initiates deadlock searching changes. If the time period for the alarm is greater than zero, the task will not initiate the search at the time of sleeping on the lock. Instead, a periodic alarm handler will increment a counter until the specified time period is reached. The alarm handler works in conjunction with an &#34;engine scheduler&#34; to make sure that, if a task sleeps on a lock request for more than the configured time period, the task is awakened. This time, however, the task is awakened to initiate deadlock searching. More specifically, the alarm handler checks a &#34;lock sleeptask queue&#34; (described below) to see if there is a sleeping task that has waited for at least the &#34;deadlock checking period.&#34; If such a task is found, the task is awakened so that it can perform its own deadlock search. In a preferred embodiment, only a single deadlock search may be conducted at any given time. Therefore, a semaphore (rdlc --  status) is employed to control the process. If a deadlock search is being conducted by a first task, a second task may have to wait longer than the &#34;deadlock checking period&#34; period before initiating its own deadlock search. 
     In the present invention, the deadlock search is accomplished by examining the above-described locking structures--lock objects, semawaits, and lock records (as well as other data structures)--for identifying sleeping tasks that may be part of a cycle. This task is complicated by the fact that locks are managed in parallel: because multiple spin locks are employed on the Lock Manager data structures, the appropriate spin lock must be grabbed before the deadlock search can extract the necessary information from a particular hash bucket. To ensure that performance is not hampered, the system of the present invention preferably implements deadlock searches by repeatedly grabbing and releasing locks on hash buckets without holding more than one hash bucket spin lock at any given time. Specifically, during the deadlock search, the task performing the search first grabs the spin lock on the hash bucket for the first lock investigated in the search. Then after retrieving and recording the necessary information from data structures in that hash bucket, the Lock Manager releases the spin lock and takes out the spin lock on the hash bucket holding information about the next lock in the chain. After the relevant information is extracted from the second hash bucket, its spin lock is released, and so forth and so on. 
     Because the Lock Manager employs multiple spin locks when traversing Lock Manager data structures (namely, the hash buckets and their chains), deadlock detection should account for changes in lock status during the course of the search. Note that the various spin locks on the hash buckets are not frozen during the search so locks may be released and/or granted after the search is started but before it ends. Thus, deadlock searching itself is are dynamic--occurring while the system itself continues to operate. As a result, care must be taken to ensure that the data structures relied upon remain consistent. In contrast, if a single spin lock is provided for the entire Lock Manager, as was the case in with prior art systems, no data structures within the Lock Manager can change during the course of the deadlock search. That approach is simpler, but systems implementing that approach are effectively frozen while deadlock detection is underway. 
     Although deadlock searching in the system of the present invention occurs while the certain Lock Manager data structures are changing, the deadlock itself (i.e., the data structures associated therein) will not change. This is a unique property which can be exploited for providing improved deadlock detection methodology of the present invention. Specifically, if data structures change, then there is no deadlock and, hence, no need to continue the search. 
     3. Deadlock detection: dependency graph changes 
     The search algorithm detects deadlocks by forming a &#34;directed dependency graph&#34; (such as that shown in FIG. 5B) among tasks waiting for locks. In a directed dependency graph, each node is labeled by a task which is waiting for a lock. Consider the directed dependency graph 601, shown in FIG. 6A, at the point when T3 (third task) is adding the L3 (lock request) edge. The sequence of events is indicated by a sequence numbers (SN). Here, T1 (lock request L1) is waiting on T2 which, in turn, waits on T3 (lock request L2). Now, T3 adds the L3 &#34;edge&#34; (i.e., it wants the L3 lock which T4 holds). 
     In this example, task T1 would initiate the deadlock search, since it has waited longer. T1 holds a hash bucket spin lock for seeing what T1 is waiting on (specifically, what semawait). Here, T1 is waiting on T2. This information is recorded with a timestamp, whereupon the spin lock for the bucket is dropped. Now, the search examines T2, under spin lock protection (i.e., a different spin lock), to determine what it is waiting on. Again, the information is recorded with a timestamp, whereupon the spin lock for the T2 hash bucket is dropped. 
     Note that the sequence repeats: grabbing spin lock, recording information, and dropping spin lock for traversing the chain. Because spin locks are not held at the point of traversing from on member of the chain to another, the state of the chain can change--the data structures are potentially changing on-the-fly--while deadlock detection is underway. This is not a problem, however. Recall the unique property of a deadlock that it will not change. If anything is changing on-the-fly, then it is not a deadlock. 
     4. Spurious deadlocks 
     As noted, the search algorithm traverses lock object, semawait, and lock record data structures in appropriate hash buckets to generate the dependency graph. The system must detect deadlocks and avoid detection of spurious deadlocks. A spurious deadlock includes an apparent &#34;edge&#34; which has been recorded but which no longer exists as a result of on-the-fly changes to data structures. Since such a cycle is spurious, it must be distinguished from a true deadlock. 
     FIG. 6A illustrates how a spurious deadlock (caused by a cycle formed after a search was initiated) may arise and be recognized. Recall that, in FIG. 6A, four tasks are sleeping. The order in which the tasks went to sleep is indicated by the sequence number; &#34;sn=0&#34; representing the first sleep event, &#34;sn=1&#34; representing the next sleep event, and so forth and so on. 
     At a given point, task T1--the longest waiting task, will be awakened by the alarm routine for beginning a deadlock search. Suppose that, while the search is being conducted, task T2 releases the lock on which task T3 was waiting: edges L2 and L3 are now eliminated. Assume, however, that the deadlock search does not recognize this--possibly because it has already traversed edge L2. Now assume further that while the search is progressing, task T3 goes to sleep waiting on task T1 to release a lock. At that instant, the dependency graph would appear as shown in FIG. 6B. However, because the search traversed edge L2 before it was eliminated, the system may be lead to believe that the dependency graph appears as shown in FIG. 6C, with the dashed line for L2 assumed to exist. Thus, because the deadlock search is being conducted on a dynamic system, the searching task may believe that a deadlock exists when in fact none does. 
     To reduce the risk of detecting spurious deadlocks, the search algorithm preferably restricts its traversal to only those dependency edges that were already formed when the search started. The search is limited to those edges having &#34;sequence numbers&#34; less than or equal to the sequence number in existence for the task when the search began. Each number is an incrementally-increasing number assigned to a task which has to wait for a lock. During the search, the system uses the lock wait sequence number on the lock sleeptask queue to detect and discard edges that were formed after the search started. When performing deadlock detection, any edge the task is traversing which has a sequence number which is less than or equal that for the task will be examined. If the sequence number is greater than that for task, a new edge has formed and, thus, deadlock search need not be continued. Any new cycles formed from new edges may be detected by later searches (initiated after the cycles are formed). For the case of FIG. 6C, the search would disregard edge L7 having a sequence number of 3 because that edge was created after the search began. Specifically, when the search begins, the initiating task grabs the spin lock on the sleeptask queue and identifies the sequence number at that time. With this number, it can decide whether to record or ignore edges it encounters in its search. 
     5. Innocent bystander deadlock handling 
     FIG. 7 illustrates how the deadlock search method treats a cycle in which the initiating task does not part of the cycle. Initially, a task T1 queues a lock request L1 but is blocked because of a shared lock held by two tasks T2 and T5. It detects no deadlock when it traverses the edge (L1) to T5; however, it does identify a cycle when it traverses to task T4 through a task T3. At this point, the dependency graph contains a path in T1, T2, T3, and T4, with a cycle. Note, however, that T1 is not participating in the cycle and, thus, can be ignored. Nevertheless, the detection method does identify this as an &#34;innocent bystander&#34; deadlock. In a preferred embodiment, the search method does not try to break these types of deadlocks because of the expense involved. To choose an optimal set of victims, the method would have to record all the cycles it detects. Then it would have to select victims in a way that breaks all cycles, but leaves the least number of tasks victimized. 
     6. Multiple cycle deadlock handling 
     A task initiating a deadlock search can be involved in not just one cycle but in multiple cycles. This situation is illustrated in FIG. 8. As shown, the detection method detects a first cycle involving tasks T1, T2, T3, T4 and lock requests L1, L2, L3, and L4. It chooses one of the tasks, T1, T2, T3, and T4 as a possible victim, and the selection is recorded. The algorithm continues and eventually traverses edge L5, detecting the cycle involving T1, T2, T5, and T6 and lock requests L1, L5, L6, and L7. In a preferred embodiment, the system instead automatically rolls back the task initiating a deadlock search when that search determines that the initiating task forms part of two different cycles. Here, the detection method chooses T1 as the victim to minimize the number of deadlock victims selected. 
     7. Limiting deadlock searching 
     To limit the time that may be required to undertake a deadlock search, the system set a limit on the maximum number of nesting (recursions) performed in any search. In one embodiment, a variable &#34;recursion level&#34; is zero when the search algorithm begins and is incremented for each new level of recursion. If the value of the recursion level variable exceeds a preset maximum, the task initiating the search is chosen as the deadlock victim. 
     E. Improved &#34;free&#34; lock management: caching &#34;free&#34; locks 
     The actual locks themselves represent resources in the system represented internally by various underlying lock data structures. Each lock is either &#34;free&#34; (i.e., available for use) or &#34;allocated&#34; (i.e., in use). These data structures are maintained on a global &#34;freelock&#34; list in shared memory and which is available to all server engines. Lock requests are satisfied, in part, by allocating lock data structures from this list. The approach of a single global freelock list presents a bottleneck--a single point of contention, however. More particularly, many of the performance gains of parallel lock management would remain unrealized if a bottleneck elsewhere in the process remained uncorrected. The present invention addresses this problem by providing improved methodology for global freelist management. 
     Contention for the global freelock list is reduced by reducing the number of accesses to it. According to the present invention, each engine now has its own list of freelock structures, known as the &#34;engine freelock cache.&#34; Specifically, the Lock Manager 274 implements engine-specific freelock lists, one per engine, in addition to the server&#39;s global freelock list. The general operation is as follows. When a task executing on a particular engine requires a lock structure, the task &#34;grabs&#34; the structure from its engine&#39;s freelock cache. If no more freelock structures are available to the task (i.e., its engine&#39;s freelock list is exhausted), a (user configurable) number of lock are &#34;moved&#34; (logically) from the global freelock list to the engine&#39;s freelock cache. Actual access to this global freelock list for this operation (i.e., for logically moving freelock structures to and from the individual engine caches) occurs under the control of the abovementioned single freelock list spin lock; this spin lock is the LOCK --  FREELIST --  SPIN spin lock. When a task releases a lock structure, the structure is returned to the engine&#39;s freelock cache. To ensure better utilization of lock resources (i.e., ensure proper load balancing), freelock structures are moved (logically) back from the engine&#39;s freelock cached to the global freelock list when the number of structures or elements in the engine freelock cache exceeds a configurable threshold. Since this operation touches the global freelock list, it also occurs under spin lock protection. 
     Actual &#34;movement&#34; of locks is logical not physical. Here, the locks are not physically moved from a global list memory location to the engine-specific freelock caches but, instead, remain in the shared memory location allocated for the global freelock list at start up time. Changes are made to the pointers that link the lock structures together, so that when locks are &#34;moved,&#34; they are no longer referenced as part of a &#34;free&#34; lock on the global freelock list. 
     FIG. 9 diagrammatically illustrates the above-described approach. As shown, a global freelock list 900 containing a global freelock list 910. These are available to the Lock Manager of each engine. As shown, for instance, &#34;free&#34; locks in list 910 are available to engine freelock caches such as cache 920 for engine 0, cache 930 for engine 1, and cache 940 for engine 2. Thus, one group of freelocks 921 has been transferred from list 900 to cache 920, another group of freelocks 931 is transferred to cache 930, yet another group of freelocks 940 is transferred to cache 941, and so forth and so on, up to an arbitrary number of engines. In the preferred embodiment, the actual size of each engine freelock cache and therefore the number of &#34;free&#34; locks which each holds is user configurable. With this approach of binding certain freelocks to particular engine caches, less contention exists for the global freelock list 900. Specifically, if a server engine requires a lock, it first looks to its own freelock cache, without tying up the global freelock list (and contending for its single spin lock). Only when no more freelock structures are left in an engine&#39;s own cache will a configurable number of freelock structures be moved from the global freelock list to the engine&#39;s own freelock cache. 
     F. Tuning lock management: user configurable settings 
     In preferred exemplary embodiments of the present invention, the end user--a database administrator (DBA) or application programmer--can configure the operation of the Lock Manager. The user may effect this by setting certain configuration parameters, for example. Such parameters control, for example, the deadlock search, the spin lock decomposition, and the freelock cache. 
     In some cases, for instance, it may be desirable to allow the user (e.g., system administrator or application programmer) to eliminate the deadlock search mechanism altogether. This might be appropriate when an application is running in which a deadlock is impossible, or at least highly unlikely. Such an application occurs when each task of the application is acting on independent data subsets. By allowing the administrator user to turn off the deadlock search mechanism for these classes of applications, system performance can be improved. 
     Even in those applications requiring deadlock searching, the user can configure the system by specifying when to begin deadlock searching--immediately when a task waits on a lock or at a later point in time (i.e., deferred). Here, the user specifies a &#34;deadlock checking period&#34; time--that is, when the time when system will perform deadlock searching. Upon the user specifying that checking should begin immediately (i.e., delay of 0), the system performs deadlock checking as soon as a task waits on a lock. Here, the system will catch a deadlock immediately, but the approach is computationally more expensive. Accordingly, the default mode of operation is to specify a delay period--deferred mode. 
     With deferred mode, the system does not do deadlock checking at the time of the &#34;sleep&#34; but, instead, defers it to some later point in time. This approach has a particular advantage. If the lock (which the task is waiting on) actually gets granted during the wait, deadlock checking is avoided altogether. In other words, there can be no deadlock since the lock was granted. Thus, the system administrator can, when for appropriate applications, configure the system to be smarter about deadlock checking. 
     Another exemplary user configurable parameter is the spin lock decomposition on the hash tables of the Lock Manager. As described above, the Lock Manager includes one or more spin locks on each of hash table. In one embodiment, the administrator user may configure the actual number of spin locks employed for each hash table. Thus, for example, a database administrator may configure the Lock Manager such that one spin lock is provided for every 100 hash buckets in the database pages, hash table, or every 10 hash buckets in the database pages hash table, and so forth, up to one spin lock for each hash bucket. In this manner, the user can configure the system for increased parallelism, for those environments having resources supporting an increased number of spin locks. 
     Still further, the Lock Manager may be designed such that the end user may specify the size of a &#34;freelock cache.&#34; As noted, all locks provided by the Lock Manager are designated as either free or allocated. In this embodiment, the end user may specify what percentage of the total locks may be designated as freelocks for the database engines. It may also specify how many freelocks are transferred from the global freelock list to a server freelock cache when there are no more freelocks left in a server&#39;s freelock cache. 
     Internal operation 
     A. Lock Manager API 
     In an exemplary embodiment, the Parallel Lock Manager surfaces an application programming interface (API) to its internal clients (i.e., other modules within the database server). For purposes of implementing the present invention, the Lock Manager API preferably remains unchanged--that is, the present invention is implemented in a fashion which does not disrupt the interface to the Lock Manager. Instead, the internal operation of the Lock Manager--apart from the API--is modified for incorporating method steps of the present invention. 
     In the presently preferred embodiment, the Lock Manager in the system of the present invention supports the following API. 
     
         __________________________________________________________________________
 1:                                                                       
  /*                                                                      
 2:                                                                       
  External API&#39;s of Lock Manager:                                         
 3:                                                                       
 4:                                                                       
  */                                                                      
 5:                                                                       
 6:                                                                       
  int                                                                     
 7:                                                                       
  lock.sub.-- address(int op, int locktype, char * address,               
 8:                                                                       
  LOCKHEAD * ctxchain, LOCKREC ** lockrecp);                              
10:                                                                       
  int                                                                     
11:                                                                       
  lock.sub.-- logical(int op, int locktype, short dbid, objid.sub.-- t    
  objid,                                                                  
12:                                                                       
  pgid.sub.-- t pageno, LOCKHEAD * ctxchain, int suffclass,               
13: LOCKREC ** lockrecp);                                                 
14:                                                                       
15:                                                                       
  lock.sub.-- multiple(LOCKREQUEST reqarray  !, int nlocks);              
16:                                                                       
17:                                                                       
  int                                                                     
18:                                                                       
  lock.sub.-- psema(SYB.sub.-- SEMAPHORE * semaphore, SEMAWAIT *          
  semawait,                                                               
19: LOCKREC ** lockrecp, int flags);                                      
20:                                                                       
21:                                                                       
  void                                                                    
22:                                                                       
  lock.sub.-- vsema(SYB.sub.-- SEMAPHORE * semaphore, SEMAWAIT *          
  semawait);                                                              
23:                                                                       
24:                                                                       
  void /*not called directly, lock.sub.-- ctxchain.sub.-- release( ) used 
  instead*/                                                               
25:                                                                       
  lm.sub.-- internal.sub.-- ctxchain.sub.-- release(LOCKHEAD * ctx);      
26:                                                                       
27:                                                                       
  void                                                                    
28:                                                                       
  lock.sub.-- release(LOCKREC * lockrec);                                 
29:                                                                       
30:                                                                       
  int                                                                     
31:                                                                       
  lock.sub.-- wait(LOCKREC * lockrec);                                    
__________________________________________________________________________
 
    
     The first API call, lock --  address at lines 6-8, comprises the API call to &#34;grab&#34; an address lock. The address lock returned is added to a context chain (ctxchain), which comprises a chain of locks which have been queued up. The lock --  address API function would be called, for instance, by an internal client which desires to lock an index page in memory. 
     The next API call, lock --  logical at lines 10-13, serves to acquire a logical lock. A logical lock, which can be employed to lock a page or a table, can be held for the duration of a transaction. An address lock, in contrast, is typically held for only a brief period of time; it is not held for the duration of a transaction. 
     The lock --  multiple API call, at line 15, serves to process an array of lock (logical) requests. In operation, lock --  multiple calls lock --  logical repeatedly, for processing the lock requests. The lock --  multiple function returns all the requested locks (together), sleeping if need be for obtaining the requested locks. 
     The lock --  psema API call, at lines 17-19, is employed for a special locking purpose: locking the log page (i.e., the last page of the log). The lock --  psema function serves to hold a lock briefly--releasing the held lock at the end of the log operation. The lock --  vsema call, at lines 21-22, functions in a similar manner. 
     The lm --  internal --  ctxchain --  release API call, at lines 24-25, serves to release locks which have been queued up in the context chain. The API call is invoked indirectly through lock --  ctxchain --  release. The lock --  release API call, at lines 27-28, serves simply to release a lock. The particular lock being released is indicated via a lock record which is passed in as a parameter to the function. 
     On occasion, a requested lock might not be available for granting. Accordingly, the Lock Manager also includes a lock --  wait API call, as shown at lines 30-31. In operation, the API call waits on the lock (indicated by the passed in lock record) until the lock is granted. If a client does not receive a lock which it has requested, the client undertakes responsibility for calling lock --  wait, for waiting for the lock to be granted. As described in further detail below, only when the system is waiting on a lock will it undertake deadlock searching. In such a case, the Lock Manager invokes the internal function lock --  perform --  deadlock --  search (described below) which performs deadlock searching. 
     B. Lock Manager data structures 
     Before describing operation of the Lock Manager API functions in further detail, it is first helpful to describe data structures which are employed by the functions. A first data structure, op (i.e., operation), serves as a flag for indicating a type of lock operation required. This data structure may be constructed simply as an enumerated (integer) type as follows. 
     
         ______________________________________                                    
/*                                                                        
 Flag parameter values for op.                                            
 Passed to lock.sub.-- logical, lock.sub.-- address, and lock.sub.--      
psema                                                                     
*/                                                                        
# define LOCK.sub.-- ACQUIRE                                              
                0x0                                                       
# define LOCK.sub.-- NOWAIT                                               
                0x1                                                       
# define LOCK.sub.-- RELEASE                                              
                0x2                                                       
# define LOCK.sub.-- VERIFY                                               
                0x4                                                       
# define LOCK.sub.-- NOQUEUE                                              
                0x10                                                      
______________________________________                                    
 
    
     LOCK --  ACQUIRE is a flag indicating that the operation desired is to acquire a lock. LOCK --  NOWAIT is a flag also indicating that a lock should be acquired; however, the flag also indicates that the system should not wait for a lock to be acquired. LOCK --  RELEASE is a flag indicating that the operation desired is to release the lock. The LOCK --  VERIFY flag indicates that the system should verify a lock--that is, see whether a valid lock exists. LOCK --  NOQUEUE is a flag indicating that a lock should be acquired; however, the flag also indicates that if the lock cannot be granted, the request for the lock should not be queued. LOCK --  NOWAIT, in contrast, does queue the lock (but does not wait for it to be granted). LOCK --  NOQUEUE, on the other hand, indicates that the client does not want the lock if it cannot be obtained immediately. 
     Different types of locks are available in the system. For logical locks, seven types are provided as follows. 
     
         ______________________________________                                    
        /* Lock types for lock.sub.-- logical */                          
        # define EX.sub.-- TAB                                            
                      1                                                   
        # define SH.sub.-- TAB                                            
                      2                                                   
        # define EX.sub.-- INT                                            
                      3                                                   
        # define SH.sub.-- INT                                            
                      4                                                   
        # define EX.sub.-- PAGE                                           
                      5                                                   
        # define SH.sub.-- PAGE                                           
                      6                                                   
        # define UP.sub.-- PAGE                                           
                      7                                                   
        # define NLOCKTYPES                                               
                      7                                                   
______________________________________                                    
 
    
     The first two lock types, EX --  TAB and SH --  TAB, indicate an exclusive table lock and a shared table lock, respectively. The next two lock types, EX --  INT and SH --  INT, indicate an exclusive intent lock and a shared intent lock, respectively. An &#34;intent&#34; lock is employed in the context of acquiring page locks. Here, the client employs the &#34;intent&#34; lock to make sure that the entire table is not locked. In other words, the client employs an intent lock on a table when the client is holding page locks in that table. The page-level locks, EX --  PAGE, SH --  PAGE, and UP --  PAGE, represent an exclusive page lock, a shared page lock, and an update page lock, respectively. 
     For address locks, the following types are provided. 
     
         ______________________________________                                    
        /* Lock types for address locks */                                
        # define EX.sub.-- ADDR                                           
                      8                                                   
        # define SH ADDR                                                  
                      9                                                   
______________________________________                                    
 
    
     As shown, two types of locks are provided: an exclusive address lock (EX --  ADDR) and a shared address lock (SH --  ADDR). The shared address lock is provided so that multiple readers of an object, such as multiple readers scanning the pages of an index, can do so in a shared fashion, thereby avoiding the need for the readers to serialize one after another. 
     The actual lock is represented by a lock record, LOCKREC. In an exemplary embodiment, it may be constructed as follows. 
     
         ______________________________________                                    
/*                                                                        
** LOCKREC                                                                
**                                                                        
** A lockrec identifies a task waiting for a semaphore, and facilitates   
** backout by being linked onto the task&#39;s global data structures.        
**                                                                        
** NOTE: lockobjs, semawaits, lockrecs, and freelocks are all the         
** same size so they can be freed to the same free list.                  
*/                                                                        
struct lockrec                                                            
 /* lrprnext and lrprprev must be first to match up with lockhead */      
 struct lockrec  *lrprprev;  /* prev lock on sdes, xdes, or pss */        
 struct lockrec  *lrprnext;  /* next lock on sdes, xdes, or pss */        
 struct spinlock  *lrspinlock; /* spinlock for this hash bucket */        
 /* lrnext and lrprev must be next to match up with semawait */           
 struct lockrec  *lrprev;  /* prev lock on semawait */                    
 struct lockrec  *lrnext;  /* next lock on semawait */                    
 spid.sub.-- t                                                            
        lrspid;  /* spid of task holding or waiting */                    
                 /* spid = server process ID */                           
 BYTE   lrtype;  /* lock type for logical locks */                        
 BYTE   lrstatus;                                                         
                 /* lock flags */                                         
 int16  lrsuffclass;                                                      
                 /* sufficiency class */                                  
0x6c72 */rmagic; /* &#34;lr&#34;                                                  
 struct semawait *lrsemawait;  /* semawait for these locks */             
};                                                                        
______________________________________                                    
 
    
     As shown, the lock record includes a &#34;previous&#34; pointer (lrprprev) and a &#34;next&#34; pointer (lrprnext), for establishing a linked list of lock records--the &#34;context chain&#34;. The context chain exists on a per task basis: given a task, the chain indicates the locks which the task holds. The next set of pointers, lrprev and lrnext, link the lock record to the log manager data structures (i.e., a global representation of the lock). A spin lock for the hash bucket is provided by lrspinlock. This is a new data member which is added for supporting parallel lock management. An identifier representing the task holding or waiting for the lock is stored by lrspid. The next data member, lrtype, stores a lock type for logical locks (e.g., EX --  TAB, SH --  TAB, EX --  INT, SH --  INT, and the like). 
     The lrstatus data member stores status flags for the lock. Exemplary flags include the following. 
     
         __________________________________________________________________________
/* Values for `lrflags`. */                                               
# define LR.sub.-- NOTWAITING  0x01                                       
                /* caller used LOCK.sub.-- NOWAIT and lock                
                ** manager returned LOCK.sub.-- DIDNTWAIT.                
                */                                                        
# define LR.sub.-- GRANTED  0x02                                          
                /* lockrec is or was at the head                          
                ** of the semaphore queue.                                
                */                                                        
# define LR.sub.-- MOVED  0x04                                            
                /* lockrec was moved to another                           
                ** semawait in lock.sub.-- requeue( ).                    
                */                                                        
# define LR.sub.-- UPGRADE  0x08                                          
                /* lockrec caused an upgrade */                           
# define LR.sub.-- REQUEUE  0x010                                         
                /* lockrec could cause a reque */                         
# define LR.sub.-- UPGRDDLK  0x020                                        
                /* lockrec caused an upgrade deadlock */                  
__________________________________________________________________________
 
    
     The LR --  NOTWAITING flag indicates that the caller is not waiting (e.g., called with LOCK --  NOWAIT). The LR --  GRANTED flag indicates that the lock has been granted--the lock record is (or was) at the head of the semaphore queue. The LR --  MOVED flag indicates that the lock record (request) was moved (i.e., to another semawait). This indicates that a task went to sleep on a particular semawait but some other task requeued the lock record (request), moving from one semawait to another. Similarly, LR --  REQUEUE indicates that a lock record (request) went to sleep on a particular semawait but awoke on another. Here, the &#34;semawait&#34; indicates the position in the queue or line (for granting the lock request). LR --  UPGRADE indicates an upgrade of the lock request (to a different level of granularity). The LR --  UPGRDDLK flag indicates that the upgrade caused a deadlock. 
     The next data member of the lock record, lrsuffclass, stores information about the sufficiency of the lock, that is, whether future lock requests for the same task could be covered by the current locks. In other words, this information indicates whether the locks currently held cover the incoming lock request. The lrmagic data member serves as an integrity check for the record. The system can test this value for determining whether the lock record has been corrupted. 
     The lrsemawait data member stores a pointer to the semawait which can hold multiple lock requests. Recall from the lrprev and lrnext pointers that a lock record can participate on a semawait chain (of lock requests). 
     C. Lock Manager operation 
     Operation of the Lock Manager is perhaps best illustrated by example, such as processing of a client request for an address lock. The following example will focus on a single client which takes out a lock on an index page in memory (i.e., an address lock), with particular emphasis on modifications made to the Lock Manager for implementing parallel lock management. Here, one or more index pages had been read from disk into system memory. In order to gain access to one of those pages in memory, the client takes out an address lock. 
     For this example, the caller or &#34;client&#34; is getindexpage, a routine for locking an index page. The caller, shown in skeletonized form (to focus on the Lock Manager API call), is as follows. 
     
         __________________________________________________________________________
 1: /*                                                                    
 2: **                                                                    
    GETINDEXPAGE                                                          
 3: **                                                                    
 4: **                                                                    
    Traverse an index from parent to the child page given in              
 5: **                                                                    
    sdes--&gt;scur. Return a buffer pointer to the kept, rlocked child.      
 6: **                                                                    
    This routine is the only way to safely traverse from parent to        
 7: **                                                                    
    child in an index. It has a symbiotic relationship with its only      
 8: **                                                                    
    caller, srchindex. Getindexpage implements two searching methods      
 9: **                                                                    
    based on the ladderlock flag.                                         
10: **                                                                    
11: **                                                                    
    If `ladderlock` is TRUE, hold the rlock on the parent while           
12: **                                                                    
    reading and rlocking the child. This guarantees the identity of       
13: **                                                                    
    the child (because the parent must be updated to split or shink       
14: **                                                                    
    the child) , and ensures that progress will be made, but reduces      
15: **                                                                    
    index concurrency. With ladder locking, both parent and child         
16: **                                                                    
    pages are kept and rlocked on return.                                 
17: **                                                                    
    If `ladderlock` is FALSE, drop the rlock on the parent before         
18: **                                                                    
    rlocking the child, and verify the child&#39;s identity by                
19: **                                                                    
    rechecking the parent&#39;s timestamp (the parent would have to have      
20: **                                                                    
    changed for the child&#39;s identity to have changed). This scheme        
21: **                                                                    
    increases index concurrency because only one rlock is held at a       
22: **                                                                    
    time. However, in an index with many splits and/or shrinks, a         
23: **                                                                    
    search using this method may never make progress. With this           
24: **                                                                    
    method the child is returned kept and rlocked while parent is         
25: **                                                                    
    unrlocked and unkept.                                                 
26: **                                                                    
    For either case of timestamp locking or ladder locking, if            
27: **                                                                    
    a deadlock is detected then we will tolerate this deadlock by:        
28: **                                                                    
29: **                                                                    
    1. If ladder locking then release the parent rlock and wait           
30: **                                                                    
     on the child rlock. When get the child rlock, release                
31: **                                                                    
     it and return failure to start index search over again.              
32: **                                                                    
33: **                                                                    
    2. If timestamp locking then parent has already been released         
34: **                                                                    
     so simple wait on the child and when get it release it and           
35: **                                                                    
     return failure and start the index search over again.                
36: **                                                                    
37: **                                                                    
    We wait on the child page and immediately release it to prevent       
38: **                                                                    
    thrashing on the index structure.                                     
39: **                                                                    
40: **                                                                    
    PARAMETERS:                                                           
41: **                                                                    
    srchlock.sub.-- info -                                                
           Index lock structure holding relevant lock                     
42: **     info                                                           
43: **                                                                    
    RETURNS                                                               
44: **                                                                    
    Pointer to kept buffer for child page, or NULL if the desired         
45: **                                                                    
    page could not be obtained for some reason. If NULL is                
46: **                                                                    
    returned, then sdes--&gt;sridoff holds:                                  
47: **                                                                    
Child may have changed identity;                                          
48: **                                                                    
        caller can restart search.                                        
49: **                                                                    
Deadlock obtaining child rlock.                                           
50: **                                                                    
51: **                                                                    
    SIDE EFFECTS                                                          
52: **                                                                    
    If ladder locking, both parent and child are kept and rlocked         
53: **                                                                    
    on return. If not ladder locking, parent is unrlocked and             
54: **                                                                    
    unkept, and child is rlocked and kept. On error, both parent          
55: **                                                                    
    and child are unrlocked and unkept.                                   
56: **                                                                    
    Ex.sub.-- raise if a page belonging to the wrong object is arrived    
    at.                                                                   
57: **                                                                    
    Ex.sub.-- raise if attention condition raised.                        
58: **                                                                    
59: **                                                                    
    MP SYNCHRONIZATION:                                                   
60: **                                                                    
    The search methods rely on the way that indexes are changed in        
61: **                                                                    
    order to guarantee that the search is not misdirected to the          
62: **                                                                    
    wrong page. Both methods rely on rlocks being obtained on all         
63: **                                                                    
    pages involved in a split or shrink and being held until the          
64: **                                                                    
    split or shrink is complete. The timestamp method requires that       
65: **                                                                    
    the page timestamp changes be made while the rlocks are held.         
66: **                                                                    
67: */                                                                    
68: BUF *                                                                 
69: getindexpage(INDEXLOCK * srchlock.sub.-- info)                        
70: {                                                                     
71:                                                                       
72: /*                                                                    
    . . . do setup and initialization */                                  
73:                                                                       
74: /*                                                                    
    . . . check the lock mode (timestamp or ladder locking) */            
75:                                                                       
76: /*                                                                    
    get the page in memory */                                             
77:                                                                       
   /*                                                                     
78:                                                                       
   ** We do not want a DEADLOCK error to make it back to the client       
79:                                                                       
   ** here as we will retry the RLOCK( ) below. Update bp in              
80:                                                                       
   ** srchlock.sub.-- info, so we can cleanup our resources held.         
81:                                                                       
   */                                                                     
82:                                                                       
   bp = getpage(sdes);                                                    
83:                                                                       
   *srchlock.sub.-- info--&gt;bp = bp;                                       
84:                                                                       
   /*                                                                     
85:                                                                       
   acquire the appropriate address lock on the page                       
86:                                                                       
   (shared or exclusive);                                                 
87:                                                                       
   */                                                                     
88:                                                                       
   if (ladderlock)                                                        
89:                                                                       
    lockstat = RLOCK.sub.-- EXCLUSIVE(bp, sdes, srchlock.sub.-- info--&gt;bpl
   ock);                                                                  
90:                                                                       
   else                                                                   
91:                                                                       
    lockstat = RLOCK.sub.-- SHARE(bp, sdes, srchlock.sub.-- info--&gt;bplock)
   ;                                                                      
92:                                                                       
93:                                                                       
   /* . . . */                                                            
94: }                                                                     
__________________________________________________________________________
 
    
     At line 72, the routine performs general setup and initialization steps. Then, at line 74, it checks the lock mode, which specifies how a lock is released when traversing from parent to child page. At lines 76-83, the actual page is retrieved in memory, by a call to getpage. At lines 85-91, the Lock Manager is invoked for obtaining an appropriate address lock on the page (either shared or exclusive). 
     As shown at line 89, the actual call to the Lock Manager is made through a macro, RLOCK --  EXCLUSIVE, for ladder locking; otherwise, the call to the Lock Manager is made through the RLOCK --  SHARE macro at line 91 (for timestamp locking). These macros, in turn, expand out into calls to the Lock Manager&#39;s lock --  address API function, as shown by the following macro definitions. 
     # define RLOCK --  EXCLUSIVE (bp, sdes, lockrecp)\lock --  address (LOCK --  ACQUIRE, EX --  ADDR, (char *) bp, &amp;sdes→slocks, lockrecp) 
     # define RLOCK --  SHARE (bp, sdes, lockrecp)\lock --  address (LOCK --  ACQUIRE, SH --  ADDR, (char *) bp, &amp;sdes→slocks, lockrecp) 
     For a ladder locking traversal of the index pages, therefore, an exclusive lock is requested by the getindexpage function as follows: 
     lock --  address (LOCK --  ACQUIRE, EX --  ADDR, (char *) bp, &amp;sdes→slocks, lockrecp) 
     As shown, the first parameter to the lock --  address call specifies a LOCK --  ACQUIRE operation. The second parameter indicates the type of lock required, here, an exclusive address lock (EX --  ADDR). The address for which the lock is required, the third parameter, is simply the address in memory (i.e., pointer to the index page; here, bp--a buffer). The fourth parameter, slocks, specifies a context chain which the client provides (via the session descriptor, sdes). Here, the session descriptor or sdes includes a slocks member, for storing the address of the lock context chain (i.e., the linked list of lock records). 
     The lock --  address function serves to acquire a lock on a data server address. Typically, the address will correspond to a data structure associated with an object, such as a buffer header. Address locks can be requested in shared or exclusive lock types. Compatibility for these lock types is represented by the following table. 
     
         ______________________________________                                    
            EX.sub.-- ADDR                                                
                    SH.sub.-- ADDR                                        
______________________________________                                    
FX.sub.-- ADDR                                                            
              No        No                                                
SH.sub.-- ADDR                                                            
              No        Yes                                               
______________________________________                                    
 
    
     As shown, the only compatibility which exists is between two shared address locks. Address locks are granted in the order that they are requested, thereby avoiding &#34;starvation&#34; of exclusive address requests. If there exists multiple readers, for instance, no reader bypasses an exclusive access. This approach leads to increased efficiency of the code (which itself is executed frequently). 
     In an exemplary embodiment, the lock --  address function may be constructed as follows. 
     
         __________________________________________________________________________
1:  /*                                                                    
2:  ** LOCK.sub.-- ADDRESS                                                
3:  **                                                                    
4:  ** Acquire a lock ona dataserver address.                             
5:  ** Generally the address will correspond to a                         
6:  ** datastructure associated with an object, such as                   
7:  ** a buffer header.                                                   
8:  ** Address locks available in shared or exclusive lock types.         
9:  **                                                                    
10: ** Compatibility table for these two lock types.                      
11: **                                                                    
12: **      EX.sub.-- ADDR  SH.sub.-- ADDR                                
13: ** 14: ** 15: ** 16: ** 17: **                                        
     ##STR1##                                                             
18: **                                                                    
19: **  Address locks are granted in the order that they are              
20: **  requested. This avoids starvation of EX.sub.-- ADDR requests.     
21: **                                                                    
22: ** Parameters:                                                        
23: **                                                                    
24: ** op   Operation to perform. The following operations are            
25: **  supported:                                                        
26: **                                                                    
27: **  LOCK.sub.-- ACQUIRE  Acquire a lock on the given address.         
28: **                                                                    
29: **  The following value can be or&#39;d with LOCK.sub.-- ACQUIRE:         
30: **                                                                    
31: **  LOCK.sub.-- NOWAIT  Do not sleep if the lock cannot be            
32: **   granted immediately. The caller must then call                   
33: **   lock.sub.-- wait to wait for it.                                 
34: **                                                                    
35: **  locktype  The type of this address lock request.                  
36: **      EX.sub.-- ADDR or SH.sub.-- ADDR.                             
37: **                                                                    
38: ** address   An address to lock. Subsequent lockers of this           
39: **   address will have to wait.                                       
40: **                                                                    
41: ** ctxchain   The context chain to which to link the lock.            
42: **                                                                    
43: ** lockrecp  A pointer to a pointer to a lockrec, or NULL. If         
44: **   it is non-null, lock.sub.-- address will fill it in with a       
45: **   pointer to the lock of interest. This may then *only* be         
46: **   passed to lock.sub.-- wait or lock.sub.-- release.               
47: **                                                                    
48: ** Returns:                                                           
49: **                                                                    
50: ** One of these status values is returned:                            
51: **                                                                    
52:                                                                       
   ** LOCK.sub.-- GRANTED                                                 
                (+) The lock was granted immediately.                     
53:                                                                       
   ** LOCK.sub.-- WAITED                                                  
                (+) The caller&#39;s task had to wait for the lock            
54:                                                                       
   **    to be granted. The lock WAS granted.                             
55:                                                                       
   ** LOCK.sub.-- DIDNTWAIT                                               
                (-) The caller would have had to wait, but                
56:                                                                       
   **    LOCK.sub.-- NOWAIT was specified. Lockrecp was filled            
57:                                                                       
   **    in with the lockrec to wait for later.                           
58:                                                                       
   ** LOCK.sub.-- DEADLOCK (-) The caller was selected as a deadlock      
   victim.                                                                
59:                                                                       
   ** LOCK.sub.-- INTERRUPTED (-) The lock was not granted because the    
   task                                                                   
60:                                                                       
   **    received an attention condition while                            
61:                                                                       
   **    waiting for it.                                                  
62:                                                                       
   **                                                                     
63:                                                                       
   ** MP Synchronization:                                                 
64:                                                                       
   ** No explicit synchronization required of the caller. Obtains         
65:                                                                       
   ** the spinlock from lock address hash bucket to protect lock          
66:                                                                       
   **  states and chains.                                                 
67:                                                                       
   **                                                                     
68:                                                                       
   ** Side Effects:                                                       
69:                                                                       
   ** Before we ex.sub.-- raise an error in this routine we will turn     
   off                                                                    
70:                                                                       
   ** P2.sub.-- LOCKRETRY status bit in the pss. We don&#39;t have to do the  
71:                                                                       
   **  same for each return as the caller of this routine with this       
72:                                                                       
   ** status bit set will clear out this bit in the pss.                  
73:                                                                       
   ** This status bit should only be used by srchindex().                 
74:                                                                       
   **                                                                     
75:                                                                       
   */                                                                     
76:                                                                       
   int                                                                    
77:                                                                       
   lock.sub.-- address(int op, int locktype, char * address, LOCKHEAD *   
78:                                                                       
    ctxchain, LOCKREC ** lockrecp)                                        
79:                                                                       
   {                                                                      
80:                                                                       
    LOCALPSS(pss);  /* initialize local copy of global pss */             
81:                                                                       
    LOCKOBJ  *bucket;  /* hash table overflow chain pointer */            
82:                                                                       
    LOCKOBJ  *obj;  /* object we&#39;re locking */                            
83:                                                                       
    SEMAWAIT  *sw;  /* semawait cursor */                                 
84:                                                                       
    LOCKREC  *lr;  /* lockrec cursor */                                   
85:                                                                       
    int  ret;  /* return value */                                         
86:                                                                       
    SPINLOCK  *spinlock;  /* spinlock for the hash chain */               
87:                                                                       
88:                                                                       
89:                                                                       
   /*                                                                     
90:                                                                       
   ** Hash the address into the address lock hash table. This can         
91:                                                                       
   ** be done without the spinlock since we are simply doing an           
92:                                                                       
   ** array index.                                                        
93:                                                                       
   */                                                                     
94:                                                                       
95:                                                                       
   bucket = LOCK.sub.-- .sub.-- ADDR.sub.-- HASH(address);                
96:                                                                       
   spinlock = ((HASHLOCKHEAD *) bucket)-&gt;hlhspinlock;                     
97:                                                                       
98:                                                                       
   /*                                                                     
99:                                                                       
   ** Before getting the spinlock for the hash chain, make sure           
100:                                                                      
   ** there this task has enough locks cached. If not, get some           
101:                                                                      
   ** more from the shared free list.                                     
102:                                                                      
   */                                                                     
103:                                                                      
   if (Eresource-&gt;erfreelock.sub.-- cache.fcfreelocknum                   
   &lt; MINLOCKSTRUCTS)                                                      
104:                                                                      
   {                                                                      
105:                                                                      
    lock.sub.-- .sub.-- fillcache();                                      
106:                                                                      
   }                                                                      
107:                                                                      
108:                                                                      
   /*                                                                     
109:                                                                      
   ** Get hash bucket spinlock in order to search chains and              
110:                                                                      
   ** guarantee contents and identity of the data structures. It          
111:                                                                      
   ** won&#39;t be released until this routine returns.                       
112:                                                                      
   */                                                                     
113:                                                                      
114:                                                                      
   P.sub.-- SPINLOCK(spinlock);                                           
115:                                                                      
116:                                                                      
   /*                                                                     
117:                                                                      
   ** Look for the object on the hash overflow chain.                     
118:                                                                      
   */                                                                     
119:                                                                      
120:                                                                      
   for (obj = (LOCKOBJ *) bucket-&gt;lonext; obj |= (LOCKOBJ *) bucket;      
121:                                                                      
    obj = obj-&gt;lonext)                                                    
122:                                                                      
   {                                                                      
123:                                                                      
    if (obj-&gt;loobjidpageno == (long) address)                             
124:                                                                      
    break;                                                                
125:                                                                      
   }                                                                      
126:                                                                      
127:                                                                      
   /*                                                                     
128:                                                                      
   ** Allocate a lockobj if there wasn&#39;t one in the hash                  
129:                                                                      
   ** table already, and link it onto the hash chain.                     
130:                                                                      
   ** This provides a head (semaphore) to chain waiters from.             
131:                                                                      
   */                                                                     
132:                                                                      
   if (obj == (LOCKOBJ *) bucket)                                         
133:                                                                      
   {                                                                      
134:                                                                      
    /* Allocate and link onto hash table. */                              
135:                                                                      
    LOCK.sub.-- .sub.-- ALLOC.sub.-- LOCKOBJ(Eresource-&gt;erfreelock.sub.-- 
   cache, obj);                                                           
136:                                                                      
137:                                                                      
    /* Identify the object. */                                            
138:                                                                      
    obj-&gt;loobjidpageno = (long) address;                                  
139:                                                                      
140:                                                                      
    /* Link to the hash table */                                          
141:                                                                      
    LINK TO HASHTABLE(bucket, obj, spinlock);                             
142:                                                                      
   }                                                                      
143:                                                                      
144:                                                                      
   /*                                                                     
145:                                                                      
   ** Allocate a semawait, initialize it, and link it to                  
146:                                                                      
   ** the tail of the semaphore queue.                                    
147:                                                                      
   ** Allocation of the new semawait will be done when any one of         
148:                                                                      
   ** the following conditions is true:                                   
149:                                                                      
   **                                                                     
150:                                                                      
   ** 1) Request is for an exclusive address lock.                        
151:                                                                      
   ** 2) Request is for a shared address lock but the semawait queue      
for                                                                       
152:                                                                      
   **     the lock object is empty.                                       
153:                                                                      
   ** 3) Request is for a shared address lock but the last semawait in    
154:                                                                      
   **     the semawait queue for the lock object is for an exclusive      
155:                                                                      
   **     address lock.                                                   
156:                                                                      
   **                                                                     
157:                                                                      
   ** Note that condition 3) maintainss the FIFO order of address         
158:                                                                      
   ** lock requests and avoids starvation.                                
159:                                                                      
   **                                                                     
160:                                                                      
   */                                                                     
161:                                                                      
   if ((locktype == EX.sub.-- ADDR) ||                  
162:                                                                      
    ((obj-&gt;losemaphore.smtail == (SEMAWAIT *)obj) ||    
163:                                                                      
    (obj-&gt;losemaphore.smtail-&gt;swlhead-&gt;lrtype == EX.sub.-- ADDR)))        
164:                                                                      
   {                                                                      
165:                                                                      
    LOCK.sub.-- .sub.-- ALLOC.sub.-- SEMAWAIT(Eresource-&gt;erfreelock       
   cache, sw);                                                            
166:                                                                      
    sw-&gt;swstatus = 0;                                                     
167:                                                                      
    sw-&gt;swspinlock = spinlock;                                            
168:                                                                      
    LINK.sub.-- TO SEMAPHORE(&amp;obj-&gt;losemaphore, sw);                      
169:                                                                      
   }                                                                      
170:                                                                      
   else                                                                   
171:                                                                      
   {                                                                      
172:                                                                      
    /*                                                                    
173:                                                                      
    ** This is a shared lock request and one of the following             
174:                                                                      
    ** conditions is true:                                                
175:                                                                      
    ** 1) The last waiter is also a shared lock request.                  
176:                                                                      
    ** 2) All the current owners are shared mode owners and there         
177:                                                                      
    **     is no waiter. Note that there would only be one semawait       
178:                                                                      
    **     in the queue in this case.                                     
179:                                                                      
    ** In either case we do not have to allocate a new semawait. We       
180:                                                                      
    ** can just queue the current request to the last semawait in         
181:                                                                      
    ** the queue.                                                         
182:                                                                      
    */                                                                    
183:                                                                      
    sw = obj-&gt;losemaphore.smtail;                                         
184:                                                                      
   }                                                                      
185:                                                                      
   /* Allocate a lockrec, initialize it,                                  
186:                                                                      
   and link it to the semawait. */                                        
187:                                                                      
   LOCK.sub.-- .sub.-- ALLOC.sub.-- LOCKREC(Eresource-&gt;erfreelock.sub.--  
   cache, lr);                                                            
188:                                                                      
   lr-&gt;lrspid = pss-&gt;pspid;                                               
189:                                                                      
   lr-&gt;lrstatus = 0;                                                      
190:                                                                      
   lr-&gt;lrtype = locktype;                                                 
191:                                                                      
   lr-&gt;lrsuffclass = LOCKSUFFCLASS.sub.-- NONE;                           
192:                                                                      
   LINK.sub.-- TO.sub.-- SEMAWAIT(sw, lr, spinlock);                      
193:                                                                      
194:                                                                      
   /* Sdes&#39;s sometimes get cleared. This would make the context chain     
195:                                                                      
   ** invalid, since it&#39;s supposed to point to itself when empty.         
196:                                                                      
   ** If we detect a cleared-out context chain, presume it&#39;s tneant to    
197:                                                                      
   ** be empty and re-initialize it. Obviously it would be better         
198:                                                                      
   ** to fix sdes management, but that&#39;s not too easy.                    
199:                                                                      
   */                                                                     
200:                                                                      
   if (ctxchain-&gt;lhhead == 0)                                             
201:                                                                      
    MKPSSLKHD( (*ctxchain));                                              
202:                                                                      
203:                                                                      
   /* Link the lock to the context chain */                               
204:                                                                      
   LINK TO.sub.-- LOCKHEAD(ctxchain,.lr);                                 
205:                                                                      
206:                                                                      
   /*                                                                     
207:                                                                      
   ** The lock has been created and found it&#39;s place in line,             
208:                                                                      
   ** now see what to do about it.                                        
209:                                                                      
   */                                                                     
210:                                                                      
211:                                                                      
   /* Lock is at the head of the queue; grant it immediately. */          
212:                                                                      
   if (sw == obj-&gt;losemaphore.smhead)                                     
213:                                                                      
   {                                                                      
214:                                                                      
    if (lockrecp)                                                         
215:                                                                      
    *lockrecp = lr;                                                       
216:                                                                      
    lr-&gt;lrstatus |= LR.sub.-- GRANTED;                           
217:                                                                      
    V.sub.-- SPINLOCK(spinlock);                                          
218:                                                                      
    return(LOCK.sub.-- GRANTED);                                          
219:                                                                      
   }                                                                      
220:                                                                      
221:                                                                      
   /* Caller has to wait but doesn&#39;t want to, at least now. */            
222:                                                                      
   if (op &amp; LOCK.sub.-- NOWAIT)                                           
223:                                                                      
   {                                                                      
224:                                                                      
    if (lockrecp)                                                         
225:                                                                      
    *lockrecp = lr;                                                       
226:                                                                      
    /* Mark the lockrec so we can tell that even though                   
227:                                                                      
    ** the semawait is not at the head of the queue, this task            
228:                                                                      
    ** still won&#39;t be waiting for it (until it calls lock wait)           
229:                                                                      
    */                                                                    
230:                                                                      
    lr-&gt;lrstatus |= LR.sub.-- NOTWAITING;                        
231:                                                                      
    V.sub.-- SPINLOCK(spinlock);                                          
232:                                                                      
    return(LOCK.sub.-- DIDNTWAIT);                                        
233:                                                                      
   }                                                                      
234:                                                                      
235:                                                                      
   /* Otherwise, wait for the lock. Lock.sub.-- semawait                  
236:                                                                      
   ** releases the spinlock.                                              
237:                                                                      
   */                                                                     
238:                                                                      
   ret = lock.sub.-- semawait(sw, lr);                                    
239:                                                                      
240:                                                                      
   if (lockrecp)                                                          
241:                                                                      
    *lockrecp = (ret == LOCK.sub.-- WAITED ? lr : 0);                     
242:                                                                      
243:                                                                      
   return (ret);                                                          
244:                                                                      
   }                                                                      
__________________________________________________________________________
 
    
     The steps of the function are as follows. Local (stack) variables are declared initially, at lines 80-86. At lines 89-96, the function hashes the passed-in address into an address lock hash table. This yields a particular &#34;bucket.&#34; Here, the bucket is protected by a spinlock; at line 96, this spinlock is grabbed. At lines 98-106, the function checks to make sure that the current task has enough locks cached. If not, more locks will be obtained from a shared/global freelist of locks. 
     At lines 106-114, the spinlock is asserted, thereby assuring exclusive access to the hash bucket (for guaranteeing contents and integrity of the data structures). The spinlock will be held until termination of the routine. At lines 116-125, the function looks for the object(s) which hang off the hash bucket (i.e., hash overflow chain). If the address (for the address lock) already exists in the hash bucket, an address lock has already been taken out for that object. If the address is not found there, then the current request is the first lock request for the bucket, for that particular address. At lines 127-142, a lock object is allocated (if one does not already exist in the hash table) and is linked into the hash chain. This provides a head (semaphore) in which &#34;waiters&#34; (i.e., those waiting for lock requests to be granted) can be chained from. The locked object is linked to the hash table using a macro, as shown specifically at line 141. 
     At lines 144-184, the function allocates a semawait, initializes it, and links it to the tail of the semaphore queue. Allocation of the new semawait occurs when: 
     (1) Request is for an exclusive address lock; 
     (2) Request is for a shared address lock but the semawait queue for the lock object is empty; or 
     (3) Request is for a shared address lock but the last semawait in the semawait queue for the lock object is for an exclusive address lock. 
     These conditions are specifically tested at lines 161-163. 
     An exclusive address lock has the property that there will exist only one lock record per semawait. In other words, a semawait will be allocated and an address lock will be allocated. If, on the other hand, the lock type is shared, multiple lock records or objects can be hanging off one semawait. Lines 161-163 specifically test if the locktype is an &#34;exclusive address&#34; type, or there exists no SEMAWAIT in the object (i.e., the object is linked to itself--no other SEMAWAIT is hanging off), or the head is equal to EX --  ADDR. If any of these conditions hold true, then a new SEMAWAIT is allocated (line 165) and linked in (line 168). Otherwise, the function uses the tail semawait. Here, the tail semawait already has a shared address lock and the incoming lock is a shared address lock, which can be queued to the tail. 
     At lines 168-192, a lock record or object is initialized and linked to the semawait. Additionally, the status flag is cleared (line 189); the lock type is set equal to the requested locktype (line 190); and the sufficiency is set equal to none (line 191). Here, only logical locks have a sufficiency class; accordingly, address locks set the sufficiency class variable to none. 
     At lines 194-204, the lock record is linked into the context chain. However if no context chain exists (tested at line 200), a new context chain is allocated (line 201). At lines 211-219, the lock is granted immediately if the function detects that the lock is at the head of the queue. The status flag is upgraded to &#34;granted,&#34; at line 216. The spinlock is released at line 217, and the method returns, at line 218, with the result of LOCK --  GRANTED. If the lock is not granted at this point (i.e., the test at line 212 reveals that the request is not at the head of the queue), then the lock request must wait. 
     How the method terminates at this point depends on whether the caller, when invoking the function, indicated that it wanted to wait for the lock request to be granted. If the caller does not want to wait (i.e., operation is LOCK --  NOWAIT), then the lock record is marked (line 225), the status flag is set to &#34;not waiting&#34; (line 230), the spinlock is released (line 231), and the function returns a result of LOCK --  DIDNTWAIT (line 232). Marking the lock record allows the task to later call lock 13  wait, to wait for the lock. 
     If the caller did not specify a &#34;no wait&#34; operation, then the function will wait for the lock by calling lock --  semawait, at line 238. This represents a common case. At this point, the task sleeps on the lock. When the call to lock --  semawait returns, the return result is stored in a local variable (ret), at line 238. In turn, the result is returned to the caller to lock --  address at line 243, whereupon the function is done. 
     Deadlock searching 
     A. Overview 
     Deadlock search can be initiated either at the time of waiting for a lock, or can be deferred for a later time. Here, deferred deadlock detection represents an optimization in the system of the present invention. In such a case, a lock can be granted without undertaking deadlock detection. The point at which the system waits on a lock is the point where the system decides whether to undertake deadlock detection (i.e., before going to sleep on the lock) or deferred detection until a later time. If the deadlock checking is deferred for a later time then a mechanism is needed to initialize the search at the later time. This is initiated by an alarm handler routine. The frequency of deadlock checking is configurable by adjusting a user-settable deadlock checking period configuration parameter. 
     The search method detects deadlocks by detecting cycles in a directed dependency graph amongst the tasks waiting for locks. In this graph there would be a directed edge from task A to task B if task A was waiting for a lock that task B owns. The dependency graph is not explicitly created by the method. The method actually represents only the nodes in the graph in a list called &#34;sleeping owners list.&#34; The edges are implicitly represented through recursion of the method. 
     The deadlock search method employs a global sequence number and a queue. The global sequence number is called a &#34;lock wait sequence number.&#34; This sequence number is initialized at server start-up time and incremented whenever a task has to wait for a lock. The sequence number is incremented under LOCK --  SLEEPTASK --  SPIN spinlock by the task which has to wait for its lock request. The queue, on the other hand, is called a &#34;lock --  sleeptask queue.&#34; Each task which has to wait for a lock adds an entry to lock --  sleeptask --  queue. Once the lock is granted, the task will remove its entry from this queue. The queue comprises one or more &#34;sleeptask&#34; records. Each record describes the sleeping task. It stores the semawait on which the task is sleeping along with the lock wait sequence number at the time it went to sleep. Lock wait sequence number will change when the task initiates a deadlock search. 
     The access to this queue is serialized by LOCK --  SLEEPTASK --  SPIN spinlock. Each entry in the lock --  sleeptask --  queue has a sequence number field. When a task adds an entry to the sleeptask queue, it stores the current value of the global lock wait sequence number in the sequence number field of the entry. The task then increments the global lock wait sequence number while holding the LOCK --  SLEEPTASK --  SPIN spinlock. An array called dlscurrent --  path is used to record tasks visited in the path from the initiator of the search to the current task. 
     Deadlock search is initiated by the task which has to wait for a lock. The search method proceeds by creating a list of tasks which own this lock and which are waiting for some other lock. This list is the &#34;sleeping owners list.&#34; The search method repeats recursively for each task in the sleeping owners list. A deadlock is detected when the method encounters the initiator of the search as a sleeping owner (at some level of recursion). 
     Spin locks are held only while creating the sleeping owners list. The relevant bucket spinlock is held while traversing the list of granted lockrecs on the head semawait to record all the sleeping owners of the lock. Then LOCK --  SLEEPTASK --  SPIN (spin lock) is held while ascertaining that the recorded owners are indeed sleeping even after the release of the bucket spinlock. 
     Since the deadlock search holds only the spinlock corresponding to one hash bucket at a time, lock operations can proceed on other hash buckets. In other words, the method allows the dependency graph to change while the deadlock search is in progress. As a result, new dependency edges might form and old edges might be deleted as the search is traversing the dependency graph. For correctness of the search, the search method restricts the traversal to dependency edges that were formed since the search started. Any cycles formed because of new edges are detected by later searches initiated by the tasks which created the edges. The sequence number on the lock --  sleeptask --  queue is used to detect and discard edges that were formed after the search started. Any entry added to the queue after the search started has a sequence number greater than the sequence number when the deadlock search started. 
     The deadlock search method does not resolve deadlocks which do not involve the initiator of the search. In the currently preferred embodiment, the method detects up to two cycles involving the initiator. The search is stopped after determining that the initiator is involved in two deadlocks. This is primarily to limit the cost of search. 
     B. Detailed operation of deadlock searching 
     During system operation, a queue of tasks exists which are sleeping on locks. Deadlocks can only happen among tasks which are sleeping. Thus, these tasks are the only candidates for performing deadlock searching. Accordingly, the deadlock search method of the present invention exploits this feature for implementing its approach. 
     1. lock --  semawait 
     At a high level, a given task invokes the lock --  semawait function for waiting until either a semaphore (semawait) is granted or until the task is chosen as a deadlock victim. In an exemplary embodiment, the lock --  semawait function may be constructed as follows. 
     
         __________________________________________________________________________
 1: /*                                                                    
 2: **                                                                    
    LOCK.sub.-- SEMAWAIT                                                  
 3: **                                                                    
 4: **                                                                    
    Wait for a semaphore until it is granted or the current process is    
 5: **                                                                    
    chosen as a deadlock victim.                                          
 6: **                                                                    
 7: **                                                                    
    The caller has determined that the semawait is not at the head of     
 8: **                                                                    
    its semaphore queue. The caller should also be aware that upon        
 9: **                                                                    
    return of this routine the semawait passed in may no longer be        
 10: **                                                                   
    valid. DO NOT USE IT AFTER THIS ROUTINE HAS BEEN CALLED.              
 11: **                                                                   
 12: **                                                                   
    This routine performs either deferred deadlock search or              
 13: **                                                                   
    non-deferred deadlock search. Decision to perform deadlock search     
 14: **                                                                   
    is based on the configuration parameter &#34;deadlock checking            
 15: **                                                                   
    period&#34;. If this is zero, the task attempts to perform deadlock       
 16: **                                                                   
    checking before going to sleep on the lock.                           
 17: **                                                                   
 18: **                                                                   
    Parameters:                                                           
 19: **                                                                   
the semawait to wait for                                                  
 20: **                                                                   
the lockrec to wait for                                                   
 21: **                                                                   
 22: **                                                                   
    Returns:                                                              
 23: **                                                                   
     LOCK.sub.-- WAITED   (+) The caller&#39;s task had to wait for the lock  
    to                                                                    
 24: **                                                                   
        be granted. The lock WAS granted.                                 
 25: **                                                                   
     LOCK.sub.-- DEADLOCK   (-) The caller was selected as a deadlock     
    victim.                                                               
 26: **                                                                   
     LOCK.sub.-- INTERRUPTED (-) The lock was not granted because the     
    task                                                                  
 27: **                                                                   
        received an attention condition while                             
 28: **                                                                   
        waiting for it.                                                   
 29: **                                                                   
 30: **                                                                   
    MP Synchronization:                                                   
 31: **                                                                   
     Assumes caller holds hash bucket spinlock, but this routine          
 32: **                                                                   
     always releases it before returning.                                 
 33: **                                                                   
     This routine acquires LOCK.sub.-- SLEEPTASK.sub.-- SPIN spinlock to  
    add an entry                                                          
 34: **                                                                   
     to the lock sleeptask queue and to increment the lock wait           
 35: **                                                                   
     sequence number.                                                     
 36: **                                                                   
    Side effects:                                                         
 37: **                                                                   
     If the task has to wait, the global lock wait sequence number is     
 38: **                                                                   
     incremented. If the current value of global lock wait sequence       
 39: **                                                                   
     number is unsafe to compare with the oldest sequence number in the   
 40: **                                                                   
     lock sleeptask queue, the lock sleep task queue entries are          
 41: **                                                                   
     reinitialized with the current value. If the task is woken up to     
 42: **                                                                   
     perform deadlock search, it will wake up the next task to perform    
 43: **                                                                   
     deadlock search after finishing its deadlock search. This routine    
 44: **                                                                   
     might cause the current process to schedule out.                     
 45: */                                                                   
 46: int                                                                  
 47: lock.sub.-- semawait(SEMAWAIT * semawait, LOCKREC * lockrec)         
 48: {                                                                    
 49:                                                                      
   LOCALPSS(pss);    /* initialize local copy of global pss */            
 50:                                                                      
   SEMAWAIT  *event = 0;                                                  
 51:                                                                      
   SPINLOCK  *spinlock;                                                   
 52:                                                                      
   circ.sub.-- long lockwait.sub.-- seqno; /* store current value of      
   global seqno */                                                        
 53:                                                                      
   int16   ststatus;  /* status of lock sleeptask queue entry */          
 54:                                                                      
   SYB.sub.-- TIMEVAL  cur, sav, res; /*used for lock sleep stats, flag   
   1203*/                                                                 
 55:                                                                      
   uint32   millisecs;                                                    
 56:                                                                      
 57:                                                                      
   spinlock = semawait--&gt;swspinlock;                                      
 58:                                                                      
   SPINLOCKHELD(spinlock);                                                
 59:                                                                      
 60:                                                                      
   /*                                                                     
 61:                                                                      
   ** Check if we would sleep on this semaphore. If we would then         
 62:                                                                      
   ** we might need to do deadlock search. We would wait on this          
 63:                                                                      
   ** semaphore only if it is not at the head of the semawait queue.      
 64:                                                                      
   */                                                                     
 65:                                                                      
   if (semawait--&gt;swsemaphore--&gt;smhead |= semawait)                       
 66:                                                                      
   {                                                                      
 67: /* Initialization */                                                 
 68:                                                                      
 69: /* Release spinlock as we are goin to sleep */                       
 70: V.sub.-- SPINLOCK(spinlock);                                         
 71:                                                                      
 72:                                                                      
 73: /*                                                                   
 74: ** Add an entry to the lock sleep task queue after incrementing      
 75: ** current lock wait sequence number. This ordering is IMPORTANT|    
 76: ** Deadlock search depends on the fact that the lock wait sequence   
 77: ** number is incremented before recording it in the lock sleep       
 78: ** task entry. This value in the entry is used to punt the task in   
 79: ** deadlock seach.                                                   
 80: */                                                                   
 81: P.sub.-- SPINLOCK(LOCK.sub.-- SLEEPTASK.sub.-- SPIN);                
 82: lockwait.sub.-- seqno = CIRCLE.sub.-- NEXT(Resource--&gt;rdlc.sub.--    
     lockwait.sub.-- seqno);                                              
 83: LOCK.sub.-- INIT.sub.-- SLEEPTASK(pss, semawait, lockwait.sub.--     
     seqno);                                                              
 84: INSQTAIL(&amp;Resource--&gt;rlocksleeptaskq, pss--&gt;plocksleepq.sub.--       
     entry);                                                              
 85:                                                                      
 86: /*                                                                   
 87: ** Determine if we are in deferred or non-deferred                   
 88: ** deadlock detection mode.                                          
 89: */                                                                   
 90: if (Resource--&gt;rcdlcheck.sub.-- period |= 0)                         
 91: {                                                                    
 92:  /*                                                                  
 93:  ** We can defer deadlock checking until later|                      
 94:  ** Just release the sleep task spinlock.                            
 95:  */                                                                  
 96:  V.sub.-- SPINLOCK(LOCK.sub.-- SLEEPTASK.sub.-- SPIN);               
 97: }                                                                    
 98: else                                                                 
 99: {                                                                    
100:  /*                                                                  
101:  ** We need to do deadlock checking now|                             
102:  ** Only one deadlock search is allowed at a                         
103:  ** time. This task will initiate deadlock search                    
104:  ** only if no other deadlock search is in progress.                 
105:  **                                                                  
106:  ** Update the iteration count such that when the current            
107:  ** deadlock search finishes, it will wake us up to do               
108:  ** deadlock search.                                                 
109:  */                                                                  
110:  Resource--&gt;rdlc.sub.-- iteration.sub.-- count = lockwait.sub.--     
      seqno;                                                              
111:                                                                      
112:  if ( | (Resource--&gt;rdlc.sub.-- inprogress))                         
113:  {                                                                   
114:   /*                                                                 
115:   ** We can do deadlock search now. Since splds are non-zero,        
116:   ** value of the flag is set to the tasks spld. This can be         
117:   ** used as a debugging aid to discover which was the last task     
118:   ** to perform deadlock search.                                     
119:   */                                                                 
120:   Resource--&gt;rdlc.sub.-- inprogress = pss--&gt;pspid;                   
121:   pss--&gt;plocksleepq.sub.-- entry--&gt;ststatus |= STDLSDONE;   
122:                                                                      
123:   /*                                                                 
124:   ** Decrement the count of tasks which have not yet done            
125:   ** deadlock search.                                                
126:   */                                                                 
127:   Resource--&gt;rdlc.sub.-- dlsnotdone.sub.-- count--;                  
128:                                                                      
129:   /*                                                                 
130:   ** Reinitialize the sequence numbers in the lock sleep task        
131:   ** queue if the current sequence number cannot be compared         
132:   ** safely with the oldest sequence number in the lock              
133:   ** sleeptask queue. Since the lock sleeptask queue is ordered      
134:   ** on sequence number, the oldest value in the queue would be      
135:   ** at the head of the queue.                                       
136:   */                                                                 
137:   if (|CIRCLE.sub.-- SAFE(                                           
138:    ((SLEEPTASK *) (QUE.sub.-- NEXT(&amp;Resource--&gt;rlocksleeptaskq)))    
139:     --&gt;stsequence.sub.-- number,                                     
140:     lockwait.sub.-- seqno))                                          
141:   {                                                                  
142:                                                                      
143:    LOCK.sub.-- REINITIALIZE.sub.-- STSEQNO(lockwait.sub.-- seqno);   
144:   }                                                                  
145:   V.sub.-- SPINLOCK(LOCK.sub.-- SLEEPTASK.sub.-- SPIN);              
146:   /*                                                                 
147:   ** Perform deadlock search and victim cleanup.                     
148:   ** Routine returns LOCK.sub.-- DEADLOCK if there was a             
149:   ** deadlock and we were the victim.                                
150:   */                                                                 
151:   if (lock.sub.-- perform.sub.-- deadlock.sub.-- search (semawait,   
       lockrec)                                                           
152:    == LOCK.sub.-- DEADLOCK)                                          
153:    return LOCK.sub.-- DEADLOCK;                                      
154:  }                                                                   
155:  else                                                                
156:  {                                                                   
157:   /*                                                                 
158:   ** We are in non-deferred mode, but a deadlock search is           
159:   ** in progress. So defer the search until the current search       
160:   ** finishes.                                                       
161:   */                                                                 
162:   V.sub.-- SPINLOCK(LOCK.sub.-- SLEEPTASK.sub.-- SPIN);              
163:  }                                                                   
164: }                                                                    
165:                                                                      
    }                                                                     
166:                                                                      
    else                                                                  
167:                                                                      
    {                                                                     
168: /*                                                                   
169: ** The semawait is at the head| Our lock must have been granted by   
170: ** now.                                                              
171: */                                                                   
172: V.sub.-- SPINLOCK(spinlock);                                         
173:                                                                      
    }                                                                     
174:                                                                      
175:                                                                      
    /* Save the current time in this task&#39;s pss. */                       
176:                                                                      
    P.sub.-- SPINLOCK(RDATETIME.sub.-- SPIN);                             
177:                                                                      
    pss--&gt;pblocktime.dtdays = Resource--&gt;rdate;                           
178:                                                                      
    pss--&gt;pblocktime.dttime = Resource--&gt;rtime;                           
179:                                                                      
    V.sub.-- SPINLOCK(RDATETIME.sub.-- SPIN);                             
180:                                                                      
181:                                                                      
    /*                                                                    
182:                                                                      
    ** Sleep for the semaphore. It is not necessary to obtain the         
183:                                                                      
    ** hash bucket spinlock again since there is one wakeup per           
184:                                                                      
    ** semawait; when this task wakes up, our caller will obtain          
185:                                                                      
    ** the hash bucket spinlock if necessary.                             
186:                                                                      
    ** However, there could be a late wakeup from a previous use          
187:                                                                      
    ** of this semawait. So we have to sleep until the pwaitsema          
188:                                                                      
    ** field in the pss is cleared.                                       
189:                                                                      
    */                                                                    
190:                                                                      
   sleepfor.sub.-- lock:                                                  
191:                                                                      
192:                                                                      
    /* Increase this task&#39;s priority. This is because once                
193:                                                                      
    ** the lock is released, the first task on the queue owns             
194:                                                                      
    ** it, even while it&#39;s asleep. Increasing the priority                
195:                                                                      
    ** here means the task will run sooner once it&#39;s granted              
196:                                                                      
    ** the lock. Running sooner hopefully means it will                   
197:                                                                      
    ** release the lock sooner, decreasing lock contention.               
198:                                                                      
    */                                                                    
199:                                                                      
    (vold) uppri(-1);                                                     
200:                                                                      
201:                                                                      
    /* Wait for the lock. To avold race conditions with both              
202:                                                                      
    ** normal lock releases and deadlock wakeups via uppwakeup,           
203:                                                                      
    ** the PL.sub.-- SLEEP bit is set in the plockstat field instead of   
    using                                                                 
204:                                                                      
    ** the SEMA.sub.-- WAITING bit in the semawait.                       
205:                                                                      
    */                                                                    
206:                                                                      
    (void) upsleepgeneric(SYB.sub.-- EVENT.sub.-- STRUCT(semawait),       
207:                                                                      
       (char *) &amp;pss--&gt;plockstat, sizeof(pss--&gt;plockstat)                 
208:                                                                      
       (long) PL.sub.-- SLEEP, 1);                                        
209:                                                                      
210:                                                                      
    /* Restore priority now that we&#39;ve woken up. */                       
211:                                                                      
    (vold) uppri(1);                                                      
212:                                                                      
213:                                                                      
214:                                                                      
    /*                                                                    
215:                                                                      
    ** Check to see if lockrec was moved to another semawait              
216:                                                                      
    ** while we were sleeping. This could happen if lock.sub.-- regueue(  
    )                                                                     
217:                                                                      
    ** was called. For details see the prologue of                        
218:                                                                      
    ** lock.sub.-- requeue( ) and the code section where we set the bit.  
219:                                                                      
    */                                                                    
220:                                                                      
    P.sub.-- SPINLOCK(spinlock);                                          
221:                                                                      
    if (lockrec--&gt;lrstatus &amp; LR.sub.-- MOVED)                             
222:                                                                      
     semawait = lockrec--&gt;lrsemawait;                                     
223:                                                                      
224:                                                                      
    /*                                                                    
225:                                                                      
    ** There are four reasons for us to wake up from                      
226:                                                                      
    ** sleep:                                                             
227:                                                                      
    ** 1. We got the lock| In this case we get out in a hurry.            
228:                                                                      
    ** 2. We were woken up because of an attention.                       
229:                                                                      
    ** 3. We were woken up to initiate deadlock search. In this           
230:                                                                      
    **  case we have to initiate deadlock search.                         
231:                                                                      
    ** 4. We were selected as a deadlock victim. In this case we          
232:                                                                      
    **  need to cleanup state and return with LOCK.sub.-- DEADLOCK        
    status.                                                               
233:                                                                      
    ** We will handle each of these conditions in order.                  
234:                                                                      
    */                                                                    
235:                                                                      
236:                                                                      
    /* We got the lock. */                                                
237:                                                                      
    if ( lockrec--&gt;lrstatus &amp; LR.sub.-- GRANTED)                          
238:                                                                      
    {                                                                     
239: /* We can release the spinlock now. */                               
240: V.sub.-- SPINLOCK(spinlock);                                         
241: /*                                                                   
242: ** At this point this task should own the semaphore, and the         
243: ** semawait should be at the head of the queue. If this is           
244: ** true, neither the swstatus field nor the semaphore head           
245: ** pointer can change, even though this task doesn&#39;t hold the        
246: ** hash bucket spinlock. This sanity test is the bottom line         
247: ** for correct operation of the lock manager so it&#39;s left in         
248: ** even if SANITY isn&#39;t defined.                                     
249: */                                                                   
250: if ((semawait--&gt;swstatus &amp; SEMA.sub.-- WAITING)                      
251:  | | semawait--&gt;swsemaphore--&gt;smhead |= semawait)  
252: {                                                                    
253:  ex raise(LOCKM, LKPREMATUREWAKE, EX.sub.-- CMDFATAL, 1);            
254: }                                                                    
255:                                                                      
256: goto gotlock;                                                        
257:                                                                      
    }                                                                     
258:                                                                      
259:                                                                      
    /*                                                                    
260:                                                                      
    ** See if our sleep may have been interrupted.                        
261:                                                                      
    */                                                                    
262:                                                                      
    if ((PSS.sub.-- GOT.sub.-- ATTENTION(pss)) | |      
    (pss--&gt;pstat &amp; P.sub.-- KILLYOURSELF))                                
263:                                                                      
    {                                                                     
264: /*                                                                   
265: ** If we are here, it means that the lock has not been granted       
266: ** yet. This is because we checked whether the lock was              
267: ** granted before checking for attention and we have not yet         
268: ** released the spinlock. All that remains to be done is to          
269: ** cleanup and return with LOCK.sub.-- INTERRUPTED status.           
270: */                                                                   
271:                                                                      
272: /* Release lockrec and mark the task as not waiting for lock. */     
273: pss--&gt;pwaitsema = 0;                                                 
274: event = lock.sub.-- unlink(lockrec);                                 
275: V.sub.-- SPINLOCK(spinlock);                                         
276:                                                                      
277: if (event)                                                           
278:  (vold) upwakeup(SYB.sub.-- EVENT.sub.-- STRUCT(event));             
279:                                                                      
280: /* Drain the freelock cache if necessary */                          
281: CHECKFREELOCKCACHE.sub.-- OVERFLOW (Eresource--&gt;erfreelock.sub.--    
     cache,                                                               
282:      Resource--&gt;rcmaxfreelock.sub.-- engine);                        
283:                                                                      
284: /* Remove lock sleep queue entry */                                  
285: P.sub.-- SPINLOCK(LOCK.sub.-- SLEEPTASK.sub.-- SPIN);                
286: LOCK.sub.-- DELETE.sub.-- SLEEPTASK(pss, ststatus);                  
287: /*                                                                   
288: ** Pick the next task to perform deadlock search if we were to       
289: ** have initiated a deadlock search now.                             
290: */                                                                   
291: if (ststatus &amp; STCHECKDEADLOCK)                                      
292: {                                                                    
293:  /* This routine will release LOCK.sub.-- SLEEPTASK.sub.-- SPIN */   
294:  lock.sub.-- check.sub.-- timeout(LOCK.sub.-- CONTINUE.sub.--        
     STQSCAN);                                                            
295: }                                                                    
296: else                                                                 
297: {                                                                    
298:  V.sub.-- SPINLOCK(LOCK.sub.-- SLEEPTASK.sub.-- SPIN);               
299: }                                                                    
300:                                                                      
301: return(LOCK.sub.-- INTERRUPTED);                                     
302:                                                                      
    }                                                                     
303:                                                                      
304:                                                                      
    /* Check if we were woken up to perform deadlock search */            
305:                                                                      
    if ((pss--&gt;plocksleepq.sub.-- entry--&gt;ststatus &amp; STINUSE) &amp;&amp;          
306:                                                                      
     (pss--&gt;plocksleepq.sub.-- entry--&gt;ststatus &amp; STCHECKDEADLOCK ))      
307:                                                                      
    {                                                                     
308: /*                                                                   
309: ** We need to initiate deadlock search.                              
310: ** First release the bucket spinlock                                 
311: */                                                                   
312: pss--&gt;plockstat |= PL.sub.-- SLEEP                          
313: V.sub.-- SPINLOCK(spinlock);                                         
314:                                                                      
315: P.sub.-- SPINLOCK(LOCK.sub.-- SLEEPTASK.sub.-- SPIN);                
316: /*                                                                   
317: ** Initiate deadlock search|                                         
318: */                                                                   
319: pss--&gt;plocksleepq.sub.-- entry--&gt;ststatus &amp;= ˜STCHECKDEADLOCK  
     ;                                                                    
320: /*                                                                   
321: ** Alarm handler might wakeup a task which has already               
322: ** completed deadlock search. So scan the queue for a task           
323: ** which needs to perform deadlock search.                           
324: */                                                                   
325: if (pss--&gt;plocksleepq.sub.-- entry--&gt;ststatus &amp; STDLSDONE)           
326: {                                                                    
327:  lock.sub.-- check timeout(LOCK.sub.-- CONTINUE.sub.-- STQSCAN);     
328:                                                                      
329:  /* Go back to sleep on semawait */                                  
330:  goto sleepfor.sub.-- lock;                                          
331: }                                                                    
332: /*                                                                   
333: ** Update the sequence number in pss so that the most                
334: ** recent version of the dependancy graph is checked for cycles.     
335: ** Also increment the global sequence number so that any sleep       
336: ** task entry added after the search starts would have a higher      
337: ** sequence number than the initiator and hence would not be part    
338: ** of the search.                                                    
339: */                                                                   
340: lockwait.sub.-- seqno = CIRCLE.sub.-- NEXT(Resource--&gt;rdlc.sub.--    
     lockwait.sub.-- seqno);                                              
341: /*                                                                   
342: ** Reinitialize the sequence numbers in the lock sleep task          
343: ** queue if the current sequence number cannot be compared           
344: ** safely with the oldest sequence number in the lock                
345: ** sleeptask queue. Since the lock sleeptask queue is ordered        
346: ** on sequence number, the oldest value in the queue would be        
347: ** at the head of the queue.                                         
348: */                                                                   
349: if (|CIRCLE.sub.-- SAFE(                                             
350:  ((SLEEPTASK *) (QUE.sub.-- NEXT (&amp;Resource--&gt;rlocksleeptaskq)))     
351:   --&gt;stsequence.sub.-- number,                                       
352:   lockwait.sub.-- seqno))                                            
353: {                                                                    
354:  LOCK.sub.-- REINITIALIZE.sub.-- STSEQNO(lockwait.sub.-- seqno);     
355: }                                                                    
356: pss--&gt;plocksleepq.sub.-- entry--&gt;stsequence.sub.-- number            
     = lockwait.sub.-- seqno;                                             
357: pss--&gt;plocksleepq.sub.-- entry--&gt;ststatus |= STDLSDONE;     
358: Resource--&gt;rdlc.sub.-- dlsnotdone.sub.-- count--;                    
359: V.sub.-- SPINLOCK(LOCK.sub.-- SLEEPTASK.sub.-- SPIN);                
360: /*                                                                   
361: ** Routine returns LOCK.sub.-- DEADLOCK if there was a               
362: ** deadlock and we were the victim.                                  
363: */                                                                   
364: if (lock.sub.-- perform.sub.-- deadlock.sub.-- search (semawait,     
     lockrec)                                                             
365:  == LOCK.sub.-- DEADLOCK) {                                          
366:   return LOCK.sub.-- DEADLOCK;                                       
367: }                                                                    
368: else                                                                 
369: {                                                                    
370:  goto sleepfor.sub.-- lock; /* go back to wait on lock */            
371: }                                                                    
372:                                                                      
    }                                                                     
373:                                                                      
374:                                                                      
    /* See if we were a deadlock victim. */                               
375:                                                                      
    if (pss--&gt;plockstat &amp; PL.sub.-- DEADLOCKED)                           
376:                                                                      
    {                                                                     
377:                                                                      
     lock.sub.-- cleanup.sub.-- self (lockrec, FALSE);                    
378:                                                                      
     return(LOCK.sub.-- DEADLOCK);                                        
379:                                                                      
    }                                                                     
380:                                                                      
381:                                                                      
    /* Release bucket spinlock */                                         
382:                                                                      
    V.sub.-- SPINLOCK(spinlock);                                          
383:                                                                      
    goto sleepfor.sub.-- lock;                                            
384:                                                                      
385:                                                                      
   gotlock:                                                               
386:                                                                      
387:                                                                      
388:                                                                      
    /*                                                                    
389:                                                                      
    ** We would get here only if we own the semaphore|                    
390:                                                                      
    ** Release the sleep task structure.                                  
391:                                                                      
    */                                                                    
392:                                                                      
    if (pss--&gt;plocksleepq.sub.-- entry--&gt;ststatus &amp; STINUSE)              
393:                                                                      
    {                                                                     
394: P.sub.-- SPINLOCK(LOCK.sub.-- SLEEPTASK.sub.-- SPIN);                
395: LOCK.sub.-- DELETE.sub.-- SLEEPTASK(pss, ststatus);                  
396: if (ststatus &amp; STCHECKDEADLOCK)                                      
397: {                                                                    
398:  /* This routine will release LOCK.sub.-- SLEEPTASK.sub.-- SPIN */   
399:  lock.sub.-- check timeout(LOCK.sub.-- CONTINUE.sub.-- STQSCAN);     
400: }                                                                    
401: else                                                                 
402: {                                                                    
403:  V.sub.-- SPINLOCK(LOCK.sub.-- SLEEPTASK.sub.-- SPIN);               
404: }                                                                    
405:                                                                      
    }                                                                     
406:                                                                      
407:                                                                      
    return(LOCK.sub.-- WAITED);                                           
408:                                                                      
   }                                                                      
__________________________________________________________________________
 
    
     Before calling the lock --  semawait function, the caller has determined that the semawait is not at the head of its semaphore queue (otherwise, the lock is granted). As shown, the function is invoked with the semaphore to wait for and the lock record to wait for. As shown at lines 22-28, the function can return a result value or flag of LOCK --  WAITED, LOCK --  DEADLOCK, or LOCK --  INTERRUPTED. LOCK --  WAITED indicates that the lock which the task had to wait for was granted. LOCK --  DEADLOCK indicates that the caller (process) was selected as a deadlock victim. LOCK --  INTERRUPTED indicates that the lock was not granted and that the task was &#34;interrupted&#34; while waiting for the lock. In such a case, the caller must return at some later point in time for the lock, or it can relinquish its request for the lock altogether. 
     The steps of the function are as follows. At line 57, the spinlock stored by the semawait is copied to a local variable, spinlock. At line 58, the function tests whether the spinlock is held (since the caller is expected to invoke this function with the spinlock held). At line 65, the function tests whether the semawait is at the head of the queue. Here, the task will only wait on the semaphore if it is not at the head of the semawait queue. As previously described, if it is at the head, then the lock has been granted (i.e., it does not need to wait). If the condition does not hold true (i.e., the semaphore is not at the head), the function knows that it has to sleep on the semaphore. Accordingly, the spinlock is released, at lines 69-70. After it has decided that it is going to sleep, the function adds an entry to the lock sleep task queue--a representation of all the tasks that are sleeping in the system for some lock, at lines 73-84. Specifically, the function grabs a sequence number (line 82) and initializes the sleep task structure (line 83). 
     The sleep task structure itself is constructed as follows. 
     
         __________________________________________________________________________
typedef struct sleeptask                                                  
 LINK   stqueue;   /* Link to the sleep task queue */                     
 struct pss  *stpss;  /* back link to the pss of the sleeping task */     
 struct semawait  *stsemawait;   /* semawait task is sleeping on */       
 circ.sub.-- long stsequence.sub.-- number; /* current value of lock wait 
sequence                                                                  
number */                                                                 
 long   stunused1;    /* Filler to match up with the magic */             
 int16   ststatus;    /* deadlock search status */                        
0x5354 */stmagic;    /* &#34;st&#34;                                              
#if  HAVE.sub.-- INT64                                                    
 int32   stunused3;                                                       
#endif  /* HAVE.sub.-- INT64 */                                           
 long   stunused4;                                                        
#if USE.sub.-- NATIVE.sub.-- TASKING                                      
 syb.sub.-- event.sub.-- t event;                                         
#endif /* USE.sub.-- NATIVE.sub.-- TASKING */                             
}SLEEPTASK;                                                               
/*                                                                        
 Status bits for the sleep task entry.                                    
*/                                                                        
# define STDLSDONE 0x01   /* Deadlock search has been done for this       
         lock request once. */                                            
# define STCHECKDEADLOCK  0x02  /* Task has to initiate deadlock search.  
*/                                                                        
# define STINUSE    0x04   /*  Pss--&gt;pstentry  is in use                  
__________________________________________________________________________
*/                                                                        
 
    
     Use of the structure is as follows. Each task is associated with one instance of the sleep task structure or record. Specifically, a task can only sleep on one lock at a time. In essence, the sleep task structure serves as a descriptor for a particular sleep on a lock. The sleep task structure or record is inserted into the sleep task queue, at line 84. 
     Once the sleep task record structure is inserted into the sleep task queue, the function determines whether to perform deferred or non-deferred deadlock detection. If the detection can be deferred (tested at line 90), the function releases the sleep task spinlock (line 96) and then goes to sleep. In a preferred embodiment, deferred detection is the default mode. Otherwise (i.e., &#34;false&#34; at line 90), the function proceeds to perform deadlock detection. 
     Before actually undertaking deadlock detection, the function must first ascertain whether another task is currently performing deadlock detection. In the preferred embodiment, only one task at a time can do deadlock detection. If another task is doing deadlock detection, the current task falls back to the deferred mode of deadlock detection. The specific check for whether deadlock detection is currently in progress is performed at line 1 12. The fall back to deferred mode occurs in the &#34;else&#34; statement at lines 155-163. If deadlock searching is not in progress (i.e., &#34;true&#34; at line 112), then the function proceeds to line 114. Now, the function can initiate deadlock searching. After initial housekeeping (e.g., setting the &#34;in progress&#34; flag at line 120), the function calls a high-level deadlock search routine--lock --  perform --  deadlock --  search--at line 151. If a deadlock is in fact detected, the call will return a result of LOCK --  DEADLOCK, at line 153. 
     With deferred mode deadlock detection, the task initially sleeps before undertaking deadlock detection. When it awakens, it determines the &#34;reason&#34; why it woke up. The task may awaken for one of the following reasons: 
     (1) The lock was granted; 
     (2) The task was interrupted (i.e., because of another matter requiring &#34;attention&#34;); 
     (3) The task was awakened to initiate a deadlock search; or 
     (4) The task was selected as a deadlock victim. 
     Accordingly, the function will test for these conditions. 
     At line 237, the function tests whether the lock was granted, by testing the status flag for LR --  GRANTED. If the condition holds &#34;true,&#34; the function releases the spinlock (line 240) and goes to the &#34;gotlock&#34; label at line 256, thereby bypassing the other tests. The function tests at line 262 whether the task was awakened because of an interrupt or &#34;attention&#34; condition. Here, the lock has not been granted. Therefore, the function performs cleanup, including releasing the spinlock (275) and removing the sleep task structure (line 286), before returning LOCK --  INTERRUPTED, at line 301. 
     At lines 305-306, the function tests whether the task was awakened to perform deadlock search. In this case, the task has deferred undertaking the search until it is awakened. Recall for the non-deferred deadlock search, on the other hand, the task undertakes deadlock search before sleeping or waiting. When a task is awakened from its sleep to undertake deadlock searching (i.e., &#34;true&#34; at lines 305-306), deadlock searching is initiated as follows. At lines 308-313, the function releases the bucket spinlock. A &#34;deadlock&#34; status flag is set at line 319. Then, after further initialization/housekeeping, the function invokes the deadlock search subroutine, lock --  perform --  deadlock --  search, at line 364. If a deadlock is detected, LOCK --  DEADLOCK is returned by the routine and is, in turn, returned by the lock --  semawait function, at line 366. Otherwise (i.e., no deadlock detected), the task goes back to waiting or sleeping on the lock, as indicated by line 370. 
     At line 375, the function tests for the fourth condition--the task is a deadlock victim. When this condition holds true (i.e., &#34;true&#34; at line 375), the function performs lock cleanup (line 377) and returns LOCK --  DEADLOCK, at line 378. 
     In the event that the task got a lock (e.g., as was shown for the &#34;lock granted case,&#34; lines 263-257), the function proceeds to the gotlock label, shown at line 385. Here, the function &#34;passes the baton&#34; from one task to another. Recall that only one task at any given time can perform deadlock searching. The sleep task spinlock is released, at lines 398-399. Finally, the function returns LOCK --  WAITED, at line 407, since this represents a case where the lock was granted after waiting. 
     2. lock --  perform --  deadlock --  search 
     In an exemplary embodiment, the lock --  perform --  deadlock --  search function may be constructed as follows. 
     
         __________________________________________________________________________
 1:                                                                       
   /*                                                                     
 2:                                                                       
   ** LOCK.sub.-- .sub.-- PERFORM.sub.-- DEADLOCK.sub.-- SEARCH           
 3:                                                                       
   **                                                                     
 4:                                                                       
   ** int                                                                 
 5:                                                                       
   ** lock.sub.-- perform.sub.-- deadlock.sub.-- search (SEMAWAIT         
   *semawait,                                                             
 6:                                                                       
   **  LOCKREC *lockrec)                                                  
 7:                                                                       
   ** Perform deadlock search by calling lock.sub.-- .sub.-- check.sub.-- 
   deadlock. If                                                           
 8:                                                                       
   ** the deadlock search finds a victim cleanup the victim. If the       
 9:                                                                       
   ** victim is us then release the lockrec also.                         
 10:                                                                      
   **                                                                     
 11:                                                                      
   ** Parameters:                                                         
 12:                                                                      
   ** semawait : the semawait which is causing the wait leading to        
 13:                                                                      
   **    deadlock search                                                  
 14:                                                                      
   ** lockrec : lock request which is causing the wait leading to         
 15:                                                                      
   **    deadlock search                                                  
 16:                                                                      
   **                                                                     
 17:                                                                      
   ** Returns:                                                            
 18:                                                                      
   ** LOCK.sub.-- DEADLOCK - if the current task is the deadlock victim   
 19:                                                                      
   **    0 - otherwise                                                    
 20:                                                                      
   **                                                                     
 21:                                                                      
   ** Side Effects:                                                       
 22:                                                                      
   ** Current tasks lock sleep task entry might be moved to the           
 23:                                                                      
   ** tail of the lock sleep task queue.                                  
 24:                                                                      
   ** Note that as part of deadlock victim cleanup, victims lock          
 25:                                                                      
   ** sleep task entry could be deleted from the lock sleep task          
 26:                                                                      
   ** queue. The lockrec would be released if the current task            
 27:                                                                      
   ** is the deadlock victim (all this would be done by                   
 28:                                                                      
   ** lock.sub.-- cleanup.sub.-- deadlock.sub.-- victim called by this    
   routine)                                                               
 29:                                                                      
   **                                                                     
 30:                                                                      
   ** Called by:                                                          
 31:                                                                      
   ** lock.sub.-- .sub.-- semawait( )                                     
 32:                                                                      
   **                                                                     
 33:                                                                      
   ** MP synchronization:                                                 
 34:                                                                      
   ** LOCK.sub.-- SLEEPTASK.sub.-- SPIN is held when moving the current   
   tasks                                                                  
 35:                                                                      
   ** lock sleep task entry to the tail.                                  
 36:                                                                      
   **                                                                     
 37:                                                                      
   */                                                                     
 38:                                                                      
   SYB.sub.-- STATIC int                                                  
 39:                                                                      
   lock.sub.-- perform.sub.-- deadlock.sub.-- search (SEMAWAIT            
   *semawait,                                                             
 40: LOCKREC *lockrec)                                                    
 41:                                                                      
   {                                                                      
 42:                                                                      
     LOCALPSS(pss);   /* initialize local copy of global pss */           
 43:                                                                      
 44:                                                                      
    /* place holder for call to deadlock search */                        
 45:                                                                      
    LOCKSLEEPOWNER waiter.sub.-- data;                                    
 46:                                                                      
    /* deadlock table holding multiple chains. */                         
 47:                                                                      
    DLTAB  deadlock.sub.-- tab;                                           
 48:                                                                      
 49:                                                                      
    PSS   *victim;   /* deadlock victim */                                
 50:                                                                      
 51:                                                                      
    /* Record spid visited in  */                                         
 52:                                                                      
    spid.sub.-- t  dlcurrent.sub.-- path SEMA.sub.-- MAX.sub.-- RECURSION!
    ;                                                                     
 53:     /* deadlock search recursion*/                                   
 54:                                                                      
    circ.sub.-- long lockwait.sub.-- seqno;                               
 55:                                                                      
 56:                                                                      
   #if |WORK.sub.-- GP.sub.-- PRIM                                        
 57:                                                                      
    MONEVENT.sub.-- LOCK.sub.-- DEADLOCK.sub.-- BEGIN( );                 
 58:                                                                      
    MONITOR.sub.-- INC(mc.sub.-- lock(deadlock.sub.-- search));           
 59:                                                                      
   #endif /* WORK.sub.-- GP.sub.-- PRIM */                                
 60:                                                                      
 61:                                                                      
    /*                                                                    
 62:                                                                      
    ** Set up the waiting task data for the first call                    
 63:                                                                      
    ** to deadlock search.                                                
 64:                                                                      
    */                                                                    
 65:                                                                      
    waiter.sub.-- data.lsosemawait = semawait;                            
 66:                                                                      
    waiter.sub.-- data.lsospinlock = semawait--&gt;swspinlock;               
 67:                                                                      
    waiter.sub.-- data.lsospid = pss--&gt;pspid;                             
 68:                                                                      
 69:                                                                      
    /*                                                                    
 70:                                                                      
    ** Clear the bitmap indicating visited spids before starting          
 71:                                                                      
    ** the deadlock search.                                               
 72:                                                                      
    ** The number of bits in the scanarray is 1 + MAXSPID because         
 73:                                                                      
    ** spid&#39;s range from 1 to MAXSPID. We are wasting zeroth bit.         
 74:                                                                      
    */                                                                    
 75:                                                                      
    MEMZERO(Resource--&gt;rdlc.sub.-- scanarray, BYTESLEN(MAXSPID+1));       
 76:                                                                      
 77:                                                                      
    /* Initialize key info in the deadlock table. */                      
 78:                                                                      
    deadlock.sub.-- tab.dltxcess.sub.-- recur = FALSE;                    
 79:                                                                      
    deadlock.sub.-- tab.dltcycle.sub.-- count = 0;                        
 80:                                                                      
 81:                                                                      
    /*                                                                    
 82:                                                                      
    ** Check for deadlocks involving ourselves                            
 83:                                                                      
    ** Note that Resource--&gt;rdlc.sub.-- sleepownerlist has enough space   
    to                                                                    
 84:                                                                      
    ** record two times configured number of pss. This would be           
 85:                                                                      
    ** enough because we break only two cycles so we would end up         
 86:                                                                      
    ** recording the same task at most twice. If we increase the          
 87:                                                                      
    ** number of cycles detected by this algorithm then we should         
 88:                                                                      
    ** increase the size of Resource--&gt;rdlc.sub.-- s1eepowner1ist.        
 89:                                                                      
    */                                                                    
 90:                                                                      
    lock.sub.-- check.sub.-- deadlock(&amp;waiter.sub.-- data, FIRST.sub.--   
    DEADLOCK.sub.-- CALL,                                                 
 91:     &amp;deadlock.sub.-- tab, (LOCKSLEEPOWNER *)                         
 92:     Resource--&gt;rdlc.sub.-- sleepownerlist, dlcurrent.sub.-- path);   
 93:                                                                      
    /*                                                                    
 94:                                                                      
    ** If we generate more than one deadlock, victimize the               
 95:                                                                      
    ** initiator.                                                         
 96:                                                                      
    */                                                                    
 97:                                                                      
    if (deadlock.sub.-- tab.dltcycle.sub.-- count &gt; 1)                    
 98:                                                                      
    {                                                                     
 99:                                                                      
   #if |WORK.sub.-- GP.sub.-- PRIM                                        
100: MONITOR.sub.-- INC(mc.sub.-- lock(lock.sub.-- multipledeadlock));    
101:                                                                      
   #endif /* WORK.sub.-- GP.sub.-- PRIM */                                
102:                                                                      
103: if (pss--&gt;pstat &amp; P.sub.-- BACKOUT)                                  
104: {                                                                    
105:  /*                                                                  
106:  ** If the initiator was a backout task and it was involved          
107:  ** in more than one cycle then lock.sub.-- .sub.-- check.sub.--     
      deadlock would                                                      
108:  ** have stored an alternate victim in the second deadlock           
109:  ** chain. Use that as the victim instead of the initiator.          
110:  */                                                                  
111:  victim = deadlock.sub.-- tab.dltchains 1! .dlcvictim.sub.-- pss;    
112: }                                                                    
113: else                                                                 
114: {                                                                    
115:  victim = pss;                                                       
116: }                                                                    
117:                                                                      
    }                                                                     
118:                                                                      
    else                                                                  
119:                                                                      
    {                                                                     
120: victim = deadlock.sub.-- tab.dltchains 0! .dlcvictim.sub.-- pss;     
121:                                                                      
    }                                                                     
122:                                                                      
123:                                                                      
    if (victim |= pss)                                                    
124:                                                                      
    {                                                                     
125: /*                                                                   
126: ** Move our sleep task entry from head to                            
127: ** tail because our deadlock search is over and we                   
128: ** are not the victim.                                               
129: */                                                                   
130: P.sub.-- SPINLOCK(LOCK.sub.-- SLEEPTASK.sub.-- SPIN);                
131: lockwait.sub.-- seqno = CIRCLE.sub.-- NEXT(Resource--&gt;rdlc.sub.--    
     lockwait.sub.-- seqno);                                              
132: LOCK.sub.-- .sub.-- MOVE.sub.-- TO.sub.-- TAIL(pss--&gt;plocksleepq.sub.
     -- entry, lockwait.sub.-- seqno);                                    
133: V.sub.-- SPINLOCK(LOCK.sub.-- SLEEPTASK.sub.-- SPIN);                
134:                                                                      
    }                                                                     
135:                                                                      
136:                                                                      
    /* Cleanup the victim if our search found a deadlock victim */        
137:                                                                      
    if (victim)                                                           
138:                                                                      
    {                                                                     
139:                                                                      
   #if |WORK.sub.-- GP.sub.-- PRIM                                        
140: MONITOR.sub.-- INC(mc.sub.-- lock(deadlocks));                       
141:                                                                      
   #endif /* WORK.sub.-- GP.sub.-- PRIM */                                
142:                                                                      
143: /*                                                                   
144: ** There is a deadlock victim|                                       
145: */                                                                   
146: Resource--&gt;rdeadlock.sub.-- id++;                                    
147:                                                                      
148: /* Cleanup victim */                                                 
149: (void) lock.sub.-- cleanup.sub.-- deadlock.sub.-- victim(victim,     
150:   &amp;deadlock.sub.-- tab, lockrec);                                    
151:                                                                      
    }                                                                     
152:                                                                      
153:                                                                      
    /*                                                                    
154:                                                                      
    ** Wake up the next person in the sleep task queue to                 
155:                                                                      
    ** continue the search if necessary.                                  
156:                                                                      
    */                                                                    
157:                                                                      
    P.sub.-- SPINLOCK(LOCK.sub.-- SLEEPTASK.sub.-- SPIN);                 
158:                                                                      
    lock.sub.-- .sub.-- check.sub.-- timeout(LOCK.sub.-- CONTINUE.sub.--  
    STQSCAN);                                                             
159:                                                                      
160:                                                                      
   #if |WORK.sub.-- GP.sub.-- PRIM                                        
161:                                                                      
    MONEVENT.sub.-- LOCK.sub.-- DEADLOCK.sub.-- END(0);                   
162:                                                                      
   #endif /* WORK.sub.-- GP.sub.-- PRIM */                                
163:                                                                      
    /*                                                                    
164:                                                                      
    ** If we are the victim, there is nothing more to do                  
165:                                                                      
    ** except to return with DEADLOCK status.                             
166:                                                                      
    */                                                                    
167:                                                                      
    if (victim == pss)                                                    
168:                                                                      
     return LOCK.sub.-- DEADLOCK;                                         
169:                                                                      
    else                                                                  
170:                                                                      
     return 0;                                                            
171:                                                                      
   }                                                                      
__________________________________________________________________________
 
    
     The function largely serves as a wrapper, performing certain initialization and housekeeping chores, before invoking the &#34;real&#34; deadlock workhorse routine, lock --  check --  deadlock (at line 90). The call to lock --  check --  deadlock returns status information, via deadlock --  tab. If multiple deadlocks are detected (tested at line 97), the function victimizes the initiator at line 111. 
     If the current task is not the victim (tested at line 123), then the sleep task entry or record is moved from head to tail, as the deadlock search has completed and the current task is not the victim. At lines 136-151, the function performs cleanup of any victim found in the deadlock search. Specifically, this is done by a call to a cleanup subroutine, at line 149. In the event that the current task is the victim, the function returns LOCK --  DEADLOCK, at line 168. Otherwise, it returns a result value of zero. 
     3. lock --  check --  deadlock 
     Actual deadlock checking is performed by the lock --  check --  deadlock function. In an exemplary embodiment, the function may be constructed as follows. 
     
         __________________________________________________________________________
1: /*                                                                     
2: **                                                                     
     LOCK.sub.-- CHECK.sub.-- DEADLOCK                                    
3: **                                                                     
4: **                                                                     
     void                                                                 
5: **                                                                     
     lock.sub.-- check.sub.-- deadlock(LOCKSLEEPOWNER *waiter.sub.--      
     dlcheck.sub.-- data,                                                 
6: **   int level; short *dlindex, DLTAB * deadlock.sub.-- tab,           
7: **   LOCKSLEEPOWNER *sleepowner.sub.-- 1ist,                           
8: **   spid.sub.-- t dlcurrent.sub.-- path  !)                           
9: **                                                                     
10:                                                                       
   **                                                                     
     See if sleeping on a semawait would cause deadlocks.                 
11:                                                                       
   **                                                                     
     The search algorithm detects deadlocks by detecting cycles in        
12:                                                                       
   **                                                                     
     a directed dependency graph amongst tasks waiting for locks.         
13:                                                                       
   **                                                                     
     In this graph there would be a directed edge from task A to          
14:                                                                       
   **                                                                     
     task B if task A was waiting for a lock that task B owns. The        
15:                                                                       
   **                                                                     
     dependency graph is not explicitly created. The algorithm            
16:                                                                       
   **                                                                     
     actually represents only the nodes in the graph in a list            
17:                                                                       
   **                                                                     
     called sleeping owners list. The edges are implicitly                
18:                                                                       
   **                                                                     
     represented through recursion.                                       
19:                                                                       
   **                                                                     
20:                                                                       
   **                                                                     
     The algorithm accepts the spid of a waiting task and the lock        
21:                                                                       
   **                                                                     
     semawait it is waiting on as input (in waiter.sub.-- dlcheck.sub.--  
     data).                                                               
22:                                                                       
   **                                                                     
     It first creates a list of owners of this lock which are             
23:                                                                       
   **                                                                     
     themselves sleeping for other locks (by calling                      
24:                                                                       
   **                                                                     
     lock.sub.-- set.sub.-- sleeping.sub.-- owners.sub.-- list) . Then    
     the algorithm is                                                     
25:                                                                       
   **                                                                     
     repeated recursively on each of the sleeping owners until            
26:                                                                       
   **                                                                     
     either the list is empty or the search has exceeded                  
27:                                                                       
   **                                                                     
     SEMA.sub.-- MAX.sub.-- RECURSION levels of recursion.                
28:                                                                       
   **                                                                     
     The recursion at any level stops when either the list is             
29:                                                                       
   **                                                                     
     empty or if one of the sleeping owners is the initiator              
30:                                                                       
   **                                                                     
     itself. In the latter case we have detected a deadlock and a         
31:                                                                       
   **                                                                     
     victim is selected to break the deadlock.                            
32:                                                                       
   **                                                                     
33:                                                                       
   **                                                                     
     The algorithm only detects deadlocks involving the task which        
34:                                                                       
   **                                                                     
     initiated the search. It detects upto two cycles involving           
35:                                                                       
   **                                                                     
     the initiator. The search is stopped after detecting two             
36:                                                                       
   **                                                                     
     cycles to contain the cost of search.                                
37:                                                                       
   **                                                                     
38:                                                                       
   **                                                                     
     This routine does not acquire any spinlocks since:                   
39:                                                                       
   **                                                                     
40:                                                                       
   ** 1. The spinlocks are needed only when we traverse the               
41:                                                                       
   **  lock hash chains or the lock sleep task queue to                   
42:                                                                       
   **  record sleeping owners. A11 this is done in                        
43:                                                                       
   **  lock.sub.-- set.sub.-- sleepingowner.sub.-- list.                  
44:                                                                       
   ** 2. The algorithm allows the lock manager data structures            
45:                                                                       
   **  to change while deadlock search is in progress.                    
46:                                                                       
   ** 3. Only one task is allowed to perform deadlock search at           
47:                                                                       
   **  any time;                                                          
48:                                                                       
   **                                                                     
     One interesting fallout of this observation is that this             
49:                                                                       
   **                                                                     
     task can yield now if it has almost exhausted its time               
50:                                                                       
   **                                                                     
     quantum.                                                             
51:                                                                       
   **                                                                     
     We can detect a tree of deadlock chains; for instance 1.2.3          
52:                                                                       
   **                                                                     
     and 1.2.4 are two deadlock chains which have the first two           
53:                                                                       
   **                                                                     
     dlcitem&#39;s in common. We record the entire first deadlock             
54:                                                                       
   **                                                                     
     information (1.2.3) in the 1st DLCHAIN and the entire second         
55:                                                                       
   **                                                                     
     deadlock in the 2nd DLCHAIN. This algorithm could support            
56:                                                                       
   **                                                                     
     more than 2 DLCHAINs, if later we decide to kill a list of           
57:                                                                       
   **                                                                     
     victims instead of the caller.                                       
58:                                                                       
   **                                                                     
59:                                                                       
   **                                                                     
     We will resolve only deadlock chains starting at level 0. The        
60:                                                                       
   **                                                                     
     algorithm would detect deadlocks starting at other levels but        
61:                                                                       
   **                                                                     
     would not try to break the deadlocks. This is done to limit          
62:                                                                       
   **                                                                     
63:                                                                       
   **                                                                     
     the cost of deadlock search. Note that the dlcinfo structure         
64:                                                                       
   **                                                                     
     in each dltchain is filled up as soon as the deadlock is             
65:                                                                       
   **                                                                     
     detected (lock.sub.-- fill.sub.-- dltab( ) is called once to fill in 
     the                                                                  
66:                                                                       
   **                                                                     
     information.                                                         
67:                                                                       
   **                                                                     
68:                                                                       
   **                                                                     
     Parameters:                                                          
69:                                                                       
   **                                                                     
     waiter.sub.-- dlcheck.sub.-- data : Information recorded for the     
     waiter by                                                            
70:                                                                       
   **                                                                     
     the caller.                                                          
71:                                                                       
   **                                                                     
     level   : Level of recursion (used to prevent excessive              
72:                                                                       
   **        recursion                                                    
73:                                                                       
   **                                                                     
     dlindex : Index into deadlock chain structures off of                
74:                                                                       
   **        deadlock.sub.-- tab.                                         
75:                                                                       
   **                                                                     
     deadlock.sub.-- tab                                                  
             : table holding deadlock information about                   
76:                                                                       
   **        possible                                                     
77:                                                                       
   **        multiple deadlock chains                                     
78:                                                                       
   **                                                                     
     sleepowner.sub.-- list                                               
             : Pointer to record the lock sleep owner                     
79:                                                                       
   **        list at this level.                                          
80:                                                                       
   **                                                                     
     dlcurrent.sub.-- path                                                
             : Array of spids involved in this cycle in                   
81:                                                                       
   **        the order they were visited.                                 
82:                                                                       
   **                                                                     
83:                                                                       
   **                                                                     
     Returns:                                                             
84:                                                                       
   **                                                                     
     Nothing.                                                             
85:                                                                       
   **                                                                     
86:                                                                       
   **                                                                     
     Side Effects:                                                        
87:                                                                       
   **                                                                     
     dlindex                                                              
contains the number of deadlocks found, if it                             
88:                                                                       
   **        contains 0, there is no deadlock dectected.                  
89:                                                                       
   **                                                                     
     deadlock.sub.-- tab                                                  
contains the list of victim pss.                                          
90:                                                                       
   **                                                                     
91:                                                                       
   **                                                                     
     Fields affected in deadlock.sub.-- tab:                              
92:                                                                       
   **                                                                     
dltchains (mapped to deadlock.sub.-- chain)                               
93:                                                                       
   **        are used to track deadlock info for each                     
94:                                                                       
   **        deadlock.                                                    
95:                                                                       
   **                                                                     
dltxcess.sub.-- recur                                                     
   may get set if we exceed the max                                       
96:                                                                       
   **        recursion level                                              
97:                                                                       
   **                                                                     
98:                                                                       
   **                                                                     
     Fields affected in each deadlock.sub.-- chain                        
99:                                                                       
   **                                                                     
dlcvictim.sub.-- pss                                                      
             is used to communicate the detection of a                    
100:                                                                      
   **        deadlock in lock.sub.-- check.sub.-- deadlock()              
101:                                                                      
   **        If we are recursing in                                       
102:                                                                      
   **        lock.sub.-- check.sub.-- deadlock( ), this value may         
103:                                                                      
   **        not be the final victim.                                     
104:                                                                      
   **                                                                     
dlclast.sub.-- item                                                       
   is incremented for each collection of                                  
105:                                                                      
   **        deadlock info                                                
106:                                                                      
   **        in the different levels of recursion in                      
107:                                                                      
   **        lock.sub.-- check.sub.-- deadlock( ).                        
108:                                                                      
   **                                                                     
109:                                                                      
   **                                                                     
     Called by:                                                           
110:                                                                      
   **                                                                     
     lock semawait( ), lock.sub.-- check.sub.-- deadlock ( )              
111:                                                                      
   **                                                                     
112:                                                                      
   **                                                                     
     MP synchronization:                                                  
113:                                                                      
   **                                                                     
     None|                                                                
114:                                                                      
   **                                                                     
115:                                                                      
   */                                                                     
116:                                                                      
   SYB.sub.-- STATIC void                                                 
117:                                                                      
   lock.sub.-- check.sub.-- deadlock(LOCKSLEEPOWNER *waiter.sub.--        
   dlcheck.sub.-- data,                                                   
118:     int level,                                                       
119:     DLTAB * deadlock.sub.-- tab,                                     
120:     LOCKSLEEPOWNER *sleepowner.sub.-- list,                          
121:     spid.sub.-- t dlcurrent.sub.-- path  !)                          
122:                                                                      
   {                                                                      
123:                                                                      
    LOCALPSS (pss) ;   /* initialize local copy of global Pss */          
124:                                                                      
    REGISTER PSS   *ownerpss;                                             
125:                                                                      
    PSS   *victimpss;  /* Pss ptr of victim found in search */            
126:                                                                      
    DLCHAIN   *deadlock.sub.-- chain;/* local ptr to a deadlock.sub.--    
    chain. */                                                             
127:                                                                      
    int32   ownerpid;                                                     
128:                                                                      
    LOCKSLEEPOWNER   *newsleepowner.sub.-- list;                          
129:                                                                      
    int   noofsleepers;                                                   
130:                                                                      
    int   i;                                                              
131:                                                                      
    REGISTER PSS   *tmp.sub.-- pss;                                       
132:                                                                      
    CFG.sub.-- VALUE   cfgprtdlockinfo;                                   
133:                                                                      
134:                                                                      
    /*                                                                    
135:                                                                      
    ** Check for stack overflow in this recursive routine.                
136:                                                                      
    ** This must appear directly after the declaration section            
137:                                                                      
    ** in the routine.                                                    
138:                                                                      
    */                                                                    
139:                                                                      
    CHECKSTACKOFLOW;                                                      
140:                                                                      
141:                                                                      
    /*                                                                    
142:                                                                      
    ** At each level check the timeslice and yield if need to|            
143:                                                                      
    ** The state information is available in lock sleep owner list and    
144:                                                                      
    ** we allow the lock manager state to change after recording          
145:                                                                      
    ** the waiter data.                                                   
146:                                                                      
    */                                                                    
147:                                                                      
    if ( pss--&gt;ptimeslice &lt; 0)                                            
148:                                                                      
    {                                                                     
149:                                                                      
     (void) upyield( );                                                   
150:                                                                      
    }                                                                     
151:                                                                      
152:                                                                      
    (void) cfg.sub.-- getconfig(CFG.sub.-- PRTDEADLOCKINFO, CFG.sub.--    
    RUNVALUE,                                                             
153:                                                                      
       &amp;cfgprtdlockinfo)                                                  
154:                                                                      
155:                                                                      
    /* set the deadlock chain pointer, and the index on dlcitem&#39;s within  
156:                                                                      
    ** that chain                                                         
157:                                                                      
    */                                                                    
158:                                                                      
    deadlock.sub.-- chain =                                               
&amp; (deadlock.sub.-- tab--&gt;dltchains  deadlock.sub.-- tab--&gt;dltcycle.sub.-- 
count!);                                                                  
159: deadlock.sub.-- chain--&gt;dlcvictim.sub.-- pss = 0;                    
160:                                                                      
161: /*                                                                   
162: ** Terminate recursion if excessive. Nominate current process as     
the                                                                       
163: ** victim if this level is reached.                                  
164: */                                                                   
165: if (level == SEMA.sub.-- MAX.sub.-- RECURSION)                       
166: {                                                                    
167:  /*                                                                  
168:  ** Set deadlock.sub.-- tab--&gt;dltxcess.sub.-- recur to true so we    
      can                                                                 
169:  ** terminate the search for a victim.                               
170:  ** Lock.sub.-- print.sub.-- deadlockchain( ) uses this info to      
      identify                                                            
171:  ** phony deadlocks. Don&#39;t copy any info for this level.             
172:  */                                                                  
173:  deadlock.sub.-- tab--&gt;dltxcess.sub.-- recur = TRUE;                 
174:  deadlock.sub.-- tab--&gt;dltcycle.sub.-- count++;                      
175:                                                                      
176:  /* victimize pss of requestor. */                                   
177:  deadlock.sub.-- chain--&gt;dlcvictim.sub.-- pss = pss;                 
178:  /*                                                                  
179:  ** If initiator is a backout task then select the                   
180:  ** first non BACKOUT task in the path.                              
181:  ** If there is no non BACKOUT task then Tough Luck||                
182:  ** In this case we have to choose the initiator as the              
183:  ** victim.                                                          
184:  */                                                                  
185:  if (pss--&gt;pstat &amp; P.sub.-- BACKOUT)                                 
186:  {                                                                   
187:   for(i=0; i&lt;= level; i++)                                           
188:   {                                                                  
189:   tmp.sub.-- pss = GET.sub.-- UNKEPT.sub.-- PSS(dlcurrent.sub.--     
       path i!);                                                          
190:   if (| (tmp.sub.-- pss--&gt;pstat &amp; P.sub.-- BACKOUT))                 
191:   {                                                                  
192:    deadlock.sub.-- chain--&gt;dlcvictim.sub.-- pss = tmp.sub.-- pss;    
193:    break;                                                            
194:   }                                                                  
195:  }                                                                   
196: }                                                                    
197: return;                                                              
198:                                                                      
    }                                                                     
199:                                                                      
200:                                                                      
    /* Store the waiters spid in the current path array. */               
201:                                                                      
    dlcurrent.sub.-- path level! = waiter.sub.-- dlcheck.sub.-- data--&gt;lso
    spid;                                                                 
202:                                                                      
    /*                                                                    
203:                                                                      
    ** Create the sleeping owners list.                                   
204:                                                                      
    ** lock.sub.-- set.sub.-- sleepingowner.sub.-- list returns the       
    number of sleeping                                                    
205:                                                                      
    ** owners that it recorded. Advance the sleeping owner list           
206:                                                                      
    ** pointer by these many entries to pass to the next level.           
207:                                                                      
    */                                                                    
208:                                                                      
    noofsleepers                                                          
209: = lock.sub.-- set.sub.-- sleepingowner list(waiter.sub.-- dlcheck.sub
     .-- data,                                                            
210:   sleepowner.sub.-- list);                                           
211:                                                                      
    newsleepowner.sub.-- list = sleepowner.sub.-- list + noofsleepers;    
212:                                                                      
213:                                                                      
    /*                                                                    
214:                                                                      
    ** Recurse for each sleeping owner.                                   
215:                                                                      
    */                                                                    
216:                                                                      
    for *(; (ownerpid = sleepowner.sub.-- list--&gt;lsospid)                 
217: |= ENDOFLSOLIST ; sleepowner.sub.-- list++)                          
218:                                                                      
    {                                                                     
219: /* Grab the sleeping owner pss */                                    
220: ownerpss = GET.sub.-- UNKEPT.sub.-- PSS(ownerpid);                   
221: /*                                                                   
222: ** As a last check before recursing see if this task is no           
223: ** longer sleeping. If it is not then we do not need to              
224: ** recurse on this entry. This is done without holding the           
225: ** bucket spinlock.                                                  
226: */                                                                   
227: if (ownerpss--&gt;pwaitsema == o)                                       
228: {                                                                    
229:  continue;                                                           
230: }                                                                    
231:                                                                      
232: /* We have a cycle if we encounter our own pss again */              
233: if (ownerpss == pss)                                                 
234: {                                                                    
235:                                                                      
236:                                                                      
   # ifdef TRACE.sub.-- LOCK                                              
237:  if ( (TRACECMDLINE(LOCKM, 4)) | |                 
238:     cfgprtdlockinfo.int32.sub.-- value)                              
239:  {                                                                   
240:  /* Fill all the information about this chain. */                    
241:  lock.sub.-- fill.sub.-- dltab(deadlock.sub.-- chain,                
      dlcurrent.sub.-- path,                                              
242:     level, ownerpid);                                                
243: }                                                                    
244:                                                                      
   # endif /* TRACE.sub.-- LOCK */                                        
245:                                                                      
246: /*                                                                   
247: ** We found a new deadlock increment the cycle.sub.-- count.         
248: */                                                                   
249: deadlock.sub.-- tab--&gt;dltcycle.sub.-- count++;                       
250:                                                                      
251: /* stop looping if we&#39;ve reached the limits of detection */          
252: if (deadlock.sub.-- tab--&gt;dltcycle.sub.-- count &gt;= MAX.sub.--        
     VICTIMS)                                                             
253: {                                                                    
254:  /* Choose the initiator as the victim */                            
255:  deadlock chain--&gt;dlcvictim.sub.-- pss = pss;                        
256:  /*                                                                  
257:  ** If initiator is a backout task then select the                   
258:  ** first non BACKOUT task in the path. There has to be a            
259:  ** non BACKOUT task in the cycle because:                           
260:  **  1. known access method assumptions imply that we can            
261:  **   not have a cycle involving just backout tasks.                 
262:  **  2. multiple cycle requires a shared lock and BACKOUT            
263:  **   tasks acquire only address locks. So there has to              
264:  **   be a non BACKOUT task in this case.                            
265:  */                                                                  
266:  if (pss--&gt;pstat &amp; P.sub.-- BACKOUT)                                 
267:  {                                                                   
268:   for(i= 0; i&lt;= level; i++)                                          
269:   {                                                                  
270:    tmp.sub.-- pss = GET.sub.-- UNKEPT.sub.-- PSS(dlcurrent.sub.--    
       path i!);                                                          
271:    if (| (tmp.sub.-- pss--&gt;pstat &amp; P.sub.-- BACKOUT))                
272:    {                                                                 
273:     deadlock.sub.-- chain--&gt;dlcvictim.sub.-- pss = tmp.sub.-- pss;   
274:     break;                                                           
275:    }                                                                 
276:  }                                                                   
277: }                                                                    
278: return;                                                              
279:                                                                      
    }                                                                     
280:                                                                      
    /*                                                                    
281:                                                                      
    ** We found a new deadlock. Select a deadlock victim for              
282:                                                                      
    ** this chain.                                                        
283:                                                                      
    */                                                                    
284:                                                                      
    lock.sub.-- select.sub.-- deadlock.sub.-- victim(deadlock.sub.--      
    chain,                                                                
285:                                                                      
        dlcurrent.sub.-- path,                                            
286:                                                                      
        level)                                                            
287:                                                                      
288:                                                                      
    /*                                                                    
289:                                                                      
    ** Clear the visited bit for all the tasks involved in this           
290:                                                                      
    ** cycle so that we can catch other cycles involving the              
291:                                                                      
    ** same tasks.                                                        
292:                                                                      
    */                                                                    
293:                                                                      
    for (i=0; i&lt;= level; i++)                                             
294:                                                                      
    {                                                                     
295:                                                                      
     CLEAR.sub.-- BIT (Resource--&gt;rdlc.sub.-- scanarray,dlcurrent.sub.--  
    path i!);                                                             
296:                                                                      
    }                                                                     
297:                                                                      
    /*                                                                    
298:                                                                      
    ** Clear the visited bit of the remaining tasks in the                
299:                                                                      
    ** sleeping owner list for this level. Leaving their bits             
300:                                                                      
    ** on would prevent them from being recorded at other                 
301:                                                                      
    ** levels. Since we have not visited these tasks yet, they            
302:                                                                      
    ** might be part of deadlocks involving the initiator.                
303:                                                                      
    ** Clearing the bits ensures that they would be recorded              
304:                                                                      
    ** and visited if encountered at other levels.                        
305:                                                                      
    */                                                                    
306:                                                                      
    for (sleepowner.sub.-- list++;                                        
307:                                                                      
      (ownerpid = sleepowner.sub.-- list--&gt;1sospid) |=ENDOFLSOLIST;       
308:                                                                      
      sleepowner.sub.-- list++)                                           
309:                                                                      
    {                                                                     
310:                                                                      
     CLEAR.sub.-- BIT(Resource--&gt;rdlc.sub.-- scanarray,ownerpid);         
311:                                                                      
    }                                                                     
312:                                                                      
    /*                                                                    
313:                                                                      
    ** We can move return to previous level of recursion since            
314:                                                                      
    ** all deadlocks involving tasks below this level on this             
315:                                                                      
    ** path can be broken by selecting any one of the tasks in            
316:                                                                      
    ** dlcurrent.sub.-- path as the victim. This is because all such      
317:                                                                      
    ** deadlocks would have to involve all the tasks in                   
318:                                                                      
    ** dlcurrent.sub.-- path also. Since we have already chosen one of    
319:                                                                      
    ** these tasks as a potential victim there                            
320:                                                                      
    ** is no sense is searching for further deadlocks on this             
321:                                                                      
    ** path.                                                              
322:                                                                      
    */                                                                    
323:                                                                      
    return;                                                               
324:                                                                      
    }                                                                     
325:                                                                      
326:                                                                      
   /* Recurse with the current sleeping owner. */                         
327:                                                                      
   lock.sub.-- check.sub.-- deadlock(sleepowner.sub.-- list, level + 1,   
328:                                                                      
       deadlock.sub.-- tab,                                               
329:                                                                      
       newsleepowner.sub.-- list, dlcurrent.sub.-- path);                 
330:                                                                      
   victimpss = deadlock.sub.-- chain--&gt;dlcvictim.sub.-- pss;              
331:                                                                      
332:                                                                      
   if (victimpss)                                                         
333:                                                                      
   {                                                                      
334:                                                                      
335:                                                                      
    ** We have a victim. Continue to loop for other                       
336:                                                                      
    ** deadlock candidates. The tail of this deadlock                     
337:                                                                      
    ** chain has been already filled up, set up the                       
338:                                                                      
    ** new chain for the next iterations.                                 
339:                                                                      
    */                                                                    
340:                                                                      
    if ( deadlock tab--&gt;dltcycle.sub.-- count &lt; MAX.sub.-- VICTIMS)       
341:                                                                      
    {                                                                     
342:                                                                      
     deadlock.sub.-- chain                                                
343:                                                                      
      = &amp;(deadlock.sub.-- tab--&gt;dltchains                                 
344:                                                                      
         deadlock.sub.-- tab--&gt;dltcycle.sub.-- count!);                   
345:                                                                      
     deadlock chain--&gt;dlcvictim.sub.-- pss = 0;                           
346:                                                                      
      }                                                                   
347:                                                                      
     }                                                                    
348:                                                                      
349:                                                                      
    /* stop looping if we&#39;ve reached the limits of detection */           
350:                                                                      
    if (deadlock.sub.-- tab--&gt;dltcycle.sub.-- count &gt;= MAX.sub.--         
   VICTIMS)                                                               
351:                                                                      
     return;                                                              
352:                                                                      
    }                                                                     
353:                                                                      
    return;                                                               
354:                                                                      
   }                                                                      
__________________________________________________________________________
 
    
     The function operates by performing a depth-first search of a tree of dependencies among tasks waiting for locks. The tree or graph would have a &#34;directed edge&#34; from task A to task B if task A is waiting for a lock that task B owns. The dependency graph is not explicitly created but is, instead, represented by recursion which occurs in the function (at lines 326-329). For instance, if task B is waiting on task A, then the function will invoke itself recursively for the task which task A is waiting on. In order to prevent stack overflow, the depth of recursion cannot exceed a maximum number of levels (here, SEMA --  MAX --  RECURSION number of levels). The recursion at a particular level stops when the function detects that one of the sleeping tasks is itself the initiator of a deadlock. Here, a deadlock is detected and a victim is selected to break the deadlock. As shown, the currently preferred embodiment does not employ any spin locks and, thus, avoids &#34;freezing&#34; the system while deadlock search is being performed. 
     The specific steps of the function are as follows. First, at lines 134-139, the function checks for stack overflow, since this is a recursive routine. At line 147, the function checks its time slice, for determining whether it needs to yield. At line 165, the function tests whether the current level has reached the maximum number of recursions (i.e., equal to SEMA --  MAX --  RECURSION). Here, an excess recursion flag is set to &#34;true,&#34; at line 173, and a deadlock count is incremented, at line 174. Next, at lines 176-177, the requester is victimized. However, if a task is a &#34;backout&#34; task, a special case, then it is not victimized. Instead, the first non-backout task in the path is selected, as shown at lines 178-196. Here, the &#34;backout&#34; task is one which is backing out of a transaction. In order to leave the system in a consistent state, therefore, a backout task is preferably not victimized (unless there are no non-backout tasks). Finally, the &#34;if&#34; statement initiated at line 165 (i.e., for the case of maximum recursion) is concluded by a &#34;return&#34; statement, at line 197. 
     Ordinarily, maximum recursion has not been reached and the function proceeds to lines 200-201 to record the current path in a path array (i.e., from task A to task B to task C, and so forth and so on). The path is recorded by storing the task ID (spid) in the path array. At lines 202-211, the function creates the previously-described &#34;sleeping owners list&#34;--that is, the owners currently sleeping on the lock. At line 216, a &#34;for&#34; loop is established for examining each sleeping owner. At line 227, the function makes a last check for seeing whether the task is still sleeping. If the task is no longer sleeping, then it does not need to recurse on this entry in the sleeping owners list. In such a case, it executes the &#34;continue&#34; statement, at line 229. 
     At line 223, the function tests whether a cycle exists--that is, the sleeping owner is encountered again. In such a case, the function has found a new deadlock and, thus, increments a deadlock cycle count, at line 249. After a maximum number of search cycles has been reached (tested at line 252), the function stops looping and proceeds to choose the initiator as the victim, at line 255. However, as before, if the initiator is a &#34;backout&#34; task, the function instead selects the first non-backout task in the path, as shown at lines 256-277. For such a case, the method returns at line 278. 
     If the cycle count does not reach the limit, the function proceeds to lines 280-286, to select a deadlock victim for the current chain, by calling lock --  select --  deadlock --  victim. This is followed by clearing a &#34;visited&#34; bit for all tasks involved in the cycle (so that other cycles involving the same tasks can be caught). The &#34;if&#34; statement initiated at line 252 concludes with a &#34;return&#34; statement at line 323. 
     Otherwise (i.e., maximum cycle count has not been reached), the function or method proceeds to lines 326-329, for invoking itself recursively with the current sleeping owner. Upon returning from the recursion, the function tests whether a &#34;victim&#34; exists, at line 332. If one or more victims exist, the victim information is added to the deadlock chain. As the function is still at this point executing within the &#34;for&#34; loop established at line 216, the function will continue to loop for other deadlock candidates. In the event that the cycle count has reached a maximum limit, the function returns at line 351. Otherwise, the &#34;for&#34; loop loops through remaining sleeping owners. At the conclusion of the &#34;for&#34; loop, the function returns, at line 353. 
     CONCLUSION 
     While the invention is described in some detail with specific reference to a single preferred embodiment and certain alternatives, there is no intent to limit the invention to that particular embodiment or those specific alternatives. For example, while locks and many data structures associated with locks have been described as accessible through hash tables, those skilled in the art, enabled by the teachings of the present invention, will appreciate that other efficient-indexing access mechanisms may be employed. Further, although the discussion of the preferred embodiment focuses on exclusive and non-exclusive locks, those skilled in the art will appreciate that the teachings of the present invention may be applied to other types of locks (e.g., &#34;intent&#34; locks), such as the varying degrees of lock exclusivity and granularity commonly provided by database systems. In such a case, compatibility between locks can be determined by providing a lock compatibility table. Thus, the true scope of the present invention is not limited to any one of the foregoing exemplary embodiments but is instead defined by the appended claims.