Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:128942 X-Original-To: internals@lists.php.net Delivered-To: internals@lists.php.net Received: from php-smtp4.php.net (php-smtp4.php.net [45.112.84.5]) by lists.php.net (Postfix) with ESMTPS id 4D31D1A00BC for ; Fri, 24 Oct 2025 06:43:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1761288206; bh=XRKmT5Bh9pgSsP8nMBoIzBgSYS8Rxz9AGnEMG1KlhGw=; h=Date:From:To:In-Reply-To:References:Subject:From; b=jMtdJg/YVHTQfFJQB1YrY+Dc7zhCJuE5ak9MRkJWYV2w06GK+LGTjhKeznDUW9xC+ 1p6Ph6K/E6BfWYZlGCVm9YgARa0DQHunVBL02WlZIaZI1sJPiRMT9SrYS9fHkOECIQ kzxNX5XUAkLyV7jk63T7tZmdxwlbfUajwl1GgpJptbzD2OUlZc1RHAo9V4xzSMU0Do X4KbmwlH0f2SMl8dwp5u72aJ7Qi0Js7Ixu82pnhqdfpf2Ve6w9P5hRZa2zkxCs5E5A MvYQEEbZrvCzt5CqTJIiIp3Jb9Z1qjDbVwkYM3hEMhf7btySIbuuBqsJ/Ju17QiB7M Ym6s4XGR5lZPg== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 470A1180083 for ; Fri, 24 Oct 2025 06:43:25 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-0.1 required=5.0 tests=BAYES_50,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_MISSING,HTML_MESSAGE, RCVD_IN_DNSWL_LOW,RCVD_IN_MSPIKE_H2,SPF_HELO_PASS,SPF_PASS autolearn=no autolearn_force=no version=4.0.1 X-Spam-Virus: No X-Envelope-From: Received: from fhigh-b6-smtp.messagingengine.com (fhigh-b6-smtp.messagingengine.com [202.12.124.157]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Fri, 24 Oct 2025 06:43:24 +0000 (UTC) Received: from phl-compute-05.internal (phl-compute-05.internal [10.202.2.45]) by mailfhigh.stl.internal (Postfix) with ESMTP id 44AC07A0112; Fri, 24 Oct 2025 02:43:19 -0400 (EDT) Received: from phl-imap-05 ([10.202.2.95]) by phl-compute-05.internal (MEProxy); Fri, 24 Oct 2025 02:43:19 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bottled.codes; h=cc:content-type:content-type:date:date:from:from:in-reply-to :in-reply-to:message-id:mime-version:references:reply-to:subject :subject:to:to; s=fm3; t=1761288199; x=1761374599; bh=XRKmT5Bh9p gSsP8nMBoIzBgSYS8Rxz9AGnEMG1KlhGw=; b=G/C6s1Hlx8Hrk/KOm46Ysa8WCt dcPvqsC9A+Awq61w24eAl3mzUqgy/bD2own9aoXgGjV6VV2jj6K1amFUroeEO2ZH h1SW9OCVyxxyM+nhL54TdQkwqmJlffFGfrZuz2SIrFTGH8LYB+gKbfXIfV1vqzLP T/CnkYk4fqWzF5HUl7Mf+Y0QpP+HmDSAihC9ZkpgiL/ssGB3zG3yWTYIlzi6YN67 4eXIL97gy0Evk/16AohvnWA5n0H1Z9D2iUpDLwrAvt/wtX2AcABiwM20+CR+OfMp /cH0T1aA7BhR3y+cW+p+hMlmfPC6n8WAYaVY8HbXVA5WDOjNgqAF+tdK8RsQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:subject:subject:to :to:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s=fm2; t= 1761288199; x=1761374599; bh=XRKmT5Bh9pgSsP8nMBoIzBgSYS8Rxz9AGnE MG1KlhGw=; b=x53bUPePYfcQLaX7GmDuwKy10xDuduw13GbWuqwZLnBGx2wA5Hy OFJmsDSqQHsitfTzl4/LIh2R+KG05C33Xyva/mrbasqugSDNnncV/Pc/n5+uQ+P/ a00/HRQbJpoawyMOnTZTtfX6z+JpWimMGmCkCMUcoVJQYEjlLYlUu3bhveZFlkKM r0OxvRdLo99q7fWKeYnOM5eLIbXfjXS+SX9fEVe2to/FEhu71a7WiNBLvKIVSIBB aMURyBzyyQ2blK+UCAyKHM+eplmg9qhQAGwCPNOTxk6UWqGICo+++VsaFSorQsJC cOmz4vr7/PIexc007XWWs8EUwz696YZaG+g== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeffedrtdeggddugeekieehucetufdoteggodetrf dotffvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfurfetoffkrfgpnffqhgenuceu rghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmnecujf gurhepofggfffhvffkjghfufgtsegrtderreertdejnecuhfhrohhmpedftfhosgcunfgr nhguvghrshdfuceorhhosgessghothhtlhgvugdrtghouggvsheqnecuggftrfgrthhtvg hrnheptdeujedttefhueelhfdtleeiudetlefftdduleehffegtdeihefhleeijefgveeg necuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomheprhhosg essghothhtlhgvugdrtghouggvshdpnhgspghrtghpthhtohepgedpmhhouggvpehsmhht phhouhhtpdhrtghpthhtohepvggumhhonhgurdhhthesghhmrghilhdrtghomhdprhgtph htthhopehinhhtvghrnhgrlhhssehlihhsthhsrdhphhhprdhnvghtpdhrtghpthhtohep rggrrhhonhesthhrohifshhkihdrtghomhdprhgtphhtthhopehjsggrfhhfohhrugesii horhhtrdhnvght X-ME-Proxy: Feedback-ID: ifab94697:Fastmail Received: by mailuser.phl.internal (Postfix, from userid 501) id 992B21820054; Fri, 24 Oct 2025 02:43:18 -0400 (EDT) X-Mailer: MessagingEngine.com Webmail Interface Precedence: list list-help: list-unsubscribe: list-post: List-Id: x-ms-reactions: disallow MIME-Version: 1.0 X-ThreadId: Ao23bsqP5RK3 Date: Fri, 24 Oct 2025 08:42:58 +0200 To: "Edmond Dantes" , "php internals" , "John Bafford" , "Aaron Piotrowski" Message-ID: In-Reply-To: References: Subject: [PHP-DEV] Re: PHP and parallelism Content-Type: multipart/alternative; boundary=fd5c8e7defbe4d90bc47a9a487d38819 From: rob@bottled.codes ("Rob Landers") --fd5c8e7defbe4d90bc47a9a487d38819 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable On Fri, Oct 24, 2025, at 08:34, Edmond Dantes wrote: > Hello everyone, >=20 > In the TrueAsync RFC 4 thread, John Bafford raised a question about > parallelism. > I would like to share my thoughts and highlight this branch of the dis= cussion. >=20 > # What is parallelism in the context of coroutines? > Parallelism for coroutines means the ability to execute a coroutine in > one thread or another (but not simultaneously). > In this case, the Scheduler not only manages the execution order of > coroutines but also decides which thread from the thread pool should > be assigned to a coroutine. >=20 > # What is the problem? >=20 > If two different coroutines run in separate threads and write to the > same memory region =E2=80=94 that is, to the same PHP object =E2=80=94= it will lead to > unpredictable consequences. >=20 > The issue is that PHP does not control memory access in such cases and > effectively allows the programmer to shoot themselves in the head. >=20 > How can this be solved? >=20 > ### Object transfer policy >=20 > The object transfer policy defines how a PHP object can be safely > passed to another coroutine. > Possible transfer methods: > 1. Via parameters > 2. Via context > 3. Via channels >=20 > All these methods can be viewed as variations of passing objects > through channels (similar to the Erlang model =E2=80=94 no direct shar= ing). >=20 > The same policy applies to all methods. >=20 > An interface is defined to guarantee an object=E2=80=99s =E2=80=9Cthre= ad safety.=E2=80=9D For > example, if a coroutine receives an object parameter without this > interface, an error occurs. >=20 > In PHP, there are several possible ways to achieve thread safety: > 1. **Ownership transfer**, as in C++/Rust =E2=80=94 the object leaves = the > current scope and becomes owned by the coroutine. > 2. **Proxy-based access**, using a special constructor that creates a > proxy object for access to the real one (similar to actors in Swift). > 3. **Shared access** with a built-in mutex. >=20 > This approach requires minimal changes to the language syntax =E2=80=94= in > fact, none at all. > From a performance standpoint, it is perfectly acceptable for PHP to > validate function parameters, so no significant overhead should be > expected. >=20 > The main challenge of parallelism lies in making PHP=E2=80=99s interna= l state, > garbage collector, and memory manager thread-safe. >=20 > # How does this relate to TrueAsync? >=20 > At the moment, TrueAsync does not perform additional checks when > passing parameters to coroutines. > This is a problem =E2=80=94 because once PHP introduces concurrency, > developers will start freely using references (&reference) and shared > objects. >=20 > When multitasking is later added to coroutines, it will break existing > PHP code that has already been written. >=20 > Therefore, if PHP truly aims to become multithreaded, its memory > ownership model must be carefully designed today. >=20 > --- > Best Regards, Ed >=20 I=E2=80=99m not sure this is the place to discuss it, nor the time. But = C# is cooperative multitasking on top of a VM which is preemptive multit= asking. This basically allows you to run multithreaded without worrying = about =E2=80=9Cshooting yourself in the foot=E2=80=9D by accessing the s= ame memory, most of the time. In other words, there is plenty of prior a= rt out there and ways to solve this problem. It doesn=E2=80=99t mean we = need to solve it today. Trying to solve a problem that doesn=E2=80=99t c= urrently exist and =E2=80=9Cmight=E2=80=9D be a problem later is exactly= how you end up with overengineered solutions that never actually solve = the problem.=20 =E2=80=94 Rob --fd5c8e7defbe4d90bc47a9a487d38819 Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: quoted-printable


On Fri, Oct 24, 2025, at 08:34, Edmond Dantes wrote:
Hello everyone,<= /div>

In the TrueAsync RFC 4 thread, John Bafford rai= sed a question about
parallelism.
I would like to sh= are my thoughts and highlight this branch of the discussion.
<= br>
# What is parallelism in the context of coroutines?
<= div>Parallelism for coroutines means the ability to execute a coroutine = in
one thread or another (but not simultaneously).
I= n this case, the Scheduler not only manages the execution order of
=
coroutines but also decides which thread from the thread pool shoul= d
be assigned to a coroutine.

# What = is the problem?

If two different coroutines run= in separate threads and write to the
same memory region =E2=80= =94 that is, to the same PHP object =E2=80=94 it will lead to
= unpredictable consequences.

The issue is that P= HP does not control memory access in such cases and
effectivel= y allows the programmer to shoot themselves in the head.

<= /div>
How can this be solved?

### Object tr= ansfer policy

The object transfer policy define= s how a PHP object can be safely
passed to another coroutine.<= /div>
Possible transfer methods:
1. Via parameters
2. Via context
3. Via channels

All= these methods can be viewed as variations of passing objects
= through channels (similar to the Erlang model =E2=80=94 no direct sharin= g).

The same policy applies to all methods.

An interface is defined to guarantee an object=E2=80= =99s =E2=80=9Cthread safety.=E2=80=9D For
example, if a corout= ine receives an object parameter without this
interface, an er= ror occurs.

In PHP, there are several possible = ways to achieve thread safety:
1. **Ownership transfer**, as i= n C++/Rust =E2=80=94 the object leaves the
current scope and b= ecomes owned by the coroutine.
2. **Proxy-based access**, usin= g a special constructor that creates a
proxy object for access= to the real one (similar to actors in Swift).
3. **Shared acc= ess** with a built-in mutex.

This approach requ= ires minimal changes to the language syntax =E2=80=94 in
fact,= none at all.
From a performance standpoint, it is perfectly a= cceptable for PHP to
validate function parameters, so no signi= ficant overhead should be
expected.

T= he main challenge of parallelism lies in making PHP=E2=80=99s internal s= tate,
garbage collector, and memory manager thread-safe.
=

# How does this relate to TrueAsync?

<= /div>
At the moment, TrueAsync does not perform additional checks wh= en
passing parameters to coroutines.
This is a probl= em =E2=80=94 because once PHP introduces concurrency,
develope= rs will start freely using references (&reference) and shared
<= div>objects.

When multitasking is later added t= o coroutines, it will break existing
PHP code that has already= been written.

Therefore, if PHP truly aims to = become multithreaded, its memory
ownership model must be caref= ully designed today.

---
Best Regards= , Ed


I=E2=80=99m no= t sure this is the place to discuss it, nor the time. But C# is cooperat= ive multitasking on top of a VM which is preemptive multitasking. This b= asically allows you to run multithreaded without worrying about =E2=80=9C= shooting yourself in the foot=E2=80=9D by accessing the same memory, mos= t of the time. In other words, there is plenty of prior art out there an= d ways to solve this problem. It doesn=E2=80=99t mean we need to solve i= t today. Trying to solve a problem that doesn=E2=80=99t currently exist = and =E2=80=9Cmight=E2=80=9D be a problem later is exactly how you end up= with overengineered solutions that never actually solve the problem.&nb= sp;

=E2=80=94 Rob
--fd5c8e7defbe4d90bc47a9a487d38819--