Lock tuning and registry release fix.

Change-Id: Id075e6f1e2adff994017398f79b4555ac00dfcec
Signed-off-by: Juha Hyttinen <juha.hyttinen@nokia.com>
diff --git a/pkg/control/control.go b/pkg/control/control.go
index 7e2d673..d27844e 100644
--- a/pkg/control/control.go
+++ b/pkg/control/control.go
@@ -111,9 +111,10 @@
 func (c *Control) rmrSend(params *xapp.RMRParams) (err error) {
 	status := false
 	i := 1
-	rmrSendMutex.Lock()
 	for ; i <= 10 && status == false; i++ { 
+		rmrSendMutex.Lock()
 		status = xapp.Rmr.Send(params, false)
+		rmrSendMutex.Unlock()
 		if status == false {
 			xapp.Logger.Info("rmr.Send() failed. Retry count %v, Mtype: %v, SubId: %v, Xid %s",i, params.Mtype, params.SubId, params.Xid)
 			time.Sleep(500 * time.Millisecond)
@@ -123,7 +124,6 @@
 		err = errors.New("rmr.Send() failed")
 		xapp.Rmr.Free(params.Mbuf)
 	}
-	rmrSendMutex.Unlock()
 	
 	/*
 	if !xapp.Rmr.Send(params, false) {
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
 	}
 }
diff --git a/pkg/control/tracker.go b/pkg/control/tracker.go
index dfab96e..9b984a3 100644
--- a/pkg/control/tracker.go
+++ b/pkg/control/tracker.go
@@ -24,13 +24,12 @@
 	"sync"
 )
 
-var trackerMutex = &sync.Mutex{}
-
 /*
 Implements a record of ongoing transactions and helper functions to CRUD the records.
 */
 type Tracker struct {
 	transactionTable map[TransactionKey]Transaction
+	mutex sync.Mutex
 }
 
 func (t *Tracker) Init() {
@@ -42,8 +41,8 @@
 Returns error if there is similar transatcion ongoing.
 */
 func (t *Tracker) TrackTransaction(key TransactionKey, xact Transaction) error {
-	trackerMutex.Lock()
-	defer trackerMutex.Unlock()
+	t.mutex.Lock()
+	defer t.mutex.Unlock()
 	if _, ok := t.transactionTable[key]; ok {
 		// TODO: Implement merge related check here. If the key is same but the value is different.
 		err := fmt.Errorf("transaction tracker: Similar transaction with sub id %d and type %s is ongoing", key.SubID, key.transType)
@@ -59,8 +58,8 @@
 */
 func (t *Tracker) UpdateTransaction(SubID uint16, transType Action, xact Transaction) error {
 	key := TransactionKey{SubID, transType}
-	trackerMutex.Lock()
-	defer trackerMutex.Unlock()
+	t.mutex.Lock()
+	defer t.mutex.Unlock()
 	if _, ok := t.transactionTable[key]; ok {
 		// TODO: Implement merge related check here. If the key is same but the value is different.
 		err := fmt.Errorf("transaction tracker: Similar transaction with sub id %d and type %v is ongoing", key.SubID, key.transType)
@@ -76,8 +75,8 @@
 */
 func (t *Tracker) RetriveTransaction(subID uint16, act Action) (Transaction, error) {
 	key := TransactionKey{subID, act}
-	trackerMutex.Lock()
-	defer trackerMutex.Unlock()
+	t.mutex.Lock()
+	defer t.mutex.Unlock()
 	var xact Transaction
 	if xact, ok := t.transactionTable[key]; ok {
 		return xact, nil
@@ -93,8 +92,8 @@
 func (t *Tracker) completeTransaction(subID uint16, act Action) (Transaction, error) {
 	key := TransactionKey{subID, act}
 	var emptyTransaction Transaction
-	trackerMutex.Lock()
-	defer trackerMutex.Unlock()
+	t.mutex.Lock()
+	defer t.mutex.Unlock()
 	if xact, ok := t.transactionTable[key]; ok {
 		delete(t.transactionTable, key)
 		return xact, nil