Lock tuning and registry release fix.

Change-Id: Id075e6f1e2adff994017398f79b4555ac00dfcec
Signed-off-by: Juha Hyttinen <juha.hyttinen@nokia.com>
diff --git a/pkg/control/registry.go b/pkg/control/registry.go
index 98aa97e..03e90ae 100644
--- a/pkg/control/registry.go
+++ b/pkg/control/registry.go
@@ -24,11 +24,11 @@
 	"sync"
 )
 
-var registryMutex = &sync.Mutex{}
 
 type Registry struct {
 	register map[uint16]bool
 	counter  uint16
+	mutex sync.Mutex
 }
 
 // This method should run as a constructor
@@ -40,8 +40,8 @@
 // Reserves and returns the next free sequence number
 func (r *Registry) ReserveSequenceNumber() (uint16, bool) {
 	// Check is current SequenceNumber valid
-	registryMutex.Lock()
-	defer registryMutex.Unlock()
+	r.mutex.Lock()
+	defer r.mutex.Unlock()
 	sequenceNumber := r.counter
 	if _, ok := r.register[sequenceNumber]; ok {
 		xapp.Logger.Error("Invalid SeqenceNumber sequenceNumber: %v",sequenceNumber)
@@ -60,8 +60,8 @@
 
 // This function checks the validity of the given subscription id
 func (r *Registry) IsValidSequenceNumber(sn uint16) bool {
-	registryMutex.Lock()
-	defer registryMutex.Unlock()
+	r.mutex.Lock()
+	defer r.mutex.Unlock()
 	xapp.Logger.Debug("Registry map: %v", r.register)
 	if _, ok := r.register[sn]; ok {
 		return true
@@ -71,26 +71,26 @@
 
 // This function sets the give id as confirmed in the register
 func (r *Registry) setSubscriptionToConfirmed(sn uint16) {
-	registryMutex.Lock()
-	defer registryMutex.Unlock()
+	r.mutex.Lock()
+	defer r.mutex.Unlock()
 	r.register[sn] = true
 }
 
 //This function sets the given id as unused in the register
 func (r *Registry) deleteSubscription(sn uint16) {
-	registryMutex.Lock()
-	defer registryMutex.Unlock()
+	r.mutex.Lock()
+	defer r.mutex.Unlock()
 	r.register[sn] = false
 }
 
 //This function releases the given id as unused in the register
 func (r *Registry) releaseSequenceNumber(sn uint16) bool {
-	registryMutex.Lock()
-	defer registryMutex.Unlock()
-	if r.register[sn] {
-		return false
-		} else {
+	r.mutex.Lock()
+	defer r.mutex.Unlock()
+	if _, ok := r.register[sn]; ok {
 		delete(r.register, sn)
 		return true
+	} else {
+		return false
 	}
 }