SemaphoreGet, SemaphoreRelease (Semaphore Get, Semaphore Release)

The SemaphoreGet and SemaphoreRelease instructions are used to lock a hardware component or memory shared by two different sequences within a program, so that one sequence cannot access the hardware or memory while the other is acting upon it.

Syntax

SemaphoreGet ( SemNumber )

          'section of code

SemaphoreRelease (SemNumber )

Remarks

These instructions are useful if two concurrent sequences are both accessing the same hardware (for example, COMport) or the same memory (for example, variable). SemaphoreGet() will wait until the semaphore is not in use to flag it as available and then proceed. SemaphoreRelease() will release the semaphore so that some other sequence can access it. The SemNumber argument is a constant assigned to the semaphore.

An example application is when separate SDI12Recorder instructions that use the same control port occur in different but concurrent scans. SemaphoreGet and SempaphoreRelease can be used to avoid a hardware conflict that can result in ClosedNAN Not a number. A data word indicating a measurement or processing error. Voltage overrange, SDI-12 sensor error, and undefined mathematical results can produce NAN. returned by the SDI12Recorder instruction (see example #2).

Care should be taken when using semaphores as they can hold up program execution. In general, the most efficient programs get the semaphore for the least amount of time possible to do the necessary operations.

If the program is running in ClosedPipeLineMode A CRBasic program execution mode wherein instructions are evaluated in groups of like instructions, with a set group prioritization. and skipped scans occur due to waiting for a semaphore to be released, increasing the BufferOption in the Scan() instruction may avoid or reduce skipped scans.

Reserved Number, 4

NOTE:  Semaphore number 4 is fully implemented in OS versions newer than 13.02.

Semaphore number 4 is only applicable when the data logger is configured as a server. In this mode, another device (the client) initiates a connection and tries to send or receive data. When Semaphore 4 is used, it prevents the data logger from returning a file to the client for a specific reason, usually because the requested file is currently being edited.

An example application is a data logger generating a data file or image that is also served via HTTP. SemaphoreGet(4) and SemaphoreRelease(4) can be used around the data file generation process to ensure that the data logger does not change the contents of the file while it is being uploaded over HTTP.

NOTE:  Semaphore 4 has no effect on CRBasic client instructions such as FTPClient(), HTTPGet(), or HTTPPost() because, in these situations, the data logger is acting as a client rather than a server.