generic callback in a concurrent java/c++ sys

(Note OO is a development-time concept. Once compiled, an OO program looks no different from a C program with tons of structs and function pointers.)

Most financial systems today are concurrent OO systems. The lowly callback pattern is extremely common but actually requires a thorough understanding of both thread and object interaction. Let’s examine some. Note just about every player in the callback scenarios is a pointer, including function pointers.

In the simplest 1-thread case, an observer pointee registers interest with a subject object, by giving its own address. After subject state changes in Thr 1, the same thread loops through the list of observers and invokes the callback method on each observer. Note the observer object must not be deallocated.

For onMsg() in tibrv or onMessage() in JMS, there’s a remote “upstream” thread in the message broker’s PID/runtime, and there’s a separate downstream thread in the listener’s PID. I believe this thread starts out blocking for messages. Upon receiving a msg, the runtime somehow wakes up this blocking thread and this thread invokes onMsg() on the listener pointee. Meanwhile what’s the remote upstream thread doing? Simplest design is synchronous — upstream thread waits for the listener thread to finish. This means the upstream thread can only dispatch the message to remote listeners sequentially. I feel it’s much better to get the upstream thread to return right away after sending the one-way-message[3] (see Doug Lea’s book) so this thread can contact the 2nd listener in the list. Rewind to registration time — Any thread could register the listener with “Hollywood”. This thread could terminate immediately but the listener pointee must live.

[3] in classic Reo, we actually send one-way messages to the market-facing gateway. We get the status message on a separate thread via onMessage().

In C/C++, you can also register into “Hollywood” a ptr to a free func. No object, usually no state (unless the free func uses stateful static objects). Free functions are free of destruction. Hollywood could save the ptr in any collection. When there’s an event, Hollywood calls us back, in any thread.

c++function entities — 2nd class citizens?

Compared to java, I feel c++ is more flexible, “creative” and “loose” with functions; c++ weaves functions more into its rich syntax; and c++ apps rely more heavily on passable function entities. Any algorithm (any trivial body of code) can be passed around like a variable or object.

Inside the computer, a function (like objects) occupies a chunk of memory in “text” section, so it’s reasonable to pass its address to where it’s needed. C++ lets your *package* algorithms into passable function entities. These function entities are often stateless[2]. Consider functional programming. I call these 2nd-class citizen objects because they normally do not have state, variables, virtuals etc.

(Java Threads also should not but often have state.)

Java has interfaces and Method objects. C++ has func pointers and functor objects. Both designs are powerful, widespread and proven.

[2] functors are almost always pbclone in STL. Some functors are stateful — (beside generators) Dinkumware’s hash tables have hashing traits/policies as state in the hashing functors. See separate blog post.

http://www.newty.de/fpt/fpt.html#callconv shows some func ptr syntax examples —

someObject.someMethodReturningAFunctor()(arg1, arg2); // not the number of () pairs
(instance1.*pt2Member)(12, ‘a’, ‘b’);

(*this.*pt2Member)(12, 'a', 'b');     
(instance2->*pt2Member)(12, 'a', 'b');  

(*(someComplexExpressionReturningFuncPtr))(arg1, arg2)

struts self-quiz

Q: what kind of obj is returned by execute()? How is it related to the httpRequest’s forward() call?
Q: how does execute() get the form input data? P741
Q: how many servlets are there in a typical web app? how many are there in a struts app? P743
Q: a session is for one web app only?
Q: how 2 share a session obj btw 2 web apps?

SureStop and Thread.stop()

SureStop.java from Paul Hyde seems to be fairly professional[1] and robust. But I wonder how it avoids the deadly pitfalls of Thread.stop()? Answer is in the post on unlock@thread death

[1] Paul even included a test.

Sometimes we do want to forcibly kill a runaway thread. Look at Pushmail. Also,

* a thread stuck in an infinite loop
* a thread blocked in IO
* a thread trying to acquire a lock
* deadlock

Many useful implementation techniques
– SureStop.java is Based on the simple self-running object. In fact SureStop is an extremely simple implementation of a simple idea, given the power it has. Simplicity is power.
– uses highest thread priority to avoid starvation.
– daemon thread

option-related jargon encountered recently

You asked what option-related jargon I learned recently. Here are some
(“vol” and “volatility” intermixed)

greeks of common spread portfolios like straddle, strangle, condor, butterfly… ?

Put-call parity
synthetic put, synthetic call, synthetic long/short underlier. WHY would traders prefer the synthetic forms to the simpler forms?

variance swap? Why is it so popular?
barrier options?

how does dividend announcement affect options
how do people forecast dividends many years into the future?
How is libor curve affect option pricing
forward curve bootstrapping
forward price and how is it estimated
ex-dividend day and how it affects option exercise
repo curve used in option pricing?
dividend blending in forward curve bootstrap?

local volatility?
Historical vol
Realized vol
implied vol
volatility cone

volatility skew — why
volatility smile — why
How is fat tail related to skew/smile
higher moments of the price distribution function

VIX – most important index on options
typical values of VIX values
typical values of stock volatility values

arctan surface – for what underliers
taylor surface – for what underliers

price relative
What is the expected daily price relative for a stock with expected volatility of 20%

vol of ETF, indices and single stocks
vol of currency pairs
vol of interest rates

what applications do option traders need
what market data do option traders need
How up-to-date and live do option traders need their market data to be
How are vol surfaces calibrated, finalized and published
What types of users rely on vol surfaces and why

Greeks represent exposures. Which exposures do traders want to avoid by hedging, and which ones they don’t want to avoid
European/American/Bermuda options
among ATM / ITM / OTM what type of options are most popular and why
log-normal distribution vs normal distribution
How do traders price OTC options from listed option prices
What risks do long-term option contracts create and how to reduce those risks
how many underliers have listed options
wings of the volatility surface
Basic techniques to remove outliers during volatility fitting
cost of vol fitting
basic steps to fit an entire surface from a bunch of option quotes on different expirations and different strikes
precisely when would an option holder want to exercise an option?
assignment process upon option exercise
typical values of delta for ATM options, OTM and ITM options
when would delta exceed 1
option decay
Basically, how is historical vol measured
Can you compare implied vol against historical vol

Mockito partial mock (googlemock?

partial mock using thenCallRealMethod()

First understand the norm — full mock. Usually your MethodUnderTest is on Object A, which depends on Object B. Object B is hard to
instantiate => mock up B. ALLLLL the methods in B will mock using thenReturn().

Now what if MUT in A depends on A.m2() which depends on external/infrastructure, => hard to run during test => mockup m2() using
thenReturn(). => A must be a mock object. To test MUT, you have to register MUT with thenCallRealMethod.

Not sure how googlemock (c++) does…