User`s manual

88 digi.com Multitasking with Dynamic C
void RandomNumberTask(void *pdata)
{
// Declare as auto to ensure reentrancy.
auto OS_TCB data;
auto INT8U err;
auto INT16U RNum;
OSTaskQuery(OS_PRIO_SELF, &data);
while(1)
{
// Rand is not reentrant, so access must be controlled via a semaphore.
OSSemPend(RandomSem, 0, &err);
RNum = (int)(rand() * 100);
OSSemPost(RandomSem);
printf("Task%02d's random #: %d\n",data.OSTCBPrio,RNum);
// Wait 3 seconds in order to view output from each task.
OSTimeDlySec(3);
}
}
5.10.5 Compatibility with TCP/IP
The TCP/IP stack is reentrant and may be used with the µC/OS real-time kernel. The line
#use ucos2.lib
must appear before the line
#use dcrtcp.lib
A call to OSInit() must be made before calling sock_init().
5.10.5.1 Socket Locks
Each socket used in a µC/OS-II application program has an associated socket lock. Each socket lock uses
one semaphore of type OS_EVENT. Therefore, the macro MAX_OS_EVENTS must take into account each
of the socket locks, plus any events that the application program may be using (semaphores, queues, mail-
boxes, event flags, or mutexes).
Determining OS_MAX_EVENTS may get a little tricky, but it isn't too bad if you know what your program
is doing. Since MAX_SOCKET_LOCKS is defined as:
#define MAX_SOCKET_LOCKS (MAX_TCP_SOCKET_BUFFERS +
MAX_UDP_SOCKET_BUFFERS)
OS_MAX_EVENTS may be defined as:
#define OS_MAX_EVENTS MAX_TCP_SOCKET_BUFFERS +
MAX_UDP_SOCKET_BUFFERS + 2 + z
The constant “2” is included for the two global locks used by TCP/IP, and “z” is the number of
OS_EVENTS (semaphores, queues, mailboxes, event flags, or mutexes) required by the program.
If either MAX_TCP_SOCKET_BUFFERS or MAX_UDP_SOCKET_BUFFERS is not defined by the appli-
cation program prior to the #use statements for ucos.lib and dcrtcp.lib, default values will be
assigned.