9d60077ba8d693c4dcc10d2f46b58d00c5b08b4f
[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         {
28                 case 0:
29                         return "NONE";
30                 case EINVAL:
31                         return "EINVAL";
32                 case ENOMEM:
33                         return "ENOMEM";
34                 case EBUSY:
35                         return "EBUSY";
36                 case EDEADLK:
37                         return "EDEADLK";
38         }
39         return "UNKNOWN";
40 }
41
42 int dp_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr)
43 {
44         int ret = pthread_mutex_init(mutex, attr);
45         if (0 == ret || EBUSY == ret)
46                 return 0;
47         else
48                 TRACE_ERROR("error:%d.%s", ret, __print_pthread_error(ret));
49         return -1;
50 }
51
52 void dp_mutex_lock(pthread_mutex_t *mutex, const char *func, int line)
53 {
54         int ret = pthread_mutex_lock(mutex);
55         if (ret != 0)
56                 TRACE_ERROR("%s:%d error:%d.%s", func, line, ret,
57                         __print_pthread_error(ret));
58 }
59
60 int dp_mutex_check_lock(pthread_mutex_t *mutex, const char *func, int line)
61 {
62         int ret = pthread_mutex_lock(mutex);
63         if (ret != 0)
64                 TRACE_ERROR("%s:%d error:%d.%s", func, line, ret,
65                         __print_pthread_error(ret));
66         return ret;
67 }
68
69 int dp_mutex_trylock(pthread_mutex_t *mutex, const char *func, int line)
70 {
71         int ret = pthread_mutex_trylock(mutex);
72         if (ret != 0 && ret != EINVAL) {
73                 TRACE_ERROR("%s:%d error:%d.%s", func, line, ret,
74                         __print_pthread_error(ret));
75         }
76         return ret;
77 }
78
79 int dp_mutex_timedlock(pthread_mutex_t *mutex, int sec, const char *func, int line)
80 {
81         struct timespec deltatime;
82         deltatime.tv_sec = sec;
83         deltatime.tv_nsec = 0;
84         int ret = pthread_mutex_timedlock(mutex, &deltatime);
85         if (ret != 0) {
86                 TRACE_ERROR("%s:%d error:%d.%s", func, line, ret,
87                         __print_pthread_error(ret));
88         }
89         return ret;
90 }
91
92 void dp_mutex_unlock(pthread_mutex_t *mutex, const char *func, int line)
93 {
94         int ret = pthread_mutex_unlock(mutex);
95         if (ret != 0)
96                 TRACE_ERROR("%s:%d error:%d.%s", func, line, ret,
97                         __print_pthread_error(ret));
98 }
99
100 void dp_mutex_destroy(pthread_mutex_t *mutex)
101 {
102         int ret = pthread_mutex_destroy(mutex);
103         if (ret != 0) {
104                 TRACE_ERROR("error:%d.%s", ret, __print_pthread_error(ret));
105                 if(EBUSY == ret) {
106                         if (pthread_mutex_unlock(mutex) == 0)
107                                 pthread_mutex_destroy(mutex);
108                 }
109         }
110 }