<?xml version="1.0" encoding="UTF-8"?>
<!--Generated by Squarespace V5 Site Server v5.13.159 (http://www.squarespace.com) on Thu, 23 May 2013 10:05:23 GMT--><rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0"><channel><title>Michael Bebenita</title><link>http://michael.bebenita.com/imported-20100930232226/</link><description></description><lastBuildDate>Fri, 19 Oct 2012 20:51:47 +0000</lastBuildDate><copyright></copyright><language>en-US</language><generator>Squarespace V5 Site Server v5.13.159 (http://www.squarespace.com)</generator><item><title>LLJS: Low-Level JavaScript</title><dc:creator>Michael Bebenita</dc:creator><pubDate>Fri, 11 May 2012 21:53:33 +0000</pubDate><link>http://michael.bebenita.com/imported-20100930232226/2012/5/11/lljs-low-level-javascript.html</link><guid isPermaLink="false">693478:8113215:16224404</guid><description><![CDATA[<p>What would JavaScript look like if you added low level features to it? Well, three weeks ago, <a href="http://rfrn.org/~shu/">Shu-yu Guo</a> and I set about to find out. We prototyped the idea with two different languages, <a href="http://lljs.org">LLJS</a>&nbsp;based on JavaScript and&nbsp;<a href="http://github.com/syg/heap.coffee/">heap.coffee</a>&nbsp;based on CoffeeScript.&nbsp;</p>
<p>Both are typed dialects of JavaScript that offer a C-like type system with manual memory management. They compile to JavaScript and let you write memory-efficient and GC pause-free code less painfully. This is early research prototype work, so don't expect anything rock solid just yet.&nbsp;The research goal here is to explore low-level statically typed features in a high-level dynamically typed language. Think of it as inline assembly in C, or the&nbsp;<tt>unsafe</tt>&nbsp;keyword in C#. It's not pretty, but it gets the job done.</p>
<p>You can play with LLJS compiler <a href="http://mbebenita.github.com/Mvm/">online</a>&nbsp;or check out the source on&nbsp;<a href="http://github.com/mbebenita/Mvm/">github</a>.</p>]]></description><wfw:commentRss>http://michael.bebenita.com/imported-20100930232226/rss-comments-entry-16224404.xml</wfw:commentRss></item><item><title>Updated Broadway.js Demo using Web Workers and WebGL</title><dc:creator>Michael Bebenita</dc:creator><pubDate>Tue, 06 Mar 2012 06:06:31 +0000</pubDate><link>http://michael.bebenita.com/imported-20100930232226/2012/3/6/updated-broadwayjs-demo-using-web-workers-and-webgl.html</link><guid isPermaLink="false">693478:8113215:15316028</guid><description><![CDATA[<p>After some hacking, I managed to demux and play video streams directly from .mp4 files. An updated demo is available at:</p>
<p><a href="http://mbebenita.github.com/Broadway/storyDemo.html">http://mbebenita.github.com/Broadway/storyDemo.html</a></p>
<p>or</p>
<p><a href="http://mbebenita.github.com/Broadway/treeDemo.html">http://mbebenita.github.com/Broadway/treeDemo.html</a></p>
<p>Warning, the video file needs to be completely downloaded before playback can begin, so it's a bit slow at first.</p>
<p>Moreover, Broadway.js now supports WebGL and background decoding in web workers. Decoding in a worker is still a bit slow due to the overhead of passing decoded video frames via postMessage, but hopefully that should go away once array buffer transfers land.</p>]]></description><wfw:commentRss>http://michael.bebenita.com/imported-20100930232226/rss-comments-entry-15316028.xml</wfw:commentRss></item><item><title>Broadway.js Live Demo</title><dc:creator>Michael Bebenita</dc:creator><pubDate>Tue, 01 Nov 2011 17:21:39 +0000</pubDate><link>http://michael.bebenita.com/imported-20100930232226/2011/11/1/broadwayjs-live-demo.html</link><guid isPermaLink="false">693478:8113215:13553700</guid><description><![CDATA[<p>Due to popular demand, we have posted a live and easily accessible <a href="http://mbebenita.github.com/Broadway/broadway.html">demo</a> of Broadway.js. Keep in mind that the video clip needs to be completely downloaded before playback can begin.</p>
<p>The performance score is computed by averaging the number of frames per second in the first 60 seconds of the video clip. On my Mac Book Air (1.8 GHz Intel Core i7, 4GB) I get a score of 30 fps.</p>]]></description><wfw:commentRss>http://michael.bebenita.com/imported-20100930232226/rss-comments-entry-13553700.xml</wfw:commentRss></item><item><title>Broadway.js - H.264 in JavaScript</title><dc:creator>Michael Bebenita</dc:creator><pubDate>Fri, 28 Oct 2011 08:44:00 +0000</pubDate><link>http://michael.bebenita.com/imported-20100930232226/2011/10/28/broadwayjs-h264-in-javascript.html</link><guid isPermaLink="false">693478:8113215:13495705</guid><description><![CDATA[<p>Recently I joined Mozilla, it's a fantastic place! My first task was to look into pure software H.264 decoding in JavaScript! I must admit, at first I thought it was a little bit crazy, and that it couldn't be done. Turns out that, luckily, I was very wrong.</p>
<p>Alon Zakai and I managed to take an existing H.264 decoder, simplify it, and compile it with Alon's awesome Emscripten compiler, which translates LLVM bitcode to JavaScript. The result is quite remarkable, we can reach a rate of nearly 30 fps decoding video purely in JavaScript, with no real optimizatsions other than what Emscripten already performs. There are lots of improvements ahead, such as hardware acceleration using WebGL, parallel processing, etc.. This is VERY EARLY WORK, just to show off how far JavaScript performance has come.</p>
<p>Brendan Eich showed a demo (video <a href="http://yfrog.com/nmng0z">http://yfrog.com/nmng0z</a>) of this at OOPSLA (slides <a href="http://www.slideshare.net/BrendanEich/splash-9915475">http://www.slideshare.net/BrendanEich/splash-9915475</a>), and enough people were interested that we've decided to make the code public (<a href="https://github.com/mbebenita/Broadway">https://github.com/mbebenita/Broadway</a>). To run the demo, simply clone the Git repo and open the Demo/broadway.html file in the nightly build of Firefox which includes a fancy type inference that really speeds up the JS JIT.</p>
<p>&nbsp;</p>]]></description><wfw:commentRss>http://michael.bebenita.com/imported-20100930232226/rss-comments-entry-13495705.xml</wfw:commentRss></item><item><title>Done with Gradschool</title><dc:creator>Michael Bebenita</dc:creator><pubDate>Fri, 28 Oct 2011 08:31:48 +0000</pubDate><link>http://michael.bebenita.com/imported-20100930232226/2011/10/28/done-with-gradschool.html</link><guid isPermaLink="false">693478:8113215:13495645</guid><description><![CDATA[<p>Finally finished up my dissertation, and with that nearly 6 years of gradschool. It surely was fun!</p>
<p>Here's the thesis, if you're in the mood to read about <a href="http://www.ics.uci.edu/~mbebenit/bebenita-dissertation.pdf">Trace Based Compilation and Optimization In Meta-Circular Virtual Execution Environments</a></p>
<p>&nbsp;</p>]]></description><wfw:commentRss>http://michael.bebenita.com/imported-20100930232226/rss-comments-entry-13495645.xml</wfw:commentRss></item><item><title>Class Notes, Jan 25, 2011</title><dc:creator>Michael Bebenita</dc:creator><pubDate>Wed, 26 Jan 2011 03:08:32 +0000</pubDate><link>http://michael.bebenita.com/imported-20100930232226/2011/1/25/class-notes-jan-25-2011.html</link><guid isPermaLink="false">693478:8113215:10226781</guid><description><![CDATA[<p>Here are the <a href="http://michael.bebenita.com/storage/ssa_class_notes.pdf">SSA class notes</a> I mentioned in class. <span class="full-image-block ssNonEditable"><span><img src="http://michael.bebenita.com/storage/ssa_class_notes.pdf?__SQUARESPACE_CACHEVERSION=1296011918974" alt="" /></span></span></p>]]></description><wfw:commentRss>http://michael.bebenita.com/imported-20100930232226/rss-comments-entry-10226781.xml</wfw:commentRss></item><item><title>How Not To Write A Profiler On OSX</title><dc:creator>Michael Bebenita</dc:creator><pubDate>Fri, 22 Oct 2010 20:35:19 +0000</pubDate><link>http://michael.bebenita.com/imported-20100930232226/2010/10/22/how-not-to-write-a-profiler-on-osx.html</link><guid isPermaLink="false">693478:8113215:9257420</guid><description><![CDATA[<p>Measuring the performance of code is difficult. There are generally two techniques to do this: instrumentation and sampling. The instrumentation technique modifies the code of the profiled program with counters that measure the time spent in various parts of the program. This works quite well if you're only interested in measuring the performance of your program at a coarse level. The problem with fine grained instrumentation is that the overhead of the instrumentation code itself skews profiling results, not to mention that it makes your program quite slow. The sampling technique is much more lightweight and relies on statistical sampling to infer where your program is spending most of its time. A sample profiler stops the execution of your program at regular intervals and collects stack traces, at the end of the profiling session these stack traces can be used to infer what your program is doing.</p>
<p>Great, with this in mind I set out to track down some of the performance problems in Maxpath. I pulled up the well-regarded Shark profiler and profiled the execution of a benchmark. I wasn't expecting much but the results were quite disappointing. I spent 23% of the time in an unknown library at an unknown symbol, not very useful. This is because Shark can't figure out Maxine's symbols, because it has none.</p>
<p><span class="full-image-block ssNonEditable"><span><img src="http://michael.bebenita.com/storage/ProfMax.gif?__SQUARESPACE_CACHEVERSION=1287781284933" alt="" /></span></span></p>
<p>Well, surely there's a way for Shark to ask the profiled process for symbol information as it's running, NO there isn't. There is however a way for Shark to load symbols after the fact "Symbolicate" but it expects a Mach-O file with symbols in it which is quite troublesome to generate from a VM that is not compiled with the standard tool chain and that dynamically generates executable code.</p>
<p>Well if Shark can't do it, then maybe I can just write my own profiler. The big question is how do I sample the execution of another program at regular intervals. After some searching around I ran into the profil() system call but this has been long deprecated in favor for CHUD tools and DTrace. Unfortunately these don't help either since they all expect symbols and have no facilities for asking the profiled process for symbols and stack traces during profiling.</p>
<p>After some more searching I ran into the SIGPROF signal. On OSX there is a way to have the kernel send a signal to a profiled process at regular intervals using the setitimer function. This seemed like a great idea since I could have Maxpath profile itself by sending itself SIGPROF signals at regular intervals. Since the profiler and the profiled process were one and the same, I could have full access to the data structures within Maxpath to inspect symbols and collect stack traces. But there is a problem, the SIGPROF signal is delivered at random to one of the Maxpath threads, so there is no way to predict who gets the signal. In fact, most of the time I was receiving the signal on a suspended thread which is totally useless in terms of profiling.&nbsp;</p>
<p>Next step is to look at creating a profiler thread within the same process that has a higher priority and suspends all other VM threads at regular intervals, hopefully I'll have more luck with that.</p>
<p>That's my profiling story so far ...</p>]]></description><wfw:commentRss>http://michael.bebenita.com/imported-20100930232226/rss-comments-entry-9257420.xml</wfw:commentRss></item><item><title>The Maxpath Virtual Machine Source Code</title><category>Maxpath</category><dc:creator>Michael Bebenita</dc:creator><pubDate>Thu, 21 Oct 2010 20:05:28 +0000</pubDate><link>http://michael.bebenita.com/imported-20100930232226/2010/10/21/the-maxpath-virtual-machine-source-code.html</link><guid isPermaLink="false">693478:8113215:9245794</guid><description><![CDATA[<p><span class="full-image-float-left ssNonEditable"><span><img src="http://michael.bebenita.com/storage/MaxPath.png?__SQUARESPACE_CACHEVERSION=1287695204415" alt="" /></span></span>So after nearly 2 years of development Maxpath has become stable enough to be of use to someone interested in trace based compilers. I made the <a href="https://bitbucket.org/mbebenita/maxpath">source</a> available on bitbucket.</p>
<p>Refer to the Wiki section for directions and help on how to build and run Maxpath. We recently published a paper on how tracing works in Maxpath which can be found <a href="http://www.ics.uci.edu/~mbebenit/pubs/pppj-2010.pdf">here</a>.</p>
<p>For general Maxine questions visit the <a href="http://wikis.sun.com/display/MaxineVM/Home">Project Site</a> at Sun Labs.</p>]]></description><wfw:commentRss>http://michael.bebenita.com/imported-20100930232226/rss-comments-entry-9245794.xml</wfw:commentRss></item><item><title>Callbacks in C++</title><dc:creator>Michael Bebenita</dc:creator><pubDate>Sun, 10 Oct 2010 00:30:04 +0000</pubDate><link>http://michael.bebenita.com/imported-20100930232226/2010/10/9/callbacks-in-c.html</link><guid isPermaLink="false">693478:8113215:9146007</guid><description><![CDATA[<p>I've been looking for an elegant way to perfrom callbacks to C++ member functions. This is somewhat a problem because you can't just pass function pointers around since member functions need a "this" pointer to access member data. Therefore you need to wrap function pointers in special objects that carry around the "this" pointer and pass those wrapper objects around instead. Here is what I settled on:</p>
<div class="allcode">
<table border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td></td>
<td>
<pre class="textmate-source"><pre class="sunburst"><span class="storage storage_type storage_type_template storage_type_template_c++">template</span>&lt;<span class="storage storage_modifier storage_modifier_c++">typename</span> ReturnType, <span class="storage storage_modifier storage_modifier_c++">typename</span> Parameter&gt;
<span class="meta meta_class-struct-block meta_class-struct-block_c++"><span class="storage storage_type storage_type_c++">class</span> <span class="entity entity_name entity_name_type entity_name_type_c++">Callback</span> {
<span class="storage storage_modifier storage_modifier_c++">public:</span>
    <span class="storage storage_modifier storage_modifier_c++">virtual</span> ReturnType execute(Parameter parameter) = <span class="constant constant_numeric constant_numeric_c">0</span>;
}</span>;

