reentrant - What is really re-entrant function? -
i have read may threads on re-entrant subjects on as on http://en.wikipedia.org/wiki/reentrancy_(computing).
i might ideas of re-entrant functions. when read example on wiki site, confused.
the first example:
int t; void swap(int *x, int *y) { t = *x; *x = *y; // hardware interrupt might invoke isr() here! *y = t; } void isr() { int x = 1, y = 2; swap(&x, &y); } as site explained: " it still fails reentrant, , continue cause problems if isr() called in same context thread executing swap()." => kinds of prolems might happen here? swapping result not correct? or value of t variable modified?
and second example, improved first one:
int t; void swap(int *x, int *y) { int s; s = t; // save global variable t = *x; *x = *y; // hardware interrupt might invoke isr() here! *y = t; t = s; // restore global variable } void isr() { int x = 1, y = 2; swap(&x, &y); } how improves first one? mean variable t kept unchanged inside swap() function?
re-entrant means function can interrupted @ point, , able correctly finish executing after interruption, in cases when same function called 1 or more times in interrupted state.
the crucial part here invocation of function called in interrupted state must finish before original call state restored. main difference between re-entrancy , thread safety: in order thread-safe, function must able proceed if interrupting invocation unfinished before control gets original call.
that's why second version of swap re-entrant: leaves t in unchanged state upon exiting, entering , exiting swap in middle of interrupted call not alter global state seen interrupted invocation.
Comments
Post a Comment