<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <HTML ><HEAD ><TITLE >How scheduling works</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK REL="HOME" TITLE="GStreamer Plugin Writer's Guide" HREF="index.html"><LINK REL="UP" TITLE="Advanced Filter Concepts" HREF="part-advanced.html"><LINK REL="PREVIOUS" TITLE="Advanced Filter Concepts" HREF="part-advanced.html"><LINK REL="NEXT" TITLE="The Optimal Scheduler" HREF="section-sched-opt.html"></HEAD ><BODY CLASS="chapter" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#840084" ALINK="#0000FF" ><DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="3" ALIGN="center" ><SPAN CLASS="application" >GStreamer</SPAN > Plugin Writer's Guide</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="part-advanced.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="section-sched-opt.html" ACCESSKEY="N" >Next</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><DIV CLASS="chapter" ><H1 ><A NAME="chapter-loopbased-sched" ></A >Chapter 11. How scheduling works</H1 ><P > Scheduling is, in short, a method for making sure that every element gets called once in a while to process data and prepare data for the next element. Likewise, a kernel has a scheduler to for processes, and your brain is a very complex scheduler too in a way. Randomly calling elements' chain functions won't bring us far, however, so you'll understand that the schedulers in <SPAN CLASS="application" >GStreamer</SPAN > are a bit more complex than this. However, as a start, it's a nice picture. <SPAN CLASS="application" >GStreamer</SPAN > currently provides two schedulers: a <SPAN CLASS="emphasis" ><I CLASS="emphasis" >basic</I ></SPAN > scheduler and an <SPAN CLASS="emphasis" ><I CLASS="emphasis" >optimal</I ></SPAN > scheduler. As the name says, the basic scheduler (<SPAN CLASS="QUOTE" >"basic"</SPAN >) is an unoptimized, but very complete and simple scheduler. The optimal scheduler (<SPAN CLASS="QUOTE" >"opt"</SPAN >), on the other hand, is optimized for media processing, but therefore also more complex. </P ><P > Note that schedulers only operate on one thread. If your pipeline contains multiple threads, each thread will run with a separate scheduler. That is the reason why two elements running in different threads need a queue-like element (a <CODE CLASS="classname" >DECOUPLED</CODE > element) in between them. </P ><DIV CLASS="sect1" ><H1 CLASS="sect1" ><A NAME="section-sched-basic" >11.1. The Basic Scheduler</A ></H1 ><P > The <SPAN CLASS="emphasis" ><I CLASS="emphasis" >basic</I ></SPAN > scheduler assumes that each element is its own process. We don't use UNIX processes or POSIX threads for this, however; instead, we use so-called <SPAN CLASS="emphasis" ><I CLASS="emphasis" >co-threads</I ></SPAN >. Co-threads are threads that run besides each other, but only one is active at a time. The advantage of co-threads over normal threads is that they're lightweight. The disadvantage is that UNIX or POSIX do not provide such a thing, so we need to include our own co-threads stack for this to run. </P ><P > The task of the scheduler here is to control which co-thread runs at what time. A well-written scheduler based on co-threads will let an element run until it outputs one piece of data. Upon pushing one piece of data to the next element, it will let the next element run, and so on. Whenever a running element requires data from the previous element, the scheduler will switch to that previous element and run that element until it has provided data for use in the next element. </P ><P > This method of running elements as needed has the disadvantage that a lot of data will often be queued in between two elements, as the one element has provided data but the other element hasn't actually used it yet. These storages of in-between-data are called <SPAN CLASS="emphasis" ><I CLASS="emphasis" >bufpens</I ></SPAN >, and they can be visualized as a light <SPAN CLASS="QUOTE" >"queue"</SPAN >. </P ><P > Note that since every element runs in its own (co-)thread, this scheduler is rather heavy on your system for larger pipelines. </P ></DIV ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="part-advanced.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >Home</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="section-sched-opt.html" ACCESSKEY="N" >Next</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >Advanced Filter Concepts</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="part-advanced.html" ACCESSKEY="U" >Up</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >The Optimal Scheduler</TD ></TR ></TABLE ></DIV ></BODY ></HTML >