Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dcli compile broken on mac os #253

Open
bsutton opened this issue Aug 19, 2024 · 7 comments
Open

dcli compile broken on mac os #253

bsutton opened this issue Aug 19, 2024 · 7 comments

Comments

@bsutton
Copy link
Collaborator

bsutton commented Aug 19, 2024

Hi, I have same problem.
dcli version is 6.0.5 and dart 3.4.3, on macos arm64

ArgumentError: Invalid argument(s): Couldn't resolve native function 'pthread_mutex_timedlock' in 'package:native_synchronization_temp/src/bindings/pthread.dart' : No asset with id 'package:native_synchronization_temp/src/bindings/pthread.dart' found. No available native assets. Attempted to fallback to process lookup. dlsym(RTLD_DEFAULT, pthread_mutex_timedlock): symbol not found.

This problem also affects and dcli function that uses withNamedLockAsync

@bsutton
Copy link
Collaborator Author

bsutton commented Aug 19, 2024

@bsutton The solution seems to be to provide a custom implementation of the pthread_mutex_timedlock function.
I'm making some tries editing directly the pub content.

I don't known low level details so with the help of chatgpt I replaced pthread_mutex_timedlock function in native_synchronization_temp/lib/src/bindings/pthread.dart with:

/// at the top, add another import
import 'package:ffi/ffi.dart';

/// Constants (maybe we should get these values in another way, currently hard coded)
const int EBUSY = 16; // Resource busy
const int ETIMEDOUT = 60; // Operation timed out
const int CLOCK_REALTIME = 0;

/// Bind the clock_gettime function from the C library
final clock_gettime = DynamicLibrary.process().lookupFunction<
Int32 Function(Int32 clockId, Pointer<pthread_timespec_t> tp),
int Function(int clockId, Pointer<pthread_timespec_t> tp)>('clock_gettime');

/// Mutex lock implementation for macOS
int pthread_mutex_timedlock(
Pointer<pthread_mutex_t> mutex, Pointer<pthread_timespec_t> abstime) {
final pthreadMutexTryLock = DynamicLibrary.process().lookupFunction<
Int Function(Pointer<pthread_mutex_t>),
int Function(Pointer<pthread_mutex_t>)>('pthread_mutex_trylock');

final nanosleep = DynamicLibrary.process().lookupFunction<
Int Function(
Pointer<pthread_timespec_t> req, Pointer<pthread_timespec_t> rem),
int Function(Pointer<pthread_timespec_t> req,
Pointer<pthread_timespec_t> rem)>('nanosleep');

final timeout = abstime.ref;
final now = malloc<pthread_timespec_t>();
final remainingTime = malloc<pthread_timespec_t>();

while (true) {
final ret = pthreadMutexTryLock(mutex);
if (ret == 0) {
malloc.free(now);
malloc.free(remainingTime);
return 0;
} else if (ret != EBUSY) {
malloc.free(now);
malloc.free(remainingTime);
return ret;
}

clock_gettime(CLOCK_REALTIME, now);

if (now.ref.tv_sec > timeout.tv_sec ||
    (now.ref.tv_sec == timeout.tv_sec &&
        now.ref.tv_nsec >= timeout.tv_nsec)) {
  malloc.free(now);
  malloc.free(remainingTime);
  return ETIMEDOUT;
}

remainingTime.ref.tv_sec = timeout.tv_sec - now.ref.tv_sec;
remainingTime.ref.tv_nsec = timeout.tv_nsec - now.ref.tv_nsec;
if (remainingTime.ref.tv_nsec < 0) {
  remainingTime.ref.tv_sec -= 1;
  remainingTime.ref.tv_nsec += 1000000000;
}

nanosleep(remainingTime, nullptr);

}
}
Now the cli works, I have tried with a 'some command'.run (that previously didn't work)

I don't know if the provided code is a right mock implementation of the pthread_mutex_timedlock.

@bsutton
Copy link
Collaborator Author

bsutton commented Aug 19, 2024

And now I've just read your updated comment and realised what the code was for.

So you are saying that macos is missing the pthread_mutex_timedlock. That puts quite a different shade on the problem.

So I don't have time now but I will look at the practicalities of integrating your mutex code into the library.

At a quick look I don't think the chat gpt implementation is correct.

This link proposes a solution:

https://lists.apache.org/thread/yqdnsky6svc5cfdkx1s8m4d3j8h9xyj6

edit: updated link

@bsutton
Copy link
Collaborator Author

bsutton commented Aug 19, 2024

So, the issue is more related to native_synchronization_temp/native_synchronization?

@bsutton
Copy link
Collaborator Author

bsutton commented Aug 19, 2024

@danieletulone I'm heavily committed this week, If you have time to look at the above link and raise a PR it would be appreciated.

@jifferon
Copy link

Hi, @bsutton! Any updates on this issue?

@bsutton
Copy link
Collaborator Author

bsutton commented Oct 16, 2024 via email

@bsutton
Copy link
Collaborator Author

bsutton commented Oct 19, 2024

I've released 6.1.0 which I think solves the problem.
Some of my unit tests are broken on macos so I've not yet managed to get a full test run, but will work on that over the next week.

Give it a spin and let me know if you have any issues.

For the broader audience, this also fixes similar problems in Windows.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants