I’m making calls into a 3rd party library that blocks on read() or write() to files. Those are pseudo-files used to talk to the Linux kernel. Sometimes, the 3rd party library may block an application for several seconds and in some cases up to 1 minute. For that reason, I run these calls in threads using Git.Task.run_in_thread().
I now need a way to terminate a task before the blocking call returns. In other words, I would like to send a SIGINT (or SIGTERM) to the thread to force the read() or write() operations to return immediately with a EINTR. POSIX provides functions to send signals to threads (e.g. kill, pthread_kill, tgkill, pthread_sigqueue, etc), but these functions require the PID of the thread.
My question is whether GLib provides a way to signal the thread associated with run_in_thread() or is there a way to retrieve the PID associated with the thread so that I can send a signal.
You mean g_task_run_in_thread(), yes? And you want to get its tid? No, there is no way to get the tid for the thread. Even if you could do this, killing it would be wrong because it’s going to be a thread pool thread that will be used to run other unrelated tasks: it’s not a one-off thread that’s used only for your particular task. So what can you do instead? I would give up on GTask, for starters. It looks like GThread is also not powerful enough to do what you want. You’ll probably need to use pthreads APIs directly. Good luck.
I’m not sure that sending a Unix signal to the thread will accomplish what you want? I assume that if you send SIGINT or SIGTERM to a thread, the entire process will die. I admit I’m not certain, though. I would look into using pthread_cancel() instead.
I had try to expose gthread cancel to glib some time ago, but it still doesn’t behave reliably.
So… I’d say it’s just better to call and forget, canceling the task so that once unblocked it will just return.
This topic was automatically closed 45 days after the last reply. New replies are no longer allowed.