当前位置: 首页>>代码示例>>C++>>正文


C++ SharedMemoryProcessDataHeader::SetData方法代码示例

本文整理汇总了C++中SharedMemoryProcessDataHeader::SetData方法的典型用法代码示例。如果您正苦于以下问题:C++ SharedMemoryProcessDataHeader::SetData方法的具体用法?C++ SharedMemoryProcessDataHeader::SetData怎么用?C++ SharedMemoryProcessDataHeader::SetData使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在SharedMemoryProcessDataHeader的用法示例。


在下文中一共展示了SharedMemoryProcessDataHeader::SetData方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: SharedMemoryException


//.........这里部分代码省略.........

    // Create or open the shared memory
    bool created;
    SharedMemoryProcessDataHeader *processDataHeader =
        SharedMemoryProcessDataHeader::CreateOrOpen(
            name,
            SharedMemorySharedDataHeader(SharedMemoryType::Mutex, SyncSystemVersion),
            sizeof(NamedMutexSharedData),
            createIfNotExist,
            &created);
    if (createdRef != nullptr)
    {
        *createdRef = created;
    }
    if (created)
    {
        // If the shared memory file was created, the creation/deletion file lock would have been acquired so that we can
        // initialize the shared data
        autoCleanup.m_acquiredCreationDeletionFileLock = true;
    }
    if (processDataHeader == nullptr)
    {
        _ASSERTE(!createIfNotExist);
        return nullptr;
    }
    autoCleanup.m_processDataHeader = processDataHeader;

    if (created)
    {
        // Initialize the shared data
        new(processDataHeader->GetSharedDataHeader()->GetData()) NamedMutexSharedData;
    }

    if (processDataHeader->GetData() == nullptr)
    {
    #if !NAMED_MUTEX_USE_PTHREAD_MUTEX
        // Create the lock files directory
        char lockFilePath[SHARED_MEMORY_MAX_FILE_PATH_CHAR_COUNT + 1];
        SIZE_T lockFilePathCharCount =
            SharedMemoryHelpers::CopyString(lockFilePath, 0, SHARED_MEMORY_LOCK_FILES_DIRECTORY_PATH);
        if (created)
        {
            SharedMemoryHelpers::EnsureDirectoryExists(lockFilePath, true /* isGlobalLockAcquired */);
        }

        // Create the session directory
        lockFilePath[lockFilePathCharCount++] = '/';
        SharedMemoryId *id = processDataHeader->GetId();
        lockFilePathCharCount = id->AppendSessionDirectoryName(lockFilePath, lockFilePathCharCount);
        if (created)
        {
            SharedMemoryHelpers::EnsureDirectoryExists(lockFilePath, true /* isGlobalLockAcquired */);
            autoCleanup.m_lockFilePath = lockFilePath;
            autoCleanup.m_sessionDirectoryPathCharCount = lockFilePathCharCount;
        }

        // Create or open the lock file
        lockFilePath[lockFilePathCharCount++] = '/';
        lockFilePathCharCount =
            SharedMemoryHelpers::CopyString(lockFilePath, lockFilePathCharCount, id->GetName(), id->GetNameCharCount());
        int lockFileDescriptor = SharedMemoryHelpers::CreateOrOpenFile(lockFilePath, created);
        if (lockFileDescriptor == -1)
        {
            _ASSERTE(!created);
            if (createIfNotExist)
            {
                throw SharedMemoryException(static_cast<DWORD>(SharedMemoryError::IO));
            }
            return nullptr;
        }
        autoCleanup.m_createdLockFile = created;
        autoCleanup.m_lockFileDescriptor = lockFileDescriptor;
    #endif // !NAMED_MUTEX_USE_PTHREAD_MUTEX

        // Create the process data
        void *processDataBuffer = SharedMemoryHelpers::Alloc(sizeof(NamedMutexProcessData));
        AutoFreeBuffer autoFreeProcessDataBuffer(processDataBuffer);
        NamedMutexProcessData *processData =
            new(processDataBuffer)
            NamedMutexProcessData(
                processDataHeader
            #if !NAMED_MUTEX_USE_PTHREAD_MUTEX
                ,
                lockFileDescriptor
            #endif // !NAMED_MUTEX_USE_PTHREAD_MUTEX
            );
        autoFreeProcessDataBuffer.Cancel();
        processDataHeader->SetData(processData);

        // If the mutex was created and if requested, acquire the lock initially while holding the creation/deletion locks
        if (created && acquireLockIfCreated)
        {
            MutexTryAcquireLockResult tryAcquireLockResult = processData->TryAcquireLock(0);
            _ASSERTE(tryAcquireLockResult == MutexTryAcquireLockResult::AcquiredLock);
        }
    }

    autoCleanup.m_cancel = true;
    return processDataHeader;
}
开发者ID:jamesqo,项目名称:coreclr,代码行数:101,代码来源:mutex.cpp


注:本文中的SharedMemoryProcessDataHeader::SetData方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。