APIThreading model

Threading model

The Java implementation takes advantage of multi-threading. Operations (e.g. store access) are non-blocking, and call a done handler when finished.

A typical piece of code looks as follows:

			Store.ListDone doneHandler = new Store.ListDone() {
				void onListDone(ArrayList<Hash> hashes) {
					...
				}

				void onListFailed(String error) {
					...
				}
			};

			store.list(accountHash, boxLabel, doneHandler);
		

The code lists a box of a store (e.g. over HTTP). The list operation is executed on a separate thread, and calls either onListDone or onListFailed on the main thread upon completion:

main thread store.list(..., done) storeaccess store thread done.onListDone(hashes)

The code after store.list(...) is always executed before any done handler. If the operation completes immediately, it posts the result to the main thread's task queue rather than calling the done handler immediately:

main thread store.list(..., done) done.onListDone(hashes)

Computation

For computationally expensive work, Condensation creates a separate thread upon initialization. This thread is used for RSA key generation, hash calculation as well as object en- and decryption.

You may run your own code on the computation thread as follows:

			public class SomeOperation implements BackgroundTask {
				public final Done done;
				private Result result_ = null;

				public SomeOperation(Done done) {
					this.done = done;
					Condensation.computationExecutor.run(this);
				}

				@Override
				public void background() {
					// This is running on the computation thread.
					result_ = ...
				}

				@Override
				public void after() {
					// This is running on the main thread after "background" completed.
					done.onOperationDone(result_);
				}

				public interface Done {
					void onOperationDone(Result result);
				}
			}
		

The JavaScript implementation takes advantage of multi-threading. Operations (e.g. store access) are non-blocking, and call a done handler when finished.

A typical piece of code looks as follows:

			var listOperation = store.list(accountHash, boxLabel);

			listOperation.onDone_ = function(hashes) {
				...
			};

			listOperation.onFailed_ = function(error) {
				...
			};
		

The code lists a box of a store over HTTP. The actual HTTP request is processed by the browser's request queue. Once the operation completes, the listOperation.onDone or listOperation.onFailed handler are called:

main thread var operation = store.list(...) storeaccess HTTP request queue operation.onDone(hashes)

Computation

Computationally expensive work, such as RSA key generation, hash calculation or object en- and decryption, is split into small pieces and executed when the main thread is idle.

The Perl implementation is single-threaded. All operations (e.g. store access) are blocking, i.e. they wait for completion and return the result.

Hence, operations are simple function calls:

my ($hashes, $error) = $store->list($accountHash, $boxLabel);