aboutsummaryrefslogtreecommitdiff
path: root/utils/src/workqueue.cpp
blob: 1d1fbf78cda65b8bf76f5ec9ed6acb3a7f473d78 (plain)
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/*
 * workqueue.cpp, workqueue class
 *
 * Copyright (c) 2009-2010 Wind River Systems, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <workqueue.h>

WorkQueue::WorkQueue()
{
    stop = false;
    executing = true;
    wait_for_works = false;
    works = NULL;

    pthread_mutex_init(&wlock, NULL);
    pthread_cond_init(&wcond, NULL);

    pthread_mutex_init(&executing_lock, NULL);
    pthread_cond_init(&executing_wait, NULL);
    pthread_cond_init(&paused_wait, NULL);
}

WorkQueue::~WorkQueue()
{
    StopWork();

    pthread_cond_destroy(&wcond);
    pthread_mutex_destroy(&wlock);

    pthread_cond_destroy(&paused_wait);
    pthread_cond_destroy(&executing_wait);
    pthread_mutex_destroy(&executing_lock);
}

int WorkQueue::StartWork(bool executing)
{
    this->executing = executing;

    return Start();
}

void WorkQueue::StopWork(void)
{
    /* discard all scheduled works */
    pthread_mutex_lock(&wlock);
    while (works)
        works = __list_delete(works, works);
    pthread_mutex_unlock(&wlock);

    /*  wakeup DoWork() if it's sleeping */
    /*
     * FIXME
     *
     * if DoWork() is sleeping, Work()'s called one more time at this moment.
     * if DoWork()::wi->Work() called ScheduleWork() (self-rescheduling),
     * this function would be sleeping forever at Join() because works list
     * never be empty.
     */
    ResumeWork();

    pthread_mutex_lock(&wlock);
    stop = true;
    pthread_cond_signal(&wcond); /* wakeup Run() if it's sleeping */
    pthread_mutex_unlock(&wlock);

    Join();
}

/* it returns when Run() is sleeping at executing_wait or at wcond */
void WorkQueue::PauseWork(void)
{
    pthread_mutex_lock(&executing_lock);
    executing = false;
    /* this prevents deadlock if Run() is sleeping with locking wcond */
    if (!wait_for_works)
        pthread_cond_wait(&paused_wait, &executing_lock); /* wokeup by Run() */
    pthread_mutex_unlock(&executing_lock);
}

void WorkQueue::ResumeWork(void)
{
    pthread_mutex_lock(&executing_lock);
    executing = true;
    pthread_cond_signal(&executing_wait);
    pthread_mutex_unlock(&executing_lock);
}

void WorkQueue::Run(void)
{
    while (true) {
        pthread_mutex_lock(&wlock);
        if (stop) {
            pthread_mutex_unlock(&wlock);
            break;
        }

        if (!works) {
            pthread_mutex_lock(&executing_lock);
            wait_for_works = true;
            /* wake up PauseWork() if it's sleeping */
            pthread_cond_signal(&paused_wait);
            pthread_mutex_unlock(&executing_lock);

            /*
             * sleeps until works're available.
             * wokeup by ScheduleWork() or FlushWork() or ~WorkQueue()
             */
            pthread_cond_wait(&wcond, &wlock);

            pthread_mutex_lock(&executing_lock);
            wait_for_works = false;
            pthread_mutex_unlock(&executing_lock);
        }

        while (works) {
            struct list *entry = works;
            WorkableInterface *wi =
                static_cast<WorkableInterface *>(entry->data);

            works = __list_delete(works, entry);
            pthread_mutex_unlock(&wlock);

            /*
             * 1. if PauseWork() locks executing_lock right before Run() locks
             *    the lock, Run() sends the paused signal and go to sleep.
             * 2. if Run() locks executing_lock first, DoWork() is called and
             *    PausedWork() waits for paused_wait signal. Run() sends the
             *    signal during next loop processing or at the end of loop
             *    in case of works're not available.
             */
            pthread_mutex_lock(&executing_lock);
            if (!executing) {
                pthread_cond_signal(&paused_wait);
                pthread_cond_wait(&executing_wait, &executing_lock);
            }
            pthread_mutex_unlock(&executing_lock);

            DoWork(wi);

            pthread_mutex_lock(&wlock);
        }

        pthread_mutex_unlock(&wlock);
    }
}

void WorkQueue::DoWork(WorkableInterface *wi)
{
    if (wi)
        wi->Work();
}

void WorkQueue::Work(void)
{
    return;
}

void WorkQueue::ScheduleWork(void)
{
    pthread_mutex_lock(&wlock);
    works = list_add_tail(works, static_cast<WorkableInterface *>(this));
    pthread_cond_signal(&wcond); /* wakeup Run() if it's sleeping */
    pthread_mutex_unlock(&wlock);
}

void WorkQueue::ScheduleWork(WorkableInterface *wi)
{
    pthread_mutex_lock(&wlock);
    if (wi)
        works = list_add_tail(works, wi);
    else
        works = list_add_tail(works, static_cast<WorkableInterface *>(this));
    pthread_cond_signal(&wcond); /* wakeup Run() if it's sleeping */
    pthread_mutex_unlock(&wlock);
}

void WorkQueue::CancelScheduledWork(WorkableInterface *wi)
{
    pthread_mutex_lock(&wlock);
    works = list_delete_all(works, wi);
    pthread_mutex_unlock(&wlock);
}

void WorkQueue::FlushWork(void)
{
    FlushBarrier fb;
    bool needtowait = false;

    pthread_mutex_lock(&wlock);
    if (works) {
        list_add_tail(works, &fb);
        pthread_cond_signal(&wcond); /* wakeup Run() if it's sleeping */

        needtowait = true;
    }
    pthread_mutex_unlock(&wlock);

    if (needtowait)
        fb.WaitCompletion(); /* wokeup by FlushWork::Work() */
}

WorkQueue::FlushBarrier::FlushBarrier()
{
    pthread_mutex_init(&cplock, NULL);
    pthread_cond_init(&complete, NULL);
}

WorkQueue::FlushBarrier::~FlushBarrier()
{
    pthread_cond_destroy(&complete);
    pthread_mutex_destroy(&cplock);
}

void WorkQueue::FlushBarrier::WaitCompletion(void)
{
    pthread_mutex_lock(&cplock);
    pthread_cond_wait(&complete, &cplock);
    pthread_mutex_unlock(&cplock);
}

void WorkQueue::FlushBarrier::Work(void)
{
    pthread_mutex_lock(&cplock);
    pthread_cond_signal(&complete); /* wakeup WaitCompletion() */
    pthread_mutex_unlock(&cplock);
}