Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into exception1
Browse files Browse the repository at this point in the history
  • Loading branch information
bernardnormier committed Jun 26, 2024
2 parents 2c9fc1f + 1bb1167 commit 7639f16
Show file tree
Hide file tree
Showing 30 changed files with 1,680 additions and 1,545 deletions.
2 changes: 1 addition & 1 deletion cpp/src/Ice/ConnectionI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1461,7 +1461,7 @@ Ice::ConnectionI::message(ThreadPoolCurrent& current)
{ self->upcall(std::move(connectionStartCompleted), std::move(sentCBs), std::move(messageUpcall), *stream); },
self);
#else
if (!_hasExecutor) // Optimization, call dispatch() directly if there's no executor.
if (!_hasExecutor) // Optimization, call upcall() directly if there's no executor.
{
upcall(std::move(connectionStartCompleted), std::move(sentCBs), std::move(messageUpcall), messageStream);
}
Expand Down
34 changes: 31 additions & 3 deletions cpp/src/slice2js/Gen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ namespace
replace(module.begin(), module.end(), '\\', '_');
replace(module.begin(), module.end(), '.', '_');
}

return module;
}

Expand Down Expand Up @@ -2040,7 +2041,7 @@ Slice::Gen::TypeScriptImportVisitor::addImport(const ContainedPtr& definition)
}
else
{
string f = removeExtension(filename);
string f = filename;
if (IceInternal::isAbsolutePath(f))
{
// If the include file is an absolute path, we need to generate a relative path.
Expand All @@ -2051,7 +2052,8 @@ Slice::Gen::TypeScriptImportVisitor::addImport(const ContainedPtr& definition)
// Make the import relative to the current file.
f = "./" + f;
}
_importedTypes[definition->scoped()] = pathToModule(f);
_importedTypes[definition->scoped()] = "__module_" + pathToModule(f) + ".";
_importedModules.insert(removeExtension(f) + ".js");
}
}
}
Expand Down Expand Up @@ -2507,6 +2509,27 @@ Slice::Gen::TypeScriptVisitor::visitClassDefStart(const ClassDefPtr& p)
return false;
}

namespace
{
bool areRemainingParamsOptional(const ParamDeclList& params, string name)
{
auto it = params.begin();
do
{
it++;
} while (it != params.end() && (*it)->name() != name);

for (; it != params.end(); ++it)
{
if (!(*it)->optional())
{
return false;
}
}
return true;
}
}

