Archive for December, 2009

glibc-2.7 makecontext issues on x86_64

Monday, December 28th, 2009

Looks like passing 64 bit values(e.g pointers) into makecontext is not working properly on x86_64. We are using makecontext for coroutines implementation and its proper working is vital for us.

I’ve been struggling with this bug for a couple of days and have finally found a solution for it. Actually the solution is trivial it was the actual process of spotting this bug which took so much time. By the way, this issue was resolved in the latest releases of glibc, so nothing to worry about if you are using gcc older than 4.2.4.
(more…)

Redirect build errors into vim

Thursday, December 10th, 2009

Here is a small bash function which wraps the executed command in the shell and redirects all build errors right into vim. In vim you can jump between errors using standard :cn,:cp commands(as well as view them all using :cope).

function vimize () 
{ 
  local file=/tmp/vimize.errors
  if [ "$1" != "" ] ; then
    rm $file 2> /dev/null
    $1 "$2" "$3" "$4" "$5" "$6" "$7" "$8" "$9" 2>&1 | tee $file
  fi
  grep ': error:' $file 
  if [ "$?" == "0" ] ; then
    vim -q $file -c :copen
  fi
}

Just put it into your ~/.bashrc, reload the shell and ejoy it. It can be used as follows:

$ vimize ./run_some_build_script

Using boost::threadpool with boost::future

Thursday, December 10th, 2009

Here is a small C++ function which allows you to submit async jobs into the thread pool and track their execution status using the concept of futures:

#include "boost/threadpool.hpp"
#include "boost/future.hpp"
#include "boost/utility/result_of.hpp"
#include "boost/shared_ptr.hpp"
 
template<typename Thp, typename Func>
boost::shared_future< typename boost::result_of<Func()>::type >
submit_job(Thp& thp, Func f)
{
  typedef typename boost::result_of<Func()>::type result;
  typedef boost::packaged_task<result> packaged_task;
  typedef boost::shared_ptr<boost::packaged_task<result> > packaged_task_ptr;
 
  packaged_task_ptr task(new packaged_task(f));
  boost::shared_future<result> res(task->get_future());
  boost::threadpool::schedule(thp, boost::bind(&packaged_task::operator(), task));
 
  return res;
}

A typical example of its possible usage:

User lookup_user_in_database(int id) { ... }
...
int main()
{
  boost::threadpool::pool thp(3);//number of threads
  boost::shared_future<User> future = 
    submit_job(boost::bind(lookup_user_in_database, 10));
  while(!future.is_ready())
  { 
  //do something useful
  }
  User = future.get();
  ...
}

boost::futures are now finally officially shipped with boost starting with 1.41 release. boost::threadpool is not yet an official boost library, however you can find it here.

Much kudos to authors of these amazing libraries!