Move write_lock from attrs to each attribute
[platform/core/multimedia/libmm-common.git] / mm_attrs_private.c
index d37905c..f239b05 100644 (file)
@@ -19,7 +19,7 @@
  *
  */
 
+
 
 #include <stdio.h>
 #include <string.h>
@@ -50,16 +50,15 @@ int mmf_value_copy(mmf_value_t *dest, const mmf_value_t *src)
                dest->value.d_val = src->value.d_val;
                break;
        case MM_ATTRS_TYPE_STRING:
-               if (src->value.s_val)
-               {
-                       dest->value.s_val = strdup(src->value.s_val);
-                       dest->size = src->size;
-               }
-               else
-               {
+               if (dest->value.s_val) {
+                       free(dest->value.s_val);
                        dest->value.s_val = NULL;
                        dest->size = 0;
                }
+               if (src->value.s_val) {
+                       dest->value.s_val = strdup(src->value.s_val);
+                       dest->size = src->size;
+               }
                break;
        case MM_ATTRS_TYPE_DATA:
                dest->value.p_val = src->value.p_val;
@@ -116,7 +115,7 @@ int mmf_value_set_string(mmf_value_t *v, const char *sval,int size)
        return 0;
 }
 
-const char* mmf_value_get_string(const mmf_value_t *v, int *size)
+char* mmf_value_get_string(const mmf_value_t *v, int *size)
 {
        return_val_if_fail(v && v->type == MMF_VALUE_TYPE_STRING, NULL);
        *size=v->size;
@@ -143,19 +142,19 @@ void mmf_value_dump(const mmf_value_t *value)
        return_if_fail(value);
        switch (value->type) {
        case MMF_VALUE_TYPE_INT:
-               mmf_debug(MMF_DEBUG_LOG, "value[int]: %d\n", value->value.i_val);
+               //mmf_debug(MMF_DEBUG_LOG, "value[int]: %d\n", value->value.i_val);
                break;
        case MMF_VALUE_TYPE_DOUBLE:
-               mmf_debug(MMF_DEBUG_LOG, "value[double]: %f\n", value->value.d_val);
+               //mmf_debug(MMF_DEBUG_LOG, "value[double]: %f\n", value->value.d_val);
                break;
        case MMF_VALUE_TYPE_STRING:
-               mmf_debug(MMF_DEBUG_LOG, "value[string]: %s\n", value->value.s_val);
+               //mmf_debug(MMF_DEBUG_LOG, "value[string]: %s\n", value->value.s_val);
                break;
        case MMF_VALUE_TYPE_DATA:
-               mmf_debug(MMF_DEBUG_LOG, "value[data]: %p\n", value->value.p_val);
+               //mmf_debug(MMF_DEBUG_LOG, "value[data]: %p\n", value->value.p_val);
                break;
        default:
-               mmf_debug(MMF_DEBUG_LOG, "value invalid!!\n");
+               //mmf_debug(MMF_DEBUG_LOG, "value invalid!!\n");
                break;
        }
 }
@@ -339,16 +338,16 @@ bool mmf_attribute_validate_int(mmf_attribute_t *item, int val)
 {
        return_val_if_fail(item, false);
        return_val_if_fail(item->value.type == MMF_VALUE_TYPE_INT, false);
-       
+
        bool valid = true;
        int i = 0;
-       
+
        switch (item->value_spec.type) {
        case MMF_VALUE_SPEC_INT_RANGE:
-               if (val < item->value_spec.spec.int_spec.range.min || 
+               if (val < item->value_spec.spec.int_spec.range.min ||
                                val > item->value_spec.spec.int_spec.range.max) {
                        valid = false;
-                       mmf_debug(MMF_DEBUG_LOG, "[mmf_attribute:%s] out of range\n", item->name);
+                       //mmf_debug(MMF_DEBUG_LOG, "[mmf_attribute:%s] out of range\n", item->name);
                }
                break;
        case MMF_VALUE_SPEC_INT_ARRAY:
@@ -360,13 +359,13 @@ bool mmf_attribute_validate_int(mmf_attribute_t *item, int val)
                        }
                }
                if (!valid) {
-                       mmf_debug(MMF_DEBUG_LOG, "[mmf_attribute:%s] out of array\n", item->name);
+                       //mmf_debug(MMF_DEBUG_LOG, "[mmf_attribute:%s] out of array\n", item->name);
                }
                break;
        default:
-               break;          
+               break;
        }
-       
+
        return valid;
 }
 
