@@ -125,15 +125,15 @@ static inline void join(const std::shared_ptr<PromiseHolder> &left, const std::s
125
125
// Unlock and then lock
126
126
#if PROMISE_MULTITHREAD
127
127
struct unlock_guard_t {
128
- inline unlock_guard_t (Mutex * mutex)
128
+ inline unlock_guard_t (std::shared_ptr< Mutex> mutex)
129
129
: mutex_(mutex)
130
130
, lock_count_(mutex->lock_count ()) {
131
131
mutex_->unlock (lock_count_);
132
132
}
133
133
inline ~unlock_guard_t () {
134
134
mutex_->lock (lock_count_);
135
135
}
136
- Mutex * mutex_;
136
+ std::shared_ptr< Mutex> mutex_;
137
137
size_t lock_count_;
138
138
};
139
139
#endif
@@ -147,8 +147,8 @@ static inline void call(std::shared_ptr<Task> task) {
147
147
// lock for 1st stage
148
148
{
149
149
#if PROMISE_MULTITHREAD
150
- Mutex & mutex = promiseHolder->mutex_ ;
151
- std::lock_guard<Mutex> lock (mutex);
150
+ std::shared_ptr< Mutex> mutex = promiseHolder->mutex_ ;
151
+ std::lock_guard<Mutex> lock (* mutex);
152
152
#endif
153
153
154
154
if (task->state_ != TaskState::kPending ) return ;
@@ -170,14 +170,14 @@ static inline void call(std::shared_ptr<Task> task) {
170
170
}
171
171
else {
172
172
#if PROMISE_MULTITHREAD
173
- Mutex * mutex0 = nullptr ;
173
+ std::shared_ptr< Mutex> mutex0 = nullptr ;
174
174
auto call = [&]() -> any {
175
- unlock_guard_t lock (& mutex);
175
+ unlock_guard_t lock (mutex);
176
176
const any &value = task->onResolved_ .call (promiseHolder->value_ );
177
177
// Make sure the returned promised is locked before than "mutex"
178
178
if (value.type () == typeid (Promise)) {
179
179
Promise &promise = value.cast <Promise &>();
180
- mutex0 = & promise.sharedPromise_ ->obtainLock ();
180
+ mutex0 = promise.sharedPromise_ ->obtainLock ();
181
181
}
182
182
return value;
183
183
};
@@ -220,14 +220,14 @@ static inline void call(std::shared_ptr<Task> task) {
220
220
else {
221
221
try {
222
222
#if PROMISE_MULTITHREAD
223
- Mutex * mutex0 = nullptr ;
223
+ std::shared_ptr< Mutex> mutex0 = nullptr ;
224
224
auto call = [&]() -> any {
225
- unlock_guard_t lock (& mutex);
225
+ unlock_guard_t lock (mutex);
226
226
const any &value = task->onRejected_ .call (promiseHolder->value_ );
227
227
// Make sure the returned promised is locked before than "mutex"
228
228
if (value.type () == typeid (Promise)) {
229
229
Promise &promise = value.cast <Promise &>();
230
- mutex0 = & promise.sharedPromise_ ->obtainLock ();
230
+ mutex0 = promise.sharedPromise_ ->obtainLock ();
231
231
}
232
232
return value;
233
233
};
@@ -279,8 +279,8 @@ static inline void call(std::shared_ptr<Task> task) {
279
279
{
280
280
// get next task
281
281
#if PROMISE_MULTITHREAD
282
- Mutex & mutex = promiseHolder->mutex_ ;
283
- std::lock_guard<Mutex> lock (mutex);
282
+ std::shared_ptr< Mutex> mutex = promiseHolder->mutex_ ;
283
+ std::lock_guard<Mutex> lock (* mutex);
284
284
#endif
285
285
std::list<std::shared_ptr<Task>> &pendingTasks2 = promiseHolder->pendingTasks_ ;
286
286
if (pendingTasks2.size () == 0 ) {
@@ -295,8 +295,8 @@ static inline void call(std::shared_ptr<Task> task) {
295
295
Defer::Defer (const std::shared_ptr<Task> &task) {
296
296
std::shared_ptr<SharedPromise> sharedPromise (new SharedPromise{ task->promiseHolder_ .lock () });
297
297
#if PROMISE_MULTITHREAD
298
- Mutex & mutex = sharedPromise->obtainLock ();
299
- std::lock_guard<Mutex> lock (mutex, std::adopt_lock_t ());
298
+ std::shared_ptr< Mutex> mutex = sharedPromise->obtainLock ();
299
+ std::lock_guard<Mutex> lock (* mutex, std::adopt_lock_t ());
300
300
#endif
301
301
302
302
task_ = task;
@@ -306,8 +306,8 @@ Defer::Defer(const std::shared_ptr<Task> &task) {
306
306
307
307
void Defer::resolve (const any &arg) const {
308
308
#if PROMISE_MULTITHREAD
309
- Mutex & mutex = this ->sharedPromise_ ->obtainLock ();
310
- std::lock_guard<Mutex> lock (mutex, std::adopt_lock_t ());
309
+ std::shared_ptr< Mutex> mutex = this ->sharedPromise_ ->obtainLock ();
310
+ std::lock_guard<Mutex> lock (* mutex, std::adopt_lock_t ());
311
311
#endif
312
312
313
313
if (task_->state_ != TaskState::kPending ) return ;
@@ -319,8 +319,8 @@ void Defer::resolve(const any &arg) const {
319
319
320
320
void Defer::reject (const any &arg) const {
321
321
#if PROMISE_MULTITHREAD
322
- Mutex & mutex = this ->sharedPromise_ ->obtainLock ();
323
- std::lock_guard<Mutex> lock (mutex, std::adopt_lock_t ());
322
+ std::shared_ptr< Mutex> mutex = this ->sharedPromise_ ->obtainLock ();
323
+ std::lock_guard<Mutex> lock (* mutex, std::adopt_lock_t ());
324
324
#endif
325
325
326
326
if (task_->state_ != TaskState::kPending ) return ;
@@ -385,6 +385,17 @@ void Mutex::unlock(size_t lock_count) {
385
385
}
386
386
#endif
387
387
388
+ PromiseHolder::PromiseHolder ()
389
+ : owners_()
390
+ , pendingTasks_()
391
+ , state_(TaskState::kPending )
392
+ , value_()
393
+ #if PROMISE_MULTITHREAD
394
+ , mutex_(std::make_shared<Mutex>())
395
+ #endif
396
+ {
397
+ }
398
+
388
399
PromiseHolder::~PromiseHolder () {
389
400
if (this ->state_ == TaskState::kRejected ) {
390
401
PromiseHolder::onUncaughtException (this ->value_ );
@@ -418,19 +429,18 @@ void PromiseHolder::handleUncaughtException(const any &onUncaughtException) {
418
429
}
419
430
420
431
#if PROMISE_MULTITHREAD
421
- Mutex &SharedPromise::obtainLock () const {
422
- Mutex *mutex = nullptr ;
432
+ std::shared_ptr<Mutex> SharedPromise::obtainLock () const {
423
433
while (true ) {
424
- mutex = & this ->promiseHolder_ ->mutex_ ;
434
+ std::shared_ptr<Mutex> mutex = this ->promiseHolder_ ->mutex_ ;
425
435
mutex->lock ();
426
436
427
437
// pointer to mutex may be changed after locked,
428
438
// in this case we should try to lock and test again
429
- if (mutex == & this ->promiseHolder_ ->mutex_ )
430
- break ;
439
+ if (mutex == this ->promiseHolder_ ->mutex_ )
440
+ return mutex ;
431
441
mutex->unlock ();
432
442
}
433
- return *mutex ;
443
+ return nullptr ;
434
444
}
435
445
#endif
436
446
@@ -460,10 +470,10 @@ Promise &Promise::then(const any &deferOrPromiseOrOnResolved) {
460
470
Promise &promise = deferOrPromiseOrOnResolved.cast <Promise &>();
461
471
462
472
#if PROMISE_MULTITHREAD
463
- Mutex & mutex0 = this ->sharedPromise_ ->obtainLock ();
464
- std::lock_guard<Mutex> lock0 (mutex0, std::adopt_lock_t ());
465
- Mutex & mutex1 = promise.sharedPromise_ ->obtainLock ();
466
- std::lock_guard<Mutex> lock1 (mutex1, std::adopt_lock_t ());
473
+ std::shared_ptr< Mutex> mutex0 = this ->sharedPromise_ ->obtainLock ();
474
+ std::lock_guard<Mutex> lock0 (* mutex0, std::adopt_lock_t ());
475
+ std::shared_ptr< Mutex> mutex1 = promise.sharedPromise_ ->obtainLock ();
476
+ std::lock_guard<Mutex> lock1 (* mutex1, std::adopt_lock_t ());
467
477
#endif
468
478
469
479
if (promise.sharedPromise_ && promise.sharedPromise_ ->promiseHolder_ ) {
@@ -482,8 +492,8 @@ Promise &Promise::then(const any &deferOrPromiseOrOnResolved) {
482
492
483
493
Promise &Promise::then (const any &onResolved, const any &onRejected) {
484
494
#if PROMISE_MULTITHREAD
485
- Mutex & mutex = this ->sharedPromise_ ->obtainLock ();
486
- std::lock_guard<Mutex> lock (mutex, std::adopt_lock_t ());
495
+ std::shared_ptr< Mutex> mutex = this ->sharedPromise_ ->obtainLock ();
496
+ std::lock_guard<Mutex> lock (* mutex, std::adopt_lock_t ());
487
497
#endif
488
498
489
499
std::shared_ptr<Task> task = std::make_shared<Task>(Task {
@@ -528,8 +538,8 @@ Promise &Promise::finally(const any &onFinally) {
528
538
529
539
void Promise::resolve (const any &arg) const {
530
540
#if PROMISE_MULTITHREAD
531
- Mutex & mutex = this ->sharedPromise_ ->obtainLock ();
532
- std::lock_guard<Mutex> lock (mutex, std::adopt_lock_t ());
541
+ std::shared_ptr< Mutex> mutex = this ->sharedPromise_ ->obtainLock ();
542
+ std::lock_guard<Mutex> lock (* mutex, std::adopt_lock_t ());
533
543
#endif
534
544
535
545
std::list<std::shared_ptr<Task>> &pendingTasks_ = this ->sharedPromise_ ->promiseHolder_ ->pendingTasks_ ;
@@ -542,8 +552,8 @@ void Promise::resolve(const any &arg) const {
542
552
543
553
void Promise::reject (const any &arg) const {
544
554
#if PROMISE_MULTITHREAD
545
- Mutex & mutex = this ->sharedPromise_ ->obtainLock ();
546
- std::lock_guard<Mutex> lock (mutex, std::adopt_lock_t ());
555
+ std::shared_ptr< Mutex> mutex = this ->sharedPromise_ ->obtainLock ();
556
+ std::lock_guard<Mutex> lock (* mutex, std::adopt_lock_t ());
547
557
#endif
548
558
549
559
std::list<std::shared_ptr<Task>> &pendingTasks_ = this ->sharedPromise_ ->promiseHolder_ ->pendingTasks_ ;
0 commit comments