15 messages in net.java.dev.jna.usersRe: [jna-users] linux libc link error
FromSent OnAttachments
QuartzOct 27, 2007 7:55 am 
Timothy WallOct 27, 2007 8:14 am 
QuartzOct 27, 2007 11:10 am 
Timothy WallOct 27, 2007 12:54 pm 
QuartzOct 28, 2007 11:03 am 
QuartzOct 28, 2007 12:44 pm 
Timothy WallOct 28, 2007 1:44 pm 
Timothy WallOct 28, 2007 1:49 pm 
Timothy WallOct 28, 2007 1:55 pm 
Timothy WallOct 28, 2007 2:30 pm 
QuartzOct 29, 2007 8:23 am 
Timothy WallOct 29, 2007 1:56 pm 
QuartzOct 29, 2007 7:39 pm 
Timothy WallOct 30, 2007 6:37 am 
QuartzNov 3, 2007 9:39 am 
Actions with this message:
Paste this link in email or IM:
Paste this link in email or IM:
Atom feed for this thread
Paste this URL into your reader:
Subject:Re: [jna-users] linux libc link errorActions...
From:Timothy Wall (twal@dev.java.net)
Date:Oct 30, 2007 6:37:38 am
List:net.java.dev.jna.users

On Oct 29, 2007, at 10:40 PM, Quartz wrote:

This ends up (or does it...) as an ugly hardcoded very platform dependant class (see below): The pthread struct (in descr.h) is too difficult for my poor brain. Nothing if not persistent, I used brute force

Good qualities in a developer, I think.

hex dump and found my thread id number... It is at offset 144... it seems.

import com.sun.jna.Library; import com.sun.jna.Native; import com.sun.jna.Structure;

public interface Pthread extends Library {

Pthread INSTANCE = (Pthread)Native.loadLibrary("pthread", Pthread.class); //int pthread_self(); PthreadStruct pthread_self();

public static class PthreadStruct extends Structure { public long a,c,b,d,e,f,g,h, i,j,k,l,m,n,o,p; //16*8 = 128 public long q,r; //+16

An easier way to do this would be this:

public long[] offset = new byte[144/8]; // use "long" to ensure best alignment

//64bits of the form 0xpppppppptttttttt where p's are for pid, and t's are for thread id. //somehow, if I read two ints as in // int pid; // int tid; //the values gets inverted! WTF?

This is affected by the "endianness" of the CPU. Big-endian will store the upper bits first; little-endian stores the lower bits first.

//this 64 bits starts at offset 18*8 = 144; public long pidtid; } }

I get the thread id by using:

Pthread.PthreadStruct ps = Pthread.INSTANCE.pthread_self(); return (ps.pidtid & 0xffffFFFFL);

Some doc show that it could simply be a syscall (as in this dumb .c) but I have no clue how to do that with jna, and some sites say that syscall are not portable...

#include <stdio.h> #include <pthread.h> #include <sys/types.h> #include <linux/unistd.h> _syscall0(pid_t,gettid) pid_t gettid(void); int main(void) { printf("tid %d\n", gettid()); }

If I remember correctly, syscalls aren't actually function calls but macros around inline assembly, so it may be inaccessible from JNA. If it *is* a function call, then it shouldn't be a problem. As for portability, if the system is relying on task IDs, it is most likely to have gettid functionality. No it won't work on BSD, but BSD doesn't have /proc/self/task either.

Any cleaner suggestion to make?

Rather than defining a structure to get the task id, you could just allocate a raw block of memory that's big enough and do Pointer.getInt (offset) to pull out the field. But I would think there ought to be some API to get the ID explicitly.

BTW, do you know what package/RPM has descr.h in it? Is it glibc?