Skip to content
paulran edited this page Apr 15, 2016 · 1 revision

The features of shmtx

  1. To implement sharing lock between processes.

The define and implementation of shmtx

View ngx_shmtx.h and ngx_shmtx.c.

typedef unsigned long               ngx_atomic_uint_t;
typedef volatile ngx_atomic_uint_t  ngx_atomic_t;

typedef struct {
#if (NGX_HAVE_ATOMIC_OPS)
    ngx_atomic_t  *lock;
#if (NGX_HAVE_POSIX_SEM)
    ngx_atomic_t  *wait;
    ngx_uint_t     semaphore;
    sem_t          sem;
#endif
#else
    ngx_fd_t       fd;
    u_char        *name;
#endif
    ngx_uint_t     spin;
} ngx_shmtx_t;

Notes:

  1. Using mmap() allocates a ngx_atomic_t lock and a ngx_atomic_t wait in the shared memory between processes.
  2. Atomic operation ngx_atomic_cmp_set(lock, old, set) to lock to judge whether the process get the lock.
  3. Atomic operation lock is written to the PID, which can be accurately labeled by which process to get the lock. According to this, the process can be forced to unlock. When lock is 0, it shows that there is no process for getting this lock.
  4. The purpose of struct ngx_shmtx_t adding the variable sem_t sem is to implement waiting other process release lock and notification other processes can get lock.
  5. test codes: test-shmtx.c.
Clone this wiki locally