2
2
3
3
struct scheduled_fn_t
4
4
{
5
- scheduled_fn_t * mNext ;
6
- std::function<void (void )> mFunc ;
5
+ scheduled_fn_t * mNext ;
6
+ std::function<void (void )> mFunc ;
7
7
};
8
8
9
9
static scheduled_fn_t * sFirst = 0 ;
@@ -16,82 +16,82 @@ static int sCount = 0;
16
16
17
17
static void init_lists ()
18
18
{
19
- if (sCount != 0 ) {
20
- return ;
21
- }
22
- while (sCount < SCHEDULED_FN_INITIAL_COUNT) {
23
- scheduled_fn_t * it = new scheduled_fn_t ;
24
- if (sCount == 0 ) {
25
- sFirstUnused = it;
26
- }
27
- else {
28
- sLastUnused ->mNext = it;
29
- }
30
- sLastUnused = it;
31
- ++sCount ;
32
- }
33
- sLastUnused ->mNext = NULL ;
19
+ if (sCount != 0 ) {
20
+ return ;
21
+ }
22
+ while (sCount < SCHEDULED_FN_INITIAL_COUNT) {
23
+ scheduled_fn_t * it = new scheduled_fn_t ;
24
+ if (sCount == 0 ) {
25
+ sFirstUnused = it;
26
+ }
27
+ else {
28
+ sLastUnused ->mNext = it;
29
+ }
30
+ sLastUnused = it;
31
+ ++sCount ;
32
+ }
33
+ sLastUnused ->mNext = NULL ;
34
34
}
35
35
36
36
static scheduled_fn_t * get_fn () {
37
- scheduled_fn_t * result = NULL ;
38
- // try to get an item from unused items list
39
- if (sFirstUnused ) {
40
- result = sFirstUnused ;
41
- sFirstUnused = result->mNext ;
42
- if (sFirstUnused == NULL ) {
43
- sLastUnused = NULL ;
44
- }
45
- }
46
- // if no unused items, and count not too high, allocate a new one
47
- else if (sCount != SCHEDULED_FN_MAX_COUNT) {
48
- result = new scheduled_fn_t ;
49
- result->mNext = NULL ;
50
- ++sCount ;
51
- }
52
- return result;
37
+ scheduled_fn_t * result = NULL ;
38
+ // try to get an item from unused items list
39
+ if (sFirstUnused ) {
40
+ result = sFirstUnused ;
41
+ sFirstUnused = result->mNext ;
42
+ if (sFirstUnused == NULL ) {
43
+ sLastUnused = NULL ;
44
+ }
45
+ }
46
+ // if no unused items, and count not too high, allocate a new one
47
+ else if (sCount != SCHEDULED_FN_MAX_COUNT) {
48
+ result = new scheduled_fn_t ;
49
+ result->mNext = NULL ;
50
+ ++sCount ;
51
+ }
52
+ return result;
53
53
}
54
54
55
55
static void recycle_fn (scheduled_fn_t * fn)
56
56
{
57
- if (!sLastUnused ) {
58
- sFirstUnused = fn;
59
- }
60
- else {
61
- sLastUnused ->mNext = fn;
62
- }
63
- fn->mNext = NULL ;
64
- sLastUnused = fn;
57
+ if (!sLastUnused ) {
58
+ sFirstUnused = fn;
59
+ }
60
+ else {
61
+ sLastUnused ->mNext = fn;
62
+ }
63
+ fn->mNext = NULL ;
64
+ sLastUnused = fn;
65
65
}
66
66
67
67
bool schedule_function (std::function<void (void )> fn)
68
68
{
69
- scheduled_fn_t * item = get_fn ();
70
- if (!item) {
71
- return false ;
72
- }
73
- item->mFunc = fn;
74
- item->mNext = NULL ;
75
- if (!sFirst ) {
76
- sFirst = item;
77
- }
78
- else {
79
- sLast ->mNext = item;
80
- }
81
- sLast = item;
82
- return true ;
69
+ scheduled_fn_t * item = get_fn ();
70
+ if (!item) {
71
+ return false ;
72
+ }
73
+ item->mFunc = fn;
74
+ item->mNext = NULL ;
75
+ if (!sFirst ) {
76
+ sFirst = item;
77
+ }
78
+ else {
79
+ sLast ->mNext = item;
80
+ }
81
+ sLast = item;
82
+ return true ;
83
83
}
84
84
85
85
void run_scheduled_functions ()
86
86
{
87
- while (sFirst ) {
88
- scheduled_fn_t * item = sFirst ;
89
- sFirst = item->mNext ;
90
- if (sFirst == NULL ) {
91
- sLast = NULL ;
92
- }
93
- item->mFunc ();
94
- item->mFunc = std::function<void (void )>();
95
- recycle_fn (item);
96
- }
87
+ while (sFirst ) {
88
+ scheduled_fn_t * item = sFirst ;
89
+ sFirst = item->mNext ;
90
+ if (sFirst == NULL ) {
91
+ sLast = NULL ;
92
+ }
93
+ item->mFunc ();
94
+ item->mFunc = std::function<void (void )>();
95
+ recycle_fn (item);
96
+ }
97
97
}
0 commit comments