-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
128 lines (108 loc) · 2.45 KB
/
main.c
File metadata and controls
128 lines (108 loc) · 2.45 KB
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#define _GNU_SOURCE
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <errno.h>
#include <time.h>
#include "log.h"
#include "pthread_worker.h"
struct dummy_list_t
{
char *text;
struct dllist link;
};
void *unlocked_worker_callback(int id, struct dllist *list, void *custom)
{
return NULL;
}
void *locked_worker_callback(int id, struct dllist *list, void *custom)
{
struct dummy_list_t *dummy = container_of(list->prev, struct dummy_list_t, link);
dllist_remove(list->prev);
logg_err("(%d) call %s", id, dummy->text);
free(dummy->text);
free(dummy);
return NULL;
}
int locked_main_callback(struct dllist *list, void *custom)
{
static int i = 1;
if(!i--)
{
int u = 0;
for (u = 0; u < 5; u++)
{
struct dummy_list_t *dummy = calloc(1, sizeof(*dummy));
if(!dummy)
{
logg_err("calloc error");
return 0;
}
if(asprintf(&dummy->text, "hello %d", rand()) < 0)
{
logg_err("asprintf error");
free(dummy);
return 0;
}
logg_err("signal %s", dummy->text);
dllist_insert(list, &dummy->link);
}
i = 1;
return 1;
}
logg_err("check list");
return 0;
}
int unlocked_main_callback(void *custom)
{
sleep(1);
return 0;
}
void cond_list_destroy_callback(struct dllist *list, void *custom)
{
struct dummy_list_t *dummy, *tmp;
logg_err("call, size %d", dllist_length(list));
dllist_for_each_safe(dummy, tmp, list, link)
{
dllist_remove(&dummy->link);
free(dummy->text);
free(dummy);
}
}
void custom_destroy_callback(void *custom)
{
logg_err("call");
}
int main(int argc, char *argv[])
{
struct pthread_worker_main_obj_t *p_upload_worker_obj;
int i = 3;
struct pthread_worker_callbacks_t callbacks = { .locked_worker_callback = locked_worker_callback,
.unlocked_worker_callback = unlocked_worker_callback,
.locked_main_callback = locked_main_callback,
.unlocked_main_callback = unlocked_main_callback,
.cond_list_destroy_callback = cond_list_destroy_callback,
.custom_destroy_callback = custom_destroy_callback,
};
log_open("demo");
p_upload_worker_obj = pthread_worker_init(5, &callbacks, NULL);
if(!p_upload_worker_obj)
{
logg_err("error alloc user_data");
exit(EXIT_FAILURE);
}
pthread_worker_start(p_upload_worker_obj);
while(i)
{
sleep(2);
i--;
}
pthread_worker_stop(p_upload_worker_obj);
sleep(5);
pthread_worker_start(p_upload_worker_obj);
sleep(5);
pthread_worker_destroy(p_upload_worker_obj);
exit(EXIT_SUCCESS);
}