Tizen 2.1 base
[platform/upstream/libbullet.git] / Extras / PhysicsEffects / include / physics_effects / low_level / task / pfx_task_manager.h
1 /*\r
2 Physics Effects Copyright(C) 2010 Sony Computer Entertainment Inc.\r
3 All rights reserved.\r
4 \r
5 Physics Effects is open software; you can redistribute it and/or\r
6 modify it under the terms of the BSD License.\r
7 \r
8 Physics Effects is distributed in the hope that it will be useful,\r
9 but WITHOUT ANY WARRANTY; without even the implied warranty of\r
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\r
11 See the BSD License for more details.\r
12 \r
13 A copy of the BSD License is distributed with\r
14 Physics Effects under the filename: physics_effects_license.txt\r
15 */\r
16 \r
17 #ifndef _SCE_PFX_TASK_MANAGER_H\r
18 #define _SCE_PFX_TASK_MANAGER_H\r
19 \r
20 #include "../../base_level/base/pfx_common.h"\r
21 #include "../../base_level/base/pfx_heap_manager.h"\r
22 #include "pfx_sync_components.h"\r
23 \r
24 #define SCE_PFX_IO_BUFF_BYTES 1048576\r
25 namespace sce {\r
26 namespace PhysicsEffects {\r
27 \r
28 //J 並列処理するためのタスクマネージャクラス\r
29 //E Task manager class for parallel computation\r
30 \r
31 struct PfxTaskArg\r
32 {\r
33         int taskId;\r
34         int maxTasks;\r
35         PfxBarrier *barrier;\r
36         PfxCriticalSection *criticalSection;\r
37         void *io;\r
38         PfxUInt32 data[4];\r
39 };\r
40 \r
41 typedef void (*PfxTaskEntry)(PfxTaskArg *arg);\r
42 \r
43 class PfxTaskManager\r
44 {\r
45 protected:\r
46         PfxUInt32 m_numTasks;\r
47         PfxUInt32 m_maxTasks;\r
48         SCE_PFX_PADDING(1,4)\r
49         PfxUInt8 SCE_PFX_ALIGNED(16) m_ioBuff[SCE_PFX_IO_BUFF_BYTES];\r
50         PfxHeapManager m_pool;\r
51         PfxHeapManager m_ioPool;\r
52         PfxTaskEntry m_taskEntry;\r
53         PfxTaskArg *m_taskArg;\r
54         SCE_PFX_PADDING(2,8)\r
55 \r
56         PfxTaskManager() : m_pool(NULL,0),m_ioPool(NULL,0) {}\r
57 \r
58 public:\r
59         void *allocate(size_t bytes) {return m_ioPool.allocate(bytes);}\r
60         void deallocate(void *p) {m_ioPool.deallocate(p);}\r
61         void clearPool() {m_ioPool.clear();}\r
62 \r
63         virtual PfxUInt32 getSharedParam(int i) = 0;\r
64         virtual void setSharedParam(int i,PfxUInt32 p) = 0;\r
65 \r
66         virtual void startTask(int taskId,void *io,PfxUInt32 data1,PfxUInt32 data2,PfxUInt32 data3,PfxUInt32 data4) = 0;\r
67         virtual void waitTask(int &taskId,PfxUInt32 &data1,PfxUInt32 &data2,PfxUInt32 &data3,PfxUInt32 &data4) = 0;\r
68 \r
69         virtual void setTaskEntry(void *entry) {m_taskEntry = (PfxTaskEntry)entry;}\r
70 \r
71 public:\r
72         PfxTaskManager(PfxUInt32 numTasks,PfxUInt32 maxTasks,void *workBuff,PfxUInt32 workBytes)\r
73                 : m_pool((unsigned char*)workBuff,workBytes),m_ioPool(m_ioBuff,SCE_PFX_IO_BUFF_BYTES)\r
74         {\r
75                 SCE_PFX_ASSERT(numTasks>0);\r
76                 SCE_PFX_ASSERT(numTasks<=maxTasks);\r
77                 m_numTasks = numTasks;\r
78                 m_maxTasks = maxTasks;\r
79                 m_taskArg = (PfxTaskArg*)m_pool.allocate(sizeof(PfxTaskArg)*m_maxTasks);\r
80         }\r
81 \r
82         virtual ~PfxTaskManager()\r
83         {\r
84                 m_pool.clear();\r
85         }\r
86         \r
87         virtual PfxUInt32 getNumTasks() const {return m_numTasks;}\r
88         virtual void setNumTasks(PfxUInt32 tasks) {m_numTasks = SCE_PFX_MIN(tasks,m_maxTasks);}\r
89         \r
90         virtual void initialize() = 0;\r
91         virtual void finalize() = 0;\r
92 };\r
93 \r
94 } //namespace PhysicsEffects\r
95 } //namespace sce\r
96 #endif // _SCE_PFX_TASK_MANAGER_H