dss源码分析1:OSMutex

OSMutex 内部通过当前线程id来判断是否多次锁定,fHolder记录的当前线程id, fHolderCount代表锁定次数。
RecursiveTryLock 如果没有锁定,直接锁定。如果已经锁定,直接退出。
OSMutexLocker内部维护了OSMutex指针,在构造函数锁定,在析构函数释放。实用时比较方便。

/*
 *
 * @APPLE_LICENSE_HEADER_START@
 * 
 * Copyright (c) 1999-2003 Apple Computer, Inc.  All Rights Reserved.
 * 
 * This file contains Original Code and/or Modifications of Original Code
 * as defined in and that are subject to the Apple Public Source License
 * Version 2.0 (the 'License'). You may not use this file except in
 * compliance with the License. Please obtain a copy of the License at
 * http://www.opensource.apple.com/apsl/ and read it before using this
 * file.
 * 
 * The Original Code and all software distributed under the License are
 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
 * Please see the License for the specific language governing rights and
 * limitations under the License.
 * 
 * @APPLE_LICENSE_HEADER_END@
 *
 */
/*
    File:       OSMutex.h

    Contains:   Platform - independent mutex header. The implementation of this object
                is platform - specific. Each platform must define an independent
                OSMutex.h & OSMutex.cpp file.
                
                This file is for Mac OS X Server only

    

*/

#ifndef _OSMUTEX_H_
#define _OSMUTEX_H_

#include <stdlib.h>
#include "SafeStdLib.h"
#ifndef __Win32__
#include <sys/errno.h>
    #if __PTHREADS_MUTEXES__
        #if __MacOSX__
            #ifndef _POSIX_PTHREAD_H
                #include <pthread.h>
            #endif
        #else
            #include <pthread.h>
        #endif
        #include <unistd.h>
        
    #else
        #include "mymutex.h"
    #endif
#endif

#include "OSHeaders.h"
#include "OSThread.h"
#include "MyAssert.h"

class OSCond;

class OSMutex
{
    public:

        OSMutex();
        ~OSMutex();

        inline void Lock();
        inline void Unlock();
        
        // Returns true on successful grab of the lock, false on failure
        inline Bool16 TryLock();

    private:

#ifdef __Win32__
        CRITICAL_SECTION fMutex;
        
        DWORD       fHolder;
        UInt32      fHolderCount;
        
#elif !__PTHREADS_MUTEXES__
        mymutex_t fMutex;
#else
        pthread_mutex_t fMutex;
        // These two platforms don't implement pthreads recursive mutexes, so
        // we have to do it manually
        pthread_t   fHolder;
        UInt32      fHolderCount;
#endif

#if __PTHREADS_MUTEXES__ || __Win32__       
        void        RecursiveLock();
        void        RecursiveUnlock();
        Bool16      RecursiveTryLock();
#endif
        friend class OSCond;
};

class   OSMutexLocker
{
    public:

        OSMutexLocker(OSMutex *inMutexP) : fMutex(inMutexP) { if (fMutex != NULL) fMutex->Lock(); }
        ~OSMutexLocker() {  if (fMutex != NULL) fMutex->Unlock(); }
        
        void Lock()         { if (fMutex != NULL) fMutex->Lock(); }
        void Unlock()       { if (fMutex != NULL) fMutex->Unlock(); }
        
    private:

        OSMutex*    fMutex;
};

void OSMutex::Lock()
{
#if __PTHREADS_MUTEXES__ || __Win32__
    this->RecursiveLock();
#else
    mymutex_lock(fMutex);
#endif //!__PTHREADS__
}

void OSMutex::Unlock()
{
#if __PTHREADS_MUTEXES__ || __Win32__
    this->RecursiveUnlock();
#else
    mymutex_unlock(fMutex);
#endif //!__PTHREADS__
}

Bool16 OSMutex::TryLock()
{
#if __PTHREADS_MUTEXES__ || __Win32__
    return this->RecursiveTryLock();
#else
    return (Bool16)mymutex_try_lock(fMutex);
#endif //!__PTHREADS__
}

#endif //_OSMUTEX_H_

/*
 *
 * @APPLE_LICENSE_HEADER_START@
 * 
 * Copyright (c) 1999-2003 Apple Computer, Inc.  All Rights Reserved.
 * 
 * This file contains Original Code and/or Modifications of Original Code
 * as defined in and that are subject to the Apple Public Source License
 * Version 2.0 (the 'License'). You may not use this file except in
 * compliance with the License. Please obtain a copy of the License at
 * http://www.opensource.apple.com/apsl/ and read it before using this
 * file.
 * 
 * The Original Code and all software distributed under the License are
 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
 * Please see the License for the specific language governing rights and
 * limitations under the License.
 * 
 * @APPLE_LICENSE_HEADER_END@
 *
 */
/*
    File:       OSMutex.cpp

    Contains:   Platform - independent mutex header. The implementation of this object
                is platform - specific. Each platform must define an independent
                QTSSMutex.h & QTSSMutex.cpp file.
                
                This file is for Mac OS X Server only

    

*/

