vthread-win32.cxx
上传用户:sy_wanhua
上传日期:2013-07-25
资源大小:3048k
文件大小:7k
- /* ====================================================================
- * The Vovida Software License, Version 1.0
- *
- * Copyright (c) 2000 Vovida Networks, Inc. All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- *
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- *
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in
- * the documentation and/or other materials provided with the
- * distribution.
- *
- * 3. The names "VOCAL", "Vovida Open Communication Application Library",
- * and "Vovida Open Communication Application Library (VOCAL)" must
- * not be used to endorse or promote products derived from this
- * software without prior written permission. For written
- * permission, please contact vocal@vovida.org.
- *
- * 4. Products derived from this software may not be called "VOCAL", nor
- * may "VOCAL" appear in their name, without prior written
- * permission of Vovida Networks, Inc.
- *
- * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
- * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
- * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL VOVIDA
- * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
- * IN EXCESS OF ,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
- * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
- * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
- * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
- * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
- * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
- * DAMAGE.
- *
- * ====================================================================
- *
- * This software consists of voluntary contributions made by Vovida
- * Networks, Inc. and many individuals on behalf of Vovida Networks,
- * Inc. For more information on Vovida Networks, Inc., please see
- * <http://www.vovida.org/>.
- *
- */
- #ifdef WIN32
-
- #include <cassert>
- #include "VThread.h"
- #include "cpLog.h"
- /*********************************************************************
- VThread for WIN32
- *********************************************************************/
- VThread::VThread( void )
- {
- myId = VTHREAD_ID_INVALID;
- ::memset (&myAttributes, 0, sizeof (myAttributes));
- }
-
- VThread::~VThread( void )
- {
- if( myId != VTHREAD_ID_INVALID )
- {
- // warn user but don't kill thread
- cpLog( LOG_DEBUG, "active VThread:%d going out of scope", myId );
- }
- myId = VTHREAD_ID_INVALID;
- }
-
- int
- VThread::spawn( void *( *startFunc )( void * ),
- void *startArgs,
- unsigned long flags,
- unsigned long priority,
- int stack_size
- )
- {
- if( myId != VTHREAD_ID_INVALID )
- {
- // return without spawning another thread
- cpLog( LOG_ERR, "VThread:%d is already active", myId );
- return (int)myId;
- }
- struct sched_param priorityParams;
- int retVal = 0;
-
- assert( pthread_attr_init(&myAttributes) == 0 );
- /* not implemented in Pthreads-win32
- assert( pthread_attr_setinheritsched( &myAttributes,
- PTHREAD_EXPLICIT_SCHED ) == 0 );
- */
- // extract scheduling preference from flags
- switch( flags & VTHREAD_SCHED_MASK )
- {
- case VTHREAD_SCHED_FIFO:
- {
- /* not implemented in Pthreads-win32
- retVal = pthread_attr_setschedpolicy( &myAttributes,
- SCHED_FIFO );
- */
- if( true )
- {
- throw VThreadExceptionInvalidAttributes(
- "cannot set schedule as VTHREAD_SCHED_FIFO",
- __FILE__,
- __LINE__,
- retVal );
- }
- }
- break;
-
- case VTHREAD_SCHED_RR:
- {
- /* not implemented in Pthreads-win32
- retVal = pthread_attr_setschedpolicy( &myAttributes,
- SCHED_RR );
- */
- if( true )
- {
- throw VThreadExceptionInvalidAttributes(
- "cannot set schedule as VTHREAD_SCHED_RR",
- __FILE__,
- __LINE__,
- retVal );
- }
-
- }
- break;
- case VTHREAD_SCHED_DEFAULT:
- default:
- {
- // SCHED_OTHER is the default for pthread
- /* not implemented in Pthreads-win32
- retVal = pthread_attr_setschedpolicy( &myAttributes,
- SCHED_OTHER );
-
- if( true )
- {
- throw VThreadExceptionInvalidAttributes(
- "cannot set schedule as VTHREAD_SCHED_OTHER",
- __FILE__,
- __LINE__,
- retVal );
- }
- */
-
- }
- break;
- }
-
- if( priority != VTHREAD_PRIORITY_DEFAULT )
- {
- // probably should improve to use relative values
- priorityParams.sched_priority = priority;
- retVal = pthread_attr_setschedparam( &myAttributes,
- &priorityParams );
- if( retVal )
- {
- throw VThreadExceptionInvalidPriority(
- "cannot set priority",
- __FILE__,
- __LINE__,
- retVal );
- }
- }
-
- return( pthread_create( &myId, &myAttributes, startFunc, startArgs ) );
- }
-
- int
- VThread::join( void **status )
- {
- int retVal = pthread_join( myId, status );
- myId = VTHREAD_ID_INVALID;
- return retVal;
- }
-
- int
- VThread::getPriority( void ) const
- {
- struct sched_param priorityParams;
-
- assert( pthread_attr_getschedparam( &myAttributes,
- &priorityParams ) == 0 );
- return priorityParams.sched_priority;
- }
- int
- VThread::setPriority( int priority )
- {
- struct sched_param priorityParams;
- int policy = 0;
- int retVal;
-
- retVal = pthread_attr_setschedparam( &myAttributes, &priorityParams );
- if( retVal )
- {
- throw VThreadExceptionInvalidPriority(
- "cannot set priority",
- __FILE__,
- __LINE__,
- retVal );
- }
-
- /* not implemented in Pthreads-win32
- retVal = pthread_attr_getschedpolicy( &myAttributes, &policy );
- if( retVal )
- {
- throw VThreadExceptionInvalidPriority(
- "cannot set policy",
- __FILE__,
- __LINE__,
- retVal );
- }
- */
- return( pthread_setschedparam( myId, policy, &priorityParams ) );
- }
- const vthread_t*
- VThread::getId( void ) const
- {
- return &myId;
- }
-
- const vthread_attr_t*
- VThread::getAttributes( void ) const
- {
- return &myAttributes;
- }
- void
- VThread::exit( void )
- {
- if( myId != VTHREAD_ID_INVALID )
- {
- pthread_cancel( myId );
- myId = VTHREAD_ID_INVALID;
- }
- }
- const vthread_t
- VThread::selfId( void )
- {
- return pthread_self();
- }
- int
- sleep( int seconds )
- {
- Sleep (seconds * 1000);
- return 0;
- }
- int
- usleep( int useconds )
- {
- Sleep(useconds / 1000);
- return 0;
- }
- #endif // WIN32