From 8492756a0e317c2cbba1c689ca04c80f2f79bf34 Mon Sep 17 00:00:00 2001 From: Kris Budde Date: Sat, 23 Aug 2014 14:39:31 +0200 Subject: [PATCH] Always unlock mutex. Just to be sure. --- rpio.go | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/rpio.go b/rpio.go index f2c9655..c02353f 100644 --- a/rpio.go +++ b/rpio.go @@ -172,6 +172,7 @@ func PinMode(pin Pin, direction Direction) { shift := (uint8(pin) % 10) * 3 memlock.Lock() + defer memlock.Unlock() if direction == Input { mem[fsel] = mem[fsel] &^ (pinMask << shift) @@ -179,7 +180,6 @@ func PinMode(pin Pin, direction Direction) { mem[fsel] = (mem[fsel] &^ (pinMask << shift)) | (1 << shift) } - memlock.Unlock() } // WritePin sets a given pin High or Low @@ -194,6 +194,7 @@ func WritePin(pin Pin, state State) { setReg := p/32 + 7 memlock.Lock() + defer memlock.Unlock() if state == Low { mem[clearReg] = 1 << (p & 31) @@ -201,7 +202,6 @@ func WritePin(pin Pin, state State) { mem[setReg] = 1 << (p & 31) } - memlock.Unlock() } // Read the state of a pin @@ -234,6 +234,7 @@ func PullMode(pin Pin, pull Pull) { shift := (uint8(pin) % 32) memlock.Lock() + defer memlock.Unlock() switch pull { case PullDown, PullUp: @@ -253,8 +254,6 @@ func PullMode(pin Pin, pull Pull) { mem[pullReg] = mem[pullReg] &^ 3 mem[pullClkReg] = 0 - memlock.Unlock() - } // Open and memory map GPIO memory range from /dev/mem . @@ -276,6 +275,7 @@ func Open() (err error) { defer file.Close() memlock.Lock() + defer memlock.Unlock() // Memory map GPIO registers to byte array mem8, err = syscall.Mmap( @@ -296,13 +296,12 @@ func Open() (err error) { mem = *(*[]uint32)(unsafe.Pointer(&header)) - memlock.Unlock() return nil } // Close unmaps GPIO memory -func Close() { +func Close() error { memlock.Lock() - syscall.Munmap(mem8) - memlock.Unlock() + defer memlock.Unlock() + return syscall.Munmap(mem8) }