sem

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 16, 2021 License: MIT Imports: 2 Imported by: 1

README

sem

PkgGoDev Build Status Go Report Card LICENSE

Package sem provides a way to use System V semaphores.

Get started

Install
go get github.com/hslam/sem
Import
import "github.com/hslam/sem"
Usage
Example
package main

import (
	"fmt"
	"github.com/hslam/ftok"
	"github.com/hslam/sem"
	"time"
)

func main() {
	key, err := ftok.Ftok("/tmp", 0x22)
	if err != nil {
		panic(err)
	}
	nsems := 1
	semid, err := sem.Get(key, nsems, 0666)
	if err != nil {
		semid, err = sem.Get(key, nsems, sem.IPC_CREAT|sem.IPC_EXCL|0666)
		if err != nil {
			panic(err)
		}
		defer sem.Remove(semid)
		for semnum := 0; semnum < nsems; semnum++ {
			_, err := sem.SetValue(semid, semnum, 1)
			if err != nil {
				panic(err)
			}
		}
	}
	semnum := 0
	if count, err := sem.GetValue(semid, semnum); err != nil {
		panic(err)
	} else if count == 0 {
		fmt.Printf("%s semnum %d wait\n", time.Now().Format("15:04:05"), semnum)
	}
	ok, err := sem.P(semid, semnum, sem.SEM_UNDO)
	if err != nil {
		panic(err)
	}
	fmt.Printf("%s semnum %d P %t\n", time.Now().Format("15:04:05"), semnum, ok)
	time.Sleep(time.Second * 10)
	ok, err = sem.V(semid, semnum, sem.SEM_UNDO)
	if err != nil {
		panic(err)
	}
	fmt.Printf("%s semnum %d V %t\n", time.Now().Format("15:04:05"), semnum, ok)
	time.Sleep(time.Second * 20)
}
Output
$ go run main.go
12:35:21 semnum 0 P true
12:35:31 semnum 0 V true

In another terminal.

$ go run main.go
12:35:25 semnum 0 wait
12:35:31 semnum 0 P true
12:35:41 semnum 0 V true
License

This package is licensed under a MIT license (Copyright (c) 2020 Meng Huang)

Author

sem was written by Meng Huang.

Documentation

Overview

Package sem provides a way to use System V semaphores.

Index

Constants

View Source
const (
	// GETNCNT returns the value of semncnt {READ}.
	GETNCNT = 14
	// GETPID returns the value of sempid {READ}
	GETPID = 11
	// GETVAL returns the value of semval {READ}
	GETVAL = 12
	// GETALL returns semvals into arg.array {READ}
	GETALL = 13
	// GETZCNT returns the value of semzcnt {READ}
	GETZCNT = 15
	// SETVAL sets the value of semval to arg.val {ALTER}
	SETVAL = 16
	// SETALL sets semvals from arg.array {ALTER}
	SETALL = 17
)
View Source
const (
	// IPC_CREAT creates if key is nonexistent
	IPC_CREAT = 01000

	// IPC_EXCL fails if key exists.
	IPC_EXCL = 02000

	// IPC_NOWAIT returns error no wait.
	IPC_NOWAIT = 04000

	// IPC_PRIVATE is private key
	IPC_PRIVATE = 00000

	// SEM_UNDO sets up adjust on exit entry
	SEM_UNDO = 010000

	// IPC_RMID removes identifier
	IPC_RMID = 0
	// IPC_SET sets ipc_perm options.
	IPC_SET = 1
	// IPC_STAT gets ipc_perm options.
	IPC_STAT = 2
)

Variables

This section is empty.

Functions

func Get

func Get(key int, nsems int, semflg int) (int, error)

Get calls the semget system call.

The semget() system call returns the System V semaphore set identifier associated with the argument key.

A new set of nsems semaphores is created if key has the value IPC_PRIVATE or if no existing semaphore set is associated with key and IPC_CREAT is specified in semflg.

If semflg specifies both IPC_CREAT and IPC_EXCL and a semaphore set already exists for key, then semget() fails with errno set to EEXIST.

The argument nsems can be 0 (a don't care) when a semaphore set is not being created. Otherwise, nsems must be greater than 0 and less than or equal to the maximum number of semaphores per semaphore set.

If successful, the return value will be the semaphore set identifier, otherwise, -1 is returned, with errno indicating the error.

func GetValue

func GetValue(semid int, semnum int) (int, error)

GetValue calls the semctl GETVAL system call.

func Op

func Op(semid int, sops uintptr, nsops int) (bool, error)

Op calls the semop system call.

semop() performs operations on selected semaphores in the set indi‐ cated by semid. Each of the nsops elements in the array pointed to by sops is a structure that specifies an operation to be performed on a single semaphore. The elements of this structure are of type struct sembuf, containing the following members:

unsigned short sem_num; /* semaphore number */ short sem_op; /* semaphore operation */ short sem_flg; /* operation flags */

Flags recognized in sem_flg are IPC_NOWAIT and SEM_UNDO. If an operation specifies SEM_UNDO, it will be automatically undone when the process terminates.

The set of operations contained in sops is performed in array order, and atomically, that is, the operations are performed either as a complete unit, or not at all. The behavior of the system call if not all operations can be performed immediately depends on the presence of the IPC_NOWAIT flag in the individual sem_flg fields, as noted be‐ low.

func Operate

func Operate(semid int, sops []Sembuf) (bool, error)

Operate calls the semop system call.

func P

func P(semid int, semnum int, semflg int) (bool, error)

P calls the semop P system call. Flags recognized in semflg are IPC_NOWAIT and SEM_UNDO. If an operation specifies SEM_UNDO, it will be automatically undone when the process terminates.

func Remove

func Remove(semid int) error

Remove removes the semaphore set with the given id.

func SetValue

func SetValue(semid int, semnum int, semun int) (bool, error)

SetValue calls the semctl SETVAL system call.

func V

func V(semid int, semnum int, semflg int) (bool, error)

V calls the semop V system call. Flags recognized in semflg are IPC_NOWAIT and SEM_UNDO. If an operation specifies SEM_UNDO, it will be automatically undone when the process terminates.

Types

type Sembuf

type Sembuf struct {
	SemNum uint16
	SemOp  int16
	SemFlg int16
}

Sembuf represents an operation.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL