AGM: protect graph_read/graph_write with mutex

graph_read/graph_write both should be protected with mutex since
the sess_obj->graph can be freed in session_close()

Change-Id: Iab0cce243bfe4737afa231e4d908087df157506e
diff --git a/service/src/session_obj.c b/service/src/session_obj.c
index 1ae4057..1319543 100644
--- a/service/src/session_obj.c
+++ b/service/src/session_obj.c
@@ -2362,10 +2362,8 @@
         AGM_LOGE("Cannot issue read in state:%d\n",
                            sess_obj->state);
         ret = -EINVAL;
-        pthread_mutex_unlock(&sess_obj->lock);
         goto done;
     }
-    pthread_mutex_unlock(&sess_obj->lock);
 
     buffer.timestamp = 0x0;
     buffer.flags = 0;
@@ -2378,6 +2376,7 @@
     }
 
 done:
+    pthread_mutex_unlock(&sess_obj->lock);
     return ret;
 }
 
@@ -2391,10 +2390,8 @@
         AGM_LOGE("Cannot issue write in state:%d\n",
                             sess_obj->state);
         ret = -EINVAL;
-        pthread_mutex_unlock(&sess_obj->lock);
         goto done;
     }
-    pthread_mutex_unlock(&sess_obj->lock);
 
     buffer.timestamp = 0x0;
     buffer.flags = 0;
@@ -2407,6 +2404,7 @@
     }
 
 done:
+    pthread_mutex_unlock(&sess_obj->lock);
     return ret;
 }
 
@@ -2652,16 +2650,16 @@
         AGM_LOGE("Cannot issue write in state:%d\n",
                             sess_obj->state);
         ret = -EINVAL;
-        pthread_mutex_unlock(&sess_obj->lock);
         goto done;
     }
-    pthread_mutex_unlock(&sess_obj->lock);
+
     ret = graph_write(sess_obj->graph, buffer, consumed_size);
     if (ret) {
         AGM_LOGE("Error:%d writing to graph\n", ret);
     }
 
 done:
+    pthread_mutex_unlock(&sess_obj->lock);
     return ret;
 }
 
@@ -2675,10 +2673,8 @@
         AGM_LOGE("Cannot issue read in state:%d\n",
                            sess_obj->state);
         ret = -EINVAL;
-        pthread_mutex_unlock(&sess_obj->lock);
         goto done;
     }
-    pthread_mutex_unlock(&sess_obj->lock);
 
     size_t read_size;
     ret = graph_read(sess_obj->graph, buffer, &read_size);
@@ -2689,6 +2685,7 @@
     *captured_size = (uint32_t)read_size;
 
 done:
+    pthread_mutex_unlock(&sess_obj->lock);
     return ret;
 }