Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions src/runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,20 @@
#ifndef HAVE_PARALLEL_RUNTIME_H
#define HAVE_PARALLEL_RUNTIME_H

#if PHP_VERSION_ID < 80200
#define zend_atomic_bool bool
#define zend_atomic_bool_store(dest, value) (*(dest) = value)
#if PHP_VERSION_ID >= 80700
typedef atomic_bool php_parallel_interrupt_t;
#elif PHP_VERSION_ID >= 80200
typedef zend_atomic_bool php_parallel_interrupt_t;
#else
typedef bool php_parallel_interrupt_t;
#endif

typedef struct _php_parallel_runtime_t {
pthread_t thread;
php_parallel_monitor_t *monitor;
zend_string *bootstrap;
struct {
zend_atomic_bool *interrupt;
php_parallel_interrupt_t *interrupt;
} child;
struct {
void *server;
Expand Down
15 changes: 13 additions & 2 deletions src/scheduler.c
Original file line number Diff line number Diff line change
Expand Up @@ -743,6 +743,17 @@ void php_parallel_scheduler_push(php_parallel_runtime_t *runtime, zval *closure,
php_parallel_monitor_unlock(runtime->monitor);
}

static zend_always_inline void php_parallel_scheduler_request_interrupt(php_parallel_runtime_t *runtime)
{
#if PHP_VERSION_ID >= 80700
atomic_store(runtime->child.interrupt, true);
#elif PHP_VERSION_ID >= 80200
zend_atomic_bool_store(runtime->child.interrupt, true);
#else
*runtime->child.interrupt = true;
#endif
}

void php_parallel_scheduler_join(php_parallel_runtime_t *runtime, bool kill)
{
php_parallel_monitor_lock(runtime->monitor);
Expand All @@ -756,7 +767,7 @@ void php_parallel_scheduler_join(php_parallel_runtime_t *runtime, bool kill)
if (kill) {
php_parallel_monitor_set(runtime->monitor, PHP_PARALLEL_KILLED);

zend_atomic_bool_store(runtime->child.interrupt, true);
php_parallel_scheduler_request_interrupt(runtime);
} else {
php_parallel_monitor_set(runtime->monitor, PHP_PARALLEL_CLOSE);
}
Expand Down Expand Up @@ -790,7 +801,7 @@ bool php_parallel_scheduler_cancel(php_parallel_future_t *future)
php_parallel_monitor_lock(future->monitor);

if (!php_parallel_monitor_check(future->monitor, PHP_PARALLEL_READY)) {
zend_atomic_bool_store(future->runtime->child.interrupt, true);
php_parallel_scheduler_request_interrupt(future->runtime);

php_parallel_monitor_set(future->monitor, PHP_PARALLEL_CANCELLED);
php_parallel_monitor_wait_locked(future->monitor, PHP_PARALLEL_READY);
Expand Down
Loading