-
Notifications
You must be signed in to change notification settings - Fork 1
/
temp
81 lines (60 loc) · 1.63 KB
/
temp
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
struct asyncer_t {
bool done;
pthread_t pid;
pthread_cond_t *cond;
pthread_mutex_t *mutex;
};
struct asyncer_t *asyncer_init();
void asyncer_start(struct asyncer_t *asyncer);
void asyncer_shutdown(struct asyncer_t *asyncer);
void asyncer_enqueue();
void asyncer_clear();
//
// Created by ckb on 2018/6/18.
//
#include <stdlib.h>
#include "ys_asyncer.h"
struct asyncer_t *asyncer_init() {
struct asyncer_t *ret = malloc(sizeof(struct asyncer_t *));
ret->done = false;
ret->mutex = malloc(sizeof(pthread_mutex_t *));
ret->cond = malloc(sizeof(pthread_cond_t *));
pthread_mutex_init(ret->mutex, NULL);
pthread_cond_init(ret->cond, NULL);
return ret;
}
void *asd(struct asyncer_t *asyncer) {
while (!asyncer->done && (action = queue.poll()) == null) {
pthread_cond_wait(asyncer->cond, asyncer->mutex);
}
return action;
}
void *start_loop(void *arg) {
struct asyncer_t *asyncer = arg;
while (!asyncer->done && asyncer->pid == pthread_self()) {
void *action = asd(asyncer);
if (action != NULL) {
pthread_mutex_lock(asyncer->mutex);
//DO SOMETHING
pthread_mutex_unlock(asyncer->mutex);
}
}
//DONE
while (!queue.isEmpty()) {
Action action = queue.remove();
if (action != null) {
action.execute();
}
}
//queue clear
queue.clear();
}
void asyncer_start(struct asyncer_t *asyncer) {
pthread_create(&asyncer->pid, NULL, start_loop, asyncer);
}
void asyncer_shutdown(struct asyncer_t *asyncer) {
}
void asyncer_enqueue(void * action) {
}
void asyncer_clear() {
}