Tim Ellison wrote:
> ... there is a dependency on
> threadlib from the portlib, and the portlib functions don't get a
> reference that can reach 'back' to the VMI struct.
>
> So while it may be apparent in the launcher, it would be equally
> apparent in the portlib functions themselves, since they would have no
> reachable threadlib.
>
> So maybe, as you say, we need to make the threadlib functions an
> extension of the current portlib function table rather than the VMI
> struct, and have them provided by a DLL loaded by the VM (and the thread
> function table populated in the portlib initialization code).
>
> Any other options?
The list of dependencies of port library on thread library is as follows
(got from `dumpbin /imports hyprt.dll`)
48 hythread_tls_get
40 hythread_self
49 hythread_tls_set
47 hythread_tls_free
23 hythread_monitor_exit
2B hythread_monitor_notify_all
14 hythread_global
32 hythread_monitor_wait
4 hythread_attach
9 hythread_detach
20 hythread_monitor_destroy
28 hythread_monitor_init_with_name
21 hythread_monitor_enter
45 hythread_tls_alloc
Analysis:
* hythread_tls_get, hythread_tls_set, hythread_tls_free, hythread_tls_alloc,
hythread_self are used exclusively for TLS access, which can be provided by
thin OS-function wrappers independently of the threading library.
Attached patch is a dirty way to eliminate dependency,
intended as an illustration of how this can be done.
* hythread_global is used exclusively as hythread_global("global_monitor"),
with a purpose of having one big lock. I'm not exactly sure if it is relevant
that LUNI and ARCHIVE want to use the same global monitor. I suspect that this
is not really needed, and PORTLIB, ARCHIVE and LUNI can use different monitors.
* hythread_monitor_ functions provide native synchronization primitives, which
are not connected to java objects in any way, and are not exposed to the VM,
so these also can be implemented directly in portlib as thin OS-function
wrappers.
* hythread_attach and hythread_detach -- these imply attaching thread to the
VM, and thus cannon be implemented without dependency on threading library.
Resume:
it seems that the issue of making thread library completely VM-specific boils
down to finding a good way of attaching/detaching a thread to/from VM.
Currently, I'm not sure how can this be done, since the first time launcher
initializes the porting library, and port library calls hythread_attach(), is
even before launcher knows which VM it will use. Chicken-and-egg problem again.
Thoughts?
>From 54f95c0ee50a0ea317ace6f8d4e3cd37d258166d Mon Sep 17 00:00:00 2001
From: Salikh Zakirov <[EMAIL PROTECTED]>
Date: Tue, 30 Jan 2007 13:06:08 +0300
Subject: [PATCH] replaced hythread tls functions with system-dependent macros
---
depends/build/defines.mak | 2 +-
.../src/main/native/archive/shared/jarfile.c | 2 +
.../src/main/native/include/shared/hythread.h | 32 ++++--
.../src/main/native/thread/shared/hythread.c | 102 --------------------
.../main/native/thread/shared/hythreadinspect.c | 17 ----
.../src/main/native/thread/windows/hythr.def | 6 -
6 files changed, 24 insertions(+), 137 deletions(-)
diff --git a/depends/build/defines.mak b/depends/build/defines.mak
index ae3893c..b177cba 100644
--- a/depends/build/defines.mak
+++ b/depends/build/defines.mak
@@ -31,7 +31,7 @@ DLLPATH=$(HY_HDK)\jdk\jre\bin\# ditto
SHAREDSUB=..\shared\# ditto
HYCOMMONCFLAGS = \
- -WX -GF -Gs -MD -Zm400 \
+ -GF -Gs -MD -Zm400 \
-D_DLL -D_MT -D_WINSOCKAPI_ \
/I$(HY_HDK)\include /I$(HY_HDK)\jdk\include /I.
diff --git a/modules/archive/src/main/native/archive/shared/jarfile.c
b/modules/archive/src/main/native/archive/shared/jarfile.c
index c4bebc5..30a28a7 100644
--- a/modules/archive/src/main/native/archive/shared/jarfile.c
+++ b/modules/archive/src/main/native/archive/shared/jarfile.c
@@ -77,7 +77,9 @@ JNIEXPORT jarray JNICALL
Java_java_util_jar_JarFile_getMetaEntriesImpl (JNIEnv * env, jobject recv,
jbyteArray zipName)
{
+#ifndef MAX_PATH
#define MAX_PATH 1024
+#endif
#define RESULT_BUF_SIZE 256
PORT_ACCESS_FROM_ENV (env);
diff --git a/modules/portlib/src/main/native/include/shared/hythread.h
b/modules/portlib/src/main/native/include/shared/hythread.h
index 76161bc..34c42f0 100644
--- a/modules/portlib/src/main/native/include/shared/hythread.h
+++ b/modules/portlib/src/main/native/include/shared/hythread.h
@@ -24,7 +24,12 @@ extern "C"
#endif
#include <stddef.h>
#include "hycomp.h"
- typedef UDATA hythread_tls_key_t;
+
+#undef BOOLEAN
+#include <windows.h>
+
+ typedef DWORD hythread_tls_key_t;
+
#define HYTHREAD_PROC VMCALL
typedef int (HYTHREAD_PROC * hythread_entrypoint_t) (void *);
typedef void (HYTHREAD_PROC * hythread_tls_finalizer_t) (void *);
@@ -101,8 +106,10 @@ extern "C"
extern HY_CFUNC void VMCALL hythread_detach PROTOTYPE ((hythread_t thread));
extern HY_CFUNC UDATA VMCALL
hythread_lib_set_flags PROTOTYPE ((UDATA flags));
- extern HY_CFUNC IDATA VMCALL
- hythread_tls_alloc PROTOTYPE ((hythread_tls_key_t * handle));
+
+#define hythread_tls_alloc(handle) \
+ ((*(handle) = TlsAlloc()) == 0xFFFFFFFF)
+
extern HY_CFUNC IDATA VMCALL
hythread_sleep_interruptable PROTOTYPE ((I_64 millis, IDATA nanos));
extern HY_CFUNC IDATA VMCALL
@@ -141,9 +148,11 @@ extern "C"
PROTOTYPE ((hythread_monitor_t monitor));
extern HY_CFUNC void VMCALL hythread_jlm_thread_clear
PROTOTYPE ((hythread_t thread));
- extern HY_CFUNC hythread_t VMCALL hythread_self PROTOTYPE ((void));
- extern HY_CFUNC IDATA VMCALL
- hythread_tls_free PROTOTYPE ((hythread_tls_key_t key));
+
+#define hythread_self() NULL
+
+#define hythread_tls_free(key) (TlsFree(key))
+
extern HY_CFUNC UDATA VMCALL
hythread_clear_priority_interrupted PROTOTYPE ((void));
extern HY_CFUNC void VMCALL
@@ -158,9 +167,9 @@ extern "C"
extern HY_CFUNC void VMCALL hythread_suspend PROTOTYPE ((void));
extern HY_CFUNC void VMCALL
hythread_interrupt PROTOTYPE ((hythread_t thread));
- extern HY_CFUNC IDATA VMCALL
- hythread_tls_set
- PROTOTYPE ((hythread_t thread, hythread_tls_key_t key, void *value));
+
+#define hythread_tls_set(thread, key, value) (TlsSetValue(key, value))
+
extern HY_CFUNC IDATA VMCALL hythread_create
PROTOTYPE ((hythread_t * handle, UDATA stacksize, UDATA priority,
UDATA suspend, hythread_entrypoint_t entrypoint,
@@ -237,8 +246,9 @@ extern "C"
PROTOTYPE ((hythread_monitor_t monitor));
extern HY_CFUNC UDATA VMCALL hythread_get_priority
PROTOTYPE ((hythread_t thread));
- extern HY_CFUNC void *VMCALL hythread_tls_get
- PROTOTYPE ((hythread_t thread, hythread_tls_key_t key));
+
+#define hythread_tls_get(thread, key) (TlsGetValue(key))
+
extern HY_CFUNC char *VMCALL hythread_monitor_get_name
PROTOTYPE ((hythread_monitor_t monitor));
extern HY_CFUNC hythread_monitor_t VMCALL hythread_monitor_walk
diff --git a/modules/portlib/src/main/native/thread/shared/hythread.c
b/modules/portlib/src/main/native/thread/shared/hythread.c
index f2f1296..00c712d 100644
--- a/modules/portlib/src/main/native/thread/shared/hythread.c
+++ b/modules/portlib/src/main/native/thread/shared/hythread.c
@@ -167,25 +167,6 @@ init_cleanup1:lib->initStatus = -1;
#undef CDEV_CURRENT_FUNCTION
-#define CDEV_CURRENT_FUNCTION hythread_self
-/**
- * Return the hythread_t for the current thread.
- *
- * @note Must be called only by an attached thread
- *
- * @return hythread_t for the current thread
- *
- * @see hythread_attach
- *
- */
-hythread_t VMCALL
-hythread_self (void)
-{
- return MACRO_SELF ();
-}
-
-#undef CDEV_CURRENT_FUNCTION
-
#define CDEV_CURRENT_FUNCTION hythread_create
/**
* Create a new OS thread.
@@ -734,89 +715,6 @@ hythread_monitor_notify_all (hythread_monitor_t monitor)
#undef CDEV_CURRENT_FUNCTION
-#define CDEV_CURRENT_FUNCTION hythread_tls_alloc
-/**
- * Allocate a thread local storage (TLS) key.
- *
- * Create and return a new, unique key for thread local storage.
- *
- * @note The hande returned will be >=0, so it is safe to test the handle
against 0 to see if it's been
- * allocated yet.
- *
- * @param[out] handle pointer to a key to be initialized with a key value
- * @return 0 on success or negative value if a key could not be allocated
(i.e. all TLS has been allocated)
- *
- * @see hythread_tls_free, hythread_tls_set
- */
-IDATA VMCALL
-hythread_tls_alloc (hythread_tls_key_t * handle)
-{
- return hythread_tls_alloc_with_finalizer (handle, tls_null_finalizer);
-}
-
-#undef CDEV_CURRENT_FUNCTION
-
-#define CDEV_CURRENT_FUNCTION hythread_tls_free
-/**
- * Release a TLS key.
- *
- * Release a TLS key previously allocated by hythread_tls_alloc.
- *
- * @param[in] key TLS key to be freed
- * @return 0 on success or negative value on failure
- *
- * @see hythread_tls_alloc, hythread_tls_set
- *
- */
-IDATA VMCALL
-hythread_tls_free (hythread_tls_key_t key)
-{
- HyPoolState state;
- hythread_t each;
- hythread_library_t lib = GLOBAL_DATA (default_library);
- ASSERT (lib);
-
- /* clear the TLS in every existing thread */
- GLOBAL_LOCK_SIMPLE (lib);
- each = pool_startDo (lib->thread_pool, &state);
- while (each)
- {
- each->tls[key - 1] = NULL;
- each = pool_nextDo (&state);
- }
- GLOBAL_UNLOCK_SIMPLE (lib);
-
- /* now return the key to the free set */
- MUTEX_ENTER (lib->tls_mutex);
- lib->tls_finalizers[key - 1] = NULL;
- MUTEX_EXIT (lib->tls_mutex);
-
- return 0;
-}
-
-#undef CDEV_CURRENT_FUNCTION
-
-#define CDEV_CURRENT_FUNCTION hythread_tls_set
-/**
- * Set a thread's TLS value.
- *
- * @param[in] thread a thread
- * @param[in] key key to have TLS value set (any value returned by
hythread_alloc)
- * @param[in] value value to be stored in TLS
- * @return 0 on success or negative value on failure
- *
- * @see hythread_tls_alloc, hythread_tls_free, hythread_tls_get
- */
-IDATA VMCALL
-hythread_tls_set (hythread_t thread, hythread_tls_key_t key, void *value)
-{
- thread->tls[key - 1] = value;
-
- return 0;
-}
-
-#undef CDEV_CURRENT_FUNCTION
-
#define CDEV_CURRENT_FUNCTION hythread_set_priority
/**
* Set a thread's execution priority.
diff --git a/modules/portlib/src/main/native/thread/shared/hythreadinspect.c
b/modules/portlib/src/main/native/thread/shared/hythreadinspect.c
index 67f899b..6b37e68 100644
--- a/modules/portlib/src/main/native/thread/shared/hythreadinspect.c
+++ b/modules/portlib/src/main/native/thread/shared/hythreadinspect.c
@@ -121,23 +121,6 @@ hythread_monitor_walk (hythread_monitor_t monitor)
#undef CDEV_CURRENT_FUNCTION
-#define CDEV_CURRENT_FUNCTION hythread_tls_get
-/**
- * Get a thread's thread local storage (TLS) value.
- *
- * @param[in] thread a thread
- * @param[in] key key to have TLS value returned (value returned by
hythread_tls_alloc)
- * @return pointer to location of TLS or NULL on failure.
- *
- */
-void *VMCALL
-hythread_tls_get (hythread_t thread, hythread_tls_key_t key)
-{
- return (void *) READU (thread->tls[key - 1]);
-}
-
-#undef CDEV_CURRENT_FUNCTION
-
#define CDEV_CURRENT_FUNCTION pool_for_monitor
/*
* Return the monitor pool holding a monitor.
diff --git a/modules/portlib/src/main/native/thread/windows/hythr.def
b/modules/portlib/src/main/native/thread/windows/hythr.def
index bf0eedd..56ba8ab 100644
--- a/modules/portlib/src/main/native/thread/windows/hythr.def
+++ b/modules/portlib/src/main/native/thread/windows/hythr.def
@@ -31,17 +31,11 @@ EXPORTS
hythread_monitor_wait_interruptable
hythread_monitor_num_waiting
hythread_resume
- hythread_self
hythread_set_priority
hythread_get_priority
hythread_sleep
hythread_sleep_interruptable
hythread_suspend
- hythread_tls_alloc
- hythread_tls_alloc_with_finalizer
- hythread_tls_free
- hythread_tls_get
- hythread_tls_set
hythread_yield
hythread_exit
hythread_detach
--
1.4.4.4.g05d6b