The following example demonstrates the use of timeout and LAP handlers. The example assumes that the program is run in supervisor mode.
In this example, the original value of val is 10. The example includes the following steps:
A LAP is created, which doubles the value of val when it is called and posts an event.
The main program installs the LAP, sets a timeout and waits for a semaphore.
After being awakened, the program completes its function and then exits.
#include <exec/chExec.h> #include <exec/chIo.h> #include <stdcIntf.h> #include <stdio.h> #include <exec/chTimeout.h> int result; int val; KnTimeVal servTimeout = {0, 50 * K_NPERM}; /* 50 millisec */ KnTimeVal servDelay = {0, 500 * K_NPERM}; /* 5000 millisec */ KnTimeout servTimeoutDesc; KnThSem servThSem; /* * Timeout handler, used to test */ void toHandler(args, opMsg) void* args; void* opMsg; { val = val * 2; threadSemPost(&servThSem); sysWrite("timeout handler", strlen("timeout handler")); } void timeout() { void *opMsg; if (_stdc_execPrivilege == K_SUPACTOR) { KnLapDesc lapDesc; result = svLapCreate(&_stdc_execActor, toHandler, opMsg, K_LAP_SAMEHOST, &lapDesc); if (result != K_EOK) { printf("svLapCreate error\n"); } result = svSysTimeoutSet(&servTimeoutDesc, &servTimeout, 0, &lapDesc); if (result != K_EOK) { printf("svSysTimeoutSet error\n"); } /* Wait for the timeout handler to complete. should NOT return K_ETIMEOUT */ result = threadSemWait(&servThSem, &servDelay); if (result != K_EOK) { printf("threadSemWait error \n"); } printf("timeout completed\n"); } } main(int argc, char* argv[], char** envp) { val = 10; printf("val = %d\n",val); threadSemInit(&servThSem); timeout(); printf("val = %d\n",val); }