Archive for the 'ace' Category

在ACE中使用epoll

星期四, 六月 8th, 2006

        很显然,文章的标题决定了我们是在linux下使用ACE。我们知道ACE在linux下缺省是用select来实现Reactor的,epoll相对于select的好处这里就不再啰嗦了,我们直接讲操作步骤:
    第一:重新编译ACE库
     ACE库中通过ACE_Dev_Poll_Reactor类来支持epoll,但是ACE库缺省的安装是没有编译这个类的,我们要做的就是将ACE_Dev_Poll_Reactor编译连接到ACE库中(faint,又要重新编译ACE,在我那台破服务器上编译一次需要一个多小时).我的操作系统是Redhat linux AS4.0,ACE的版本是5.4.10。根据ACE压缩包中的ACE-INSTALL.html,我是用”Building ACE with GNU Autoconf“这种方式来安装的,安装步骤如下(很简单,就不翻译了):
       1 cd to the top-level ACE_wrappers directory.

       2.Create a subdirectory to hold your build’s configuration and built ACE version,     and   then change to the new directory:

       mkdir build

       cd build

     

       3.Note that you do not run the create_ace_build.pl utility mentioned in the Cloning the Source Tree section. The configure script takes care of creating all files and links that are needed.

Configure ACE for your platform by issuing the following command: c

       ../configure [options]

     
      4.Build ACE by typing make.

      5. Install ACE by typing make install.
      好,现在终于可以讲如何将ACE_Dev_Poll_Reactor编译到ACE库中去了。在上述的第一步和第二步之间修改ACE_wrappers/ace/config-linux.h,增加一行:#define ACE_HAS_EVENT_POLL,然后执行第2、3步,第3步../configure执行完之后,build目录下会生成一些文件和目录,打开ACE_wrappers/build/ace/config.h,增加一行:#define ACE_HAS_EVENT_POLL。然后执行第4步make和第5步make install.OK,在漫长的编译以后,支持epoll的ACE库总算完成了。

     第二:修改应用程序
        应用程序修改很简单,两行代码搞掂,在应用程序初始化时(必须是在第一次使用ACE_Reactor::instance()之间)加入:
       
        m_pDevPollReactor=new ACE_Dev_Poll_Reactor;
       ACE_Reactor::instance(new ACE_Reactor(m_pDevPollReactor));
      
       那么在后续的对ACE_Reactor::instance()的调用就是使用ACE_Dev_Poll_Reactor的实现了。
  
   第三:重新编译应用程序
  
        在应用程序的makefile中加入 -DACE_HAS_EVENT_POLL,重新make应用程序。OK,打完收工。
       

ACE中的Thread Mutex在linux下的使用

星期三, 五月 31st, 2006

ACE库中专门对线程同步提供了两个类,一个是ACE_Thread_Mutex另一个是ACE_REcursive_Thread_Mutex。 在我看 来,在linux下进行线程同步,不要使用ACE_Thread_Mutex,用ACE_REcursive_Thread_Mutex就可以了。原因很 简单,因为ACE_Thread_Mutex不支持线程重入。一旦重入(同一个线程调用两次ACE_Thread_Mutex::acquire)这个线 程就死锁了。

要搞清楚这个问题,我们需要搞清楚操作系统是如何实现线程锁的。Windows下很简单,用CRITICAL_SECTION实现。 CRITICAL_SECTION支持重入,所以Windows下的线程同步用ACE_Thread_Mutex或者 ACE_REcursive_Thread_Mutex都是一样的。而linux下不同,是用posix thread 库实现的。pthread 的mutex分为三种类型,fast,recursive,error checking,当线程调用pthread_mutex_lock时,如果是线程重入这把锁,则:

“fast” 挂起当前线程.
“resursive” 成功并立刻返回当前被锁定的次数
“error checking” 立刻返回EDEADLK

显然ACE_Thread_Mutex是用fast方式实现的。

我有多个平台 (Window,AIX ,Solaris,hp-ux,Linux)的C++多线程程序的开发经验,但是一直都没有想到一个不可重入的线程锁有什么用,用这样的锁需要太小心了, 一不小心就会死锁。所以一般情况下都需要手工写代码将它封装成一个可以重入的锁。ACE中也提供了这样一个封装,用mutex和cond实现的,代码如 下:

ACE_OS::recursive_mutex_lock (ACE_recursive_thread_mutex_t *m)
{
#if defined (ACE_HAS_THREADS)
#if defined (ACE_HAS_RECURSIVE_MUTEXES)
return ACE_OS::thread_mutex_lock (m);
#else
ACE_thread_t t_id = ACE_OS::thr_self ();
int result = 0;

// Acquire the guard.
if (ACE_OS::thread_mutex_lock (&m->nesting_mutex_) == -1)
result = -1;
else
{
// If there’s no contention, just grab the lock immediately
// (since this is the common case we’ll optimize for it).
if (m->nesting_level_ == 0)
m->owner_id_ = t_id;
// If we already own the lock, then increment the nesting level
// and return.
else if (ACE_OS::thr_equal (t_id, m->owner_id_) == 0)
{
// Wait until the nesting level has dropped to zero, at
// which point we can acquire the lock.
while (m->nesting_level_ > 0)
ACE_OS::cond_wait (&m->lock_available_,
&m->nesting_mutex_);

// At this point the nesting_mutex_ is held…
m->owner_id_ = t_id;
}

// At this point, we can safely increment the nesting_level_ no
// matter how we got here!
m->nesting_level_++;
}

{
// Save/restore errno.
ACE_Errno_Guard error (errno);
ACE_OS::thread_mutex_unlock (&m->nesting_mutex_);
}
return result;
#endif /* ACE_HAS_RECURSIVE_MUTEXES */
#else
ACE_UNUSED_ARG (m);
ACE_NOTSUP_RETURN (-1);
#endif /* ACE_HAS_THREADS */
}

这个封装是用在那些posix thread库不支持recursive mutex的平台上的。如果posix thread支持recursive ,那么直接用pthread_mutex_lock就可以了。而在ACE环境下,直接使用ACE_REcursive_Thread_Mutex,忘记 ACE_Thread_Mutex的存在。