The following examples illustrate the abort handling capabilities of the ChorusOS operating system.
This example enables an application to abort a thread whose identication is given as an argument. The first four parameters correspond to the capability of the thread home actor, and the fifth argument is the local identication of the thread in the home actor.
#include <utilities.h> KnCap actorCap; int result; int main(int argc, char *argv[ ]) { if (argc != 6) { fprintf(stderr, "bad call\n"); exit(1); } readCap(argv+1, &actorCap); result = threadAbort(&actorCap, atoi(argv[5])); if (result < 0) fprintf(stderr, "error on threadAbort: %d\n", strSysError(result)); }
In the following example, a thread requests an abort of itself before it enters a loop and commits to an infinite abortable delay.
#include <chorus.h> int main( ) { int i, result; threadAbort(K_MYACTOR, K_MYSELF); printf("Before looping\n"); for (i = 0; i < 50000000; i++); printf("After looping\n"); result = threadDelay(K_NOTIMEOUT); if(result == K_EABORT) printf("threadDelay aborted\n") else printf("result = %d\n", result); }
The output of this example is:
neon abortableDelay_u started aid = 23 Before looping After looping threadDelay aborted |
In the previous example the abort request immediately awakens the thread when the abort request calls the threadDelay() primitive. It has no effect on the previously executed loop.
The following example modifies the code used in the previous example to produce a non-abortable application (notAbortDel). The code has been modified as follows:
The request to auto-abort the thread is deleted.
The abortable call threadDelay(K_NOTIMEOUT) is replaced by the equivalent non-abortable call threadDelay(K_NOTIMEOUT_NOABORT).
The notAbortDel application is then executed using the following:
neon-n notAbortDel_u & [1] 16123 started aid = 23 Before looping After looping |
In the previous example, a request for the thread to abort is made using the threadAbort() application created in Example 13-1. To verify that the request has no effect and that the thread or actor must be killed:
$ rsh neon arun cs -la 23 started aid = 22 ChorusOS r5.0.0 Site 0 Time 1d 19h 14m 22 ACTOR-UI KEY LID TYPE STATUS TH# NAME 200000d0 869da80a 00000017 00000000 0023 USER STARTED 001 notAbortDel_u THREAD-LI PRIORITY TT IT CTX SC-MS-PN 0007 140 00000430 00000430 ff6380 0- 1- 0 main ............................ $ neon threadAbort_u 200000d0 869da80a 17 0 7 started aid = 22 $ rsh neon aps grep notAbort 0 23 notAbortDel_u 0 N/A $ rsh neon akill 23 |
In the following example, an application called abortedState is created as follows:.
#include <chorus.h> int main() { int i, result; threadAbort(K_MYACTOR, K_MYSELF); printf("Before looping \n"); for (i=0; i<100000; i++); printf("After looping\n"); threadAbort(K_MYACTOR, K_MYSELF); result = threadAborted( ); if (result == 1) printf("Aborted state\n"); else printf("Non aborted\n"); result = threadAborted( ); if (result == 1) printf("Aborted state\n"); else printf("Non aborted\n"); }
The output of this example is:
$ neon abortedState_u started aid = 23 Before looping After looping Aborted state Non aborted |
Note that abort requests are not accumulated. An abort request for a thread that is already in the aborted state will be ignored.