RTHREADS and librthread

2007-06-25 Thread Vortechz
I hope there are people using/testing/developing rthreads out there who can
explain to me why
syscall getthrid fails with SIGSYS on my system, 4.1-release.

1. I have RTHREADS defined in my kernel. I have checked that the
rthread code is included at compile time, and I also tried this:

$ grep getthrid /bsd
Binary file /bsd matches

2. The code I'm trying to run: 
http://www.nabble.com/file/p11287569/rthread.tar rthread.tar 
It may be extremely stupid, but it compiles...

3. I compiled and installed librthread, and copied librthread.so.1.0 in
place of libpthread.so.7.0
My program is linked against libpthread:

$ ldd rthtest  
rthtest:
StartEnd  Type Open Ref GrpRef Name
  exe  10   0  rthtest
0bd7c000 2bd85000 rlib 01   0  /usr/lib/libpthread.so.7.0
0cde 2ce14000 rlib 01   0  /usr/lib/libc.so.40.3
00466000 00466000 rtld 01   0  /usr/libexec/ld.so

4. Output and info:

$ ./rthtest
Bad system call (core dumped) 

$ gdb ./rthtest rthtest.core   
GNU gdb 6.3
[...GPL...]
Core was generated by `rthtest'.
Program terminated with signal 12, Bad system call.
Reading symbols from /usr/lib/libpthread.so.7.0...done.
Loaded symbols for /usr/lib/libpthread.so.7.0
Symbols already loaded for /usr/lib/libpthread.so.7.0
Reading symbols from /usr/lib/libc.so.40.3...done.Loaded symbols for
/usr/lib/libc.so.40.3
Reading symbols from /usr/libexec/ld.so...done.
Loaded symbols for /usr/libexec/ld.so
#0  0x02a846a1 in getthrid () from /usr/lib/libc.so.40.3
(gdb) bt
#0  0x02a846a1 in getthrid () from /usr/lib/libc.so.40.3
#1  0x1c0006bb in funkythread (data=0x3c11) at rthreadtest.c:11
#2  0x0df40fcb in _thread_start ()
at /usr/src/lib/libpthread/uthread/uthread_create.c:244
#3  0x001f in ?? ()
#4  0x in ?? ()

Did I do something extremely wrong when I tried to switch to rthreads? Do I
need to recompile
lots of things?

Don't point me to using the regular libpthread, I have good reason to try
rthreads.

Alsodon't use this post to start a troll-inducing thread about
performance, scalability 
and/or personal opinions about threads and their usage. Reply with useful
information or shut up.

// Vortechz

-- 
View this message in context: 
http://www.nabble.com/RTHREADS-and-librthread-tf3976489.html#a11287569
Sent from the openbsd user - misc mailing list archive at Nabble.com.



Re: problems with kthread

2007-06-23 Thread Vortechz
I am certainly not the right person to answer this...but anyway...

After getting my securelevel down under ice, and playing a little with the
/usr/share/lkm source, 
I tried putting your code into the mycall.c and it worked for me. (I can
send you a tarball if you want to try it.)
In fact, I put NULL where you had mypr, but that makes no big difference
for a simple test.


From the manpage of kthread_create: 

 Since the system has to be up and running for creating new processes, de-
 vice drivers that want to create kernel threads early (e.g., at attach
 time) may use kthread_create_deferred() instead.  The system will call
 back the function func with argument arg when it can create threads, so
 it is up to func to call kthread_create() at that point.

This is probably the explanation to why there is no fork1 call creating a
thread 
in the kthread_run_deferred_queue function. There is no fork1 in
kthread_create_deferred either,
of course...

I don't write device drivers, so I can't help you further. However, I could
reason a little:

The global variable kthread_create_now is a state which tells the kthread
functions 
whether the kernel can create threads. 

The funky thing is that it is not obvious how to write your kernel module.
If you use kthread_create
and it returns an error != EAGAIN, you might assume that the kernel can not
create threads at that point. 
Hence, it would be right to use kthread_create_deferred, but then the
function which is supposed to
be run by the kthread is not in its own thread, so you will have to try
kthread_create from that function...
and then...if that fails with error != EAGAIN...you could put it on the
queue by using
kthread_create_deferredand thenhlp?

Conclusion is that everything is allright unless you write poorly designed
drivers. :)

Just my two cents...

// V.A.


syl-4 wrote:
 
 Hi everyone,
 
 I'm not sure if I'm at the right place to ask this question, but I
 might aswell try; I'm writing you this mail because there is one thing
 I can't understand in the openbsd kthread.
 
 Actually, it is those two functions from the kthread's man :
 kthread_create and kthread_create_deferred.. from man 9
 kthread_create creates a kernel thread and kthread_create_deferred
 adds a pointer to a function in a queue that will be then went through
 and each of it's elements will be launched in a seperate kthread.
 
 I wrote a simple syscall using the lkm and kthread_create;
 Here is the syscall :
 
 #include sys/param.h
 #include sys/kthread.h
 #include sys/types.h
 #include sys/malloc.h
 
 #define NB_THREAD   1
 
 voidtheHook(void *data)
 {
   uprintf(Goodbye threaded world\n);
   kthread_exit(0);
 }
 
 int mycall(struct proc *p, void *uap, int retval[])
 {
   int error;
   struct  proc *mypr;
 
   uprintf( I create a new thread for fun :)\n );
   kthread_create(theHook, NULL, mypr, bite!);
   return (0);
 }
 
 But it would not work : The thread was not launched.
 I therefor replaced kthread_create by kthread_create_deferred and it
 looked like this :
 
 kthread_create_deferred(theHook, NULL)
 
 And then the thread was launched ...
 
 This was disturbing me, so I went and looked at the kernel sources for
 the threads :
 /usr/src/sys/kern/kern_kthread.c
 /usr/src/sys/sys/kthread.h
 
 In kthread_create, everything seems normal, it creates a process.
 But in kthread_create_deferred i found this code :
 
 kthread_create_deferred(void (*func)(void *), void *arg)
 {
   struct kthread_q *kq;
 
   if (kthread_create_now) {
   (*func)(arg);
   return;
   }
 
   kq = malloc(sizeof *kq, M_TEMP, M_NOWAIT);
   if (kq == NULL)
   panic(unable to allocate kthread_q);
   bzero(kq, sizeof *kq);
 
   kq-kq_func = func;
   kq-kq_arg = arg;
 
   SIMPLEQ_INSERT_TAIL(kthread_q, kq, kq_q);
 }
 
 What this code roughly does is :
 Check if the variable kthread_create_now is true,
 if it is, it launchs the function sent in parameter
 else it adds it in the queue with it's attached parameters
 
 So I ask myself :When is initialised this kthread_create_now variable ?
 And I found out that it is global, and never initialized. It is also
 used in the next function : kthread_run_deferred_queue :
 
 kthread_run_deferred_queue(void)
 {
   struct kthread_q *kq;
 
   /* No longer need to defer kthread creation. */
   kthread_create_now = 1;
 
   while ((kq = SIMPLEQ_FIRST(kthread_q)) != NULL) {
   SIMPLEQ_REMOVE_HEAD(kthread_q, kq_q);
   (*kq-kq_func)(kq-kq_arg);
   free(kq, M_TEMP);
 
 
 
 
   }
 }
 
 it sets ouyr mysterious variable to 1 and then launch every function
 in the queu one by one. I can't find in any of those functions a hint
 of a thread.
 i try searching where kthread_run_deffered_queue is called, maybe
 there is where the threads are launched ?
 After a grep over all the sources, I