Skip to content

Commit b3f5d6d

Browse files
committed
Put lv2_obj::awake calls under mutex
1 parent 04c808b commit b3f5d6d

File tree

3 files changed

+35
-58
lines changed

3 files changed

+35
-58
lines changed

rpcs3/Emu/Cell/lv2/sys_cond.cpp

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -77,31 +77,24 @@ error_code sys_cond_signal(ppu_thread& ppu, u32 cond_id)
7777

7878
sys_cond.trace("sys_cond_signal(cond_id=0x%x)", cond_id);
7979

80-
const auto cond = idm::check<lv2_obj, lv2_cond>(cond_id, [](lv2_cond& cond) -> cpu_thread*
80+
const auto cond = idm::check<lv2_obj, lv2_cond>(cond_id, [](lv2_cond& cond)
8181
{
8282
if (cond.waiters)
8383
{
8484
std::lock_guard lock(cond.mutex->mutex);
8585

8686
if (const auto cpu = cond.schedule<ppu_thread>(cond.sq, cond.mutex->protocol))
8787
{
88-
return cpu;
88+
cond.awake(cpu);
8989
}
9090
}
91-
92-
return nullptr;
9391
});
9492

9593
if (!cond)
9694
{
9795
return CELL_ESRCH;
9896
}
9997

100-
if (cond.ret)
101-
{
102-
cond->awake(cond.ret);
103-
}
104-
10598
return CELL_OK;
10699
}
107100

@@ -124,21 +117,19 @@ error_code sys_cond_signal_all(ppu_thread& ppu, u32 cond_id)
124117
lv2_obj::append(cpu);
125118
count++;
126119
}
127-
}
128120

129-
return count;
121+
if (count)
122+
{
123+
lv2_obj::awake_all();
124+
}
125+
}
130126
});
131127

132128
if (!cond)
133129
{
134130
return CELL_ESRCH;
135131
}
136132

137-
if (cond.ret)
138-
{
139-
lv2_obj::awake_all();
140-
}
141-
142133
return CELL_OK;
143134
}
144135

@@ -148,11 +139,11 @@ error_code sys_cond_signal_to(ppu_thread& ppu, u32 cond_id, u32 thread_id)
148139

149140
sys_cond.trace("sys_cond_signal_to(cond_id=0x%x, thread_id=0x%x)", cond_id, thread_id);
150141

151-
const auto cond = idm::check<lv2_obj, lv2_cond>(cond_id, [&](lv2_cond& cond) -> cpu_thread*
142+
const auto cond = idm::check<lv2_obj, lv2_cond>(cond_id, [&](lv2_cond& cond) -> int
152143
{
153144
if (!idm::check_unlocked<named_thread<ppu_thread>>(thread_id))
154145
{
155-
return (cpu_thread*)(1);
146+
return -1;
156147
}
157148

158149
if (cond.waiters)
@@ -164,24 +155,21 @@ error_code sys_cond_signal_to(ppu_thread& ppu, u32 cond_id, u32 thread_id)
164155
if (cpu->id == thread_id)
165156
{
166157
verify(HERE), cond.unqueue(cond.sq, cpu);
167-
return cpu;
158+
cond.awake(cpu);
159+
return 1;
168160
}
169161
}
170162
}
171163

172-
return nullptr;
164+
return 0;
173165
});
174166

175-
if (!cond || cond.ret == (cpu_thread*)(1))
167+
if (!cond || cond.ret == -1)
176168
{
177169
return CELL_ESRCH;
178170
}
179171

180-
if (cond.ret)
181-
{
182-
cond->awake(cond.ret);
183-
}
184-
else if (!cond.ret)
172+
if (!cond.ret)
185173
{
186174
return not_an_error(CELL_EPERM);
187175
}

rpcs3/Emu/Cell/lv2/sys_lwcond.cpp

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,11 @@ error_code _sys_lwcond_signal(ppu_thread& ppu, u32 lwcond_id, u32 lwmutex_id, u3
8686
fmt::throw_exception("Unknown mode (%d)" HERE, mode);
8787
}
8888

