Change the license to GPL v3 to be compatible with Apache License 2.0.

This commit is contained in:
huanghongxun
2015-12-05 21:41:50 +08:00
parent f0212ea4eb
commit d3ad805ad3
331 changed files with 12761 additions and 1878 deletions

View File

@@ -0,0 +1,53 @@
/**
* Copyright 2013 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package rx.concurrency;
import java.util.concurrent.TimeUnit;
import rx.Scheduler;
import rx.Subscription;
import rx.subscriptions.Subscriptions;
import rx.util.functions.Action0;
import rx.util.functions.Func0;
/* package */abstract class AbstractScheduler implements Scheduler {
@Override
public Subscription schedule(Action0 action) {
return schedule(asFunc0(action));
}
@Override
public Subscription schedule(Action0 action, long dueTime, TimeUnit unit) {
return schedule(asFunc0(action), dueTime, unit);
}
@Override
public long now() {
return System.nanoTime();
}
private static Func0<Subscription> asFunc0(final Action0 action) {
return new Func0<Subscription>() {
@Override
public Subscription call() {
action.call();
return Subscriptions.empty();
}
};
}
}

View File

@@ -0,0 +1,71 @@
/**
* Copyright 2013 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package rx.concurrency;
import java.util.LinkedList;
import java.util.Queue;
import java.util.concurrent.TimeUnit;
import rx.Subscription;
import rx.util.functions.Func0;
/**
* Schedules work on the current thread but does not execute immediately. Work is put in a queue and executed after the current unit of work is completed.
*/
public class CurrentThreadScheduler extends AbstractScheduler {
private static final CurrentThreadScheduler INSTANCE = new CurrentThreadScheduler();
public static CurrentThreadScheduler getInstance() {
return INSTANCE;
}
private static final ThreadLocal<Queue<DiscardableAction>> QUEUE = new ThreadLocal<>();
private CurrentThreadScheduler() {
}
@Override
public Subscription schedule(Func0<Subscription> action) {
DiscardableAction discardableAction = new DiscardableAction(action);
enqueue(discardableAction);
return discardableAction;
}
@Override
public Subscription schedule(Func0<Subscription> action, long dueTime, TimeUnit unit) {
return schedule(new SleepingAction(action, this, dueTime, unit));
}
private void enqueue(DiscardableAction action) {
Queue<DiscardableAction> queue = QUEUE.get();
boolean exec = queue == null;
if (exec) {
queue = new LinkedList<>();
QUEUE.set(queue);
}
queue.add(action);
if (exec) {
while (!queue.isEmpty()) {
queue.poll().call();
}
QUEUE.set(null);
}
}
}

View File

@@ -0,0 +1,52 @@
/**
* Copyright 2013 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package rx.concurrency;
import java.util.concurrent.atomic.AtomicBoolean;
import rx.Subscription;
import rx.util.AtomicObservableSubscription;
import rx.util.functions.Func0;
/**
* Combines standard {@link Subscription#unsubscribe()} functionality with ability to skip execution if an unsubscribe occurs before the {@link #call()} method is invoked.
*/
/* package */class DiscardableAction implements Func0<Subscription>, Subscription {
private final Func0<Subscription> underlying;
private final AtomicObservableSubscription wrapper = new AtomicObservableSubscription();
private final AtomicBoolean ready = new AtomicBoolean(true);
public DiscardableAction(Func0<Subscription> underlying) {
this.underlying = underlying;
}
@Override
public Subscription call() {
if (ready.compareAndSet(true, false)) {
Subscription subscription = underlying.call();
wrapper.wrap(subscription);
return subscription;
}
return wrapper;
}
@Override
public void unsubscribe() {
ready.set(false);
wrapper.unsubscribe();
}
}

View File

