view examples/twothreads.c @ 3077:2dde4203a2c7

* cdl/io_can.cdl: support for add/remove compiler flags * doc/can.sgml, doc/can_driver_doc.html: updated doc reflecting API changes (range/mask filtering, listen-only mode, return values for xmt_msg/rcv_event, new flags). * include/can.h: given in a match the types of function pointers for can_callbacks_t according to the changes made in can.c. * include/canio.h: flags field in event enlarged to 32 bits to fit new events 'CYGNUM_CAN_EVENT_ERR_ACTIVE' and 'CYGNUM_CAN_EVENT_OVERRUN_RX_HW'. Made a 'flag' typedef. cyg_can_event fields reordered so flags size change keeps events the same size. Defined new filtering capabilities (range/mask). New convenience macro CYGNUM_CAN_EVENT_ALL. New listen-only mode. * src/can.c: can_rcv_event() and can_xmt_msg() now returns cyg_bool (were 'void'). flags using cyg_can_event_flags_t type. [ Bugzilla 1001453 ]
author sergeig
date Thu, 09 Feb 2012 21:16:48 +0000
parents ead06911cbae
children
line wrap: on
line source

#include <cyg/kernel/kapi.h>

#include <stdio.h>
#include <math.h>
#include <stdlib.h>

/* now declare (and allocate space for) some kernel objects,
   like the two threads we will use */
cyg_thread thread_s[2];		/* space for two thread objects */

char stack[2][4096];		/* space for two 4K stacks */

/* now the handles for the threads */
cyg_handle_t simple_threadA, simple_threadB;

/* and now variables for the procedure which is the thread */
cyg_thread_entry_t simple_program;

/* and now a mutex to protect calls to the C library */
cyg_mutex_t cliblock;

/* we install our own startup routine which sets up threads */
void cyg_user_start(void)
{
  printf("Entering twothreads' cyg_user_start() function\n");

  cyg_mutex_init(&cliblock);

  cyg_thread_create(4, simple_program, (cyg_addrword_t) 0,
		    "Thread A", (void *) stack[0], 4096,
		    &simple_threadA, &thread_s[0]);
  cyg_thread_create(4, simple_program, (cyg_addrword_t) 1,
		    "Thread B", (void *) stack[1], 4096,
		    &simple_threadB, &thread_s[1]);

  cyg_thread_resume(simple_threadA);
  cyg_thread_resume(simple_threadB);
}

/* this is a simple program which runs in a thread */
void simple_program(cyg_addrword_t data)
{
  int message = (int) data;
  int delay;

  printf("Beginning execution; thread data is %d\n", message);

  cyg_thread_delay(200);

  for (;;) {
    delay = 200 + (rand() % 50);

    /* note: printf() must be protected by a
       call to cyg_mutex_lock() */
    cyg_mutex_lock(&cliblock); {
      printf("Thread %d: and now a delay of %d clock ticks\n",
	     message, delay);
    }
    cyg_mutex_unlock(&cliblock);
    cyg_thread_delay(delay);
  }
}