modules: camera: replace pthread mutex with utils'

Change-Id: Id974e4cc743d27d59634023cfdbba545fbb64e5f
diff --git a/modules/camera/Camera.cpp b/modules/camera/Camera.cpp
index a3164e5..57cdb47 100644
--- a/modules/camera/Camera.cpp
+++ b/modules/camera/Camera.cpp
@@ -15,12 +15,12 @@
  */
 
 #include <cstdlib>
-#include <pthread.h>
 #include <stdio.h>
 #include <hardware/camera3.h>
 #include <sync/sync.h>
 #include <system/camera_metadata.h>
 #include <system/graphics.h>
+#include <utils/Mutex.h>
 #include "CameraHAL.h"
 #include "Metadata.h"
 #include "Stream.h"
@@ -57,9 +57,6 @@
     mNumStreams(0),
     mSettings(NULL)
 {
-    pthread_mutex_init(&mMutex, NULL);
-    pthread_mutex_init(&mStaticInfoMutex, NULL);
-
     memset(&mTemplates, 0, sizeof(mTemplates));
     memset(&mDevice, 0, sizeof(mDevice));
     mDevice.common.tag    = HARDWARE_DEVICE_TAG;
@@ -71,8 +68,6 @@
 
 Camera::~Camera()
 {
-    pthread_mutex_destroy(&mMutex);
-    pthread_mutex_destroy(&mStaticInfoMutex);
     if (mStaticInfo != NULL) {
         free_camera_metadata(mStaticInfo);
     }
@@ -82,9 +77,9 @@
 {
     ALOGI("%s:%d: Opening camera device", __func__, mId);
     ATRACE_CALL();
-    pthread_mutex_lock(&mMutex);
+    android::Mutex::Autolock al(mDeviceLock);
+
     if (mBusy) {
-        pthread_mutex_unlock(&mMutex);
         ALOGE("%s:%d: Error! Camera device already opened", __func__, mId);
         return -EBUSY;
     }
@@ -93,25 +88,20 @@
     mBusy = true;
     mDevice.common.module = const_cast<hw_module_t*>(module);
     *device = &mDevice.common;
-
-    pthread_mutex_unlock(&mMutex);
     return 0;
 }
 
 int Camera::getInfo(struct camera_info *info)
 {
+    android::Mutex::Autolock al(mStaticInfoLock);
+
     info->facing = CAMERA_FACING_FRONT;
     info->orientation = 0;
     info->device_version = mDevice.common.version;
-
-    pthread_mutex_lock(&mStaticInfoMutex);
     if (mStaticInfo == NULL) {
         mStaticInfo = initStaticInfo();
     }
-    pthread_mutex_unlock(&mStaticInfoMutex);
-
     info->static_camera_characteristics = mStaticInfo;
-
     return 0;
 }
 
@@ -119,17 +109,15 @@
 {
     ALOGI("%s:%d: Closing camera device", __func__, mId);
     ATRACE_CALL();
-    pthread_mutex_lock(&mMutex);
+    android::Mutex::Autolock al(mDeviceLock);
+
     if (!mBusy) {
-        pthread_mutex_unlock(&mMutex);
         ALOGE("%s:%d: Error! Camera device not open", __func__, mId);
         return -EINVAL;
     }
 
     // TODO: close camera dev nodes, etc
     mBusy = false;
-
-    pthread_mutex_unlock(&mMutex);
     return 0;
 }
 
@@ -153,8 +141,9 @@
     camera3_stream_t *astream;
     Stream **newStreams = NULL;
 
-    ATRACE_CALL();
     ALOGV("%s:%d: stream_config=%p", __func__, mId, stream_config);
+    ATRACE_CALL();
+    android::Mutex::Autolock al(mDeviceLock);
 
     if (stream_config == NULL) {
         ALOGE("%s:%d: NULL stream configuration array", __func__, mId);
@@ -170,8 +159,6 @@
     ALOGV("%s:%d: Number of Streams: %d", __func__, mId,
             stream_config->num_streams);
 
-    pthread_mutex_lock(&mMutex);
-
     // Mark all current streams unused for now
     for (int i = 0; i < mNumStreams; i++)
         mStreams[i]->mReuse = false;
@@ -209,14 +196,11 @@
 
     // Clear out last seen settings metadata
     setSettings(NULL);
-
-    pthread_mutex_unlock(&mMutex);
     return 0;
 
 err_out:
     // Clean up temporary streams, preserve existing mStreams/mNumStreams
     destroyStreams(newStreams, stream_config->num_streams);
-    pthread_mutex_unlock(&mMutex);
     return -EINVAL;
 }
 
@@ -492,8 +476,8 @@
 void Camera::dump(int fd)
 {
     ALOGV("%s:%d: Dumping to fd %d", __func__, mId, fd);
-
-    pthread_mutex_lock(&mMutex);
+    ATRACE_CALL();
+    android::Mutex::Autolock al(mDeviceLock);
 
     fdprintf(fd, "Camera ID: %d (Busy: %d)\n", mId, mBusy);
 
@@ -505,8 +489,6 @@
         fdprintf(fd, "Stream %d/%d:\n", i, mNumStreams);
         mStreams[i]->dump(fd);
     }
-
-    pthread_mutex_unlock(&mMutex);
 }
 
 const char* Camera::templateToString(int type)
@@ -529,26 +511,26 @@
 
 int Camera::setTemplate(int type, camera_metadata_t *settings)
 {
+    android::Mutex::Autolock al(mDeviceLock);
+
     if (!isValidTemplateType(type)) {
         ALOGE("%s:%d: Invalid template request type: %d", __func__, mId, type);
         return -EINVAL;
     }
-    pthread_mutex_lock(&mMutex);
+
     if (mTemplates[type] != NULL) {
         ALOGE("%s:%d: Setting already constructed template type %s(%d)",
                 __func__, mId, templateToString(type), type);
-        pthread_mutex_unlock(&mMutex);
         return -EINVAL;
     }
+
     // Make a durable copy of the underlying metadata
     mTemplates[type] = clone_camera_metadata(settings);
     if (mTemplates[type] == NULL) {
         ALOGE("%s:%d: Failed to clone metadata %p for template type %s(%d)",
                 __func__, mId, settings, templateToString(type), type);
-        pthread_mutex_unlock(&mMutex);
         return -EINVAL;
     }
-    pthread_mutex_unlock(&mMutex);
     return 0;
 }
 
diff --git a/modules/camera/Camera.h b/modules/camera/Camera.h
index 4c869bb..f1f33b1 100644
--- a/modules/camera/Camera.h
+++ b/modules/camera/Camera.h
@@ -17,9 +17,9 @@
 #ifndef CAMERA_H_
 #define CAMERA_H_
 
-#include <pthread.h>
 #include <hardware/hardware.h>
 #include <hardware/camera3.h>
+#include <utils/Mutex.h>
 #include "Metadata.h"
 #include "Stream.h"
 
@@ -100,10 +100,10 @@
         // Methods used to call back into the framework
         const camera3_callback_ops_t *mCallbackOps;
         // Lock protecting the Camera object for modifications
-        pthread_mutex_t mMutex;
+        android::Mutex mDeviceLock;
         // Lock protecting only static camera characteristics, which may
         // be accessed without the camera device open
-        pthread_mutex_t mStaticInfoMutex;
+        android::Mutex mStaticInfoLock;
         // Array of handles to streams currently in use by the device
         Stream **mStreams;
         // Number of streams in mStreams
diff --git a/modules/camera/Stream.cpp b/modules/camera/Stream.cpp
index 7703910..9b9ab98 100644
--- a/modules/camera/Stream.cpp
+++ b/modules/camera/Stream.cpp
@@ -14,11 +14,11 @@
  * limitations under the License.
  */
 
-#include <pthread.h>
 #include <stdio.h>
 #include <hardware/camera3.h>
 #include <hardware/gralloc.h>
 #include <system/graphics.h>
+#include <utils/Mutex.h>
 
 //#define LOG_NDEBUG 0
 #define LOG_TAG "Stream"
@@ -45,37 +45,32 @@
     mBuffers(0),
     mNumBuffers(0)
 {
-    // NULL (default) pthread mutex attributes
-    pthread_mutex_init(&mMutex, NULL);
 }
 
 Stream::~Stream()
 {
-    pthread_mutex_lock(&mMutex);
+    android::Mutex::Autolock al(mLock);
     unregisterBuffers_L();
-    pthread_mutex_unlock(&mMutex);
 }
 
 void Stream::setUsage(uint32_t usage)
 {
-    pthread_mutex_lock(&mMutex);
+    android::Mutex::Autolock al(mLock);
     if (usage != mUsage) {
         mUsage = usage;
         mStream->usage = usage;
         unregisterBuffers_L();
     }
-    pthread_mutex_unlock(&mMutex);
 }
 
 void Stream::setMaxBuffers(uint32_t max_buffers)
 {
-    pthread_mutex_lock(&mMutex);
+    android::Mutex::Autolock al(mLock);
     if (max_buffers != mMaxBuffers) {
         mMaxBuffers = max_buffers;
         mStream->max_buffers = max_buffers;
         unregisterBuffers_L();
     }
-    pthread_mutex_unlock(&mMutex);
 }
 
 int Stream::getType()
@@ -195,6 +190,7 @@
 int Stream::registerBuffers(const camera3_stream_buffer_set_t *buf_set)
 {
     ATRACE_CALL();
+    android::Mutex::Autolock al(mLock);
 
     if (buf_set->stream != mStream) {
         ALOGE("%s:%d: Buffer set for invalid stream. Got %p expect %p",
@@ -202,8 +198,6 @@
         return -EINVAL;
     }
 
-    pthread_mutex_lock(&mMutex);
-
     mNumBuffers = buf_set->num_buffers;
     mBuffers = new buffer_handle_t*[mNumBuffers];
 
@@ -215,12 +209,10 @@
     }
     mRegistered = true;
 
-    pthread_mutex_unlock(&mMutex);
-
     return 0;
 }
 
-// This must only be called with mMutex held
+// This must only be called with mLock held
 void Stream::unregisterBuffers_L()
 {
     mRegistered = false;
@@ -231,7 +223,7 @@
 
 void Stream::dump(int fd)
 {
-    pthread_mutex_lock(&mMutex);
+    android::Mutex::Autolock al(mLock);
 
     fdprintf(fd, "Stream ID: %d (%p)\n", mId, mStream);
     fdprintf(fd, "Stream Type: %s (%d)\n", typeToString(mType), mType);
@@ -245,8 +237,6 @@
     for (int i = 0; i < mNumBuffers; i++) {
         fdprintf(fd, "Buffer %d/%d: %p\n", i, mNumBuffers, mBuffers[i]);
     }
-
-    pthread_mutex_unlock(&mMutex);
 }
 
 } // namespace default_camera_hal
diff --git a/modules/camera/Stream.h b/modules/camera/Stream.h
index b62114e..5efbc52 100644
--- a/modules/camera/Stream.h
+++ b/modules/camera/Stream.h
@@ -20,6 +20,7 @@
 #include <hardware/camera3.h>
 #include <hardware/gralloc.h>
 #include <system/graphics.h>
+#include <utils/Mutex.h>
 
 namespace default_camera_hal {
 // Stream represents a single input or output stream for a camera device.
@@ -49,7 +50,7 @@
         bool mReuse;
 
     private:
-        // Clean up buffer state. must be called with mMutex held.
+        // Clean up buffer state. must be called with mLock held.
         void unregisterBuffers_L();
 
         // The camera device id this stream belongs to
@@ -75,7 +76,7 @@
         // Number of buffers in mBuffers
         unsigned int mNumBuffers;
         // Lock protecting the Stream object for modifications
-        pthread_mutex_t mMutex;
+        android::Mutex mLock;
 };
 } // namespace default_camera_hal