xref: /3.0.3-GA/ep-engine/src/taskqueue.h (revision 8ee8b461)
1/*
2 *     Copyright 2014 Couchbase, Inc.
3 *
4 *   Licensed under the Apache License, Version 2.0 (the "License");
5 *   you may not use this file except in compliance with the License.
6 *   You may obtain a copy of the License at
7 *
8 *       http://www.apache.org/licenses/LICENSE-2.0
9 *
10 *   Unless required by applicable law or agreed to in writing, software
11 *   distributed under the License is distributed on an "AS IS" BASIS,
12 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 *   See the License for the specific language governing permissions and
14 *   limitations under the License.
15 */
16
17#ifndef SRC_TASKQUEUE_H_
18#define SRC_TASKQUEUE_H_ 1
19
20#include "config.h"
21
22#include <queue>
23
24#include "ringbuffer.h"
25#include "task_type.h"
26#include "tasks.h"
27#include "tasklogentry.h"
28#define TASK_LOG_SIZE 80
29
30class ExecutorPool;
31
32class TaskQueue {
33    friend class ExecutorPool;
34public:
35    TaskQueue(ExecutorPool *m, task_type_t t, const char *nm);
36    ~TaskQueue();
37    void schedule(ExTask &task);
38
39    struct timeval reschedule(ExTask &task);
40
41    void checkInShard(ExTask &task);
42    void checkInShard_UNLOCKED(ExTask &task);
43    bool checkOutShard(ExTask &task);
44
45    bool fetchNextTask(ExTask &task, struct timeval &tv, int &taskIdx,
46                       struct timeval now);
47
48    void wake(ExTask &task);
49
50    static const std::string taskType2Str(task_type_t type);
51
52    const std::string getName() const;
53
54    void addLogEntry(const std::string &desc, const hrtime_t runtime,
55                     rel_time_t startRelTime, bool isSlowJob);
56
57    const std::vector<TaskLogEntry> getLog() { return tasklog.contents(); }
58
59    const std::vector<TaskLogEntry> getSlowLog() { return slowjobs.contents();}
60
61private:
62    bool empty(void);
63    void moveReadyTasks(struct timeval tv);
64    void pushReadyTask(ExTask &tid);
65    ExTask popReadyTask(void);
66
67    SyncObject mutex;
68    AtomicValue<bool> isLock;
69    const std::string name;
70    task_type_t queueType;
71    ExecutorPool *manager;
72
73    // sorted by task priority then waketime ..
74    std::priority_queue<ExTask, std::deque<ExTask >,
75                        CompareByPriority> readyQueue;
76    std::priority_queue<ExTask, std::deque<ExTask >,
77                        CompareByDueDate> futureQueue;
78
79    RingBuffer<TaskLogEntry> tasklog;
80    RingBuffer<TaskLogEntry> slowjobs;
81};
82
83#endif  // SRC_TASKQUEUE_H_
84