fix: resolve CGo type conflict in error handler
Use pure C callback instead of //export to avoid const char* vs GoString type mismatch in cgo-generated headers. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
5e2d941b4d
commit
a0f77960a1
1 changed files with 15 additions and 6 deletions
|
|
@ -25,10 +25,18 @@ package mlx
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include "mlx/c/mlx.h"
|
#include "mlx/c/mlx.h"
|
||||||
|
|
||||||
extern void goMLXErrorHandler(const char *msg, void *data);
|
static const char *last_mlx_error = NULL;
|
||||||
|
|
||||||
|
static void mlx_go_error_handler(const char *msg, void *data) {
|
||||||
|
last_mlx_error = msg;
|
||||||
|
}
|
||||||
|
|
||||||
static void set_error_handler() {
|
static void set_error_handler() {
|
||||||
mlx_set_error_handler(&goMLXErrorHandler, NULL, NULL);
|
mlx_set_error_handler(&mlx_go_error_handler, NULL, NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
static const char* get_last_error() {
|
||||||
|
return last_mlx_error;
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
import "C"
|
import "C"
|
||||||
|
|
@ -36,7 +44,6 @@ import "C"
|
||||||
import (
|
import (
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"sync"
|
"sync"
|
||||||
"unsafe"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var initOnce sync.Once
|
var initOnce sync.Once
|
||||||
|
|
@ -49,10 +56,12 @@ func Init() {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
//export goMLXErrorHandler
|
// checkError logs the last MLX error if any occurred.
|
||||||
func goMLXErrorHandler(msg *C.char, data unsafe.Pointer) {
|
func checkError() {
|
||||||
|
if msg := C.get_last_error(); msg != nil {
|
||||||
slog.Error("mlx", "error", C.GoString(msg))
|
slog.Error("mlx", "error", C.GoString(msg))
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Materialize synchronously evaluates arrays, computing their values on the GPU.
|
// Materialize synchronously evaluates arrays, computing their values on the GPU.
|
||||||
// This is the MLX equivalent of forcing lazy computation to complete.
|
// This is the MLX equivalent of forcing lazy computation to complete.
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue