resolve deadlock problem between multi threads.
authorjy910.yun <jy910.yun@samsung.com>
Fri, 12 Apr 2013 13:38:18 +0000 (22:38 +0900)
committerjy910.yun <jy910.yun@samsung.com>
Fri, 12 Apr 2013 13:43:05 +0000 (22:43 +0900)
solution : the part of sending to system server is changed the position.
before sending part is in thread func.
this mean is that other process try to access same resource like socket.

Signed-off-by: jy910.yun <jy910.yun@samsung.com>
Change-Id: Ibdb86e6b658a424f9591f08b64a6244aed8a3827

tizen/DEVICE/include/file.h
tizen/DEVICE/src/file.c
tizen/DEVICE/src/haptic.c

index bfc3a23..798ad5d 100644 (file)
@@ -70,5 +70,6 @@ int PlayBuffer(int handle, const unsigned char *vibe_buffer, int iteration, int
 int Stop(int handle);
 int OpenDevice(int handle);
 int CloseDevice(int handle);
+int GetState(int handle, int *state);
 
 #endif // __FIEL_H__
index 0f6b626..e3d4341 100644 (file)
@@ -31,6 +31,9 @@
 #define MAX_LEVEL                              255.0f
 #define DEFAULT_EFFECT_HANDLE  0x02
 
+#define STATE_PLAY     0
+#define STATE_STOP     1
+
 #define PREDEF_HAPTIC           "haptic"
 
 enum {
@@ -67,6 +70,22 @@ static int _check_valid_haptic_format(HapticFile *file)
        return 0;
 }
 
+static int __haptic_predefine_action(int handle, int prop, int val)
+{
+       char buf_pid[32];
+       char buf_prop[32];
+       char buf_handle[32];
+       char buf_val[32];
+
+       snprintf(buf_pid, sizeof(buf_pid), "%d", getpid());
+       snprintf(buf_prop, sizeof(buf_prop), "%d", prop);
+       snprintf(buf_handle, sizeof(buf_handle), "%d", handle);
+       snprintf(buf_val, sizeof(buf_val), "%d", val);
+
+       MODULE_LOG("pid : %s(%d), prop : %s, handle : %s", buf_pid, pthread_self(), buf_prop, buf_handle);
+       return __haptic_call_predef_action(PREDEF_HAPTIC, 4, buf_pid, buf_prop, buf_handle, buf_val);
+}
+
 static int _create_thread(void* data, void*(*func)(void*))
 {
        if (tid) {
@@ -92,6 +111,8 @@ static int _cancel_thread(void)
                return 0;
        }
 
+       __haptic_predefine_action(gbuffer.handle, STOP, NULL);
+
        if ((ret = pthread_cancel(tid)) < 0) {
                MODULE_ERROR("pthread_cancel is failed : %s, ret(%d)", strerror(errno), ret);
                return -1;
@@ -113,29 +134,12 @@ static int _cancel_thread(void)
        return 0;
 }
 
-static int __haptic_predefine_action(int handle, int prop, int val)
-{
-       char buf_pid[32];
-       char buf_prop[32];
-       char buf_handle[32];
-       char buf_val[32];
-
-       snprintf(buf_pid, sizeof(buf_pid), "%d", getpid());
-       snprintf(buf_prop, sizeof(buf_prop), "%d", prop);
-       snprintf(buf_handle, sizeof(buf_handle), "%d", handle);
-       snprintf(buf_val, sizeof(buf_val), "%d", val);
-
-       MODULE_LOG("pid : %s, prop : %s, handle : %s", buf_pid, buf_prop, buf_handle);
-       return __haptic_call_predef_action(PREDEF_HAPTIC, 4, buf_pid, buf_prop, buf_handle, buf_val);
-}
-
 static void __clean_up(void *arg)
 {
        BUFFER *pbuffer = (BUFFER*)arg;
        int i;
 
        MODULE_LOG("clean up handler!!! : %d", tid);
-       __haptic_predefine_action(pbuffer->handle, STOP, NULL);
 
        for (i = 0; i < pbuffer->channels; ++i) {
                free(pbuffer->ppbuffer[i]);
@@ -160,8 +164,6 @@ static void* __play_cb(void *arg)
 
        pthread_cleanup_push(__clean_up, arg);
 
-       __haptic_predefine_action(pbuffer->handle, PLAY, NULL);
-
        /* Copy buffer from source buffer */
        for (i = 0; i < pbuffer->iteration; i++) {
                for (j = 0; j < pbuffer->length; ++j) {
@@ -357,6 +359,8 @@ int PlayBuffer(int handle, const unsigned char *vibe_buffer, int iteration, int
        gbuffer.length = length;
        gbuffer.iteration = iteration;
 
+       __haptic_predefine_action(gbuffer.handle, PLAY, NULL);
+
        /* Start thread */
        if (_create_thread(&gbuffer, __play_cb) < 0) {
                MODULE_ERROR("_create_thread fail");
@@ -417,3 +421,14 @@ int CloseDevice(int handle)
        MODULE_LOG("pid : %s, prop : %s, handle : %s", buf_pid, buf_prop, buf_handle);
        return __haptic_call_predef_action(PREDEF_HAPTIC, 3, buf_pid, buf_prop, buf_handle);
 }
+
+int GetState(int handle, int *state)
+{
+       if (gbuffer.handle == handle) {
+               *state = STATE_PLAY;
+               return 0;
+       }
+
+       *state = STATE_STOP;
+       return 0;
+}
index e80341b..e7605de 100644 (file)
 #define EXTAPI __attribute__ ((visibility("default")))
 #endif
 
+#define DEFAULT_EFFECT_HANDLE  0xFFFF
 #define DEFAULT_MOTOR_COUNT            1
-#define DEFAULT_DEVICE_HANDLE  0x01
-#define DEFAULT_EFFECT_HANDLE  0x02
-#define HAPTIC_FEEDBACK_AUTO   101
 #define HAPTIC_PLAY_FILE_EXT   ".tht"
 
 /* START of Static Function Section */
@@ -231,11 +229,11 @@ static int __vibrate(int device_handle, const unsigned char *vibe_buffer, int it
 
 static void *_create_handle(void)
 {
-       return ((getpid()<<16)|time(NULL));
+       static int i = 0;
+       return ((getpid()<<16)|(time(NULL)+(i++)));
 }
 /* END of Static Function Section */
 
-
 static int _get_device_count(int *count)
 {
        if (count == NULL)
@@ -355,11 +353,14 @@ static int _vibrate_file(int device_handle, const char *file_path, int iteration
        status = __vibrate(device_handle, vibe_buffer, iteration, feedback, priority);
        free(vibe_buffer);
 
+       *effect_handle = DEFAULT_EFFECT_HANDLE;
        return status;
 }
 
 static int _vibrate_buffer(int device_handle, const unsigned char *vibe_buffer, int iteration, int feedback, int priority, int *effect_handle)
 {
+       int status;
+
        if (device_handle < 0)
                return HAPTIC_MODULE_INVALID_ARGUMENT;
 
@@ -381,7 +382,10 @@ static int _vibrate_buffer(int device_handle, const unsigned char *vibe_buffer,
        if (feedback == HAPTIC_MODULE_FEEDBACK_MIN)
                return HAPTIC_MODULE_ERROR_NONE;
 
-       return __vibrate(device_handle, vibe_buffer, iteration, feedback, priority);
+       status = __vibrate(device_handle, vibe_buffer, iteration, feedback, priority);
+
+       *effect_handle = DEFAULT_EFFECT_HANDLE;
+       return status;
 }
 
 static int _stop_effect(int device_handle, int effect_handle)
@@ -445,6 +449,9 @@ static int _resume_effect(int device_handle, int effect_handle)
 
 static int _get_effect_state(int device_handle, int effect_handle, int *state)
 {
+       int status;
+       int cur_state;
+
        if (device_handle < 0)
                return HAPTIC_MODULE_INVALID_ARGUMENT;
 
@@ -454,8 +461,14 @@ static int _get_effect_state(int device_handle, int effect_handle, int *state)
        if (state == NULL)
                return HAPTIC_MODULE_INVALID_ARGUMENT;
 
-       MODULE_ERROR("This device is not supported this function(%s)", __func__);
-       return HAPTIC_MODULE_NOT_SUPPORTED;
+       status = GetState(device_handle, &cur_state);
+       if (status < 0) {
+               MODULE_ERROR("GetState fail : %d", status);
+               return HAPTIC_MODULE_OPERATION_FAILED;
+       }
+
+       *state = cur_state;
+       return HAPTIC_MODULE_ERROR_NONE;
 }
 
 static int _create_effect(unsigned char *vibe_buffer, int max_bufsize, haptic_module_effect_element *elem_arr, int max_elemcnt)