@@ -0,0 +1,126 @@
/**
* Copyright 2013 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package rx.concurrency;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import rx.Scheduler;
import rx.Subscription;
import rx.util.functions.Func0;
/**
* A {@link Scheduler} implementation that uses an {@link Executor} or {@link ScheduledExecutorService} implementation.
* <p>
* Note that if an {@link Executor} implementation is used instead of {@link ScheduledExecutorService} then a system-wide Timer will be used to handle delayed events.
*/
public class ExecutorScheduler extends AbstractScheduler {
private final Executor executor;
/**
* Setup a ScheduledExecutorService that we can use if someone provides an Executor instead of ScheduledExecutorService.
*/
private final static ScheduledExecutorService SYSTEM_SCHEDULED_EXECUTOR;
static {
int count = Runtime.getRuntime().availableProcessors();
if (count > 8) {
count = count / 2;
}
// we don't need more than 8 to handle just scheduling and doing no work
if (count > 8) {
count = 8;
}
SYSTEM_SCHEDULED_EXECUTOR = Executors.newScheduledThreadPool(count, new ThreadFactory() {
final AtomicInteger counter = new AtomicInteger();
@Override
public Thread newThread(Runnable r) {
Thread t = new Thread(r, "RxScheduledExecutorPool-" + counter.incrementAndGet());
t.setDaemon(true);
return t;
}
});
}
public ExecutorScheduler(Executor executor) {
this.executor = executor;
}
public ExecutorScheduler(ScheduledExecutorService executor) {
this.executor = executor;
}
@Override
public Subscription schedule(Func0<Subscription> action, long dueTime, TimeUnit unit) {
final DiscardableAction discardableAction = new DiscardableAction(action);
if (executor instanceof ScheduledExecutorService) {
((ScheduledExecutorService) executor).schedule(new Runnable() {
@Override
public void run() {
discardableAction.call();
}
}, dueTime, unit);
} else {
if (dueTime == 0) {
// no delay so put on the thread-pool right now
return (schedule(action));
} else {
// there is a delay and this isn't a ScheduledExecutorService so we'll use a system-wide ScheduledExecutorService
// to handle the scheduling and once it's ready then execute on this Executor
SYSTEM_SCHEDULED_EXECUTOR.schedule(new Runnable() {
@Override
public void run() {
// now execute on the real Executor
executor.execute(new Runnable() {
@Override
public void run() {
discardableAction.call();
}
});
}
}, dueTime, unit);
}
}
return discardableAction;
}
@Override
public Subscription schedule(Func0<Subscription> action) {
final DiscardableAction discardableAction = new DiscardableAction(action);
executor.execute(new Runnable() {
@Override
public void run() {
discardableAction.call();
}
});
return discardableAction;
}
}

View File

@@ -0,0 +1,46 @@
/**
* Copyright 2013 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package rx.concurrency;
import java.util.concurrent.TimeUnit;
import rx.Subscription;
import rx.util.functions.Func0;
/**
* Executes work immediately on the current thread.
*/
public final class ImmediateScheduler extends AbstractScheduler {
private static final ImmediateScheduler INSTANCE = new ImmediateScheduler();
public static ImmediateScheduler getInstance() {
return INSTANCE;
}
private ImmediateScheduler() {
}
@Override
public Subscription schedule(Func0<Subscription> action) {
return action.call();
}
@Override
public Subscription schedule(Func0<Subscription> action, long dueTime, TimeUnit unit) {
return schedule(new SleepingAction(action, this, dueTime, unit));
}
}

View File

@@ -0,0 +1,49 @@
/**
* Copyright 2013 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package rx.concurrency;
import java.util.concurrent.TimeUnit;
import rx.Subscription;
import rx.util.functions.Func0;
/**
* Schedules work on a new thread.
*/
public class NewThreadScheduler extends AbstractScheduler {
private static final NewThreadScheduler INSTANCE = new NewThreadScheduler();
public static NewThreadScheduler getInstance() {
return INSTANCE;
}
@Override
public Subscription schedule(Func0<Subscription> action) {
final DiscardableAction discardableAction = new DiscardableAction(action);
Thread t = new Thread(discardableAction::call, "RxNewThreadScheduler");
t.start();
return discardableAction;
}
@Override
public Subscription schedule(Func0<Subscription> action, long dueTime, TimeUnit unit) {
return schedule(new SleepingAction(action, this, dueTime, unit));
}
}

View File

