| // umlrtbasicthread.cc |
| |
| /******************************************************************************* |
| * Copyright (c) 2014-2015 Zeligsoft (2009) Limited and others. |
| * All rights reserved. This program and the accompanying materials |
| * are made available under the terms of the Eclipse Public License v1.0 |
| * which accompanies this distribution, and is available at |
| * http://www.eclipse.org/legal/epl-v10.html |
| *******************************************************************************/ |
| |
| #ifndef _GNU_SOURCE |
| #define _GNU_SOURCE |
| #endif |
| |
| #include <string.h> |
| #include "basefatal.hh" |
| #include "umlrtbasicthread.hh" |
| |
| // See umlrtbasicthread.hh for documentation. |
| |
| // Bug 31 raised to move this to +/os/linux/osbasicthread.cc. |
| |
| /*static*/ void * UMLRTBasicThread::static_entrypoint( void * arg ) |
| { |
| threadargs_t *threadargs = (threadargs_t*)arg; |
| |
| return threadargs->inst->run( threadargs->args ); |
| } |
| |
| |
| UMLRTBasicThread::UMLRTBasicThread( const char * name_ ) : tid(0) |
| { |
| memset(name, 0, sizeof(name)); |
| strncpy(name, name_, sizeof(name) - 1); |
| } |
| |
| // Start the thread, passing in a single argument. |
| |
| void UMLRTBasicThread::start( void * args ) |
| { |
| pthread_attr_t attr; |
| |
| memset(&attr, 0, sizeof(attr)); |
| |
| if (pthread_attr_init( &attr) < 0) |
| { |
| FATAL_ERRNO("pthread_attr_init"); |
| } |
| threadargs.inst = this; |
| threadargs.args = args; |
| |
| if (pthread_create(&tid, &attr, static_entrypoint, &threadargs) < 0) |
| { |
| FATAL_ERRNO("pthread_create"); |
| } |
| #if 0 |
| // May not be universally available. |
| pthread_setname_np(tid, name); // Attempt this, but ignore errors. |
| #endif |
| } |
| |
| // Wait for the thread to complete and get returned value. |
| |
| void * UMLRTBasicThread::join() |
| { |
| void *ret; |
| if (pthread_join(tid, &ret) < 0) |
| { |
| FATAL_ERRNO("pthread_join"); |
| } |
| return ret; |
| } |
| |
| // Returns true if this thread is currently running thread. |
| |
| bool UMLRTBasicThread::isMyThread() |
| { |
| return pthread_self() == tid; |
| } |
| |
| // Return my name |
| |
| const char * UMLRTBasicThread::getName() const |
| { |
| return name; |
| } |
| |
| // Return running thread id. |
| /*static*/ UMLRTBasicThread::threadid_t UMLRTBasicThread::selfId() |
| { |
| return (threadid_t)pthread_self(); |
| } |
| |