⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mutex.cxx

📁 eCos/RedBoot for勤研ARM AnywhereII(4510) 含全部源代码
💻 CXX
📖 第 1 页 / 共 2 页
字号:
//==========================================================================
//
//      pthread.cxx
//
//      POSIX pthreads implementation
//
//==========================================================================
//####ECOSGPLCOPYRIGHTBEGIN####
// -------------------------------------------
// This file is part of eCos, the Embedded Configurable Operating System.
// Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
//
// eCos is free software; you can redistribute it and/or modify it under
// the terms of the GNU General Public License as published by the Free
// Software Foundation; either version 2 or (at your option) any later version.
//
// eCos is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
// for more details.
//
// You should have received a copy of the GNU General Public License along
// with eCos; if not, write to the Free Software Foundation, Inc.,
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
//
// As a special exception, if other files instantiate templates or use macros
// or inline functions from this file, or you compile this file and link it
// with other works to produce a work based on this file, this file does not
// by itself cause the resulting work to be covered by the GNU General Public
// License. However the source code for this file must still be made available
// in accordance with section (3) of the GNU General Public License.
//
// This exception does not invalidate any other reasons why a work based on
// this file might be covered by the GNU General Public License.
//
// Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
// at http://sources.redhat.com/ecos/ecos-license/
// -------------------------------------------
//####ECOSGPLCOPYRIGHTEND####
//==========================================================================
//#####DESCRIPTIONBEGIN####
//
// Author(s):           nickg
// Contributors:        nickg, jlarmour, Wade Jensen
// Date:                2000-03-27
// Purpose:             POSIX pthread implementation
// Description:         This file contains the implementation of the POSIX pthread
//                      functions.
//              
//              
//
//####DESCRIPTIONEND####
//
//==========================================================================

#include <pkgconf/posix.h>

#include <cyg/infra/cyg_trac.h>        // tracing macros
#include <cyg/infra/cyg_ass.h>         // assertion macros

#include "pprivate.h"                  // POSIX private header

#include <cyg/kernel/thread.hxx>       // thread definitions
#include <cyg/kernel/mutex.hxx>        // mutex definitions
#include <cyg/kernel/clock.hxx>        // clock definitions
#include <cyg/kernel/sched.hxx>        // scheduler primitives
#include <pthread.h>

#include <cyg/kernel/thread.inl>       // thread inlines
#include <cyg/kernel/sched.inl>        // scheduler inlines

//-----------------------------------------------------------------------------
// new operator to allow us to construct mutex objects

inline void *operator new(size_t size,  cyg_uint8 *ptr) { return (void *)ptr; };

//=============================================================================
// Mutexes

//-----------------------------------------------------------------------------
// Mutex attributes manipulation functions

//-----------------------------------------------------------------------------
// Initialize attribute object

externC int pthread_mutexattr_init ( pthread_mutexattr_t *attr)
{
    PTHREAD_ENTRY();
    
    PTHREAD_CHECK(attr);

    attr->protocol      = PTHREAD_PRIO_NONE;
#ifdef _POSIX_THREAD_PRIO_PROTECT    
    attr->prioceiling   = 0;
#endif
    
    PTHREAD_RETURN(0);
}

//-----------------------------------------------------------------------------
// Destroy attribute object

externC int pthread_mutexattr_destroy ( pthread_mutexattr_t *attr)
{
    PTHREAD_ENTRY();
    
    PTHREAD_CHECK(attr);

    // Nothing to do here...
    
    PTHREAD_RETURN(0);
}

//-----------------------------------------------------------------------------
// Optional functions depending on priority inversion protection options.

#if defined(_POSIX_THREAD_PRIO_INHERIT) || defined(_POSIX_THREAD_PRIO_PROTECT)

// Set priority inversion protection protocol
externC int pthread_mutexattr_setprotocol ( pthread_mutexattr_t *attr,
                                            int protocol)
{
    PTHREAD_ENTRY();
    
    PTHREAD_CHECK(attr);

    switch( protocol )
    {
    case PTHREAD_PRIO_NONE:
#if defined(_POSIX_THREAD_PRIO_INHERIT)        
    case PTHREAD_PRIO_INHERIT:
#endif
#if defined(_POSIX_THREAD_PRIO_PROTECT)
    case PTHREAD_PRIO_PROTECT:
#endif        
        attr->protocol = protocol;
        PTHREAD_RETURN(0);

    default:
        PTHREAD_RETURN(EINVAL);
    }
    
    PTHREAD_RETURN(0);
}

