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
|
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
|
-
+
+
-
+
-
+
-
+
-
-
+
+
-
+
|
* file.
*/
#include "config.h"
#include <errno.h>
#import "thread.h"
#import "OFPlainThread.h"
#import "macros.h"
#include <windows.h>
struct thread_context {
struct ThreadContext {
void (*function)(id);
id object;
};
static WINAPI void
functionWrapper(struct thread_context *context)
functionWrapper(struct ThreadContext *context)
{
context->function(context->object);
free(context);
}
int
of_thread_attr_init(of_thread_attr_t *attr)
OFPlainThreadAttributesInit(OFPlainThreadAttributes *attr)
{
attr->priority = 0;
attr->stackSize = 0;
return 0;
}
int
of_thread_new(of_thread_t *thread, const char *name, void (*function)(id),
id object, const of_thread_attr_t *attr)
OFPlainThreadNew(OFPlainThread *thread, const char *name, void (*function)(id),
id object, const OFPlainThreadAttributes *attr)
{
DWORD priority = THREAD_PRIORITY_NORMAL;
struct thread_context *context;
struct ThreadContext *context;
DWORD threadID;
if (attr != NULL && attr->priority != 0) {
if (attr->priority < -1 || attr->priority > 1)
return EINVAL;
if (attr->priority < 0)
|
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
|
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
|
-
+
-
+
-
+
-
+
-
+
-
+
-
+
|
case ERROR_NOT_ENOUGH_MEMORY:
error = ENOMEM;
break;
case ERROR_ACCESS_DENIED:
error = EACCES;
break;
default:
OF_ENSURE(0);
OFEnsure(0);
}
free(context);
return error;
}
if (attr != NULL && attr->priority != 0)
OF_ENSURE(!SetThreadPriority(*thread, priority));
OFEnsure(!SetThreadPriority(*thread, priority));
return 0;
}
int
of_thread_join(of_thread_t thread)
OFPlainThreadJoin(OFPlainThread thread)
{
switch (WaitForSingleObject(thread, INFINITE)) {
case WAIT_OBJECT_0:
CloseHandle(thread);
return 0;
case WAIT_FAILED:
switch (GetLastError()) {
case ERROR_INVALID_HANDLE:
return EINVAL;
default:
OF_ENSURE(0);
OFEnsure(0);
}
default:
OF_ENSURE(0);
OFEnsure(0);
}
}
int
of_thread_detach(of_thread_t thread)
OFPlainThreadDetach(OFPlainThread thread)
{
CloseHandle(thread);
return 0;
}
void
of_thread_set_name(const char *name)
OFSetThreadName(const char *name)
{
}
|