<span class="storage storage_type storage_type_template storage_type_template_c++">template</span>&lt;<span class="meta meta_class-struct-block meta_class-struct-block_c++"><span class="storage storage_type storage_type_c++">class</span> <span class="entity entity_name entity_name_type entity_name_type_c++">Class</span></span>, <span class="storage storage_modifier storage_modifier_c++">typename</span> ReturnType, <span class="storage storage_modifier storage_modifier_c++">typename</span> Parameter&gt;
<span class="meta meta_class-struct-block meta_class-struct-block_c++"><span class="storage storage_type storage_type_c++">class</span> <span class="entity entity_name entity_name_type entity_name_type_c++">Function</span> : public Callback<span class="meta meta_angle-brackets meta_angle-brackets_c++">&lt;ReturnType, Parameter&gt;</span> {
<span class="storage storage_modifier storage_modifier_c++">public:</span>
    <span class="storage storage_type storage_type_c">typedef</span> ReturnType (Class::*Method)(Parameter);

<span class="meta meta_function meta_function_constructor meta_function_constructor_c++">    <span class="entity entity_name entity_name_function entity_name_function_c++">Function</span>(Class *instance, Method method)</span> {
        <span class="variable variable_language variable_language_c++">this</span>-&gt;instance = instance;
        <span class="variable variable_language variable_language_c++">this</span>-&gt;method = method;
    }

    ReturnType execute</span>(Parameter parameter) {
        <span class="keyword keyword_control keyword_control_c">return</span> (instance-&gt;*method)(parameter);
    }

<span class="storage storage_modifier storage_modifier_c++">private:</span>
    Class* instance;
    Method method;
};
</pre>
</pre>
</td>
</tr>
</tbody>
</table>
</div>
<p>&nbsp;To use the above classes try:</p>
<div class="allcode">
<table border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td></td>
<td>
<pre class="textmate-source"><pre class="sunburst"><span class="meta meta_class-struct-block meta_class-struct-block_c++"><span class="storage storage_type storage_type_c++">class</span> <span class="entity entity_name entity_name_type entity_name_type_c++">Example</span> {
    <span class="storage storage_type storage_type_c">bool</span><span class="meta meta_function meta_function_c"> <span class="entity entity_name entity_name_function entity_name_function_c">isPositive</span>(<span class="storage storage_type storage_type_c">int</span> i)</span> {
        <span class="keyword keyword_control keyword_control_c">return</span> i &gt;= <span class="constant constant_numeric constant_numeric_c">0</span>;
    }

    <span class="storage storage_type storage_type_c">bool</span> run</span>() {
        Callback&lt;<span class="storage storage_type storage_type_c">bool</span>, <span class="storage storage_type storage_type_c">int</span>&gt; *callback =
            <span class="keyword keyword_control keyword_control_c++">new</span> Function&lt;Example, <span class="storage storage_type storage_type_c">bool</span>, <span class="storage storage_type storage_type_c">int</span>&gt;(<span class="variable variable_language variable_language_c++">this</span>, &amp;Example::isPositive);
        <span class="keyword keyword_control keyword_control_c">return</span> callback-&gt;execute(<span class="constant constant_numeric constant_numeric_c">10</span>);
    }
};</pre>
</pre>
</td>
</tr>
</tbody>
</table>
</div>
<p>If you need to pass multiple parameters you'll need to create more variants of the Callback and Function classes.</p>]]></description><wfw:commentRss>http://michael.bebenita.com/imported-20100930232226/rss-comments-entry-9146007.xml</wfw:commentRss></item><item><title>A Lock Free Queue in C++</title><dc:creator>Michael Bebenita</dc:creator><pubDate>Wed, 25 Aug 2010 06:35:00 +0000</pubDate><link>http://michael.bebenita.com/imported-20100930232226/2010/8/25/a-lock-free-queue-in-c.html</link><guid isPermaLink="false">693478:8113215:9061012</guid><description><![CDATA[<p>Here is lock free queue data structure that I adapted from the paper "Simple, Fast, and Practical Non-Blocking and Blocking Concurrent Queue Algorithms" for the <a href="http://github.com/graydon/rust">Rust</a> runtime. For details read the paper, or if you're lazy and trust me that it works just copy, paste, and run. (You may need to specify the -march=i686 compiler flag to GCC to get this to link correctly.)</p>
<p><!-- .better { border: 2px solid #000; font-size: 12px; padding: 5px} --></p>
<p>&nbsp;</p>
<div class="allcode">
<table border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td>&nbsp;</td>
<td>
<pre class="textmate-source"><pre class="sunburst"><span class="meta meta_preprocessor meta_preprocessor_c">#<span class="keyword keyword_control keyword_control_import keyword_control_import_c">ifndef</span> LOCK_FREE_QUEUE_H</span>
<span class="meta meta_preprocessor meta_preprocessor_macro meta_preprocessor_macro_c">#<span class="keyword keyword_control keyword_control_import keyword_control_import_define keyword_control_import_define_c">define</span> <span class="entity entity_name entity_name_function entity_name_function_preprocessor entity_name_function_preprocessor_c">LOCK_FREE_QUEUE_H</span></span>

<span class="comment comment_block comment_block_c">/**</span>
<span class="Comment"> * How and why this lock free queue works:</span>
<span class="Comment"> *</span>
<span class="Comment"> * Adapted from the paper titled "Simple, Fast, and Practical Non-Blocking</span>
<span class="Comment"> * and Blocking Concurrent Queue Algorithms" by Maged M. Michael,</span>

<span class="Comment"> * Michael L. Scott.</span>
<span class="Comment"> *</span>
<span class="Comment"> * Safety Properties:</span>
<span class="Comment"> *</span>
<span class="Comment"> * 1. The linked list is always connected.</span>
<span class="Comment"> * 2. Nodes are only inserted after the last node in the linked list.</span>
<span class="Comment"> * 3. Nodes are only deleted from the beginning of the linked list.</span>
<span class="Comment"> * 4. Head always points to the first node in the linked list.</span>
<span class="Comment"> * 5. Tail always points to a node in the linked list.</span>
<span class="Comment"> *</span>
<span class="Comment"> *</span>
<span class="Comment"> * 1. The linked list is always connected because the next pointer is not set</span>
<span class="Comment"> *    to null before the node is freed, and no node is freed until deleted</span>
<span class="Comment"> *    from the linked list.</span>
<span class="Comment"> *</span>
<span class="Comment"> * 2. Nodes are only inserted at the end of the linked list because they are</span>
<span class="Comment"> *    linked through the tail pointer which always points to a node in the</span>
<span class="Comment"> *    linked list (5) and an inserted node is only linked to a node that has</span>
<span class="Comment"> *    a null next pointer, and the only such node is the last one (1).</span>
<span class="Comment"> *</span>
<span class="Comment"> * 3. Nodes are deleted from the beginning of the list because they are</span>
<span class="Comment"> *    deleted only when they are pointed to by head which always points to the</span>
<span class="Comment"> *    first node (4).</span>
<span class="Comment"> *</span>
<span class="Comment"> * 4. Head always points to the first node in the list because it only changes</span>
<span class="Comment"> *    its value to the next node atomically. The new value of head cannot be</span>
<span class="Comment"> *    null because if there is only one node in the list the dequeue operation</span>
<span class="Comment"> *    returns without deleting any nodes.</span>
<span class="Comment"> *</span>
<span class="Comment"> * 5. Tail always points to a node in the linked list because it never lags</span>
<span class="Comment"> *    behind head, so it can never point to a deleted node. Also, when tail</span>
<span class="Comment"> *    changes its value it always swings to the next node in the list and it</span>
<span class="Comment"> *    never tires to change its value if the next pointer is NULL.</span>
<span class="Comment"> */</span>

<span class="meta meta_preprocessor meta_preprocessor_c meta_preprocessor_c_include">#<span class="keyword keyword_control keyword_control_import keyword_control_import_include keyword_control_import_include_c">include</span> <span class="string string_quoted string_quoted_other string_quoted_other_lt-gt string_quoted_other_lt-gt_include string_quoted_other_lt-gt_include_c">&lt;assert.h&gt;</span></span>

template &lt;class T&gt;
class lock_free_queue {

    <span class="storage storage_type storage_type_c">struct</span> node_t;
    <span class="storage storage_type storage_type_c">struct</span> pointer_t {
        node_t *node;
        <span class="support support_type support_type_stdint support_type_stdint_c">uint32_t</span> count;
        pointer_t() : node(<span class="constant constant_language constant_language_c">NULL</span>), count(<span class="constant constant_numeric constant_numeric_c">0</span>) {
            <span class="comment comment_line comment_line_double-slash comment_line_double-slash_c++">// Nop.</span>

        }
        pointer_t(node_t *node, <span class="support support_type support_type_stdint support_type_stdint_c">uint32_t</span> count) {
            this-&gt;node = node;
            this-&gt;count = count;
        }
        <span class="storage storage_type storage_type_c">bool</span><span class="meta meta_function meta_function_c"> <span class="entity entity_name entity_name_function entity_name_function_c">equals</span>(pointer_t &amp;other)</span> {
            <span class="keyword keyword_control keyword_control_c">return</span> node == other.node &amp;&amp; count == other.count;
        }
    };

    <span class="storage storage_type storage_type_c">struct</span> node_t {
        T value;
        pointer_t next;

        node_t() {
            next.node = <span class="constant constant_language constant_language_c">NULL</span>;
            next.count = <span class="constant constant_numeric constant_numeric_c">0</span>;
        }

        node_t(pointer_t next, T value) {
            this-&gt;next = next;
            this-&gt;value = value;
        }
    };

    <span class="comment comment_line comment_line_double-slash comment_line_double-slash_c++">// Always points to the first node in the list.</span>

    pointer_t head;

    <span class="comment comment_line comment_line_double-slash comment_line_double-slash_c++">// Always points to a node in the list, (not necessarily the last).</span>
    pointer_t tail;

    <span class="comment comment_line comment_line_double-slash comment_line_double-slash_c++">// Compare and swap counted pointers, we can only do this if pointr_t is</span>
    <span class="comment comment_line comment_line_double-slash comment_line_double-slash_c++">// 8 bytes or less since that the maximum size CAS can handle.</span>
    <span class="storage storage_type storage_type_c">bool</span> compare_and_swap(pointer_t *address,
        pointer_t *oldValue,
        pointer_t newValue) {

        <span class="keyword keyword_control keyword_control_c">if</span> (sync::compare_and_swap(
                (<span class="support support_type support_type_stdint support_type_stdint_c">uint64_t</span>*) address,
                *(<span class="support support_type support_type_stdint support_type_stdint_c">uint64_t</span>*) oldValue,
                *(<span class="support support_type support_type_stdint support_type_stdint_c">uint64_t</span>*) &amp;newValue)) {
            <span class="keyword keyword_control keyword_control_c">return</span> <span class="constant constant_language constant_language_c">true</span>;
        }
        <span class="keyword keyword_control keyword_control_c">return</span> <span class="constant constant_language constant_language_c">false</span>;
    }

public:
    lock_free_queue() {
        <span class="comment comment_line comment_line_double-slash comment_line_double-slash_c++">// We can only handle 64bit CAS for counted pointers, so this will</span>

        <span class="comment comment_line comment_line_double-slash comment_line_double-slash_c++">// not work with 64bit pointers.</span>
        <span class="support support_function support_function_C99 support_function_C99_c">assert</span> (<span class="keyword keyword_operator keyword_operator_sizeof keyword_operator_sizeof_c">sizeof</span>(pointer_t) == <span class="keyword keyword_operator keyword_operator_sizeof keyword_operator_sizeof_c">sizeof</span>(<span class="support support_type support_type_stdint support_type_stdint_c">uint64_t</span>));

        <span class="comment comment_line comment_line_double-slash comment_line_double-slash_c++">// Allocate a dummy node to be used as the first node in the list.</span>
        node_t *node = new node_t();

        <span class="comment comment_line comment_line_double-slash comment_line_double-slash_c++">// Head and tail both start out pointing to the dummy node.</span>

        head.node = node;
        tail.node = node;
    }

    virtual ~lock_free_queue() {
        <span class="comment comment_line comment_line_double-slash comment_line_double-slash_c++">// Delete dummy node.</span>
        delete head.node;
    }

    <span class="storage storage_type storage_type_c">bool</span><span class="meta meta_function meta_function_c"> <span class="entity entity_name entity_name_function entity_name_function_c">is_empty</span>()</span> {
        <span class="keyword keyword_control keyword_control_c">return</span> head.node == tail.node;
    }

    <span class="storage storage_type storage_type_c">void</span> enqueue(T value) {

        <span class="comment comment_line comment_line_double-slash comment_line_double-slash_c++">// Create a new node to be inserted in the linked list, and set the</span>

        <span class="comment comment_line comment_line_double-slash comment_line_double-slash_c++">// next node to NULL.</span>
        node_t *node = new node_t();
        node-&gt;value = value;
        node-&gt;next.node = <span class="constant constant_language constant_language_c">NULL</span>;
        pointer_t tail;

        <span class="comment comment_line comment_line_double-slash comment_line_double-slash_c++">// Keep trying until enqueue is done.</span>
        <span class="keyword keyword_control keyword_control_c">while</span><span class="meta meta_function meta_function_c"> (<span class="constant constant_language constant_language_c">true</span>)</span> {
            <span class="comment comment_line comment_line_double-slash comment_line_double-slash_c++">// Read the current tail which may either point to the last node</span>

            <span class="comment comment_line comment_line_double-slash comment_line_double-slash_c++">// or to the second to last node (not sure why second to last,</span>
            <span class="comment comment_line comment_line_double-slash comment_line_double-slash_c++">// and not any other node).</span>
            tail = this-&gt;tail;

            <span class="comment comment_line comment_line_double-slash comment_line_double-slash_c++">// Reads the next node after the tail which will be the last node</span>
            <span class="comment comment_line comment_line_double-slash comment_line_double-slash_c++">// if null.</span>
            pointer_t next;
            <span class="keyword keyword_control keyword_control_c">if</span><span class="meta meta_function meta_function_c"> (tail.node != <span class="constant constant_language constant_language_c">NULL</span>)</span> {
                next = tail.node-&gt;next;
            }

            <span class="comment comment_line comment_line_double-slash comment_line_double-slash_c++">// Loop if another thread changed the tail since we last read it.</span>

            <span class="keyword keyword_control keyword_control_c">if</span><span class="meta meta_function meta_function_c"> (tail.equals(this-&gt;tail)</span> == <span class="constant constant_language constant_language_c">false</span>) {
                <span class="keyword keyword_control keyword_control_c">continue</span>;
            }

            <span class="comment comment_line comment_line_double-slash comment_line_double-slash_c++">// If next is not pointing to the last node try to swing tail to</span>
            <span class="comment comment_line comment_line_double-slash comment_line_double-slash_c++">// the last node and loop.</span>
            <span class="keyword keyword_control keyword_control_c">if</span><span class="meta meta_function meta_function_c"> (next.node != <span class="constant constant_language constant_language_c">NULL</span>)</span> {
                compare_and_swap(&amp;this-&gt;tail, &amp;tail,
                    pointer_t(next.node, tail.count + <span class="constant constant_numeric constant_numeric_c">1</span>));
                <span class="keyword keyword_control keyword_control_c">continue</span>;
            }

            <span class="comment comment_line comment_line_double-slash comment_line_double-slash_c++">// Try to link node at the end of the linked list.</span>

            <span class="keyword keyword_control keyword_control_c">if</span> (compare_and_swap(&amp;tail.node-&gt;next, &amp;next,
                    pointer_t(node, next.count + <span class="constant constant_numeric constant_numeric_c">1</span>))) {
                <span class="comment comment_line comment_line_double-slash comment_line_double-slash_c++">// Enqueueing is done.</span>
                <span class="keyword keyword_control keyword_control_c">break</span>;
            }
        }

        <span class="comment comment_line comment_line_double-slash comment_line_double-slash_c++">// Enqueue is done, try to swing tail to the inserted node.</span>
        compare_and_swap(&amp;this-&gt;tail, &amp;tail,
            pointer_t(node, tail.count + <span class="constant constant_numeric constant_numeric_c">1</span>));
    }

    <span class="storage storage_type storage_type_c">bool</span> dequeue(T *value) {
        pointer_t head;

        <span class="comment comment_line comment_line_double-slash comment_line_double-slash_c++">// Keep trying until dequeue is done.</span>

        <span class="keyword keyword_control keyword_control_c">while</span>(<span class="constant constant_language constant_language_c">true</span>) {
            head = this-&gt;head;
            pointer_t tail = this-&gt;tail;
            pointer_t next = head.node-&gt;next;

            <span class="keyword keyword_control keyword_control_c">if</span><span class="meta meta_function meta_function_c"> (head.equals(this-&gt;head)</span> == <span class="constant constant_language constant_language_c">false</span>) {
                <span class="keyword keyword_control keyword_control_c">continue</span>;
            }

            <span class="comment comment_line comment_line_double-slash comment_line_double-slash_c++">// If queue is empty, or if tail is falling behind.</span>

            <span class="keyword keyword_control keyword_control_c">if</span><span class="meta meta_function meta_function_c"> (head.node == tail.node)</span> {
                <span class="comment comment_line comment_line_double-slash comment_line_double-slash_c++">// If queue is empty.</span>
                <span class="keyword keyword_control keyword_control_c">if</span><span class="meta meta_function meta_function_c"> (next.node == <span class="constant constant_language constant_language_c">NULL</span>)</span> {
                    <span class="keyword keyword_control keyword_control_c">return</span> <span class="constant constant_language constant_language_c">false</span>;
                }
                <span class="comment comment_line comment_line_double-slash comment_line_double-slash_c++">// Tail is falling behind, advance it.</span>

                compare_and_swap(&amp;this-&gt;tail,
                    &amp;tail,
                    pointer_t(next.node, tail.count + <span class="constant constant_numeric constant_numeric_c">1</span>));
            } <span class="keyword keyword_control keyword_control_c">else</span> {
                <span class="comment comment_line comment_line_double-slash comment_line_double-slash_c++">// Read value before CAS, otherwise another</span>
                <span class="comment comment_line comment_line_double-slash comment_line_double-slash_c++">// dequeue might advance it.</span>
                *value = next.node-&gt;value;
                <span class="keyword keyword_control keyword_control_c">if</span> (compare_and_swap(&amp;this-&gt;head, &amp;head,
                    pointer_t(next.node, head.count + <span class="constant constant_numeric constant_numeric_c">1</span>))) {
                    <span class="keyword keyword_control keyword_control_c">break</span>;
                }
            }
        }
        delete head.node;
        <span class="keyword keyword_control keyword_control_c">return</span> <span class="constant constant_language constant_language_c">true</span>;
    }
};


<span class="meta meta_preprocessor meta_preprocessor_c">#<span class="keyword keyword_control keyword_control_import keyword_control_import_c">endif</span></span> /* LOCK_FREE_QUEUE_H */
</pre>
</pre>
</td>
</tr>
</tbody>
</table>
</div>
<p>&nbsp;</p>]]></description><wfw:commentRss>http://michael.bebenita.com/imported-20100930232226/rss-comments-entry-9061012.xml</wfw:commentRss></item></channel></rss>