@@ -374,16 +373,16 @@ bool mmf_attribute_validate_double(mmf_attribute_t *item, double val)
 {
        return_val_if_fail(item, false);
        return_val_if_fail(item->value.type == MMF_VALUE_TYPE_DOUBLE, false);
-       
+
        bool valid = true;
        int i = 0;
-       
+
        switch (item->value_spec.type) {
        case MMF_VALUE_SPEC_DOUBLE_RANGE:
-               if (val < item->value_spec.spec.double_spec.range.min || 
+               if (val < item->value_spec.spec.double_spec.range.min ||
                                val > item->value_spec.spec.double_spec.range.max) {
                        valid = false;
-                       mmf_debug(MMF_DEBUG_LOG, "[mmf_attribute:%s] out of range\n", item->name);
+                       //mmf_debug(MMF_DEBUG_LOG, "[mmf_attribute:%s] out of range\n", item->name);
                }
                break;
        case MMF_VALUE_SPEC_DOUBLE_ARRAY:
@@ -395,13 +394,13 @@ bool mmf_attribute_validate_double(mmf_attribute_t *item, double val)
                        }
                }
                if (!valid) {
-                       mmf_debug(MMF_DEBUG_LOG, "[mmf_attribute:%s] out of array\n", item->name);
+                       //mmf_debug(MMF_DEBUG_LOG, "[mmf_attribute:%s] out of array\n", item->name);
                }
                break;
        default:
-               break;          
+               break;
        }
-       
+
        return valid;
 }
 
@@ -416,6 +415,7 @@ void mmf_attribute_clear(mmf_attribute_t *item)
        mmf_value_clear(&item->tmpval);
        mmf_value_clear(&item->value);
        mmf_value_spec_clear(&item->value_spec);
+       pthread_mutex_destroy(&item->write_lock);
 }
 
 bool mmf_attribute_is_modified(mmf_attribute_t *item)
@@ -486,9 +486,9 @@ int mmf_attribute_set_string(mmf_attribute_t *item, const char *string, int size
        return_val_if_fail(item, -1);
 
        if (mmf_value_set_string(&item->tmpval, string,size) == 0) {
-               if (string) 
-                       item->flags |= MM_ATTRS_FLAG_MODIFIED; 
-               
+               if (string)
+                       item->flags |= MM_ATTRS_FLAG_MODIFIED;
+
                return 0;
        }
        return -1;
@@ -510,9 +510,23 @@ MMHandleType mmf_attrs_new(int count)
        return_val_if_fail(count > 0, 0);
        mmf_attrs_t *attrs;
        attrs = (mmf_attrs_t *) malloc (sizeof(mmf_attrs_t));
+
+       if (attrs == NULL) {
+               debug_error("malloc failed");
+               return 0;
+       }
+
        attrs->count = count;
        attrs->items = (mmf_attribute_t *) malloc (sizeof(mmf_attribute_t) * count);
+       if(attrs->items == NULL) {
+                debug_error("Failed to malloc for attrs->items.");
+                free(attrs);
+                attrs=NULL;
+                return 0;
+        }
+
        memset(attrs->items, 0, sizeof(mmf_attribute_t) * count);
+
        return (MMHandleType) attrs;
 }
 
