ChorusOS 5.0 Application Developer's Guide

Using Time Outs

The following example demonstrates the use of timeout and LAP handlers. The example assumes that the program is run in supervisor mode.


Example 12-3 Using Timeout/LAP Handlers

In this example, the original value of val is 10. The example includes the following steps:


#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);
}