UPSEM(2)                                                 UPSEM(2)

     NAME
          upsem, downsem, altsems, initsem, semstats - optimistic user
          level semaphores

     SYNOPSIS
          #include <u.h>
          #include <libc.h>

          void upsem(Sem *s);

          int downsem(Sem *s, int block);

          int altsems(Sem *ss[], int n);

          void initsem(Sem *s, int tickets);

          void semstats(void)

          extern int semtrytimes;

     DESCRIPTION
          Upsem, downsem, and altsems provide an interface for opti-
          mistic semaphores that work without entering the kernel when
          they can proceed, and call the kernel only when it is really
          necessary (e.g., to block or to unblock another process).

          A semaphore is a struct shared among synchronizing pro-
          cesses.  Initialization is done by the user program by call-
          ing initsem. The parameter tickets must be a natural number.
          It sets the initial state of the semaphore.  After the ini-
          tialization, only the following functions should be used to
          operate on the semaphore.

          Downsem tries to acquire one unit from the semaphore. If it
          can proceed, the call works without calling the kernel. When
          it cannot proceed, the global semtrytimes controls for how
          long (how many times) the function will try to acquire with-
          out entering the kernel, doing a busy wait.  If this fails
          and block is set, the kernel is entered to block the process
          until a ticket can be acquired.  If block is not set, the
          process does not enter the kernel and the function returns
          0.  When a ticket is acquired, the function returns 1.  If
          the system call fails, it returns a negative value.

          Upsem releases one ticket. The call does not enter the ker-
          nel unless a process must be awaken.

          Altsems tries to perform a downsem in any one of the sema-
          phores pointed to by pointers in ss (there are n entries in
          that array). After a busy wait determined by semtrytimes, if

     Page 1                       Plan 9            (printed 12/21/24)

     UPSEM(2)                                                 UPSEM(2)

          no semaphore can be acquired, the kernel is entered and the
          process blocks until it can proceed. Otherwise, the opera-
          tion is performed without calling the kernel.  The function
          returns the semaphore that has been acquired.  If the opera-
          tion fails, it returns a negative value.

          Semstats prints several statistics for debugging, and may be
          useful to learn if the processes using the semaphores (any
          semaphore) did enter the kernel or not and how many times
          they did.

     SOURCE
          /sys/src/libc/9sys/upsem.c and /sys/src/9/port/syssem.c

     SEE ALSO
          fork(2), lock(2), rendezvous(2), segattach(2), thread(2),
          and semacquire(2).

     DIAGNOSTICS
          These functions set errstr. If the semaphore's internal lock
          is corrupted (note that this is indistinguishable from being
          extremely busy) the process can get a suicide note.

     BUGS
          Semalt only can be used with semaphores located in the same
          shared segment.

     Page 2                       Plan 9            (printed 12/21/24)