8c02c5935e875d10aabb6d26e99e02d6851cc1e2
[platform/framework/web/download-provider.git] / src / include / download-provider-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_PROVIDER2_PTHREAD_H
18 #define DOWNLOAD_PROVIDER2_PTHREAD_H
19
20 #ifdef __cplusplus
21 extern "C" {
22 #endif
23
24 #include <unistd.h>
25 #include <pthread.h>
26 #include <errno.h>
27
28 // download-provider use default style mutex.
29
30 #define CLIENT_MUTEX_LOCK(mutex_add) {\
31         int ret = 0;\
32         ret = pthread_mutex_lock(mutex_add);\
33         if (EINVAL == ret) {\
34                 TRACE_STRERROR("ERR:pthread_mutex_lock FAIL with EINVAL.");\
35         } else if (EDEADLK == ret) {\
36                 TRACE_STRERROR("ERR:pthread_mutex_lock FAIL with EDEADLK.");\
37         } else if (ret != 0) {\
38                 TRACE_STRERROR("ERR:pthread_mutex_lock FAIL with %d.", ret);\
39         } \
40 }
41
42 #define CLIENT_MUTEX_UNLOCK(mutex_add) {\
43         int ret = 0;\
44         ret = pthread_mutex_unlock(mutex_add);\
45         if (EINVAL == ret) {\
46                 TRACE_STRERROR("ERR:pthread_mutex_unlock FAIL with EINVAL.");\
47         } else if (EDEADLK == ret) {\
48                 TRACE_STRERROR("ERR:pthread_mutex_unlock FAIL with EDEADLK.");\
49         } else if (ret != 0) {\
50                 TRACE_STRERROR("ERR:pthread_mutex_unlock FAIL with %d.", ret);\
51         } \
52 }
53
54 #define CLIENT_MUTEX_DESTROY(mutex_add) { \
55         int ret = 0; \
56         ret = pthread_mutex_destroy(mutex_add); \
57         if(EINVAL == ret) {\
58                 TRACE_STRERROR("ERR:pthread_mutex_destroy FAIL with EINVAL."); \
59         } else if(ENOMEM == ret) {\
60                 TRACE_STRERROR("ERR:pthread_mutex_destroy FAIL with ENOMEM."); \
61         } else if(EBUSY == ret) {\
62                 TRACE_STRERROR("ERR:pthread_mutex_destroy FAIL with EBUSY."); \
63                 if (pthread_mutex_unlock(mutex_add) == 0) \
64                         pthread_mutex_destroy(mutex_add); \
65         } else if (ret != 0) {\
66                 TRACE_STRERROR("ERR:pthread_mutex_destroy FAIL with %d.", ret); \
67         } \
68 }
69
70 #define CLIENT_MUTEX_INIT(mutex_add, attr) { \
71         int ret = 0; \
72         unsigned retry = 3; \
73         while (retry > 0) { \
74                 ret = pthread_mutex_init(mutex_add, attr); \
75                 if (0 == ret) { \
76                         break; \
77                 } else if(EINVAL == ret) { \
78                         TRACE_STRERROR("ERR:pthread_mutex_init FAIL with EINVAL."); \
79                 } else if(ENOMEM == ret) { \
80                         TRACE_STRERROR("ERR:pthread_mutex_init FAIL with ENOMEM."); \
81                         usleep(1000); \
82                 } else if(EBUSY == ret) {\
83                         TRACE_STRERROR("ERR:pthread_mutex_destroy FAIL with EBUSY."); \
84                         if (pthread_mutex_unlock(mutex_add) == 0) \
85                                 pthread_mutex_destroy(mutex_add); \
86                 } else if (ret != 0) { \
87                         TRACE_STRERROR("ERR:pthread_mutex_init FAIL with %d.", ret); \
88                 } \
89                 retry--; \
90         } \
91 }
92
93 #ifdef __cplusplus
94 }
95 #endif
96 #endif