@@ -527,6 +541,9 @@ MMHandleType mmf_attrs_new_from_data(const char *name,
        mmf_attrs_t *attrs;
 
        h = mmf_attrs_new(count);
+       if(!h) {
+               return 0;
+       }
        mmf_attrs_init(h, info, count);
        attrs = (mmf_attrs_t *) h;
        attrs->name = NULL;
@@ -562,46 +579,51 @@ int mmf_attrs_init(MMHandleType h, mmf_attrs_construct_info_t *info, int count)
 {
        return_val_if_fail(h && info && count > 0, -1);
        mmf_attrs_t *attrs = (mmf_attrs_t *) h;
+       mmf_attribute_t *item = NULL;
        int i;
        int size = 0;
 
        for (i = 0; i < count; i++) {
-               mmf_attribute_init(&attrs->items[i],
+               item = &attrs->items[i];
+
+               pthread_mutex_init(&item->write_lock, NULL);
+
+               mmf_attribute_init(item,
                                   info[i].name,
                                   info[i].value_type,
                                   info[i].flags);
 
                switch (info[i].value_type) {
                case MMF_VALUE_TYPE_INT:
-                       assert(attrs->items[i].value.value.i_val == 0);
-                       mmf_value_set_int(&attrs->items[i].value,
-                                         (int)info[i].default_value);
+                       assert(item->value.value.i_val == 0);
+                       mmf_value_set_int(&item->value,
+                                         (intptr_t)info[i].default_value);
                        break;
                case MMF_VALUE_TYPE_DOUBLE:
                {
-                       int i_val = (int)info[i].default_value;
+                       int i_val = (intptr_t)info[i].default_value;
                        double d_val = (double) i_val;
-                       assert(attrs->items[i].value.value.d_val == 0);
-                       mmf_value_set_double(&attrs->items[i].value, d_val);
+                       assert(item->value.value.d_val == 0);
+                       mmf_value_set_double(&item->value, d_val);
                        break;
                }
                case MMF_VALUE_TYPE_STRING:
-                       assert(attrs->items[i].value.value.s_val == NULL);
+                       assert(item->value.value.s_val == NULL);
                        if (info[i].default_value) {
                                size = strlen(info[i].default_value)+1;
                        }
-                       mmf_value_set_string(&attrs->items[i].value,
+                       mmf_value_set_string(&item->value,
                                             (const char *)info[i].default_value,size);
                        break;
                case MMF_VALUE_TYPE_DATA:
-                       assert(attrs->items[i].value.value.p_val == NULL);
+                       assert(item->value.value.p_val == NULL);
                        if (info[i].default_value) {
                                size = sizeof(info[i].default_value)+1;
                        }
-                       mmf_value_set_data(&attrs->items[i].value, info[i].default_value,size);
+                       mmf_value_set_data(&item->value, info[i].default_value,size);
                        break;
                default:
-                       mmf_debug(MMF_DEBUG_LOG, "ERROR: Invalid MMF_VALUE_TYPE\n");
+                       //mmf_debug(MMF_DEBUG_LOG, "ERROR: Invalid MMF_VALUE_TYPE\n");
                        assert(0);
                        break;
                }
@@ -613,64 +635,81 @@ int mmf_attrs_init(MMHandleType h, mmf_attrs_construct_info_t *info, int count)
 int mmf_attrs_commit(MMHandleType h)
 {
        return_val_if_fail(h, -1);
-       
+
        mmf_attrs_t *attrs = (mmf_attrs_t * )h;
+       mmf_attribute_t *item = NULL;
        int i;
        int ret = 0;
 
        for (i = 0; i < attrs->count; ++i) {
-               if (mmf_attribute_is_modified(&attrs->items[i])) {
+               item = &attrs->items[i];
+
+               MM_ATTR_ITEM_WRITE_LOCK(item);
+
+               if (mmf_attribute_is_modified(item)) {
                        if (attrs->commit_func) {
-                               if (attrs->commit_func(i, attrs->items[i].name,
-                                                                               &attrs->items[i].tmpval,
+                               if (attrs->commit_func(i, item->name,
+                                                                               &item->tmpval,
                                                                                attrs->commit_param)) {
-                                       mmf_attribute_commit(&attrs->items[i]);
+                                       mmf_attribute_commit(item);
                                } else {
                                        /* without this, there is no way to solve modify when commit_func failed. */
-                                       if (attrs->items[i].flags & MM_ATTRS_FLAG_MODIFIED)
-                                               attrs->items[i].flags ^= MM_ATTRS_FLAG_MODIFIED;
+                                       if (item->flags & MM_ATTRS_FLAG_MODIFIED)
+                                               item->flags ^= MM_ATTRS_FLAG_MODIFIED;
                                        ret = -1;
                                }
                        } else {
-                               mmf_attribute_commit(&attrs->items[i]);
+                               mmf_attribute_commit(item);
                        }
                }
+
+               MM_ATTR_ITEM_WRITE_UNLOCK(item);
        }
+
        return ret;
 }
 
 int mmf_attrs_commit_err(MMHandleType h, char **err_attr_name)
 {
        mmf_attrs_t *attrs = (mmf_attrs_t * )h;
+       mmf_attribute_t *item = NULL;
        int i;
        int ret = 0;
 
        return_val_if_fail(h, -1);
 
        for (i = 0; i < attrs->count; ++i) {
-               if (mmf_attribute_is_modified(&attrs->items[i])) {
+               item = &attrs->items[i];
+
+               MM_ATTR_ITEM_WRITE_LOCK(item);
+
+               if (mmf_attribute_is_modified(item)) {
                        if (attrs->commit_func) {
-                               if (attrs->commit_func(i, attrs->items[i].name,
-                                                                               &attrs->items[i].tmpval,
+                               if (attrs->commit_func(i, item->name,
+                                                                               &item->tmpval,
                                                                                attrs->commit_param)) {
-                                       mmf_attribute_commit(&attrs->items[i]);
+                                       mmf_attribute_commit(item);
                                } else {
                                        /* without this, there is no way to solve modify when commit_func failed. */
-                                       if (attrs->items[i].flags & MM_ATTRS_FLAG_MODIFIED)
-                                               attrs->items[i].flags ^= MM_ATTRS_FLAG_MODIFIED;
+                                       if (item->flags & MM_ATTRS_FLAG_MODIFIED)
+                                               item->flags ^= MM_ATTRS_FLAG_MODIFIED;
                                        ret = -1;
 
                                        /* Set Error information */
                                        if (err_attr_name)
-                                               *err_attr_name = strdup(attrs->items[i].name);
+                                               *err_attr_name = strdup(item->name);
 
+                                       MM_ATTR_ITEM_WRITE_UNLOCK(item);
                                        break;
                                }
                        } else {
-                               mmf_attribute_commit(&attrs->items[i]);
+                               mmf_attribute_commit(item);
                        }
                }
+
+               MM_ATTR_ITEM_WRITE_UNLOCK(item);
        }
+
        return ret;
 }
 
@@ -721,4 +760,3 @@ int mmf_attrs_set_valid_double_array(MMHandleType h, int idx, const double *arra
        assert(attrs->items[idx].value_spec.type == MMF_VALUE_SPEC_DOUBLE_ARRAY);
        return mmf_value_spec_set_double_array(&attrs->items[idx].value_spec, array, count, dval);
 }
-