Apply tizen coding rule
[platform/framework/web/download-provider.git] / agent / include / download-agent-pthread.h
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 #ifndef _DOWNLOAD_AGENT_PTHREAD_H
18 #define _DOWNLOAD_AGENT_PTHREAD_H
19
20 #include <pthread.h>
21 #include <errno.h>
22 #include <time.h>
23
24 #include "download-agent-debug.h"
25
26 #define DA_MUTEX_INIT(mutex_add, attr) {\
27         int ret = 0;\
28         do {\
29                 ret = pthread_mutex_init(mutex_add, attr);\
30                 if (0 == ret) {\
31                         break;\
32                 } \
33                 else if (EINVAL == ret) {\
34                         DA_LOGE("pthread_mutex_init FAIL with EINVAL.");\
35                         break;\
36                 } \
37                 else if (ENOMEM == ret) {\
38                         DA_LOGE("pthread_mutex_init FAIL with ENOMEM.");\
39                         break;\
40                 } \
41                 else{\
42                         DA_LOGE("pthread_mutex_init FAIL with %d.", ret);\
43                         break;\
44                 } \
45         } while (1);\
46 }
47
48 #define DA_COND_INIT(cond_add, attr) do {\
49         if (0 != pthread_cond_init(cond_add, attr)) {\
50                 DA_LOGE("pthread_cond_init FAIL");\
51         } \
52 } while (0)
53
54 #define DA_MUTEX_LOCK(mutex_add) {\
55         int ret = 0;\
56         do {\
57                 ret = pthread_mutex_lock(mutex_add);\
58                 if (0 == ret) {\
59                         break;\
60                 } \
61                 else if (EINVAL == ret) {\
62                         DA_LOGE("pthread_mutex_lock FAIL with EINVAL.");\
63                         break;\
64                 } \
65                 else if (EDEADLK == ret) {\
66                         DA_LOGE("pthread_mutex_lock FAIL with EDEADLK.");\
67                         break;\
68                 } \
69                 else{\
70                         DA_LOGE("pthread_mutex_lock FAIL with %d.", ret);\
71                         break;\
72                 } \
73         } while (1);\
74 }
75
76 #define DA_MUTEX_UNLOCK(mutex_add) {\
77         int ret = 0;\
78         do {\
79                 ret = pthread_mutex_unlock(mutex_add);\
80                 if (0 == ret) {\
81                         break;\
82                 } \
83                 else if (EINVAL == ret) {\
84                         DA_LOGE("pthread_mutex_unlock FAIL with EINVAL.");\
85                         break;\
86                 } \
87                 else if (EPERM == ret) {\
88                         DA_LOGE("pthread_mutex_unlock FAIL with EPERM.");\
89                         break;\
90                 } \
91                 else {\
92                         DA_LOGE("pthread_mutex_unlock FAIL with %d.", ret);\
93                         break;\
94                 } \
95         } while (1);\
96 }
97
98 #define DA_COND_SIGNAL(cond_add) do {\
99                 if (0 != pthread_cond_signal(cond_add)) {\
100                         DA_LOGE("pthread_cond_signal FAIL");\
101                 } \
102         } while (0)
103
104 #define DA_COND_WAIT(cond_add, mutex_add) do {\
105                 if (0 != pthread_cond_wait(cond_add, mutex_add)) {\
106                         DA_LOGE("pthread_cond_wait FAIL");\
107                 } \
108         } while (0)
109
110 #define DA_COND_TIMED_WAIT(cond_add, mutex_add, time) do {\
111                 if (0 != pthread_cond_timedwait(cond_add, mutex_add, time)) {\
112                         DA_LOGE("pthread_cond_wait FAIL");\
113                 } \
114         } while (0)
115
116
117 #define DA_COND_DESTROY(cond_add)       do {\
118                 if (0 != pthread_cond_destroy(cond_add)) {\
119                         DA_LOGE("pthread_cond_destroy FAIL");\
120                 } \
121         } while (0)
122
123 #define DA_MUTEX_DESTROY(mutex_add) {\
124         int ret = 0;\
125         do {\
126                 ret = pthread_mutex_destroy(mutex_add);\
127                 if (0 == ret) {\
128                         break;\
129                 } \
130                 else if (EINVAL == ret) {\
131                         DA_LOGE("pthread_mutex_destroy FAIL with EINVAL.");\
132                         break;\
133                 } \
134                 else if (EBUSY == ret) {\
135                         DA_LOGE("pthread_mutex_destroy FAIL with EBUSY.");\
136                         break;\
137                 } \
138                 else {\
139                         DA_LOGE("pthread_mutex_destroy FAIL with %d.", ret);\
140                         break;\
141                 } \
142         } while (1);\
143 }
144 #endif