Gtest code for mtp-responder
[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); //      LCOV_EXCL_LINE
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                         /* LCOV_EXCL_START */
42                         ERR("pthread_attr_setdetachstate Fail [%d], errno [%d]\n", error, errno);
43                         pthread_attr_destroy(&attr);
44                         return FALSE;
45                         /* LCOV_EXCL_STOP */
46                 }
47         }
48
49         error = pthread_create(tid, &attr, thread_func, arg);
50         if (error != 0) {
51                 /* LCOV_EXCL_START */
52                 ERR("Thread creation Fail [%d], errno [%d]\n", error, errno);
53                 pthread_attr_destroy(&attr);
54                 return FALSE;
55                 /* LCOV_EXCL_STOP */
56         }
57
58         error = pthread_attr_destroy(&attr);
59         if (error != 0)
60                 ERR("pthread_attr_destroy Fail [%d] errno [%d]\n", error, errno);       //      LCOV_EXCL_LINE
61
62         return TRUE;
63 }
64
65 mtp_bool _util_thread_join(pthread_t tid, void **data)
66 {
67         mtp_int32 res = 0;
68
69         res = pthread_join(tid, data);
70         if (res != 0) {
71                 ERR("pthread_join Fail res = [%d] for thread [%u] errno [%d]\n",
72                                 res, tid, errno);       //      LCOV_EXCL_LINE
73                 return FALSE;   //      LCOV_EXCL_LINE
74         }
75
76         return TRUE;
77 }
78
79 mtp_bool _util_thread_cancel(pthread_t tid)
80 {
81         mtp_int32 res;
82
83         if (tid == 0) {
84                 ERR("tid is NULL\n");
85                 return FALSE;
86         }
87
88         res = pthread_cancel(tid);
89         if (res != 0) {
90                 ERR("pthread_cancel Fail [%d] errno [%d]\n", tid, errno);
91                 return FALSE;
92         }
93
94         return TRUE;
95 }
96
97 /* LCOV_EXCL_START */
98 void _util_thread_exit(void *val_ptr)
99 {
100         pthread_exit(val_ptr);
101         return;
102 }
103 /* LCOV_EXCL_STOP */