89-
const auto cond = idm::check<lv2_obj, lv2_lwcond>(lwcond_id, [&](lv2_lwcond& cond) -> cpu_thread*
89+
const auto cond = idm::check<lv2_obj, lv2_lwcond>(lwcond_id, [&](lv2_lwcond& cond) -> int
9090
{
9191
if (ppu_thread_id != -1 && !idm::check_unlocked<named_thread<ppu_thread>>(ppu_thread_id))
9292
{
93-
return (cpu_thread*)(1);
93+
return -1;
9494
}
9595

9696
lv2_lwmutex* mutex;
@@ -101,7 +101,7 @@ error_code _sys_lwcond_signal(ppu_thread& ppu, u32 lwcond_id, u32 lwmutex_id, u3
101101

102102
if (!mutex)
103103
{
104-
return (cpu_thread*)(1);
104+
return -1;
105105
}
106106
}
107107

@@ -143,15 +143,19 @@ error_code _sys_lwcond_signal(ppu_thread& ppu, u32 lwcond_id, u32 lwmutex_id, u3
143143
std::lock_guard lock(mutex->mutex);
144144
mutex->sq.emplace_back(result);
145145
}
146+
else
147+
{
148+
cond.awake(result);
149+
}
146150

147-
return result;
151+
return 1;
148152
}
149153
}
150154

151-
return nullptr;
155+
return 0;
152156
});
153157

154-
if (!cond || cond.ret == (cpu_thread*)(1))
158+
if (!cond || cond.ret == -1)
155159
{
156160
return CELL_ESRCH;
157161
}
@@ -173,11 +177,6 @@ error_code _sys_lwcond_signal(ppu_thread& ppu, u32 lwcond_id, u32 lwmutex_id, u3
173177
return not_an_error(CELL_EPERM);
174178
}
175179

176-
if (mode != 1)
177-
{
178-
cond->awake(cond.ret);
179-
}
180-
181180
return CELL_OK;
182181
}
183182

@@ -241,6 +240,11 @@ error_code _sys_lwcond_signal_all(ppu_thread& ppu, u32 lwcond_id, u32 lwmutex_id
241240
result++;
242241
}
243242

243+
if (need_awake)
244+
{
245+
lv2_obj::awake_all();
246+
}
247+
244248
return result;
245249
}
246250

@@ -252,11 +256,6 @@ error_code _sys_lwcond_signal_all(ppu_thread& ppu, u32 lwcond_id, u32 lwmutex_id
252256
return CELL_ESRCH;
253257
}
254258

255-
if (need_awake)
256-
{
257-
lv2_obj::awake_all();
258-
}
259-
260259
if (mode == 1)
261260
{
262261
// Mode 1: return the amount of threads (TODO)

rpcs3/Emu/Cell/lv2/sys_lwmutex.cpp

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -192,29 +192,24 @@ error_code _sys_lwmutex_unlock(ppu_thread& ppu, u32 lwmutex_id)
192192

193193
sys_lwmutex.trace("_sys_lwmutex_unlock(lwmutex_id=0x%x)", lwmutex_id);
194194

195-
const auto mutex = idm::check<lv2_obj, lv2_lwmutex>(lwmutex_id, [&](lv2_lwmutex& mutex) -> cpu_thread*
195+
const auto mutex = idm::check<lv2_obj, lv2_lwmutex>(lwmutex_id, [&](lv2_lwmutex& mutex)
196196
{
197197
std::lock_guard lock(mutex.mutex);
198198

199199
if (const auto cpu = mutex.schedule<ppu_thread>(mutex.sq, mutex.protocol))
200200
{
201-
return cpu;
201+
mutex.awake(cpu);
202+
return;
202203
}
203204

204205
mutex.signaled |= 1;
205-
return nullptr;
206206
});
207207

208208
if (!mutex)
209209
{
210210
return CELL_ESRCH;
211211
}
212212

213-
if (mutex.ret)
214-
{
215-
mutex->awake(mutex.ret);
216-
}
217-
218213
return CELL_OK;
219214
}
220215

@@ -224,29 +219,24 @@ error_code _sys_lwmutex_unlock2(ppu_thread& ppu, u32 lwmutex_id)
224219

225220
sys_lwmutex.warning("_sys_lwmutex_unlock2(lwmutex_id=0x%x)", lwmutex_id);
226221

227-
const auto mutex = idm::check<lv2_obj, lv2_lwmutex>(lwmutex_id, [&](lv2_lwmutex& mutex) -> cpu_thread*
222+
const auto mutex = idm::check<lv2_obj, lv2_lwmutex>(lwmutex_id, [&](lv2_lwmutex& mutex)
228223
{
229224
std::lock_guard lock(mutex.mutex);
230225

231226
if (const auto cpu = mutex.schedule<ppu_thread>(mutex.sq, mutex.protocol))
232227
{
233228
static_cast<ppu_thread*>(cpu)->gpr[3] = CELL_EBUSY;
234-
return cpu;
229+
mutex.awake(cpu);
230+
return;
235231
}
236232

237233
mutex.signaled |= 1 << 31;
238-
return nullptr;
239234
});
240235

241236
if (!mutex)
242237
{
243238
return CELL_ESRCH;
244239
}
245240

246-
if (mutex.ret)
247-
{
248-
mutex->awake(mutex.ret);
249-
}
250-
251241
return CELL_OK;
252242
}

0 commit comments

Comments
 (0)