#include "OSMutex.h"
#include <stdlib.h>
#include "SafeStdLib.h"
#include <string.h>

// Private globals
#if __PTHREADS_MUTEXES__
    static pthread_mutexattr_t  *sMutexAttr=NULL;
    static void MutexAttrInit();
    
    #if __solaris__
            static pthread_once_t sMutexAttrInit = {PTHREAD_ONCE_INIT};
    #else
            static pthread_once_t sMutexAttrInit = PTHREAD_ONCE_INIT;
    #endif
    
#endif

OSMutex::OSMutex()
{
#ifdef __Win32__
    ::InitializeCriticalSection(&fMutex);
    fHolder = 0;
    fHolderCount = 0;
#elif __PTHREADS_MUTEXES__
    (void)pthread_once(&sMutexAttrInit, MutexAttrInit);
    (void)pthread_mutex_init(&fMutex, sMutexAttr);
    
    fHolder = 0;
    fHolderCount = 0;
#else
    fMutex = mymutex_alloc();
#endif
}

#if __PTHREADS_MUTEXES__
void MutexAttrInit()
{
    sMutexAttr = (pthread_mutexattr_t*)malloc(sizeof(pthread_mutexattr_t));
    ::memset(sMutexAttr, 0, sizeof(pthread_mutexattr_t));
    pthread_mutexattr_init(sMutexAttr);
}
#endif

OSMutex::~OSMutex()
{
#ifdef __Win32__
    ::DeleteCriticalSection(&fMutex);
#elif __PTHREADS_MUTEXES__
    pthread_mutex_destroy(&fMutex);
#else
    mymutex_free(fMutex);
#endif
}

#if __PTHREADS_MUTEXES__ || __Win32__
void        OSMutex::RecursiveLock()
{
    // We already have this mutex. Just refcount and return
    if (OSThread::GetCurrentThreadID() == fHolder)
    {
        fHolderCount++;
        return;
    }
#ifdef __Win32__
    ::EnterCriticalSection(&fMutex);
#else
    (void)pthread_mutex_lock(&fMutex);
#endif
    Assert(fHolder == 0);
    fHolder = OSThread::GetCurrentThreadID();
    fHolderCount++;
    Assert(fHolderCount == 1);
}

void        OSMutex::RecursiveUnlock()
{
    if (OSThread::GetCurrentThreadID() != fHolder)
        return;
        
    Assert(fHolderCount > 0);
    fHolderCount--;
    if (fHolderCount == 0)
    {
        fHolder = 0;
#ifdef __Win32__
        ::LeaveCriticalSection(&fMutex);
#else
        pthread_mutex_unlock(&fMutex);
#endif
    }
}

Bool16      OSMutex::RecursiveTryLock()
{
    // We already have this mutex. Just refcount and return
    if (OSThread::GetCurrentThreadID() == fHolder)
    {
        fHolderCount++;
        return true;
    }

#ifdef __Win32__
    Bool16 theErr = (Bool16)::TryEnterCriticalSection(&fMutex); // Return values of this function match our API
    if (!theErr)
        return theErr;
#else
    int theErr = pthread_mutex_trylock(&fMutex);
    if (theErr != 0)
    {
        Assert(theErr == EBUSY);
        return false;
    }
#endif
    Assert(fHolder == 0);
    fHolder = OSThread::GetCurrentThreadID();
    fHolderCount++;
    Assert(fHolderCount == 1);
    return true;
}
#endif //__PTHREADS_MUTEXES__ || __Win32__

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Apache DolphinScheduler是一个新一代分布式大数据工作流任务调度系统,致力于“解决大数据任务之间错综复杂的依赖关系,整个数据处理开箱即用”。它以 DAG(有向无环图) 的方式将任务连接起来,可实时监控任务的运行状态,同时支持重试、从指定节点恢复失败、暂停及 Kill任务等操作。目前已经有像IBM、腾讯、美团、360等400多家公司生产上使用。 调度系统现在市面上的调度系统那么多,比如老牌的Airflow, Oozie,Kettle,xxl-job ,Spring Batch等等, 为什么要选DolphinScheduler ? DolphinScheduler 的定位是大数据工作流调度。通过把大数据和工作流做了重点标注. 从而可以知道DolphinScheduler的定位是针对于大数据体系。DolphinScheduler 发展很快 很多公司调度都切换到了DolphinScheduler,掌握DolphinScheduler调度使用势在必行,抓住新技术机遇,为跳巢涨薪做好准备。 优秀的框架都是有大师级别的人写出来的,包含了很多设计思想和技术。DolphinScheduler也不例外,它是一个非常优秀的框架,用到很多技术和设计思想,本课程会带大家深入DolphinScheduler框架源码,包括设计的思想和技术都会讲解,DolphinScheduler源码很多,会按照模块进行讲解,学习完课程后,不仅可以熟悉DolphinScheduler使用,而且可以掌握很多优秀的设计思想和技术,给自己的技术带来很大提升,为跳巢涨薪做好准备。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

sunxiaopengsun

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值