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
98
99
100
101
102
103
104
105
106
107
108
109
|
/*
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, void *data ) {
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, data );
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
#include <errno.h> /* for errno and EXXX values */
wolf_error_t wolf_thread_create( wolf_thread_t *thread, wolf_thread_func_t func, void *data ) {
/* _beginthread doesn't signal termination. Unusable!,
* CreateThread is for DLLs and surpasses CRT code, not good,
* so we use _beginthreadex (this must be adapted if somebody
* wants to write a DLL with these functions.. (see also POCO)
*/
*thread = _beginthreadex( NULL, 0, func, data, 0, NULL );
if( thread < 0 ) {
switch( errno ) {
case EINVAL:
return WOLF_ERR_INVALID_VALUE;
default:
return WOLF_ERR_INTERNAL;
}
}
return WOLF_OK;
}
wolf_error_t wolf_thread_join( wolf_thread_t *thread ) {
DWORD res;
res = WaitForSingleObject( (HANDLE)(*thread), INFINITE );
switch( res ) {
case WAIT_OBJECT_0:
/* the handle is neither cleaned up by the thread
* exit code nor the _endthreadex function, so
* we do it here!
*/
CloseHandle( (HANDLE)(*thread) );
return WOLF_OK;
default:
return WOLF_ERR_INTERNAL;
}
}
#endif /* defined _WIN32 */
|