forked from haotian0329/libstpool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo_pri.c
82 lines (63 loc) · 1.89 KB
/
demo_pri.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
/* COPYRIGHT (C) 2014 - 2020, piggy_xrh */
#include <stdio.h>
#include "stpool.h"
#ifdef _WIN32
#include <Windows.h>
#pragma comment(lib, "libstpool.lib")
#define msleep Sleep
#else
#define msleep(x) usleep(x * 1000)
#endif
int task_run(struct sttask_t *ptsk) {
printf("\n\nRun %s\n", ptsk->task_name);
msleep(1000);
return 0;
}
void task_complete(struct sttask_t *ptsk, long vmflags, int task_code) {
struct schattr_t attr;
/* Acquire the scheduling attribute */
stpool_task_getschattr(ptsk, &attr);
printf("vmflags:%ld task_code:%p [%s-%d]\n",
vmflags, (void *)task_code, ptsk->task_name, attr.sche_pri);
/* Reschedule the task if the task has been done successfully */
if (STTASK_VMARK_DONE & vmflags) {
int err = stpool_add_task(ptsk->hp_last_attached, ptsk);
if (err) {
fprintf(stderr, "**ERR: add '%s' (%d)\n",
ptsk->task_name, err);
return;
}
}
}
int main()
{
HPOOL hp;
struct schattr_t attr[] = {
{0, 90, STP_SCHE_TOP},
{0, 40, STP_SCHE_BACK},
{0, 10, STP_SCHE_BACK},
{0, 0, STP_SCHE_BACK},
};
/* Creat a pool with 1 servering threads */
hp = stpool_create(1, 0, 1, 1);
printf("%s\n", stpool_status_print(hp, NULL, 0));
/* Add a task with zero priority */
stpool_add_routine(hp, "zero_task", task_run, task_complete, NULL, &attr[3]);
stpool_add_routine(hp, "low_task", task_run, task_complete, NULL, &attr[2]);
stpool_add_routine(hp, "middle_task", task_run, task_complete, NULL, &attr[1]);
stpool_add_routine(hp, "hight_task", task_run, task_complete, NULL, &attr[0]);
/* Wake up the pool to run the tasks */
stpool_resume(hp);
getchar();
/* Remove all tasks */
stpool_remove_pending_task(hp, NULL, 1);
/* Turn the throttle on */
stpool_throttle_enable(hp, 1);
/* Wait for all tasks' completions */
stpool_task_wait(hp, NULL, -1);
/* Release the pool */
stpool_release(hp);
printf("print any key to exit.\n");
getchar();
return 0;
}