有关信号量有关问题

有关信号量问题

#ifndef __LIBAPS_SEMAPHORE_HPP__
#define __LIBAPS_SEMAPHORE_HPP__

#include <base/platform.hpp>
#include <base/exception.hpp>

namespace aps 
{
    //! 信号量
    class LIBAPS_API Semaphore 
    {
    public:
        Semaphore(unsigned int initial = 0, const char* name = 0) throw(Exception);
        ~Semaphore() throw(Exception);

        void wait() throw(Exception);
        bool tryWait() throw(Exception);
        void post() throw(Exception);

    private:
        struct sem_handle_t;
        sem_handle_t* mHandle;
    };

}

#endif // !__LIBAPS_SEMAPHORE_HPP__

#include <base/semaphore.hpp>
#include <base/platform.hpp>
#include <base/charset.hpp>
#include <base/exception.hpp>
#include <base/toolkit.hpp>
#include <base/logger.hpp>

#ifdef Z_OS_LINUX

#include <fcntl.h>
#include <semaphore.h>
#include <string.h>
#include <cstdlib>
#include <errno.h>

using namespace aps;

struct Semaphore::sem_handle_t 
{
    sem_t* semaphore;
    char* name;
    bool creator;

    sem_handle_t()
    {
        name = NULL;
        semaphore = NULL;
    }
};

Semaphore::Semaphore(unsigned int initial, const char* name) throw(Exception):
mHandle(new sem_handle_t)
{
    if(name)
    {
        sem_t* sem = NULL;
        int flags = O_RDWR | O_CREAT | O_EXCL; //O_CREAT|O_EXCL;
        mHandle->creator = true;

        for (int t = 0; ; t++)
        {
            if (t>0)
                TraceX(eTRACE, _G2U("尝试重新以%d打开信号量%s"), flags, name);
            
            sem = sem_open(name, flags, 0755, initial);
            int r = errno;
            if(sem == SEM_FAILED)
            {
                if(r != EEXIST)
                {
                    logException(Z_SOURCEINFO, r, _G2U("打开信号量%s失败:[%d][%s]"), name, r, Toolkit::formatError(r).c_str());
                }
                else 
                {
                    TraceX(eTRACE, _G2U("信号量%s已存在, 尝试打开"), name);
                    flags = 0;
                    mHandle->creator = false;
                    continue;
                }
            }
            break;
        }
        
        mHandle->semaphore = sem;
        mHandle->name      = strdup(name);
    }
    else
    {
        mHandle->name = 0;
        mHandle->semaphore = new sem_t;