thread safe php make ffi callback error(ext ffi)
Đang mở
Chưa có ai nhận issue này.
Bug
Extension: ffi
Status: Needs Triage
- Ngôn ngữ chính
- C
- Star
- 40.4k
- Fork
- 8.2k
- Merge trung bình
- 2 ngày 13 giờ
- Pull request đã merge (30 ngày)
- 96
Mô tả
Description
<?php
$ffi = FFI::cdef("
typedef void (*log_message_callback_t)(int level, const char* msg);
int test_start_str(const char* oldP, int id, log_message_callback_t log_callback);
", "test_lib.dll");
$log_callback = function($level, $msg) {
echo "Log (level $level): " . $msg . "\n";
};
$result = $ffi->test_start_str("example_parameter", 42, $log_callback);
echo "Result: $result\n";
this is the c code
#include <stdio.h>
#include <pthread.h>
#ifdef _WIN32
#define DLL_EXPORT __declspec(dllexport)
#else
#define DLL_EXPORT
#endif
typedef void (*log_message_callback_t)(int level, const char* msg);
typedef struct {
const char* oldP;
log_message_callback_t log_callback;
} thread_data_t;
void* thread_function(void* arg) {
thread_data_t* data = (thread_data_t*) arg;
const char* oldP = data->oldP;
log_message_callback_t log_callback = data->log_callback;
char message[256];
snprintf(message, sizeof(message), "Thread running with parameter %s", oldP);
if (log_callback) {
log_callback(1, message);
} else {
fprintf(stderr, "Log callback not set in thread\n");
}
return NULL;
}
DLL_EXPORT int test_start_str(const char* oldP, int id, log_message_callback_t log_callback) {
thread_data_t* data = (thread_data_t*) malloc(sizeof(thread_data_t));
if (!data) {
return -1;
}
data->oldP = oldP;
data->log_callback = log_callback;
pthread_t thread;
int result = pthread_create(&thread, NULL, thread_function, (void*) data);
if (result != 0) {
free(data);
return -1;
}
pthread_detach(thread);
return 0;
}
PHP Version
php 8.3.7
Operating System
windows10
Hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Hướng nghiên cứu
Bắt đầu bằng cách tái hiện ví dụ PHP FFI và C pthread được cung cấp trên PHP 8.3.7 trong Windows 10, ghi lại chính xác lỗi callback và thời điểm lỗi xảy ra. So sánh hành vi của callback trong thread với đường dẫn gọi đồng bộ; công việc được xem là hoàn tất khi có thể tái hiện lỗi và đã xác định rõ hành vi thread-safety dự kiến hoặc bản sửa cần thiết.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Đánh giá
- Công nghệ
- c, php
- Lĩnh vực
- backend
- Loại issue
- Lỗi
- Độ khó
- 4/5
- Thời gian dự kiến
- 3-5 ngày
- Mức độ hoạt động
- Đình trệ
- Độ rõ ràng
- Cần làm rõ
- Mức phù hợp với người mới
- 25/100