Skip to content

Commit dc773ec

Browse files
committed
Update channel.h
1 parent f6e0001 commit dc773ec

1 file changed

Lines changed: 9 additions & 2 deletions

File tree

‎include/object/channel.h‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,10 @@
2929
#include "val.h"
3030
#include "array.h"
3131

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
3436

3537
/* ════════════════════════════════════════════════════════════
3638
Channel 类
@@ -122,6 +124,7 @@ static inline void tphp_class_Channel_push(tphp_class_Channel *self, t_var v) {
122124
return;
123125
}
124126
mtx_unlock(&self->mtx);
127+
thrd_yield();
125128
}
126129
/* 阻塞等待 */
127130
mtx_lock(&self->mtx);
@@ -159,6 +162,7 @@ static inline t_var tphp_class_Channel_pop(tphp_class_Channel *self) {
159162
return VAR_NULL();
160163
}
161164
mtx_unlock(&self->mtx);
165+
thrd_yield();
162166
}
163167
/* 阻塞等待 */
164168
mtx_lock(&self->mtx);
@@ -342,6 +346,7 @@ static inline t_var tphp_class_Future_await(tphp_class_Future *self) {
342346
cnd_wait(&self->done, &self->mtx);
343347
}
344348
mtx_unlock(&self->mtx);
349+
thrd_yield();
345350
}
346351
if (self->state == _TP_FUTURE_REJECTED) {
347352
/* 抛出原始异常对象 */
@@ -385,6 +390,7 @@ static inline tphp_class_Future* tphp_class_Future_then(tphp_class_Future *self,
385390
cnd_wait(&self->done, &self->mtx);
386391
}
387392
mtx_unlock(&self->mtx);
393+
thrd_yield();
388394
}
389395
if (self->state == _TP_FUTURE_REJECTED) {
390396
/* 原 Future 被 reject — 透传错误到 next */
@@ -414,6 +420,7 @@ static inline tphp_class_Future* tphp_class_Future_catch(tphp_class_Future *self
414420
cnd_wait(&self->done, &self->mtx);
415421
}
416422
mtx_unlock(&self->mtx);
423+
thrd_yield();
417424
}
418425
if (self->state == _TP_FUTURE_REJECTED) {
419426
/* reject — 调用恢复回调 */

0 commit comments

Comments
 (0)