|
29 | 29 | #include "val.h" |
30 | 30 | #include "array.h" |
31 | 31 |
|
32 | | -/* 自旋次数:阻塞前先自旋,减少高并发场景的 syscall(vlang 使用 750) */ |
33 | | -#define TPHP_CHAN_SPIN 750 |
| 32 | +/* 自旋次数:阻塞前先自旋,减少高并发场景的 syscall。 |
| 33 | + 750→64:原值过大,每次"自旋"实为 lock/unlock,高竞争下放大锁开销。 |
| 34 | + 降低到 64 + 每轮 thrd_yield() 让出 CPU,减少无效锁竞争。 */ |
| 35 | +#define TPHP_CHAN_SPIN 64 |
34 | 36 |
|
35 | 37 | /* ════════════════════════════════════════════════════════════ |
36 | 38 | Channel 类 |
@@ -122,6 +124,7 @@ static inline void tphp_class_Channel_push(tphp_class_Channel *self, t_var v) { |
122 | 124 | return; |
123 | 125 | } |
124 | 126 | mtx_unlock(&self->mtx); |
| 127 | + thrd_yield(); |
125 | 128 | } |
126 | 129 | /* 阻塞等待 */ |
127 | 130 | mtx_lock(&self->mtx); |
@@ -159,6 +162,7 @@ static inline t_var tphp_class_Channel_pop(tphp_class_Channel *self) { |
159 | 162 | return VAR_NULL(); |
160 | 163 | } |
161 | 164 | mtx_unlock(&self->mtx); |
| 165 | + thrd_yield(); |
162 | 166 | } |
163 | 167 | /* 阻塞等待 */ |
164 | 168 | mtx_lock(&self->mtx); |
@@ -342,6 +346,7 @@ static inline t_var tphp_class_Future_await(tphp_class_Future *self) { |
342 | 346 | cnd_wait(&self->done, &self->mtx); |
343 | 347 | } |
344 | 348 | mtx_unlock(&self->mtx); |
| 349 | + thrd_yield(); |
345 | 350 | } |
346 | 351 | if (self->state == _TP_FUTURE_REJECTED) { |
347 | 352 | /* 抛出原始异常对象 */ |
@@ -385,6 +390,7 @@ static inline tphp_class_Future* tphp_class_Future_then(tphp_class_Future *self, |
385 | 390 | cnd_wait(&self->done, &self->mtx); |
386 | 391 | } |
387 | 392 | mtx_unlock(&self->mtx); |
| 393 | + thrd_yield(); |
388 | 394 | } |
389 | 395 | if (self->state == _TP_FUTURE_REJECTED) { |
390 | 396 | /* 原 Future 被 reject — 透传错误到 next */ |
@@ -414,6 +420,7 @@ static inline tphp_class_Future* tphp_class_Future_catch(tphp_class_Future *self |
414 | 420 | cnd_wait(&self->done, &self->mtx); |
415 | 421 | } |
416 | 422 | mtx_unlock(&self->mtx); |
| 423 | + thrd_yield(); |
417 | 424 | } |
418 | 425 | if (self->state == _TP_FUTURE_REJECTED) { |
419 | 426 | /* reject — 调用恢复回调 */ |
|
0 commit comments