Commit | Line | Data |
---|---|---|
72b9787e JB |
1 | /***************************************************************************** |
2 | * Copyright (C) 2013 x265 project | |
3 | * | |
4 | * Authors: Steve Borho <steve@borho.org> | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation; either version 2 of the License, or | |
9 | * (at your option) any later version. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * GNU General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
17 | * along with this program; if not, write to the Free Software | |
18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111, USA. | |
19 | * | |
20 | * This program is also available under a commercial proprietary license. | |
21 | * For more information, contact us at license @ x265.com | |
22 | *****************************************************************************/ | |
23 | ||
24 | #include "threadpool.h" | |
25 | #include "threading.h" | |
26 | #include "wavefront.h" | |
27 | #include "common.h" | |
28 | ||
29 | namespace x265 { | |
30 | // x265 private namespace | |
31 | ||
32 | bool WaveFront::init(int numRows) | |
33 | { | |
34 | m_numRows = numRows; | |
35 | ||
b53f7c52 JB |
36 | m_numWords = (numRows + 31) >> 5; |
37 | m_internalDependencyBitmap = X265_MALLOC(uint32_t, m_numWords); | |
72b9787e | 38 | if (m_internalDependencyBitmap) |
b53f7c52 | 39 | memset((void*)m_internalDependencyBitmap, 0, sizeof(uint32_t) * m_numWords); |
72b9787e | 40 | |
b53f7c52 | 41 | m_externalDependencyBitmap = X265_MALLOC(uint32_t, m_numWords); |
72b9787e | 42 | if (m_externalDependencyBitmap) |
b53f7c52 | 43 | memset((void*)m_externalDependencyBitmap, 0, sizeof(uint32_t) * m_numWords); |
72b9787e JB |
44 | |
45 | return m_internalDependencyBitmap && m_externalDependencyBitmap; | |
46 | } | |
47 | ||
48 | WaveFront::~WaveFront() | |
49 | { | |
50 | x265_free((void*)m_internalDependencyBitmap); | |
51 | x265_free((void*)m_externalDependencyBitmap); | |
52 | } | |
53 | ||
54 | void WaveFront::clearEnabledRowMask() | |
55 | { | |
b53f7c52 | 56 | memset((void*)m_externalDependencyBitmap, 0, sizeof(uint32_t) * m_numWords); |
72b9787e JB |
57 | } |
58 | ||
59 | void WaveFront::enqueueRow(int row) | |
60 | { | |
b53f7c52 JB |
61 | uint32_t bit = 1 << (row & 31); |
62 | ATOMIC_OR(&m_internalDependencyBitmap[row >> 5], bit); | |
72b9787e JB |
63 | if (m_pool) m_pool->pokeIdleThread(); |
64 | } | |
65 | ||
66 | void WaveFront::enableRow(int row) | |
67 | { | |
b53f7c52 JB |
68 | uint32_t bit = 1 << (row & 31); |
69 | ATOMIC_OR(&m_externalDependencyBitmap[row >> 5], bit); | |
72b9787e JB |
70 | } |
71 | ||
72 | void WaveFront::enableAllRows() | |
73 | { | |
b53f7c52 | 74 | memset((void*)m_externalDependencyBitmap, ~0, sizeof(uint32_t) * m_numWords); |
72b9787e JB |
75 | } |
76 | ||
77 | bool WaveFront::dequeueRow(int row) | |
78 | { | |
b53f7c52 JB |
79 | uint32_t bit = 1 << (row & 31); |
80 | return !!(ATOMIC_AND(&m_internalDependencyBitmap[row >> 5], ~bit) & bit); | |
72b9787e JB |
81 | } |
82 | ||
83 | bool WaveFront::findJob(int threadId) | |
84 | { | |
85 | unsigned long id; | |
86 | ||
87 | // thread safe | |
88 | for (int w = 0; w < m_numWords; w++) | |
89 | { | |
b53f7c52 JB |
90 | uint32_t oldval = m_internalDependencyBitmap[w] & m_externalDependencyBitmap[w]; |
91 | while (oldval) | |
72b9787e | 92 | { |
b53f7c52 | 93 | CTZ(id, oldval); |
72b9787e | 94 | |
b53f7c52 JB |
95 | uint32_t bit = 1 << id; |
96 | if (ATOMIC_AND(&m_internalDependencyBitmap[w], ~bit) & bit) | |
72b9787e | 97 | { |
b53f7c52 JB |
98 | /* we cleared the bit, we get to process the row */ |
99 | processRow(w * 32 + id, threadId); | |
72b9787e JB |
100 | return true; |
101 | } | |
b53f7c52 | 102 | |
72b9787e | 103 | // some other thread cleared the bit, try another bit |
b53f7c52 | 104 | oldval = m_internalDependencyBitmap[w] & m_externalDependencyBitmap[w]; |
72b9787e JB |
105 | } |
106 | } | |
107 | ||
108 | // made it through the bitmap without finding any enqueued rows | |
109 | return false; | |
110 | } | |
111 | } |