Corrections of the ISP thread cancellation sequence 25/161825/1
authorSylwester Nawrocki <s.nawrocki@samsung.com>
Mon, 27 Nov 2017 15:57:22 +0000 (16:57 +0100)
committerSylwester Nawrocki <s.nawrocki@samsung.com>
Mon, 27 Nov 2017 15:57:22 +0000 (16:57 +0100)
With only pthread_setcancelstate(), pthread_cancel() the front camera ISP
thread exit sequence might take too long so the isp_thread_cancel flag is used.

Change-Id: I2246d871f226611aeace387155b9ec33a75860ce
Signed-off-by: Sylwester Nawrocki <s.nawrocki@samsung.com>
camerasrc/src/camerasrc.c
camerasrc/src/include/camerasrc-common.h

index 07ab5392ad471cdd5b7ffe808b1b31e140a72a87..565695d0c0fcbc75316c6091fcab9fdc115979d8 100644 (file)
@@ -182,7 +182,6 @@ static int _camerasrc_get_device_type_string(camerasrc_handle_t *p, int fd, int
 #define CAMERASRC_SET_CMD(cmd, value) _camerasrc_set_cmd(handle, cmd, (void*)value);
 #define CAMERASRC_GET_CMD(cmd, value) _camerasrc_get_cmd(handle, cmd, (void*)value);
 
-static int do_exit = 0; // temporary !!
 
 void __print_time(const char *str, struct timeval *last_tv)
 {
@@ -1234,6 +1233,7 @@ static int _camerasrc_initialize_handle(camerasrc_handle_t *handle)
        handle->cur_af_range = CAMERASRC_AF_RANGE_NORMAL;
        handle->af_usr_data = NULL;
        handle->af_cb = NULL;
+       handle->isp_thread_cancel = FALSE;
        handle->first_frame = 1;
        handle->frame_count = 0;
        handle->request_count = 0;
@@ -1386,7 +1386,7 @@ static void *_camerasrc_front_isp_thread_func(void *arg)
 
        /* __print_timestamp(__func__, __LINE__, &last_tv); */
 
-       while (!do_exit) {
+       while (!p->isp_thread_cancel) {
                /* SENSOR - DQBUF */
                ret = _camerasrc_ioctl_dqbuf(p, p->fd_sensor,
                                             V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
@@ -1398,6 +1398,10 @@ static void *_camerasrc_front_isp_thread_func(void *arg)
                        break;
                }
 
+               ret = pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
+               if (ret != 0)
+                       camsrc_error("[ISP] pthread_setcancelstate failed");
+
                shot_sensor = (struct camera2_shot_ext *)p->buffer_sensor[buf_index_sensor].planes[1].start;
                timestamp = shot_sensor->shot.dm.sensor.timeStamp;
                p->frame_count = shot_sensor->shot.dm.request.frameCount;
@@ -1474,6 +1478,10 @@ static void *_camerasrc_front_isp_thread_func(void *arg)
                        camsrc_debug("[ISP] DQBUF index %d", buf_index_isp);
                }
 
+               ret = pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
+               if (ret != 0)
+                       camsrc_error("[ISP] pthread_setcancelstate failed");
+
                buf_index_isp = buf_index_3aa_cap;
 
                shot_isp = (struct camera2_shot_ext *)p->buffer_isp[buf_index_isp].planes[1].start;
@@ -1520,6 +1528,9 @@ static void *_camerasrc_front_isp_thread_func(void *arg)
                }
 
                camsrc_debug("[SENSOR] QBUF index %d", buf_index_sensor);
+
+               /* Short delay to allow cancelling of the thread */
+               g_usleep(1000);
        }
 
        return NULL;
@@ -1542,7 +1553,7 @@ void *_camerasrc_rear_isp_thread_func(void *arg)
 
        /* __print_timestamp(__func__, __LINE__, &last_tv); */
 
-       while (!do_exit) {
+       while (!p->isp_thread_cancel) {
                ret = _camerasrc_ioctl_dqbuf(p,
                                             p->fd_sensor,
                                             V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
@@ -1555,6 +1566,10 @@ void *_camerasrc_rear_isp_thread_func(void *arg)
                        break;
                }
 
+               ret = pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
+               if (ret != 0)
+                       camsrc_error("[ISP] pthread_setcancelstate failed");
+
                p->qbuf_count_sensor--;
                sensor_buf_count++;
 
@@ -1720,6 +1735,10 @@ void *_camerasrc_rear_isp_thread_func(void *arg)
                camsrc_debug("[3AA - CAPTURE] QBUF index %d", buf_index_3aa_cap);
                camsrc_debug("[Sensor] buffer count: %d\n", sensor_buf_count);
 
+               ret = pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
+               if (ret != 0)
+                       camsrc_error("[ISP] pthread_setcancelstate failed");
+
                /* FIXME: This delay prevents the tearing issue */
                g_usleep((G_USEC_PER_SEC / CAMERASRC_PRIMARY_FRAME_RATE) + 1);
        }
@@ -3099,9 +3118,15 @@ int camerasrc_stop_stream(camsrc_handle_t handle)
 
        camsrc_info("Stopping streaming and releasing buffers...");
 
-       /* FIXME */
-       do_exit = 1;
-       pthread_join(p->isp_thread, 0);
+       p->isp_thread_cancel = TRUE;
+
+       ret = pthread_cancel(p->isp_thread);
+       if (ret != 0)
+               camsrc_error("[ISP] ISP thread cancel failed[%x]", ret);
+
+       ret = pthread_join(p->isp_thread, 0);
+       if (ret != 0)
+               camsrc_error("[ISP] ISP thread join failed[%x]", ret);
 
        /* STREAM OFF */
        if (p->cur_dev_id == CAMERASRC_DEV_ID_PRIMARY) {
index c7fa0fdf12f08c6142a30bc5003c14b7a0582369..1b508fe2f0eb4c5d28c2f9262a676a3380500173 100644 (file)
@@ -447,7 +447,7 @@ typedef struct _camerasrc_handle_t {
 
        /* ISP thread data */
        pthread_t isp_thread;
-       int isp_thread_run;
+       int isp_thread_cancel;
 
        /* Jpg Still information */
        camerasrc_exif_info_t current_exif;     /* EXIF info of current captured image */