Apply tizen coding rule
[platform/framework/web/download-provider.git] / provider / download-provider-pthread.c
1 /*
2  * Copyright (c) 2012 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <unistd.h>
18 #include <time.h>
19 #include <errno.h>
20
21 #include "download-provider-log.h"
22 #include "download-provider-pthread.h"
23
24 static char *__print_pthread_error(int code)
25 {
26         switch (code) {
27         case 0:
28                 return "NONE";
29         case EINVAL:
30                 return "EINVAL";
31         case ENOMEM:
32                 return "ENOMEM";
33         case EBUSY:
34                 return "EBUSY";
35         case EDEADLK:
36                 return "EDEADLK";
37         }
38         return "UNKNOWN";
39 }
40
41 int dp_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr)
42 {
43         int ret = pthread_mutex_init(mutex, attr);
44         if (0 == ret || EBUSY == ret)
45                 return 0;
46         else
47                 TRACE_ERROR("error:%d.%s", ret, __print_pthread_error(ret));
48         return -1;
49 }
50
51 void dp_mutex_lock(pthread_mutex_t *mutex, const char *func, int line)
52 {
53         int ret = pthread_mutex_lock(mutex);
54         if (ret != 0)
55                 TRACE_ERROR("%s:%d error:%d.%s", func, line, ret,
56                         __print_pthread_error(ret));
57 }
58
59 int dp_mutex_check_lock(pthread_mutex_t *mutex, const char *func, int line)
60 {
61         int ret = pthread_mutex_lock(mutex);
62         if (ret != 0)
63                 TRACE_ERROR("%s:%d error:%d.%s", func, line, ret,
64                         __print_pthread_error(ret));
65         return ret;
66 }
67
68 int dp_mutex_trylock(pthread_mutex_t *mutex, const char *func, int line)
69 {
70         int ret = pthread_mutex_trylock(mutex);
71         if (ret != 0 && ret != EINVAL) {
72                 TRACE_ERROR("%s:%d error:%d.%s", func, line, ret,
73                         __print_pthread_error(ret));
74         }
75         return ret;
76 }
77
78 int dp_mutex_timedlock(pthread_mutex_t *mutex, int sec, const char *func, int line)
79 {
80         struct timespec deltatime;
81         deltatime.tv_sec = sec;
82         deltatime.tv_nsec = 0;
83         int ret = pthread_mutex_timedlock(mutex, &deltatime);
84         if (ret != 0) {
85                 TRACE_ERROR("%s:%d error:%d.%s", func, line, ret,
86                         __print_pthread_error(ret));
87         }
88         return ret;
89 }
90
91 void dp_mutex_unlock(pthread_mutex_t *mutex, const char *func, int line)
92 {
93         int ret = pthread_mutex_unlock(mutex);
94         if (ret != 0)
95                 TRACE_ERROR("%s:%d error:%d.%s", func, line, ret,
96                         __print_pthread_error(ret));
97 }
98
99 void dp_mutex_destroy(pthread_mutex_t *mutex)
100 {
101         int ret = pthread_mutex_destroy(mutex);
102         if (ret != 0) {
103                 TRACE_ERROR("error:%d.%s", ret, __print_pthread_error(ret));
104                 if (EBUSY == ret) {
105                         if (pthread_mutex_unlock(mutex) == 0)
106                                 pthread_mutex_destroy(mutex);
107                 }
108         }
109 }