summaryrefslogtreecommitdiff
path: root/src/util/mtp_thread.c
blob: e4ba76e0c76b84bef8e0fec15d4eae49ecf3e30e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*
 * Copyright (c) 2012, 2013 Samsung Electronics Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <mtp_thread.h>

/*
 * FUNCTIONS
 */
mtp_bool _util_thread_create(pthread_t *tid, const mtp_char *tname,
		mtp_int32 thread_state, thread_func_t thread_func, void *arg)
{
	int error = 0;
	pthread_attr_t attr;

	retv_if(tname == NULL, FALSE);
	retv_if(thread_func == NULL, FALSE);

	error = pthread_attr_init(&attr);
	if (error != 0) {
		ERR("pthread_attr_init Fail [%d], errno [%d]\n", error, errno);
		return FALSE;
	}

	if (thread_state == PTHREAD_CREATE_JOINABLE) {
		error = pthread_attr_setdetachstate(&attr,
				PTHREAD_CREATE_JOINABLE);
		if (error != 0) {
			ERR("pthread_attr_setdetachstate Fail [%d], errno [%d]\n", error, errno);
			pthread_attr_destroy(&attr);
			return FALSE;
		}
	}

	error = pthread_create(tid, &attr, thread_func, arg);
	if (error != 0) {
		ERR("[%s] Thread creation Fail errno [%d]\n", tname, errno);
		pthread_attr_destroy(&attr);
		return FALSE;
	}

	error = pthread_attr_destroy(&attr);
	if (error != 0)
		ERR("pthread_attr_destroy Fail [%d] errno [%d]\n", error, errno);

	return TRUE;
}

mtp_bool _util_thread_join(pthread_t tid, void **data)
{
	mtp_int32 res = 0;

	res = pthread_join(tid, data);
	if (res != 0) {
		ERR("pthread_join Fail res = [%d] for thread [%u] errno [%d]\n",
				res, tid, errno);
		return FALSE;
	}

	return TRUE;
}

mtp_bool _util_thread_cancel(pthread_t tid)
{
	mtp_int32 res;

	if (tid == 0) {
		ERR("tid is NULL\n");
		return FALSE;
	}

	res = pthread_cancel(tid);
	if (res != 0) {
		ERR("pthread_cancel Fail [%d] errno [%d]\n", tid, errno);
		return FALSE;
	}

	return TRUE;
}

void _util_thread_exit(void *val_ptr)
{
	pthread_exit(val_ptr);
	return;
}