Sync code with Tizen 3.0 branch
[platform/core/connectivity/mtp-responder.git] / include / util / mtp_thread.h
1 /*
2  * Copyright (c) 2012, 2013 Samsung Electronics Co., Ltd.
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 _MTP_THREAD_H_
18 #define _MTP_THREAD_H_
19
20 #include <pthread.h>
21 #include "mtp_datatype.h"
22 #include "mtp_util.h"
23
24 typedef void *(*thread_func_t) (void *pArg);
25
26 #define UTIL_LOCK_MUTEX(mut)\
27         do {\
28                 int lock_ret = 0;\
29                 DBG("Thread [%d] trying to lock the Mutex \n", syscall(__NR_gettid));\
30                 lock_ret = pthread_mutex_lock(mut);\
31                 if (lock_ret != 0) {\
32                         if (lock_ret == EDEADLK) {\
33                                 DBG("Mutex is already locked by the same thread");\
34                         } else {\
35                                 ERR("Error locking mutex. Error = %d \n", lock_ret);\
36                         } \
37                 } else {\
38                         DBG("Mutex locked by thread [%d] \n", syscall(__NR_gettid));\
39                 } \
40         } while (0);\
41
42
43 #define UTIL_UNLOCK_MUTEX(mut)\
44         do {\
45                 int unlock_ret = 0;\
46                 unlock_ret = pthread_mutex_unlock(mut);\
47                 if (unlock_ret != 0) {\
48                         ERR("Error unlocking mutex. Error = %d \n", unlock_ret);\
49                 } else {\
50                         DBG("Mutex unlocked by thread [%d] \n", syscall(__NR_gettid));\
51                 } \
52         } while (0);\
53
54 mtp_bool _util_thread_create(pthread_t *tid, const mtp_char *tname,
55                 mtp_int32 thread_state, thread_func_t thread_func, void *arg);
56 mtp_bool _util_thread_join(pthread_t tid, void **data);
57 mtp_bool _util_thread_cancel(pthread_t tid);
58 void _util_thread_exit(void *val_ptr);
59
60 #endif /*_MTP_THREAD_H_*/