@@ -0,0 +1,143 @@
/**
* Copyright 2013 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package rx.concurrency;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
import rx.Scheduler;
/**
* Static factory methods for creating Schedulers.
*/
public class Schedulers {
private static final ScheduledExecutorService COMPUTATION_EXECUTOR = createComputationExecutor();
private static final Executor IO_EXECUTOR = createIOExecutor();
private Schedulers() {
}
/**
* {@link Scheduler} that executes work immediately on the current thread.
*
* @return {@link ImmediateScheduler} instance
*/
public static Scheduler immediate() {
return ImmediateScheduler.getInstance();
}
/**
* {@link Scheduler} that queues work on the current thread to be executed after the current work completes.
*
* @return {@link CurrentThreadScheduler} instance
*/
public static Scheduler currentThread() {
return CurrentThreadScheduler.getInstance();
}
/**
* {@link Scheduler} that creates a new {@link Thread} for each unit of work.
*
* @return {@link NewThreadScheduler} instance
*/
public static Scheduler newThread() {
return NewThreadScheduler.getInstance();
}
/**
* {@link Scheduler} that queues work on an {@link Executor}.
* <p>
* Note that this does not support scheduled actions with a delay.
*
* @return {@link ExecutorScheduler} instance
*/
public static Scheduler executor(Executor executor) {
return new ExecutorScheduler(executor);
}
/**
* {@link Scheduler} that queues work on an {@link ScheduledExecutorService}.
*
* @return {@link ExecutorScheduler} instance
*/
public static Scheduler executor(ScheduledExecutorService executor) {
return new ExecutorScheduler(executor);
}
/**
* {@link Scheduler} intended for computational work.
* <p>
* The implementation is backed by a {@link ScheduledExecutorService} thread-pool sized to the number of CPU cores.
* <p>
* This can be used for event-loops, processing callbacks and other computational work.
* <p>
* Do not perform IO-bound work on this scheduler. Use {@link #threadPoolForComputation()} instead.
*
* @return {@link ExecutorScheduler} for computation-bound work.
*/
public static Scheduler threadPoolForComputation() {
return executor(COMPUTATION_EXECUTOR);
}
/**
* {@link Scheduler} intended for IO-bound work.
* <p>
* The implementation is backed by an {@link Executor} thread-pool that will grow as needed.
* <p>
* This can be used for asynchronously performing blocking IO.
* <p>
* Do not perform computational work on this scheduler. Use {@link #threadPoolForComputation()} instead.
*
* @return {@link ExecutorScheduler} for IO-bound work.
*/
public static Scheduler threadPoolForIO() {
return executor(IO_EXECUTOR);
}
private static ScheduledExecutorService createComputationExecutor() {
int cores = Runtime.getRuntime().availableProcessors();
return Executors.newScheduledThreadPool(cores, new ThreadFactory() {
final AtomicInteger counter = new AtomicInteger();
@Override
public Thread newThread(Runnable r) {
Thread t = new Thread(r, "RxComputationThreadPool-" + counter.incrementAndGet());
t.setDaemon(true);
return t;
}
});
}
private static Executor createIOExecutor() {
Executor result = Executors.newCachedThreadPool(new ThreadFactory() {
final AtomicLong counter = new AtomicLong();
@Override
public Thread newThread(Runnable r) {
Thread t = new Thread(r, "RxIOThreadPool-" + counter.incrementAndGet());
t.setDaemon(true);
return t;
}
});
return result;
}
}

View File

@@ -0,0 +1,49 @@
/**
* Copyright 2013 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package rx.concurrency;
import java.util.concurrent.TimeUnit;
import rx.Scheduler;
import rx.Subscription;
import rx.util.functions.Func0;
/* package */class SleepingAction implements Func0<Subscription> {
private final Func0<Subscription> underlying;
private final Scheduler scheduler;
private final long execTime;
public SleepingAction(Func0<Subscription> underlying, Scheduler scheduler, long timespan, TimeUnit timeUnit) {
this.underlying = underlying;
this.scheduler = scheduler;
this.execTime = scheduler.now() + timeUnit.toMillis(timespan);
}
@Override
public Subscription call() {
if (execTime < scheduler.now()) {
try {
Thread.sleep(scheduler.now() - execTime);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new RuntimeException(e);
}
}
return underlying.call();
}
}

View File

@@ -0,0 +1,19 @@
/**
* Copyright 2013 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Rx Schedulers
*/
package rx.concurrency;