summaryrefslogtreecommitdiff
path: root/src/threads/threads.c
blob: 79cf4bae5b8db52f84ff09ec0b5b450eb324f4d4 (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
/*
    Copyright (C) 2008 Andreas Baumann <abaumann@yahoo.com>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "threads/threads.h"

#if defined HAVE_PTHREADS

#include <errno.h>		/* for EXXX values */
#include <assert.h>		/* for assertions */
#include "port/unused.h"

wolf_error_t wolf_thread_create( wolf_thread_t *thread, wolf_thread_func_t func ) {
	pthread_attr_t attr;
	int res;

	/* intialize thread attributes with defaults */
	res = pthread_attr_init( &attr );
	if( res == ENOMEM ) {
		return WOLF_ERR_OUT_OF_MEMORY;
	} else if( res != 0 ) {
		return WOLF_ERR_INTERNAL;
	}

	/* create the thread */
	res = pthread_create( thread, &attr, func, NULL );
	if( res == EINVAL ) {
		return WOLF_ERR_INVALID_VALUE;
	} else if( res != 0 ) {
		return WOLF_ERR_INTERNAL;
	}

	/* destroy the thread attributes */
	res = pthread_attr_destroy( &attr );
	assert( res == 0 );

	return WOLF_OK;
}

wolf_error_t wolf_thread_join( wolf_thread_t *thread ) {
	int res;
	void *result;

	res = pthread_join( *thread, &result );
	if( res != 0 ) {
		return WOLF_ERR_INTERNAL;
	}

	return WOLF_OK;
}

#endif /* defined HAVE_PTHREADS */

#if defined _WIN32

#endif /* defined _WIN32 */