1e32900fbSTrond Norbye/*
2e32900fbSTrond Norbye *     Copyright 2014 Couchbase, Inc.
3e32900fbSTrond Norbye *
4e32900fbSTrond Norbye *   Licensed under the Apache License, Version 2.0 (the "License");
5e32900fbSTrond Norbye *   you may not use this file except in compliance with the License.
6e32900fbSTrond Norbye *   You may obtain a copy of the License at
7e32900fbSTrond Norbye *
8e32900fbSTrond Norbye *       http://www.apache.org/licenses/LICENSE-2.0
9e32900fbSTrond Norbye *
10e32900fbSTrond Norbye *   Unless required by applicable law or agreed to in writing, software
11e32900fbSTrond Norbye *   distributed under the License is distributed on an "AS IS" BASIS,
12e32900fbSTrond Norbye *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13e32900fbSTrond Norbye *   See the License for the specific language governing permissions and
14e32900fbSTrond Norbye *   limitations under the License.
15e32900fbSTrond Norbye */
16e32900fbSTrond Norbye
17e32900fbSTrond Norbye#ifndef SRC_TASKQUEUE_H_
18e32900fbSTrond Norbye#define SRC_TASKQUEUE_H_ 1
19e32900fbSTrond Norbye
20e32900fbSTrond Norbye#include "config.h"
21e32900fbSTrond Norbye
2224c408e6SJim Walker#include "futurequeue.h"
2374551d34SDave Rigby#include "syncobject.h"
24e32900fbSTrond Norbye#include "task_type.h"
2574551d34SDave Rigby
2674551d34SDave Rigby#include <platform/processclock.h>
2774551d34SDave Rigby
2874551d34SDave Rigby#include <list>
2974551d34SDave Rigby#include <queue>
3074551d34SDave Rigby
31e32900fbSTrond Norbyeclass ExecutorPool;
32b6534b8aSSundar Sridharanclass ExecutorThread;
33e32900fbSTrond Norbye
34e32900fbSTrond Norbyeclass TaskQueue {
35e32900fbSTrond Norbye    friend class ExecutorPool;
36e32900fbSTrond Norbyepublic:
37e32900fbSTrond Norbye    TaskQueue(ExecutorPool *m, task_type_t t, const char *nm);
38e32900fbSTrond Norbye    ~TaskQueue();
392900340fSChiyoung Seo
40e32900fbSTrond Norbye    void schedule(ExTask &task);
41e32900fbSTrond Norbye
427f156521SDave Rigby    ProcessClock::time_point reschedule(ExTask &task);
43b6534b8aSSundar Sridharan
44495aaee4SSundar Sridharan    void checkPendingQueue(void);
453af1e552SSundar Sridharan
46b6534b8aSSundar Sridharan    void doWake(size_t &numToWake);
47b6534b8aSSundar Sridharan
48b6534b8aSSundar Sridharan    bool fetchNextTask(ExecutorThread &thread, bool toSleep);
49e32900fbSTrond Norbye
50e32900fbSTrond Norbye    void wake(ExTask &task);
51e32900fbSTrond Norbye
52e32900fbSTrond Norbye    static const std::string taskType2Str(task_type_t type);
53e32900fbSTrond Norbye
54e32900fbSTrond Norbye    const std::string getName() const;
55e32900fbSTrond Norbye
5682430da3SSundar Sridharan    const task_type_t getQueueType() const { return queueType; }
57ddb60c3dSSundar Sridharan
58f1770e55SDave Rigby    size_t getReadyQueueSize();
59f1770e55SDave Rigby
60f1770e55SDave Rigby    size_t getFutureQueueSize();
61f1770e55SDave Rigby
62f1770e55SDave Rigby    size_t getPendingQueueSize();
63f1770e55SDave Rigby
6424c408e6SJim Walker    void snooze(ExTask& task, const double secs) {
6524c408e6SJim Walker        futureQueue.snooze(task, secs);
6624c408e6SJim Walker    }
6724c408e6SJim Walker
68e32900fbSTrond Norbyeprivate:
692900340fSChiyoung Seo    void _schedule(ExTask &task);
707f156521SDave Rigby    ProcessClock::time_point _reschedule(ExTask &task);
712900340fSChiyoung Seo    void _checkPendingQueue(void);
72b6534b8aSSundar Sridharan    bool _fetchNextTask(ExecutorThread &thread, bool toSleep);
732900340fSChiyoung Seo    void _wake(ExTask &task);
74b9334207SDave Rigby    bool _doSleep(ExecutorThread &thread, std::unique_lock<std::mutex>& lock);
75b6534b8aSSundar Sridharan    void _doWake_UNLOCKED(size_t &numToWake);
76d8577c54SDaniel Owen    size_t _moveReadyTasks(const ProcessClock::time_point tv);
772900340fSChiyoung Seo    ExTask _popReadyTask(void);
78e32900fbSTrond Norbye
79e32900fbSTrond Norbye    SyncObject mutex;
80e32900fbSTrond Norbye    const std::string name;
81e32900fbSTrond Norbye    task_type_t queueType;
82e32900fbSTrond Norbye    ExecutorPool *manager;
83b6534b8aSSundar Sridharan    size_t sleepers; // number of threads sleeping in this taskQueue
84e32900fbSTrond Norbye
8524c408e6SJim Walker    // sorted by task priority.
8624c408e6SJim Walker    std::priority_queue<ExTask, std::deque<ExTask>,
87e32900fbSTrond Norbye                        CompareByPriority> readyQueue;
8824c408e6SJim Walker
89e7ed2862SDave Rigby    // sorted by waketime. Guarded by `mutex`.
9024c408e6SJim Walker    FutureQueue<> futureQueue;
91ddb60c3dSSundar Sridharan
921172eb5dSSundar Sridharan    std::list<ExTask> pendingQueue;
93e32900fbSTrond Norbye};
94e32900fbSTrond Norbye
95e32900fbSTrond Norbye#endif  // SRC_TASKQUEUE_H_