merge with master
[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-type.h"
25 #include "download-agent-debug.h"
26
27 #define _da_thread_mutex_init(mutex_add, attr)  { \
28                                                                                                         int ret = 0; \
29                                                                                                         do{ \
30                                                                                                                 ret = pthread_mutex_init(mutex_add, attr); \
31                                                                                                                 if (0 == ret){ \
32                                                                                                                         break; \
33                                                                                                                 } \
34                                                                                                                 else if(EINVAL == ret){ \
35                                                                                                                         DA_LOG_ERR(Default, "pthread_mutex_init FAIL with EINVAL."); \
36                                                                                                                         break; \
37                                                                                                                 } \
38                                                                                                                 else if(ENOMEM == ret){ \
39                                                                                                                         DA_LOG_ERR(Default, "pthread_mutex_init FAIL with ENOMEM."); \
40                                                                                                                         break; \
41                                                                                                                 } \
42                                                                                                                 else{ \
43                                                                                                                         DA_LOG_ERR(Default, "pthread_mutex_init FAIL with %d.", ret); \
44                                                                                                                         break; \
45                                                                                                                 } \
46                                                                                                         }while(1); \
47                                                                                                 }
48
49 #define _da_thread_cond_init(cond_add, attr)            do{                                                             \
50                                                                                                         if (0 != pthread_cond_init(cond_add, attr)){\
51                                                                                                                 DA_LOG_ERR(Default, "pthread_cond_init FAIL");}      \
52                                                                                                         }while(0)
53
54
55
56 #define _da_thread_mutex_lock(mutex_add)                {\
57                                                                                                         int ret = 0;\
58                                                                                                         do{\
59                                                                                                                 ret = pthread_mutex_lock(mutex_add);\
60                                                                                                                 if (0 == ret){\
61                                                                                                                         break;\
62                                                                                                                 }\
63                                                                                                                 else if(EINVAL == ret){\
64                                                                                                                         DA_LOG_ERR(Default, "pthread_mutex_lock FAIL with EINVAL.");\
65                                                                                                                         break;\
66                                                                                                                 }\
67                                                                                                                 else if(EDEADLK == ret){\
68                                                                                                                         DA_LOG_ERR(Default, "pthread_mutex_lock FAIL with EDEADLK.");\
69                                                                                                                         break;\
70                                                                                                                 }\
71                                                                                                                 else{\
72                                                                                                                         DA_LOG_ERR(Default, "pthread_mutex_lock FAIL with %d.", ret);\
73                                                                                                                         break;\
74                                                                                                                 }\
75                                                                                                         }while(1);\
76                                                                                                 }
77
78
79 #define _da_thread_mutex_unlock(mutex_add)              {\
80                                                                                                         int ret = 0;\
81                                                                                                         do{\
82                                                                                                                 ret = pthread_mutex_unlock(mutex_add);\
83                                                                                                                 if (0 == ret){\
84                                                                                                                         break;\
85                                                                                                                 }\
86                                                                                                                 else if(EINVAL == ret){\
87                                                                                                                         DA_LOG_ERR(Default, "pthread_mutex_unlock FAIL with EINVAL.");\
88                                                                                                                         break;\
89                                                                                                                 }\
90                                                                                                                 else if(EPERM == ret){\
91                                                                                                                         DA_LOG_ERR(Default, "pthread_mutex_unlock FAIL with EPERM.");\
92                                                                                                                         break;\
93                                                                                                                 }\
94                                                                                                                 else{\
95                                                                                                                         DA_LOG_ERR(Default, "pthread_mutex_unlock FAIL with %d.", ret);\
96                                                                                                                         break;\
97                                                                                                                 }\
98                                                                                                         }while(1);\
99                                                                                                 }
100
101
102 #define _da_thread_cond_signal(cond_add)                        do{                                                             \
103                                                                                                                 if (0 != pthread_cond_signal(cond_add)){\
104                                                                                                                         DA_LOG_ERR(Default, "pthread_cond_signal FAIL");}               \
105                                                                                                         }while(0)
106
107
108
109 #define _da_thread_cond_wait(cond_add, mutex_add)       do{                                                             \
110                                                                                                                 if (0 != pthread_cond_wait(cond_add, mutex_add)){\
111                                                                                                                         DA_LOG_ERR(Default, "pthread_cond_wait FAIL");}     \
112                                                                                                         }while(0)
113
114 #define _da_thread_cond_timed_wait(cond_add, mutex_add, time)   do{                                                             \
115                                                                                                                 if (0 != pthread_cond_timedwait(cond_add, mutex_add, time)){\
116                                                                                                                         DA_LOG_ERR(Default, "pthread_cond_wait FAIL");}     \
117                                                                                                         }while(0)
118
119
120 #define _da_thread_cond_destroy(cond_add)       do{                                                             \
121                                                                                                                 if (0 != pthread_cond_destroy(cond_add)){\
122                                                                                                                 DA_LOG_ERR(Default, "pthread_cond_destroy FAIL");}     \
123                                                                                                         }while(0)
124
125 #define _da_thread_mutex_destroy(mutex_add)     {\
126                                                                                                         int ret = 0;\
127                                                                                                         do{\
128                                                                                                                 ret = pthread_mutex_destroy(mutex_add);\
129                                                                                                                 if (0 == ret){\
130                                                                                                                         break;\
131                                                                                                                 }\
132                                                                                                                 else if(EINVAL == ret){\
133                                                                                                                         DA_LOG_ERR(Default, "pthread_mutex_destroy FAIL with EINVAL.");\
134                                                                                                                         break;\
135                                                                                                                 }\
136                                                                                                                 else if(EBUSY == ret){\
137                                                                                                                         DA_LOG_ERR(Default, "pthread_mutex_destroy FAIL with EBUSY.");\
138                                                                                                                         break;\
139                                                                                                                 }\
140                                                                                                                 else{\
141                                                                                                                         DA_LOG_ERR(Default, "pthread_mutex_destroy FAIL with %d.", ret);\
142                                                                                                                         break;\
143                                                                                                                 }\
144                                                                                                         }while(1);\
145                                                                                                 }
146
147 #endif