-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathengi_thread.c
98 lines (67 loc) · 1.57 KB
/
engi_thread.c
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
#include <stdlib.h>
#include <pthread.h>
#include "engi_thread.h"
int engi_thread_init(engi_thread_t *self)
{
pthread_attr_init(&self->attr);
pthread_mutexattr_init(&self->mutex_attr);
pthread_mutexattr_settype(&self->mutex_attr, PTHREAD_MUTEX_ERRORCHECK);
pthread_mutexattr_setrobust(&self->mutex_attr, PTHREAD_MUTEX_ROBUST);
pthread_mutex_init(&self->mutex, &self->mutex_attr);
pthread_condattr_init(&self->cond_attr);
pthread_cond_init(&self->cond, &self->cond_attr);
return 0;
}
int engi_thread_create(engi_thread_t *self)
{
int ret = 0;
engi_task_t *task = &self->task;
ret = pthread_create(&self->thread, &self->attr, task->func, task->args);
return ret;
}
int engi_thread_join(engi_thread_t *self)
{
int ret = 0;
ret = pthread_join(self->thread, &self->retval);
return ret;
}
void engi_thread_exit(void *retptr)
{
pthread_exit(retptr);
}
int engi_task_init(engi_task_t *task, void * func(void *), void *args)
{
task->func = func;
task->args = args;
return 0;
}
int engi_mutex_lock(engi_thread_t *self)
{
int ret = 0;
ret = pthread_mutex_lock(&self->mutex);
return ret;
}
int engi_mutex_unlock(engi_thread_t *self)
{
int ret = 0;
ret = pthread_mutex_unlock(&self->mutex);
return ret;
}
int engi_cond_signal(engi_thread_t *self)
{
int ret = 0;
ret = pthread_cond_signal(&self->cond);
return ret;
}
int engi_cond_broadcast(engi_thread_t *self)
{
int ret = 0;
ret = pthread_cond_broadcast(&self->cond);
return ret;
}
int engi_cond_wait(engi_thread_t *self)
{
int ret = 0;
ret = pthread_cond_wait(&self->cond, &self->mutex);
return ret;
}