Sync code with Tizen 3.0 branch
[platform/core/connectivity/mtp-responder.git] / src / util / mtp_thread.c
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 #include <mtp_thread.h>
18
19 /*
20  * FUNCTIONS
21  */
22 mtp_bool _util_thread_create(pthread_t *tid, const mtp_char *tname,
23                 mtp_int32 thread_state, thread_func_t thread_func, void *arg)
24 {
25         int error = 0;
26         pthread_attr_t attr;
27
28         retv_if(tname == NULL, FALSE);
29         retv_if(thread_func == NULL, FALSE);
30
31         error = pthread_attr_init(&attr);
32         if (error != 0) {
33                 ERR("pthread_attr_init Fail [%d], errno [%d]\n", error, errno);
34                 return FALSE;
35         }
36
37         if (thread_state == PTHREAD_CREATE_JOINABLE) {
38                 error = pthread_attr_setdetachstate(&attr,
39                                 PTHREAD_CREATE_JOINABLE);
40                 if (error != 0) {
41                         ERR("pthread_attr_setdetachstate Fail [%d], errno [%d]\n", error, errno);
42                         pthread_attr_destroy(&attr);
43                         return FALSE;
44                 }
45         }
46
47         error = pthread_create(tid, &attr, thread_func, arg);
48         if (error != 0) {
49                 ERR("[%s] Thread creation Fail errno [%d]\n", tname, errno);
50                 pthread_attr_destroy(&attr);
51                 return FALSE;
52         }
53
54         error = pthread_attr_destroy(&attr);
55         if (error != 0)
56                 ERR("pthread_attr_destroy Fail [%d] errno [%d]\n", error, errno);
57
58         return TRUE;
59 }
60
61 mtp_bool _util_thread_join(pthread_t tid, void **data)
62 {
63         mtp_int32 res = 0;
64
65         res = pthread_join(tid, data);
66         if (res != 0) {
67                 ERR("pthread_join Fail res = [%d] for thread [%u] errno [%d]\n",
68                                 res, tid, errno);
69                 return FALSE;
70         }
71
72         return TRUE;
73 }
74
75 mtp_bool _util_thread_cancel(pthread_t tid)
76 {
77         mtp_int32 res;
78
79         res = pthread_cancel(tid);
80         if (res != 0) {
81                 ERR("pthread_cancel  Fail [%d] errno [%d]\n", tid, errno);
82                 return FALSE;
83         }
84
85         return TRUE;
86 }
87
88 void _util_thread_exit(void *val_ptr)
89 {
90         pthread_exit(val_ptr);
91         return;
92 }