|
|
|
|
4.4 Construct for Interfacing with Legacy CodeThe previously shown example of concurrently adding two arrays used an “all_done” function-call to report that the entire array has been added. In a project written entirely in spirit of stress-flow, the “all_done” function could simply be a call to another stress atom that would now use the result for something, call more atoms, which could eventually call our “add_arrays” again. However, recognizing the need to supply tools to easy coexist with code written previously, stress-flow included a “socket” construct to make it possible. A socket construct is a special case of stress-flow atom that is not defined as a separate function but rather inside another function or another stress-flow atom. Other than that, it has all elements of regular stress-flow atom (an associated lock, stressed and relaxed sections, etc). Therefore, a socket is intended to be declared together with other regular atoms inside a “detach” or other stress-flow block. If a socket has a return value, this value gets returned to the caller through stressed/relaxed section separating return instruction.
FIG. 4: An example of “socket” construct use Modified example of concurrent addition of arrays using socket construct is shown on FIG 4. Socket “all_done” is declared ahead of its use in atom “done_item,” and is declared inside “add_arrays” function. Once add_arrays finishes its loop of scheduling n of “add_item” atoms, it gets suspended waiting for “all_done” to be triggered. This happens when all finalized add_items report which triggers call to “all_done.” Routine “add_arrays” now resumes and returns to its caller, with all the results correctly calculated and stored. Thanks to “socket” construct, add_arrays could now be called from regular (non stress-flow) code just as if an ordinary non-parallel routine. Simple fact that add_arrays was declared as stress-atom itself with stressed section only guarantees that no new call to add_arrays will start before the previous one returns. |
|