// Get priority inversion protection protocol
externC int pthread_mutexattr_getprotocol ( pthread_mutexattr_t *attr,
                                            int *protocol)
{
    PTHREAD_ENTRY();
    
    PTHREAD_CHECK(attr);

    if( protocol != NULL )
        *protocol = attr->protocol;
    
    PTHREAD_RETURN(0);
}

#if defined(_POSIX_THREAD_PRIO_PROTECT)

// Set priority for priority ceiling protocol
externC int pthread_mutexattr_setprioceiling ( pthread_mutexattr_t *attr,
                                               int prioceiling)
{
    PTHREAD_ENTRY();
    
    PTHREAD_CHECK(attr);

    
    attr->prioceiling = prioceiling;
    
    PTHREAD_RETURN(0);
}

// Get priority for priority ceiling protocol
externC int pthread_mutexattr_getprioceiling ( pthread_mutexattr_t *attr,
                                               int *prioceiling)
{
    PTHREAD_ENTRY();
    
    PTHREAD_CHECK(attr);

    if( prioceiling != NULL )
        *prioceiling = attr->prioceiling;
    
    PTHREAD_RETURN(0);
}

// Set priority ceiling of given mutex, returning old ceiling.
externC int pthread_mutex_setprioceiling( pthread_mutex_t *mutex,
                                          int prioceiling,
                                          int *old_ceiling)
{
    PTHREAD_ENTRY();
    
    PTHREAD_CHECK(mutex);

    pthread_mutex_lock( mutex );

    Cyg_Mutex *mx = (Cyg_Mutex *)mutex;

    if( old_ceiling != NULL )
        *old_ceiling = mx->get_ceiling();
    
    mx->set_ceiling( prioceiling );
    
    pthread_mutex_unlock( mutex );

    PTHREAD_RETURN(0);
}

// Get priority ceiling of given mutex
externC int pthread_mutex_getprioceiling( pthread_mutex_t *mutex,
                                          int *prioceiling)
{
    PTHREAD_ENTRY();
    
    PTHREAD_CHECK(mutex);

    Cyg_Mutex *mx = (Cyg_Mutex *)mutex;

    if( prioceiling != NULL )
        *prioceiling = mx->get_ceiling();
        
    PTHREAD_RETURN(0);
}

#endif // defined(_POSIX_THREAD_PRIO_PROTECT)

#endif // defined(_POSIX_THREAD_PRIO_INHERIT) || defined(_POSIX_THREAD_PRIO_PROTECT)

//-----------------------------------------------------------------------------
// Mutex functions

//-----------------------------------------------------------------------------
// Initialize mutex. If mutex_attr is NULL, use default attributes.

externC int pthread_mutex_init (pthread_mutex_t *mutex,
                                const pthread_mutexattr_t *mutex_attr)
{
    PTHREAD_ENTRY();

    PTHREAD_CHECK( mutex );
    
    pthread_mutexattr_t use_attr;

    // Set up the attributes we are going to use
    if( mutex_attr == NULL )
        pthread_mutexattr_init( &use_attr );
    else use_attr = *mutex_attr;

    // Now translate the POSIX protocol identifier into the eCos one.
    Cyg_Mutex::cyg_protcol protocol;
    
    switch( use_attr.protocol )
    {
#if defined(_POSIX_THREAD_PRIO_PROTECT)
    case PTHREAD_PRIO_PROTECT:
        protocol = Cyg_Mutex::CEILING;
        break;
#endif
#if defined(_POSIX_THREAD_PRIO_INHERIT)
    case PTHREAD_PRIO_INHERIT:
        protocol = Cyg_Mutex::INHERIT;
        break;
#endif        
    case PTHREAD_PRIO_NONE:
        protocol = Cyg_Mutex::NONE;
        break;

    default:
        PTHREAD_RETURN(EINVAL);
    }

    Cyg_Mutex *mx = new((cyg_uint8 *)mutex) Cyg_Mutex(  protocol );

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -