The Taskthread of "Darwin's study Notes"

Source: Internet
Author: User

"Reprint Please specify source": http://blog.csdn.net/longlong530

There are three main types of learning Taskthread to focus on:

Tasktreadpool: Task thread pool

Taskthread: Task Thread

Task: Tasks


1. Taskthreadpool
Darwin runs one or more task threads and manages them in a unified online pool Taskthreadpool. The task thread receives the RTSP and RTP requests from the event thread, and then passes the request to the appropriate server module for processing, sending the data packets to the client.
By default, the core server creates a task thread for each processor. (numprocessors = Os::getnumprocessors ();)

Taskthreadpool::setnumshorttaskthreads (numshorttaskthreads);        Taskthreadpool::setnumblockingtaskthreads (numblockingthreads);        Taskthreadpool::addthreads (numthreads); Sserver->initnumthreads (numthreads);

2. Taskthread

All of the task objects will run in Taskthread, taskthread the task by running the Run method of the task type object. The number of Taskthread is configurable, by default the number of Taskthread is the same as the number of processors. Waits for a task to be called and run by Taskthread to be placed in a queue or heap.

void Taskthread::entry () {    task* thetask = NULL;        while (true)     {//} takes a task from the internal queue to return, at which time the task is neither in the heap nor in the queue;        Thetask = This->waitfortask ();        /* Omitted here, mainly for the call task of the Run method of the return value of a different branch processing, detailed analysis in the Task section Introduction */     }

Let's carefully analyze the Waitfortask method:

task* Taskthread::waitfortask () {while (true) {SInt64 thecurrenttime = Os::milliseconds (); /* If there is a task in the heap and the task has reached the execution time, the task is returned.  peekmin function See OSHeap.h, eavesdropping on the first element of the heap (but not out) */if ((fheap.peekmin () = NULL) && (Fheap.peekmin ()->getvalue () <= Thecurrenttime) {if (task_debug) qtss_printf ("Taskthread::waitfortask found timer-task=%s thread %p fheap.currentheapsize (% "_u32bitarg_") Taskelem =%p enclose=%p\n ", ((task*) fheap.peekmin ()->getenclosingobject ())->ftaskname, (void *) this, fheap.currentheapsize (), (void *) Fheap.peekmin (), (void *) fheap.peekmin (),            Getenclosingobject ());  Return (task*) fheap.extractmin ()->getenclosingobject ();//Remove the first task from the heap returns}//if there are tasks in the heap, but not yet to the execution time, calculate the time to wait//if        There is an element waiting for a timeout, figure out how long we should wait.        SInt64 thetimeout = 0;        if (fheap.peekmin () = NULL) Thetimeout = Fheap.peekmin ()->getvalue ()-thecurrenttime; Assert(thetimeout >= 0); Make sure we can ' t go-to-sleep for some ridiculously short//period of time//Don't allow a         Timeout below Ms without first verifying reliable UDP 1-2mbit live streams. Test with Streamingserver.xml pref reliabludp Printfs enabled and look for packet loss and check client for buffer Ahe    Ad recovery.                    if (Thetimeout <) Thetimeout = 10;        wait...//If the task queue is empty, wait for the thetimeout time to fetch the task back from the heap;//If the task queue is not empty, do not wait to take the task directly in the queue;        osqueueelem* Theelem = Ftaskqueue.dequeueblocking (this, (SInt32) thetimeout); if (Theelem! = NULL) {if (task_debug) qtss_printf ("Taskthread::waitfortask found signal-task=%s thr EAD%p ftaskqueue.getlength (% "_u32bitarg_") Taskelem =%p enclose=%p\n ", ((task*) Theelem->getenclosingobject ())- >ftaskname, (void *) this, ftaskqueue.getqueue ()->getlength (), (void *) Theelem, (void *) theelem->getenclosing         Object ());   Return (task*) theelem->getenclosingobject (); }////If we are supposed to stop, return NULL, which signals the caller to stop If (OSTHREAD::GETCU    Rrent ()->isstoprequested ()) return NULL; }   }

3. Task
There are two main methods for each Task object: signal and run.


3.1 Run ()

The run () method runs after the task object obtains the time slice to handle the event, and most of the work in Darwin is done in the run () function of the different task objects.
For example, common task types are rtspsession and rtpsession.
The return value of the program here is an important message:
When 1 is returned, the task is completely finished, and the program deletes the task pointer directly;
When it returns 0 o'clock, the thread then executes the next task, indicating that the task expects to be executed again immediately at the next messenger.
When a positive n is returned, the thread calls the task's run function again after n milliseconds. Add the task to the heap and record the time it wants to wait. The Entry () function is instrumented with the Waitfortask () function, and if the waiting time is up, run the task again
(Note: When we inherit the task class and rewrite the run virtual function, the first thing we do is run the getevent () function of the task class.) )
Virtual SInt64 Run () = 0;

3.2 Signal ()

The signal () method is called when the server wants to send an event to a Task object;
Joins the task to the task queue for the specified task thread and, if there is no task thread specified, randomly selects a task thread in the inline pool to run the task
void  Signal (eventflags eventflags);

"Reprint Please specify source": http://blog.csdn.net/longlong530

The Taskthread of "Darwin's study Notes"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.