Always unlock mutex. Just to be sure.

This commit is contained in:
Kris Budde 2014-08-23 14:39:31 +02:00
parent 08ca9d41c7
commit 8492756a0e

15
rpio.go
View File

@ -172,6 +172,7 @@ func PinMode(pin Pin, direction Direction) {
shift := (uint8(pin) % 10) * 3 shift := (uint8(pin) % 10) * 3
memlock.Lock() memlock.Lock()
defer memlock.Unlock()
if direction == Input { if direction == Input {
mem[fsel] = mem[fsel] &^ (pinMask << shift) mem[fsel] = mem[fsel] &^ (pinMask << shift)
@ -179,7 +180,6 @@ func PinMode(pin Pin, direction Direction) {
mem[fsel] = (mem[fsel] &^ (pinMask << shift)) | (1 << shift) mem[fsel] = (mem[fsel] &^ (pinMask << shift)) | (1 << shift)
} }
memlock.Unlock()
} }
// WritePin sets a given pin High or Low // WritePin sets a given pin High or Low
@ -194,6 +194,7 @@ func WritePin(pin Pin, state State) {
setReg := p/32 + 7 setReg := p/32 + 7
memlock.Lock() memlock.Lock()
defer memlock.Unlock()
if state == Low { if state == Low {
mem[clearReg] = 1 << (p & 31) mem[clearReg] = 1 << (p & 31)
@ -201,7 +202,6 @@ func WritePin(pin Pin, state State) {
mem[setReg] = 1 << (p & 31) mem[setReg] = 1 << (p & 31)
} }
memlock.Unlock()
} }
// Read the state of a pin // Read the state of a pin
@ -234,6 +234,7 @@ func PullMode(pin Pin, pull Pull) {
shift := (uint8(pin) % 32) shift := (uint8(pin) % 32)
memlock.Lock() memlock.Lock()
defer memlock.Unlock()
switch pull { switch pull {
case PullDown, PullUp: case PullDown, PullUp:
@ -253,8 +254,6 @@ func PullMode(pin Pin, pull Pull) {
mem[pullReg] = mem[pullReg] &^ 3 mem[pullReg] = mem[pullReg] &^ 3
mem[pullClkReg] = 0 mem[pullClkReg] = 0
memlock.Unlock()
} }
// Open and memory map GPIO memory range from /dev/mem . // Open and memory map GPIO memory range from /dev/mem .
@ -276,6 +275,7 @@ func Open() (err error) {
defer file.Close() defer file.Close()
memlock.Lock() memlock.Lock()
defer memlock.Unlock()
// Memory map GPIO registers to byte array // Memory map GPIO registers to byte array
mem8, err = syscall.Mmap( mem8, err = syscall.Mmap(
@ -296,13 +296,12 @@ func Open() (err error) {
mem = *(*[]uint32)(unsafe.Pointer(&header)) mem = *(*[]uint32)(unsafe.Pointer(&header))
memlock.Unlock()
return nil return nil
} }
// Close unmaps GPIO memory // Close unmaps GPIO memory
func Close() { func Close() error {
memlock.Lock() memlock.Lock()
syscall.Munmap(mem8) defer memlock.Unlock()
memlock.Unlock() return syscall.Munmap(mem8)
} }