Subversion Repository Public Repository

Divide-Framework

This repository has no backups
This repository's network speed is throttled to 100KB/sec

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
/*
Copyright (c) 2017 DIVIDE-Studio
Copyright (c) 2009 Ionut Cava

This file is part of DIVIDE Framework.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software
and associated documentation files (the "Software"), to deal in the Software
without restriction,
including without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
IN CONNECTION WITH THE SOFTWARE
OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

*/

#ifndef _TASK_POOL_H_
#define _TASK_POOL_H_

#include "Platform/Threading/Headers/Task.h"
#include <boost/lockfree/queue.hpp>

namespace Divide {

class TaskPool {
  public:
    explicit TaskPool(U32 maxTaskCount);
    ~TaskPool();
    
    bool init(U32 threadCount, const stringImpl& workerName = "DVD_WORKER_");
    void flushCallbackQueue();
    void waitForAllTasks(bool yeld, bool flushCallbacks, bool forceClear = false);

    TaskHandle getTaskHandle(I64 taskGUID);
    Task& getAvailableTask();
    void setTaskCallback(const TaskHandle& handle,
                         const DELEGATE_CBK<void>& callback);

    inline U32 workerThreadCount() const {
        return _workerThreadCount;
    }

  private:
    //ToDo: replace all friend class declarations with attorneys -Ionut;
    friend class Task;
    void taskCompleted(U32 poolIndex, Task::TaskPriority priority);
    inline ThreadPool& threadPool() {
        return _mainTaskPool;
    }

    void nameThreadpoolWorkers(const char* name, ThreadPool& pool);
    void runCbkAndClearTask(U32 taskIndex);

  private:
    ThreadPool _mainTaskPool;
    boost::lockfree::queue<U32> _threadedCallbackBuffer;

    vectorImpl<Task> _tasksPool;
    vectorImpl<bool> _taskStates;
    vectorImpl<DELEGATE_CBK<void>> _taskCallbacks;

    mutable std::mutex _taskStateLock;

    std::atomic<U32> _allocatedJobs;

    U32 _workerThreadCount;
};

//The following calls are identical to the ones above, but use the specified pool
//to schedule tasks
TaskHandle GetTaskHandle(TaskPool& pool,
                         I64 taskGUID);

TaskHandle CreateTask(TaskPool& pool,
                   const DELEGATE_CBK<void, const Task&>& threadedFunction,
                   const DELEGATE_CBK<void>& onCompletionFunction = DELEGATE_CBK<void>());

TaskHandle CreateTask(TaskPool& pool, 
                   I64 jobIdentifier,
                   const DELEGATE_CBK<void, const Task&>& threadedFunction,
                   const DELEGATE_CBK<void>& onCompletionFunction = DELEGATE_CBK<void>());

TaskHandle parallel_for(TaskPool& pool, 
                        const DELEGATE_CBK<void, const Task&, U32, U32>& cbk,
                        U32 count,
                        U32 partitionSize,
                        Task::TaskPriority priority = Task::TaskPriority::HIGH,
                        U32 taskFlags = 0,
                        bool waitForResult = true);

void WaitForAllTasks(TaskPool& pool, bool yeld, bool flushCallbacks, bool foceClear);

};

#endif _TASK_POOL_H_

Commits for Divide-Framework/trunk/Source Code/Core/Headers/TaskPool.h

Diff revisions: vs.
Revision Author Commited Message
842 Diff Diff IonutCava picture IonutCava Wed 01 Feb, 2017 17:25:15 +0000

[IonutCava]
- Start to implement scripting support via ChaiScript: http://chaiscript.com/
- Cleanup DELEGATE_CBK alias

836 Diff Diff IonutCava picture IonutCava Fri 27 Jan, 2017 14:59:56 +0000

[IonutCava]
- Update copyright notice

835 Diff Diff IonutCava picture IonutCava Fri 27 Jan, 2017 14:58:07 +0000

[IonutCava]
- Split Engine lib into Core and Engine lib.
- Fix Server build issues

825 Diff Diff IonutCava picture IonutCava Fri 20 Jan, 2017 14:35:31 +0000

[IonutCava]
- Small corrections and optimizations to the TaskPool
- Fix buffer update bug in LightPool

811 Diff Diff IonutCava picture IonutCava Wed 11 Jan, 2017 17:26:49 +0000

[IonutCava]
- Fix some threadpool cleanup bugs
- Fix terrain rendering (except underwater caustics)
- Fix terrain loading
- Fix terrain unloading

758 Diff Diff IonutCava picture IonutCava Tue 02 Aug, 2016 16:05:09 +0000

[IonutCava]
- Added a cross platform method of naming threads (helps with debugging)
- Reduced dependency on VAR.dvd_drawID in shaders.
- Work on Single/Multi thread toggle for GFX resource loading (crashes, bugs, etc)
- Fix a bug in TaskPool where task states were never actually updated properly
- Change threadpool implementation to use std::thread instead of boost::thread

756 Diff Diff IonutCava picture IonutCava Fri 29 Jul, 2016 16:09:29 +0000

[IonutCava]
- Cleaned up material shader:
— Add explicit functions for material colour properties
— Separate Phong from PBR
— Remove texture count variable and use TEX_OPERATION_NONE for single texture
- Added a Shader compute queue that all materials share. This allows better frame time allocation for shader compilation
- Attempt to fix Task and TaskPool race conditions

741 Diff Diff IonutCava picture IonutCava Sun 26 Jun, 2016 18:01:25 +0000

[IonutCava]
- Properly wait for all pending tasks to finish before shutdown
- Add a reset method to the FrameRateHandler
- Rework glQuery system.
- Avoid mipmap generation on the current luma render target

716 Diff Diff IonutCava picture IonutCava Tue 31 May, 2016 16:08:29 +0000

[IonutCava]
- Multithreaded scene load/unload fixes:
— Added postLoadMainThread call for scenes for thread sensitive loading steps
— GUI element adding is a thread sensitive loading step (a fast one) because CEGUI doesn’t handle threading
- Added REALTIME_WITH_CALLBACK task priority for tasks that need an immediate callback when they complete instead of using the flushCallbackQueue system.
- Some work on shadow mapping for getting it to work again
- ShaderPrograms that fail to load can cause a LockManager infinite wait. Delete the lockManager if the shader didn’t load to avoid waiting on commands that never get called.

714 IonutCava picture IonutCava Thu 26 May, 2016 16:26:23 +0000

[IonutCava]
- Initial code for multithreaded scene load:
— Tasks can have a “sync with gpu” flag that will cause them to use a shared context (so they can call GL functions)
- Added per scene GUI lists that get passed to the main GUI class (still need a base class to hold the map and accessors to avoid code duplication)
- Re-enabled threading unit tests