update sem methods

This commit is contained in:
hslam 2020-12-01 19:03:18 +08:00
parent 3a5168acb9
commit 0142fd8f36
2 changed files with 40 additions and 11 deletions

View File

@ -4,7 +4,6 @@
package ipc package ipc
import ( import (
"github.com/hslam/sem"
"strings" "strings"
"testing" "testing"
"time" "time"
@ -182,15 +181,15 @@ func TestMore(t *testing.T) {
} else if ret < 1 { } else if ret < 1 {
t.Error() t.Error()
} }
var sops [1]sem.Sembuf var sops [1]Sembuf
sops[0] = sem.Sembuf{SemNum: uint16(semnum), SemFlg: SEM_UNDO} sops[0] = Sembuf{SemNum: uint16(semnum), SemFlg: SEM_UNDO}
sops[0].SemOp = -1 sops[0].SemOp = -1
if _, err := Semop(semid, sops[:]); err != nil { if _, err := Semoperate(semid, sops[:]); err != nil {
return return
} }
text := string(data[:length]) text := string(data[:length])
sops[0].SemOp = 1 sops[0].SemOp = 1
if _, err := Semop(semid, sops[:]); err != nil { if _, err := Semoperate(semid, sops[:]); err != nil {
return return
} }

42
sem.go
View File

@ -5,8 +5,16 @@ package ipc
import ( import (
"github.com/hslam/sem" "github.com/hslam/sem"
"unsafe"
) )
// Sembuf represents an operation.
type Sembuf struct {
SemNum uint16
SemOp int16
SemFlg int16
}
// Semget calls the semget system call. // Semget calls the semget system call.
// //
// The semget() system call returns the System V semaphore set identifier // The semget() system call returns the System V semaphore set identifier
@ -56,15 +64,37 @@ func Semv(semid int, semnum int, semflg int) (bool, error) {
} }
// Semop calls the semop system call. // Semop 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 Semop(semid int, sops uintptr, nsops int) (bool, error) {
return sem.Op(semid, sops, nsops)
}
// Semoperate calls the semop system call.
// Flags recognized in SemFlg are IPC_NOWAIT and SEM_UNDO. // Flags recognized in SemFlg are IPC_NOWAIT and SEM_UNDO.
// If an operation specifies SEM_UNDO, it will be automatically undone when the // If an operation specifies SEM_UNDO, it will be automatically undone when the
// process terminates. // process terminates.
// Op calls the semop system call. func Semoperate(semid int, sops []Sembuf) (bool, error) {
// Flags recognized in SemFlg are IPC_NOWAIT and SEM_UNDO. return Semop(semid, uintptr(unsafe.Pointer(&sops[0])), len(sops))
// If an operation specifies SEM_UNDO, it will be automatically undone when the
// process terminates.
func Semop(semid int, sops []sem.Sembuf) (bool, error) {
return sem.Op(semid, sops)
} }
// Semrm removes the semaphore with the given id. // Semrm removes the semaphore with the given id.