Re: [patch] Multiple/Java thread support

Owen Taylor <> writes:

> I think there is a serious scalability problem with that.

I tend to agree.

> I'm not completely sure this is the right level of abstraction,
> perhaps it should be lower:
>   gpointer (*create_mutex) (void);
>   void     (*lock_mutex)   (gpointer mutex);
>   void     (*unlock_mutex) (gpointer mutex);

I like this level of abstract better, however, there's a problem with
it.  Under Java threads, it would not be possible to create a mutex
using a void callback.  Unlike pthreads, Java needs an environment
pointer to figure out what thread is doing what.

So for:
(*env)->MonitorEnter (env, mutex);

The VM knows what thread is attempting to gain the lock by the use of
the environment pointer.

In pthreads, you can just do a pthread_mutex_{un}lock, and the
"current" thread grabs the mutex.  In native C land, Java has no
concept of the "current" thread.

So, here's a slightly modified API, which should work:

gpointer (*create_mutex) (gpointer context);
void     (*lock_mutex)   (gpointer mutex);
void     (*unlock_mutex) (gpointer mutex);

For threading libraries which do not need the extra context pointer,
they can just pass in NULL.

For pthreads, the interface which already exists in gdkthreads will
work fine.  However, for Java threads, the "mutex" used would actually
be a mutex and a context/environment that changes for each thread, so
there needs to be some way to expose the mutex which gdkthreads

gpointer get_mutex (void);

Or, better yet, you could remove the functionality of creating a mutex
from gdk_threads_init.  The application would be responsible for
creating the correct type of mutex.

So you'd have:

void gdk_threads_init (gpointer mutex);
void gdk_threads_enter (void);
void gdk_threads_leave (void);

where gdk_threads_enter, and gdk_threads_leave would use the mutex
passed into gdk_threads_init.

Threading libraries which required a different "context" for locking
and unlocking of the mutex would just do that directly, and not use
gdk_threads_enter, or gdk_threads_leave.

Paul Fisher *

[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]