zhangmeng
2019-08-26 a0123f163eddcea3e6b9f9d36f1f3fb3aa2c835a
readwriter.go
@@ -6,12 +6,11 @@
package shm
import (
   "golang.org/x/sys/unix"
   "io"
   "sync/atomic"
   "unsafe"
   "github.com/tmthrgd/go-sem"
   "golang.org/x/sys/unix"
)
const (
@@ -55,22 +54,6 @@
// Name returns the name of the shared memory.
func (rw *ReadWriteCloser) Name() string {
   return rw.name
}
// Unlink removes the shared memory.
//
// It is the equivalent to calling Unlink(string) with
// the same name as Create* or Open*.
//
// Taken from shm_unlink(3):
//    The  operation  of shm_unlink() is analogous to unlink(2): it removes a
//    shared memory object name, and, once all processes  have  unmapped  the
//    object, de-allocates and destroys the contents of the associated memory
//    region.  After a successful shm_unlink(),  attempts  to  shm_open()  an
//    object  with  the same name will fail (unless O_CREAT was specified, in
//    which case a new, distinct object is created).
func (rw *ReadWriteCloser) Unlink() error {
   return Unlink(rw.name)
}
// Read
@@ -135,7 +118,7 @@
      block = (*sharedBlock)(unsafe.Pointer(blocks + uintptr(uint64(blockIndex)*rw.fullBlockSize)))
      if blockIndex == atomic.LoadUint32((*uint32)(&rw.readShared.WriteEnd)) {
         if err := ((*sem.Semaphore)(&rw.readShared.SemSignal)).Wait(); err != nil {
         if err := ((*Semaphore)(&rw.readShared.SemSignal)).Wait(); err != nil {
            return Buffer{}, err
         }
@@ -187,7 +170,7 @@
      atomic.CompareAndSwapUint32((*uint32)(&rw.readShared.ReadEnd), blockIndex, uint32(block.Next))
      if uint32(block.Prev) == atomic.LoadUint32((*uint32)(&rw.readShared.WriteStart)) {
         if err := ((*sem.Semaphore)(&rw.readShared.SemAvail)).Post(); err != nil {
         if err := ((*Semaphore)(&rw.readShared.SemAvail)).Post(); err != nil {
            return err
         }
      }
@@ -258,7 +241,7 @@
      block = (*sharedBlock)(unsafe.Pointer(blocks + uintptr(uint64(blockIndex)*rw.fullBlockSize)))
      if uint32(block.Next) == atomic.LoadUint32((*uint32)(&rw.writeShared.ReadEnd)) {
         if err := ((*sem.Semaphore)(&rw.writeShared.SemAvail)).Wait(); err != nil {
         if err := ((*Semaphore)(&rw.writeShared.SemAvail)).Wait(); err != nil {
            return Buffer{}, err
         }
@@ -313,7 +296,7 @@
      atomic.CompareAndSwapUint32((*uint32)(&rw.writeShared.WriteEnd), blockIndex, uint32(block.Next))
      if blockIndex == atomic.LoadUint32((*uint32)(&rw.writeShared.ReadStart)) {
         if err := ((*sem.Semaphore)(&rw.writeShared.SemSignal)).Post(); err != nil {
         if err := ((*Semaphore)(&rw.writeShared.SemSignal)).Post(); err != nil {
            return len(buf.Data), err
         }
      }