It's been a while since my last post and I've spent the last year working on lots of cool stuff at Valve. I worked on the item importer for Team Fortress 2, helped with the Linux Steam launch, and am currently working on very cool unannounced tech, which I'm looking forward to sharing with people soon.
In the meantime, I'm lucky enough to be sitting across from Bruce Dawson, who is one of the most amazing people I know when it comes to crash analysis, performance analysis, and "what the heck is my system doing?" His blog is full of all sorts of interesting things, and you can check it out at http://randomascii.wordpress.com/ It also turns out that he was the author of the GDC talk that I saw a few years ago which got me interested (and afraid of) lockless programming. It's a small world! :)
So occasionally I like to break out of what I'm doing day to day and catch up on SDL work, which always has different and interesting challenges.
This week I went and fixed a bug in SDL regarding OpenGL context caching. An OpenGL context is made "current" on a given thread, and each thread can have a different context active. SDL was caching the current context in a global variable, which is completely wrong. What I really needed here was thread-local storage.
No problem, I'll just add it to SDL!
It turns out that each supported OS has the concept of thread-local storage, and all I need is a single slot of OS thread-local storage to implement a full set of SDL based thread-local storage. However, by its very nature, multiple threads may allocate from it simultaneously and I needed an efficient way to make sure only a single thread allocates the first OS slot.
No problem, SDL already has spinlocks which are a good candidate for quick initialization like this:
static DWORD thread_local_storage = TLS_OUT_OF_INDEXES; SDL_TLSData * SDL_SYS_GetTLSData() { if (thread_local_storage == TLS_OUT_OF_INDEXES) { static SDL_SpinLock lock; SDL_AtomicLock(&lock); if (thread_local_storage == TLS_OUT_OF_INDEXES) { thread_local_storage = TlsAlloc(); } SDL_AtomicUnlock(&lock); } return (SDL_TLSData *)TlsGetValue(thread_local_storage); }
static DWORD thread_local_storage = TLS_OUT_OF_INDEXES; SDL_TLSData * SDL_SYS_GetTLSData() { if (thread_local_storage == TLS_OUT_OF_INDEXES) { static SDL_SpinLock lock; SDL_AtomicLock(&lock); if (thread_local_storage == TLS_OUT_OF_INDEXES) { DWORD storage = TlsAlloc(); SDL_MemoryBarrierRelease(); thread_local_storage = storage; } SDL_AtomicUnlock(&lock); } SDL_MemoryBarrierAcquire(); return (SDL_TLSData *)TlsGetValue(thread_local_storage); }
I've been thinking a lot about what's most important to me in new opportunities...
I want to learn new skills, be a part of a talented and healthy team, and work on great things in the games industry. But as I realized a year ago, a huge priority for me is also to have a happy home life along with my professional life.
So I haven't blogged in the past year, since I was busy at 38 Studios and much of what I was working on was confidential. Suffice it to say that it was indeed my dream job. Working with good friends on an amazing project with the full support of the company and a great work/life balance.
Lots of people have talked about what happened at the company, so I'll just give the short and sweet version...
The company ran out of money, and instead of spending the last of it gracefully shutting down, they prayed for a miracle that never happened. Yes, I believe the governor's press statements didn't help, but the company was so close to zero that an extra month of operation may not have been enough. The story is tragic, and gets more so each week as more details come to light.
But, I am thankful for one of the best game development years of my life, and I am amazed and happy at how we, our friends, and the industry as a whole have been so supportive. We intended to change the world with our game, but we are changing the world through our shared bond and the knowledge of what we had, what we did, and what can be. I feel like I have a family that will last for the rest of our lives, spreading throughout the industry and beyond.
Thank you!
So once I decided not to start my own company, I started looking around for a company in the games industry that really had employees, and more importantly, employees' families at the forefront of their priorities. I wanted a place I could raise kids, have a life, and make great games with my friends.
38 Studios stands head and shoulders above every other company in the industry in this respect. 38 Studios was founded by Curt Schilling, former Red Sox pitcher, and his passion is building teams. He knows that to create a great game you have to have great people, and treat them and their families well.
Curt will move heaven and earth for his family, and if you're on his team, you're family. Rarely have I met anyone as genuinely passionate and good hearted as he is. He has strong opinions, but he is also wise enough to trust his team and value their opinions and expertise.
When I came to visit I was very impressed by everyone I met. They are smart, nice, and honest with themselves and each other. They realize they have a tremendous task in front of them, creating a company, creating a great game, creating a world. But each time I've seen them approach a problem, I've seen them solve it in innovative and great ways. Each time I've seen them choose between the expedient path and what's right for the team, they've done what's right for the team. Not only does that hold true at the top levels, but that attitude, flexibility and self-empowerment is there in everyone I talk to.
38 Studios faces challenges, for sure. They are trying to do something that hasn't been done before. They are a privately owned company making an MMO, and trying to do it in a healthy sustainable way. They are growing quickly and trying to prepare themselves for the transition to a service organization while at the same time creating a quality content game. They are trying to find the right formula and the right people and they're trying to do it in a realistic time frame.
Can they do it? I don't know. But I am incredibly motivated to help them try, because if they can pull this off, this is my dream come true. A great company, great people, and a quality life for my family and me.
My first day starts tomorrow. Wish me luck! :)
In 2008 I created Galaxy Gameworks as a small LLC to handle commercial licensing of Simple DirectMedia Layer on the iPhone. Since then I have been maintaining it at a minimal level in my spare time to make it possible for people to use SDL on iOS and other embedded platforms.
As soon as I made the concrete decision to leave Blizzard, my brain started exploding with ideas on how to expand Galaxy Gameworks into a full blown games middleware company. My vision was to build the company into three pillars each designed to help people make games:
Blizzard doesn't tend to be very open about how and why people move around at the company, and I still have people asking me what happened, so for those interested I thought I'd share my story.
As far as I know, nothing I'm writing here is confidential, so please tell me if that's not the case.
To understand something of the story, I really should start at the beginning...
After the Wrath of the Lich King launch, I was looking to move on to something new. I loved Blizzard and I didn't want to leave the company, but I wanted to work on a smaller team, a smaller project, with more responsibility. Unexpectedly, a job opened up for technical lead on a small unannounced project that was getting underway. It was a small project, with a short timeframe, at least for Blizzard, and it was a project that I personally was very interested in. I applied, and at the beginning of 2009 I was the engineering lead on a new unannounced Blizzard game!
I was very excited, and spent the first few months researching engine technology, getting some of the infrastructure set up, and worked with the producer on a detailed technical plan for the project. After that, since the designers had a solid idea for the game and were making great progress in the prototype, I started work on proving out the gameplay systems and hiring the most urgent engineering positions.
Come July 2009, the company sounded a call to arms to help Battle.net get ready for the Starcraft II launch. There was much to do and each team loaned a person or two to help out. Since our team was so small, most of us went to help out, including our producer and UI artist. We kept a small crew of designers, an artist and our newly hired graphics engineer to keep the project alive during our tour of duty on Battle.net.
We kicked butt, took names, and Starcraft II launched smoothly a year later with Battle.net 2.0.
During the course of that year, our producer was promoted to lead Battle.net producer, and the designers made great strides in evolving the game design and the gameplay model. The graphics engineer had completed the core of the graphics engine and was roughing out the tools pipeline and content creation system.
When we returned from Battle.net, I jumped right on getting our AI/gameplay engineer up to speed on the gameplay systems and started the herculean task of migrating code I had already finished over to the new gameplay model and tools systems that were developed while we were gone.
Three months later that work was being wrapped up, and I was called in to a meeting and told that there were issues on the team. I had just worked out some issues with my gameplay engineer, but there were issues beyond that, which they needed to follow up on to give me concrete feedback.
Over the next week I implemented a great suggestion from them which improved my communication, our part-time producer led technology goal and milestone meetings which I had requested and were very helpful, I worked on a clear division of labor and responsibility among the team, and I had one-on-one meetings with all of my engineers to see how things were going. As far as I could tell morale was improved, things were going great with my gameplay engineer, and we had a clear plan for the future.
I was feeling great, and was told that even though things had improved, there were still unspecified issues that were being looked into. The meeting was very uncomfortable as though something were being danced around, so I mentioned that if the leadership of the team thought it was best, I was willing to step down. The meeting suddenly relaxed and I got nervous.
A few days later, I was asked to interview someone for technical leadership of the team! I asked for the chance to talk to whoever was having problems, and was told that would be awkward. A few days after that it was confirmed that they had already made the decision to have me step down, but that the team leadership was there to help me succeed however they could.
So after a little primal scream therapy, I sat down and really thought about what went right, what went wrong, and what really was important to me.
One of the factors that I think contributed was that we hired really experienced people in the industry, and I hadn't had experience building a small game from the ground up. Instead of mandating how things should be done, or bringing a set of best practices, I tried to work collaboratively to help us develop the best tools and approaches for our unique needs. I think this was a creative and good way for us to go given our talents, but I think it increased stress and was possibly interpreted as weakness on a team already feeling time pressure.
So, assuming that was a factor, and since I honestly did not know how other game companies work, I set out on a quest to tour the industry and learn as much as I could about how companies other than Blizzard operate. I was interested in everything including technology, development processes, team composition, project planning, and business relationships.
I want to stop here and thank everyone who helped me in this quest. Lots of companies opened their doors and under NDA showed me everything that I wanted to learn about. Blizzard management too, went way above and beyond the call of duty and introduced me to people who could help me learn about the industry. I learned that there are a ton of great companies out there, and lots of good ways to approach things. Thank you all! <3 :)
Armed with a good perspective on the industry and how games are developed, I looked back over how we set up our project and came to the conclusion that it was entirely reasonable, from a tech perspective, and felt comforted.
With that awesome tour completed, I turned my attention back to my future. My options were to stay on the team as an engineer, transition to another team at Blizzard, leave and start my own venture, or join another company in the industry.
I realized I didn't really want to stay on my team, because even though I love the team and I still think it's an awesome project, I didn't feel like I got any support in that role from management, and I never was told why I was asked to step down. I don't feel like it was personal, that the management really was trying to do the right thing for the team, but I still didn't want to stay in that environment.
I thought a lot about joining another team at Blizzard, or joining another company, but I realized that after launching Wrath of the Lich King, booting engineering on a new team, and shipping new Battle.net on StarCraft II, all in less than two years, I was starting to feel burned out. I also looked at myself and realized that sometime in the last ten years I had become a family man, and what I wanted to do more than anything else in the world was spend time with my family.
I took a deep breath and relaxed, feeling my shoulders unclench and my face break out in a smile, and knew I had made the right decision.
Every once in a while you see something that takes you completely out of your day to day routine and reminds you that this world is a wide and wonderful place.
Spider Robinson is one of my all time favorite authors, and he has been entertaining me and teaching me about love and laughter most of my life. I finished the Callahan series (again) tonight, and was touched by the plight of Doc Webster, who passed on with a brain tumor in the final story. I finally noticed in the back of the book contact information for the American Brain Tumor Association:
http://www.abta.org/
I got on the 'Net and found his website and found that his wife had passed on, in peace, almost a year ago and was able to piece together a little of what his world has been like since then. He is a brave man with a wonderful family. I'm lucky that I haven't had to deal with the death of someone close to me yet, but this world is richly textured, and people deal with pain and loss and anger and joy and triumph every single day around the world.
If you haven't seen his work, you can check it out in traditional and audio form here:
http://www.spiderrobinson.com/works.html
To all of you who are dealing with and have dealt with loss, I wish you the best, and while I know nothing can ever replace those you've loved, there are still more out there who have loved you, love you now, and will bring you joy in the future.
*HUG*
--Sam
GDC was really great this year. It was interesting watching the interplay between different companies and people, and the ebb and flow throughout the show. I got some great feedback on the business plan, got to pal around the show with an old friend of mine from high school, and learned a lot about presentation. (note to self: Comic Sans is a terrible font for professional presentation) :)
Aside from the great contacts and fun meeting people, the highlights of the show for me were the exciting things happening on the Indie scene. Unity had a great booth overflowing with people the whole time, GameSpy announced their Indie technology program, and it seems like every major technology developer has free or extremely inexpensive licensing for independent developers.
Of course each year I really enjoy the Independent Games Festival finalists. The booth was packed the whole show, so I didn't get to try everything, but there were a few that really stood out in my mind:
Solace - http://solacegame.com
Solace is a blend between top down arcade shooter action and the creative experience of emotion and sound. I've never seen anything quite like it before, it's definitely worth checking out.
Helsing's Fire - http://www.ratloop.com/?games/helsings_fire
Helsing's Fire has a unique art style and interesting gameplay. I haven't played all the way through it, but it uses the tried and true method of introducing new elements as the game progresses, so the gameplay is constantly evolving.
Oh, before I forget... I met the developers of Angry Birds! Wooo! :)
I have lots of followup e-mails to send, so I'm heading out, but until next time... Cheers! :)
For business purposes I need a contact phone and FAX number, but I don't want to have the cost and equipment requirements of a separate phone line. Given that I'm creating a virtual office, I figured there must be inexpensive and flexible ways of setting up a business phone system.
Google to the rescue, again!
Actually there are lots and lots of options available with different pricing schemes and features, but given that I'm using Google for business e-mail and document sharing, and Google Voice is free, it's a natural fit:
http://www.google.com/voice
It's a little awkward to call from - it requires you to enter the number you want to call and which phone you want to call from and then it calls both and connects the two, but it's freakin' awesome as an incoming voice message system. You can route your virtual number to any phone you want, and if it goes to voice-mail, Google will take a message, transcribe it to text, and send the message to you via SMS and e-mail.
So, now that voice is taken care of, I need a solution for FAX. Again, there are lots of options available, but given that I plan to use e-mail for most outbound communication and FAX only for receiving signed license documents, OneSuite turns out to be a good option:
http://www.onesuite.com/
You have to create an account and "charge" it with $10, but once you do that, you can activate a FAX number for unlimited incoming transmissions for $1/month. If you want outgoing FAX capability, or want the incoming FAX messages to go to multiple e-mail addresses, you can upgrade to $2.95/month + 2.5c/page for outgoing transmissions.
Business phone and FAX solution implemented. Total cost? $1/month. Pure win? Priceless.
When you're working with other people's code, you don't always have the source, and sometimes this means you have to dive into the assembly code to figure out what's going on.
If you're lucky, there will be a small function where there are well defined inputs and outputs and you can see what's going on...
In this case someone reported a bug where if they created two different streaming textures, the program would crash deep inside glTexSubImage2D() in a routine called gleCopy(). They helpfully sent a small test program, and sure enough, it crashed on my iMac.
So, not having any leads, I start stepping through the assembly code:
...
0x0424a3fc <gleCopy+88>: mtctr r4
0x0424a400 <gleCopy+92>: rlwinm r0,r9,2,0,29
0x0424a404 <gleCopy+96>: addi r9,r9,1
0x0424a408 <gleCopy+100>: lwzx r2,r11,r0
0x0424a40c <gleCopy+104>: stwx r2,r3,r0
0x0424a410 <gleCopy+108>: bdnz+ 0x424a400 <gleCopy+92>
...
0x0424a464 <gleCopy+192>: add r11,r11,r7
0x0424a46c <gleCopy+200>: add r3,r3,r8
...
while (rows_to_copy) { for (i = 0; i < count; ++i) { dst[i] = src[i]; } src += src_pitch; dst += dst_pitch; }
This past month I've been working with a friend of mine, Shawn Bellah, on rebranding Galaxy Gameworks in preparation for GDC. This is a kind of big deal, because this is the birth of the company in the public eyes, and I want to make sure it's as fun and professional as possible.
One of the important pieces of this process is the logo. The logo should be distinctive, simple, and say something awesome about your company.
We went through a very iterative process during the logo development. We started with a bunch of brainstorming sketches that Shawn had done, where we looked at different ideas to use with the logo. We liked the double-G play in the company name, and geeked out on the space theme. We looked at lots of pictures of galaxies and nebulae, and tried a few different ideas like interlocking G's, where the top of each G was an arm of a spiral galaxy, another where the G was the center of a galaxy seen edge on, another where the G was made of welded steel plates surrounding a galactic center, etc.
But I kept coming back to the first sketch that he had done to warm up, which was a solar system with the planets aligned to form the cross-bar of the G. I felt a connection there, and I wanted to go explore the planets, and above all it just looked interesting.
So he played around with that idea, creating a galaxy suggestive of the letter G with some larger stellar objects to create interest:
I'm tracking down a crash in SDL on the iPhone, and the path is not yet clear to me, but I thought some people would enjoy the view along the way.
The crash itself happens only on the real phone, not on the simulator, and it's a crash initializing an SDL_uikitopenglview, which is a view deriving from SDL_uikitview, which in turn derives from UIView.
The callstack for the crash looks like this:
_class_getMeta ()
_class_isInitialized ()
_class_initialize ()
...
objc_msgSend_uncached ()
UIKit_GL_CreateContext () at SDL_uikitopengles.m:146
Of course everything past my code is ARM assembly, which makes it a little tricky to debug. Luckily Apple has published the source to their Objective C runtime, so I can disassemble the functions using gdb and follow along:
http://www.opensource.apple.com/source/objc4/objc4-437.1
First, there's a couple useful things to know if you're poking around at this level:
The ARM calling conventions are that registers r0 through r3 are for parameters passed into functions, and they correspond to parameters from the left to the right. The return value of the function is also passed back through r0.
The Xcode debugging window has a nice interface with the code right there along with the local variables and registers. On the far right is a button to bring up the gdb console where you can do some pretty advanced things.
gdb quick reference:
b <name> - set a breakpoint at the beginning of the named function
s - go to the next line of code, stepping into function calls
n - go to the next line of code, skipping over function calls
si - go to the next assembly instruction
fin - run until the function returns
c - continue running until the next breakpoint
p <var> - print the value of a variable or register (e.g. $r0, $r1, etc.)
x <address> - lookup the symbol associated with an address
display <var> - print the value of a variable or register after each command
list - list the code around the current execution
Most of these we don't need since the Xcode UI is pretty nice, but a really handy one is 'si', since that will let us step into the assembly and then use the UI to continue tracing the execution.
So first, I set a breakpoint at the line that crashes:
view = [[SDL_uikitopenglview alloc]
Then, I bring up the gdb console and use the 'si' command a few times until I get into assembly, just to see what things look like:
static class_t *getNonMetaClass(class_t *cls) { rwlock_assert_locked(&runtimeLock); if (isMetaClass(cls)) { cls = NXMapGet(uninitializedClasses(), cls); } return cls; }which means that somehow the class for my view didn't get into the map of classes that my program has loaded.
void print_classes() { int i, numClasses; Class * classes; numClasses = objc_getClassList(NULL, 0); classes = malloc(sizeof(Class) * numClasses); numClasses = objc_getClassList(classes, numClasses); for (i = 0; i < numClasses; ++i) { char *name = class_getName(classes[i]); if (SDL_strstr(name, "SDL_")) { name; // Yay, found it! } } free(classes); }Sure enough, when I run it on the simulator I find the SDL view classes, and when I run it on the device they don't show up. If I use nm on the application binary in the app folder, I see the classes are there, in both the simulator and device binaries:
Video decoding is something that people are always trying to find ways to accelerate. Whether it's making HD video more HD or dynamically streaming video to textures in your game, we want it as fast and high quality as possible.
MPEG based codecs have basically two steps which are time consuming, the first is decoding each frame into a YUV colorspace image, and the second is converting the image from YUV to RGB. There is lots of information available on the MPEG stream decoding and YUV colorspaces, but here I'm going to focus on the YUV to RGB conversion.
To understand how to accelerate this process, we need to understand a little about the YUV format and how the conversion is done.
YV12 images consist of 3 planes, one Y image sized WxH, and a U and V image, sized W/2 x H/2. Put simply, the Y plane contains the luminance, which can be used alone for grayscale, and the U and V planes contain the red and blue color components, one value for each 2x2 block of output pixels.
The formula for converting from YUV to RGB is:
R = 1.164(Y - 16) + 1.596(V - 128) G = 1.164(Y - 16) - 0.813(V - 128) - 0.391(U - 128) B = 1.164(Y - 16) + 2.018(U - 128)
varying vec2 tcoord; uniform sampler2D tex0; // Y uniform sampler2D tex1; // U uniform sampler2D tex2; // V // YUV offset const vec3 offset = vec3(-0.0625, -0.5, -0.5); // RGB coefficients const vec3 Rcoeff = vec3(1.164, 0.000, 1.596); const vec3 Gcoeff = vec3(1.164, -0.391, -0.813); const vec3 Bcoeff = vec3(1.164, 2.018, 0.000); void main() { vec3 yuv, rgb; // Get the Y value yuv.x = texture2D(tex0, tcoord).r; // Get the U and V values tcoord *= 0.5; yuv.y = texture2D(tex1, tcoord).r; yuv.z = texture2D(tex2, tcoord).r; // Do the color transform yuv += offset; rgb.r = dot(yuv, Rcoeff); rgb.g = dot(yuv, Gcoeff); rgb.b = dot(yuv, Bcoeff); // That was easy. :) gl_FragColor = vec4(rgb, 1.0); }
I was recently asked how to use streaming textures with SDL 1.3, and while it's very simple, I didn't actually find any documentation on how to do it, so here it is!
First, why would you use a streaming texture?
Static textures are designed for sprites and backgrounds and other images that don't change much. You update them with pixels using SDL_UpdateTexture().
Streaming textures are designed for things that update frequently, every few seconds, or every frame. You can also update them with SDL_UpdateTexture(), but for optimal performance you lock them, write the pixels, and then unlock them.
Conceptually they're very simple to use:
I just added GLSL shaders to the SDL OpenGL rendering implementation.
On my hardware this ends up being about a 200-400 FPS increase in testsprite2:
SDL_RENDER_OPENGL_SHADERS=0 ./testsprite2
4259.55 frames per second
SDL_RENDER_OPENGL_SHADERS=1 ./testsprite2
4552.88 frames per second
I also got a modest increase with testsprite, using the old SDL 1.2 API:
SDL_RENDER_OPENGL_SHADERS=0 ./testsprite
1329.16 frames per second
SDL_RENDER_OPENGL_SHADERS=1 ./testsprite
1354.20 frames per second
Woot! :)
I also noticed there's not a single example of using shaders with SDL that has full source code, so I added one:
http://hg.libsdl.org/SDL/file/default/test/testshader.c
Enjoy! :)
In my recent SDL 1.3 update I made it possible for the old SDL 1.2 API to be accelerated using texture streaming.
On my system on Mac OS X and Linux, this doubled performance!
Mac OS X
I've been quiet the last week working on a massive restructuring of the SDL rendering API. The result is a simpler, easier to use, and easier to port system.
http://forums.libsdl.org/viewtopic.php?t=6869&start=18
Whew! Time to sleep! :)
I had forgotten what the rules for pointer qualifiers were, so for the ignorant and forgetful, here they are:
The stuff to the left of the * is what the pointer points at.
The stuff to the right of the * is the qualifiers on the pointer itself.
I've been meaning to add a yield() function to the SDL API for a while. I've also been mulling around with different ideas to improve the spinlocks.
Here's the current spinlock (pseudo-code):
while (locked) delay(0)
My theory was that if the lock was contended, it's better to retry a few times before sleeping. I also figured using the OS's thread yield function was a better way of giving up the current timeslice than sleeping for 0 time.
Here was my new spinlock:
if (!locked) return
while (true)
for (i = 0; i < 4; ++i)
if (!locked) return
yield()
Astonishingly, my FIFO benchmark went from 3 seconds to 10 seconds!
Thinking about this a little, it makes sense... the CPU probably doesn't see the value change in as few cycles as the loop is, so all we're really doing is wasting time in the loop. When there's high contention, as in my FIFO test, this just means the whole system is working harder.
So, I went back to my basic spinlock, but I used a yield instead of delay(0):
while (locked) yield()
Oddly enough, my FIFO benchmark wasn't any faster, in fact it was 4 seconds instead of the original 3 seconds. Apparently on Mac OS X sched_yield() is a more expensive function than nanosleep().
My conclusion?
Always benchmark your changes, even if they're obviously an improvement, and don't be afraid to leave well enough alone. :)
In the course of my business day, I frequently have to sign licensing or tax documents and e-mail them to people.
I've been looking for a paperless way to do this, and I've found a fairly easy way to do it using the GIMP (http://www.gimp.org) and OpenOffice (http://www.openoffice.org).
Setup:
I've spent a bunch of time the past few weeks learning about lock-free and wait-free algorithms for super fast multi-threaded data structure access.
I've had two motivations for this. First, I wanted to vet the new SDL atomic API, and second I wanted to potentially increase the performance of the SDL event system.
In the course of learning about lockless programming, I've gained a healthy respect for people who are doing it correctly. It's incredibly tricky to get right, and even reviewed and published papers can have subtle bugs in them. There are a lot of subtle issues and ways multi-threaded lockless code can fail, especially on the XBox 360 where data ordering between CPUs is not guaranteed at all without expensive sync instructions.
All of this has made me wonder if it's a good idea to make a cross-platform atomic API available in the first place.
But that said, let's first see what the benefits might be...
I wrote a lock-free FIFO that modeled the behavior of the SDL event queue, and created a test that could run both lock-free and using a mutex, to simulate the way the current SDL event queue is handled. I then added a watcher thread and single spinlock to the lock-free version to simulate a thread periodically coming in and having to do heavy duty manipulation of the queue.
The test took 4 writer threads and had each of them put 1 MILLION events on the queue, and then created 4 reader threads to pull them off and process them. The queue was limited to a maximum of 256 events. I defined wait states for writers if they tried to add an event and the queue was full, and defined wait states for readers if they tried to get an event and the queue was empty.
I ran the test on a 4-core Mac Pro (with 8 hardware threads), on a mostly idle system.
The results were astonishing!
FIFO test---------------------------------------
Mode: Mutex
Finished in 37.097000 sec
Writer 0 wrote 1000000 events, had 0 waits
Writer 1 wrote 1000000 events, had 0 waits
Writer 2 wrote 1000000 events, had 0 waits
Writer 3 wrote 1000000 events, had 0 waits
Reader 0 read 999998 events, had 50 waits
Reader 1 read 1000003 events, had 23 waits
Reader 2 read 999998 events, had 13 waits
Reader 3 read 1000001 events, had 7 waits
FIFO test---------------------------------------
Mode: LockFree
Finished in 0.688000 sec
Writer 0 wrote 1000000 events, had 5308 waits
Writer 1 wrote 1000000 events, had 5263 waits
Writer 2 wrote 1000000 events, had 5348 waits
Writer 3 wrote 1000000 events, had 5314 waits
Reader 0 read 1023604 events, had 7838 waits
Reader 1 read 991976 events, had 7688 waits
Reader 2 read 981770 events, had 7700 waits
Reader 3 read 1002650 events, had 7893 waits
As you can see, the lock-free version was over 50 times faster than the mutex version!
On the other hand, the mutex protected queue was able to process an event in 9 microseconds, which is well within the design specs for the SDL event queue. :)
In conclusion, lock-free algorithms are a huge benefit for code that needs to be extremely high performance and scale well across many processors. Applications of this might be fast transaction network servers, databases, or massively parallel data processing.
Resources:
http://msdn.microsoft.com/en-us/library/ee418650%28v=vs.85%29.aspx
http://www.1024cores.net/home/lock-free-algorithms
http://www.drdobbs.com/high-performance-computing/212201163
Oh, and if you're not afraid yet, think about might what happen if you're managing dynamically allocated objects with an atomic reference count...
http://www.1024cores.net/home/
http://www.drdobbs.com/architecture-and-design/184401888
This past week was the first week in a three week sprint I'm on to get SDL in a releasable state. Realistically I know there will be some bigger tasks that will still need to be done, but I feel like I'll have most of the important smaller tasks and bugs and polish taken care of. However there's a huge amount of work to be done, and to hit this goal I've been working almost non-stop for the past week.
Ahhh, weekend!
The weekend is hugely important, and I'm planning to take my weekends as family and planning time. They give me a chance to slow down, relax, smile, and get perspective. If I'm going so fast that I don't have time to relax on the weekend, I'm doing something fundamentally wrong and need to rethink things.
Ahhhhh. :)
/goes to hug his wife...
I finished setting up Galaxy Gameworks with Google mail and apps services today, and was immediately frustrated by the inability to have my business e-mail and my personal e-mail open at the same time.
I did a little digging and found that Google has indeed made it possible to have Gmail and the core web apps open on two different accounts. However, setting it up is a little tricky.
You can turn on this feature by going to Settings -> Accounts -> Google Account settings, and then editing the “Multiple sign-in” setting. Once you have done this and refreshed your browser, you can click the downward arrow next to your account name in the upper right and log in with another gmail account.
Voila! :)
SDL serves three types of video API users: