3.4 NLM Synchronization

The Synchronization Services functions, part of the NetWare API, enable applications to coordinate access to network files and other resources. These services are divided into two categories: locking and semaphores.

3.4.1 Locking

Locking enables a thread to gain exclusive access to a file-related resource, such as a file, physical record, or logical record. Threads lock resources by entering the filename or record location and the size into a log table, then issuing a single call to lock every resource listed in the table. Normally, a thread logs a group of records and then locks them as a set. However, a thread can lock a single record when it is placed in the log table.

This technique of logging files and records as a set and locking them all at once ensures that either all files and records are locked or none are locked. Thus, the developer can prevent deadlock, in which two or more applications reach a stalemate trying to access resources locked by the other application.

IMPORTANT:Don’t use locking when you are using connection 0 because locking temporarily disables the connection. This disables connection 0 for all modules using the connection. If you are going to use locking, acquire a connection using LoginToFileServer.

3.4.2 Semaphores

Locking allows only one thread to access a file-related resource, but semaphores limit the threads that can access network resources to a configurable number. You can also use semaphores to limit the number of users of a particular resource.

Semaphores can be associated with resources, such as files, structures, and devices. There are two types of semaphores:

Network semaphores

These apply to resources available to servers and workstations on the network. For more information on network semaphores, see Synchronization Concepts (Single and Intra-File Services).

Local semaphores

These apply to resources available only to a single server. For more information on local semaphores, see Interprocess Synchronization (NDK: NLM Threads Management).

If your NLM uses resources that could be used by a thread running on another workstation or server on the network, you might use network semaphores.

You can use local semaphores among multiple NLMs; however, an NLM must either pass its semaphore handles to other modules or export a function that returns the semaphore handles to other modules.

If you are using semaphores to communicate between threads in the same NLM, you might use local semaphores. Local semaphores are faster than network semaphores because they are simpler and easier for the NLM to find and implement.

IMPORTANT:Don’t use network semaphores when you are using connection 0 because locking temporarily disables the connection. This disables connection 0 for all NLM applications using the connection. If you are going to use network semaphores, acquire a connection using LoginToFileServer.