bool
Slice::Gen::TypeScriptVisitor::visitInterfaceDefStart(const InterfaceDefPtr& p)
{
Expand Down Expand Up @@ -2570,7 +2593,12 @@ Slice::Gen::TypeScriptVisitor::visitInterfaceDefStart(const InterfaceDefPtr& p)
_out << nl << fixId(op->name()) << spar;
for (const auto& param : inParams)
{
_out << (fixId(param->name()) + ":" + typeToTsString(param->type(), true, true, param->optional()));
// TypeScript doesn't allow optional parameters with '?' prefix before required parameters.
const string optionalPrefix =
param->optional() && areRemainingParamsOptional(paramList, param->name()) ? "?" : "";
_out
<< (fixId(param->name()) + optionalPrefix + ":" +
typeToTsString(param->type(), true, true, param->optional()));
}
_out << "context?:Map<string, string>";
_out << epar;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -396,17 +396,17 @@ private void connectImpl(final ConnectStrategy factory) {
}

private void dispatchCallback(Runnable runnable, com.zeroc.Ice.Connection conn) {
if (_initData.dispatcher != null) {
_initData.dispatcher.accept(runnable, conn);
if (_initData.executor != null) {
_initData.executor.accept(runnable, conn);
} else {
runnable.run();
}
}

private void dispatchCallbackAndWait(final Runnable runnable) {
if (_initData.dispatcher != null) {
if (_initData.executor != null) {
final java.util.concurrent.Semaphore sem = new java.util.concurrent.Semaphore(0);
_initData.dispatcher.accept(
_initData.executor.accept(
() -> {
runnable.run();
sem.release();
Expand Down
30 changes: 15 additions & 15 deletions java/src/Ice/src/main/java/com/zeroc/Ice/ConnectionI.java
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ public synchronized void setCloseCallback(final CloseCallback callback) {
if (_state >= StateClosed) {
if (callback != null) {
_threadPool.dispatch(
new com.zeroc.IceInternal.DispatchWorkItem(this) {
new com.zeroc.IceInternal.RunnableThreadPoolWorkItem(this) {
@Override
public void run() {
try {
Expand Down Expand Up @@ -810,9 +810,9 @@ public void message(com.zeroc.IceInternal.ThreadPoolCurrent current) {
}
}

if (!_dispatcher) // Optimization, call dispatch() directly if there's no dispatcher.
if (!_executor) // Optimization, call upcall() directly if there's no executor.
{
dispatch(startCB, sentCBs, info);
upcall(startCB, sentCBs, info);
} else {
// No need for the stream if heartbeat callback
if (info != null && info.heartbeatCallback == null) {
Expand All @@ -829,17 +829,17 @@ public void message(com.zeroc.IceInternal.ThreadPoolCurrent current) {
final StartCallback finalStartCB = startCB;
final java.util.List<OutgoingMessage> finalSentCBs = sentCBs;
final MessageInfo finalInfo = info;
_threadPool.dispatchFromThisThread(
new com.zeroc.IceInternal.DispatchWorkItem(this) {
_threadPool.executeFromThisThread(
new com.zeroc.IceInternal.RunnableThreadPoolWorkItem(this) {
@Override
public void run() {
dispatch(finalStartCB, finalSentCBs, finalInfo);
upcall(finalStartCB, finalSentCBs, finalInfo);
}
});
}
}

protected void dispatch(
protected void upcall(
StartCallback startCB, java.util.List<OutgoingMessage> sentCBs, MessageInfo info) {
int dispatchedCount = 0;

Expand Down Expand Up @@ -986,13 +986,13 @@ public Void call() throws Exception {
}

current.ioCompleted();
if (!_dispatcher) // Optimization, call finish() directly if there's no
// dispatcher.
if (!_executor) // Optimization, call finish() directly if there's no
// executor.
{
finish(close);
} else {
_threadPool.dispatchFromThisThread(
new com.zeroc.IceInternal.DispatchWorkItem(this) {
_threadPool.executeFromThisThread(
new com.zeroc.IceInternal.RunnableThreadPoolWorkItem(this) {
@Override
public void run() {
finish(close);
Expand Down Expand Up @@ -1274,7 +1274,7 @@ public ConnectionI(
_adapter = adapter;
final InitializationData initData = instance.initializationData();
// Cached for better performance.
_dispatcher = initData.dispatcher != null;
_executor = initData.executor != null;
_logger = initData.logger; // Cached for better performance.
_traceLevels = instance.traceLevels(); // Cached for better performance.
_connectTimeout = options.connectTimeout();
Expand Down Expand Up @@ -2249,7 +2249,7 @@ private void sendResponse(OutgoingResponse response, boolean isTwoWay, byte comp
boolean shutdown = false;

// We may be executing on the "main thread" (e.g., in Android together with a
// custom dispatcher) and therefore we have to defer network calls to a separate thread.
// custom executor) and therefore we have to defer network calls to a separate thread.
final boolean queueResponse = isTwoWay && _instance.queueRequests();

synchronized (this) {
Expand Down Expand Up @@ -2309,7 +2309,7 @@ private boolean sendResponseImpl(OutputStream outputStream, boolean isTwoWay, by
if (_state == StateClosing && _upcallCount == 0) {
//
// We may be executing on the "main thread" (e.g., in Android together with a custom
// dispatcher) and therefore we have to defer network calls to a separate thread.
// executor) and therefore we have to defer network calls to a separate thread.
//
if (!isTwoWay && _instance.queueRequests()) {
shutdown = true;
Expand Down Expand Up @@ -2573,7 +2573,7 @@ public void completed(LocalException ex) {
private ObjectAdapter _adapter;
private com.zeroc.IceInternal.ServantManager _servantManager;

private final boolean _dispatcher;
private final boolean _executor;
private final Logger _logger;
private final com.zeroc.IceInternal.TraceLevels _traceLevels;
private final com.zeroc.IceInternal.ThreadPool _threadPool;
Expand Down
18 changes: 8 additions & 10 deletions java/src/Ice/src/main/java/com/zeroc/Ice/InitializationData.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,16 @@ public InitializationData clone() {
public ClassLoader classLoader;

/**
* You can control which thread receives operation invocations and AMI callbacks by supplying a
* dispatcher.
* You can control which thread receives operation dispatches and async invocation callbacks by
* supplying an executor. For example, you can use this execution facility to ensure that all
* dispatches and invocation callbacks are executed in a GUI event loop thread so that it is safe
* to invoke directly on GUI objects.
*
* <p>For example, you can use this dispatching facility to ensure that all invocations and
* callbacks are dispatched in a GUI event loop thread so that it is safe to invoke directly on
* GUI objects.
*
* <p>The dispatcher is responsible for running (dispatching) the invocation or AMI callback on
* its favorite thread. It must execute the the provided <code>Runnable</code> parameter. The con
* parameter represents the connection associated with this dispatch.
* <p>The executor is responsible for running the dispatch or async invocation callback on its
* favorite thread. It must execute the the provided <code>Runnable</code> parameter. The con
* parameter represents the connection associated with this call.
*/
public java.util.function.BiConsumer<Runnable, Connection> dispatcher;
public java.util.function.BiConsumer<Runnable, Connection> executor;

/**
* Applications that make use of compact type IDs to conserve space when marshaling class
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import java.util.concurrent.CompletionStage;

public class CollocatedRequestHandler implements RequestHandler {
private class InvokeAllAsync extends DispatchWorkItem {
private class InvokeAllAsync extends RunnableThreadPoolWorkItem {
private InvokeAllAsync(
OutgoingAsyncBase outAsync,
com.zeroc.Ice.OutputStream os,
Expand All @@ -41,7 +41,7 @@ public void run() {

public CollocatedRequestHandler(Reference ref, com.zeroc.Ice.ObjectAdapter adapter) {
_reference = ref;
_dispatcher = ref.getInstance().initializationData().dispatcher != null;
_executor = ref.getInstance().initializationData().executor != null;
_adapter = adapter;
_response = _reference.getMode() == Reference.ModeTwoway;

Expand Down Expand Up @@ -132,12 +132,12 @@ int invokeAsyncRequest(OutgoingAsyncBase outAsync, int batchRequestNum, boolean
_adapter
.getThreadPool()
.dispatch(new InvokeAllAsync(outAsync, outAsync.getOs(), requestId, batchRequestNum));
} else if (_dispatcher) {
} else if (_executor) {
_adapter
.getThreadPool()
.dispatchFromThisThread(
.executeFromThisThread(
new InvokeAllAsync(outAsync, outAsync.getOs(), requestId, batchRequestNum));
} else // Optimization: directly call dispatchAll if there's no dispatcher.
} else // Optimization: directly call dispatchAll if there's no executor.
{
if (sentAsync(outAsync)) {
dispatchAll(outAsync.getOs(), requestId, batchRequestNum);
Expand Down Expand Up @@ -331,7 +331,7 @@ private void fillInValue(com.zeroc.Ice.OutputStream os, int pos, int value) {
}

private final Reference _reference;
private final boolean _dispatcher;
private final boolean _executor;
private final boolean _response;
private final com.zeroc.Ice.ObjectAdapter _adapter;
private final com.zeroc.Ice.Logger _logger;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ public final void invokeCompletedAsync() {
_instance
.clientThreadPool()
.dispatch(
new DispatchWorkItem(_cachedConnection) {
new RunnableThreadPoolWorkItem(_cachedConnection) {
@Override
public void run() {
invokeCompleted();
Expand Down Expand Up @@ -363,7 +363,7 @@ protected void dispatch(final Runnable runnable) {
_instance
.clientThreadPool()
.dispatch(
new DispatchWorkItem(_cachedConnection) {
new RunnableThreadPoolWorkItem(_cachedConnection) {
@Override
public void run() {
runnable.run();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,7 @@ public void create(
final ConnectCallback cb = new ConnectCallback(this, endpoints, hasMore, callback, selType);
//
// Calling cb.getConnectors() can eventually result in a call to connect() on a socket, which is
// not
// allowed while in Android's main thread (with a dispatcher installed).
// not allowed while in Android's main thread (with an executor installed).
//
if (_instance.queueRequests()) {
_instance
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,21 @@

//
// A helper class for thread pool work items that only need to call user
// callbacks. If a dispatcher is installed with the communicator, the
// thread pool work item is executed with the dispatcher, otherwise it's
// callbacks. If an executor is installed with the communicator, the
// thread pool work item is executed with the executor, otherwise it's
// executed by a thread pool thread (after promoting a follower thread).
//
public abstract class DispatchWorkItem implements ThreadPoolWorkItem, Runnable {
public DispatchWorkItem() {}
public abstract class RunnableThreadPoolWorkItem implements ThreadPoolWorkItem, Runnable {
public RunnableThreadPoolWorkItem() {}

public DispatchWorkItem(com.zeroc.Ice.Connection connection) {
public RunnableThreadPoolWorkItem(com.zeroc.Ice.Connection connection) {
_connection = connection;
}

@Override
public final void execute(ThreadPoolCurrent current) {
current.ioCompleted(); // Promote a follower
current.dispatchFromThisThread(this);
current.executeFromThisThread(this);
}

public com.zeroc.Ice.Connection getConnection() {
Expand Down
14 changes: 7 additions & 7 deletions java/src/Ice/src/main/java/com/zeroc/IceInternal/ThreadPool.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public ThreadPool(Instance instance, String prefix, int timeout) {
com.zeroc.Ice.Properties properties = instance.initializationData().properties;

_instance = instance;
_dispatcher = instance.initializationData().dispatcher;
_executor = instance.initializationData().executor;
_destroyed = false;
_prefix = prefix;
_selector = new Selector(instance);
Expand Down Expand Up @@ -266,10 +266,10 @@ public synchronized boolean finish(EventHandler handler, boolean closeNow) {
return closeNow;
}

public void dispatchFromThisThread(DispatchWorkItem workItem) {
if (_dispatcher != null) {
public void executeFromThisThread(RunnableThreadPoolWorkItem workItem) {
if (_executor != null) {
try {
_dispatcher.accept(workItem, workItem.getConnection());
_executor.accept(workItem, workItem.getConnection());
} catch (java.lang.Exception ex) {
if (_instance.initializationData().properties.getIcePropertyAsInt("Ice.Warn.Dispatch")
> 1) {
Expand All @@ -285,7 +285,7 @@ public void dispatchFromThisThread(DispatchWorkItem workItem) {
}
}

public synchronized void dispatch(DispatchWorkItem workItem) {
public synchronized void dispatch(RunnableThreadPoolWorkItem workItem) {
if (_destroyed) {
throw new com.zeroc.Ice.CommunicatorDestroyedException();
}
Expand Down Expand Up @@ -315,7 +315,7 @@ public void joinWithAllThreads() throws InterruptedException {
@Override
public void execute(Runnable command) {
dispatch(
new com.zeroc.IceInternal.DispatchWorkItem() {
new com.zeroc.IceInternal.RunnableThreadPoolWorkItem() {
@Override
public void run() {
command.run();
Expand Down Expand Up @@ -567,7 +567,7 @@ private synchronized boolean followerWait(ThreadPoolCurrent current) {
}

private final Instance _instance;
private final java.util.function.BiConsumer<Runnable, com.zeroc.Ice.Connection> _dispatcher;
private final java.util.function.BiConsumer<Runnable, com.zeroc.Ice.Connection> _executor;
private final ThreadPoolWorkQueue _workQueue;
private boolean _destroyed;
private final String _prefix;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ public void ioCompleted() {
_threadPool.ioCompleted(this);
}

public void dispatchFromThisThread(DispatchWorkItem workItem) {
_threadPool.dispatchFromThisThread(workItem);
public void executeFromThisThread(RunnableThreadPoolWorkItem workItem) {
_threadPool.executeFromThisThread(workItem);
}

final ThreadPool _threadPool;
Expand Down
2 changes: 1 addition & 1 deletion java/test/slice.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ task testSliceTask {
'Ice/custom/*.ice',
'Ice/defaultServant/*.ice',
'Ice/defaultValue/*.ice',
'Ice/dispatcher/*.ice',
'Ice/executor/*.ice',
'Ice/echo/*.ice',
'Ice/exceptions/*.ice',
'Ice/facets/*.ice',
Expand Down
Loading

0 comments on commit 7639f16

Please sign in to comment.