2003-12-22 19:29:21 +03:00
|
|
|
/*
|
2005-11-05 22:57:48 +03:00
|
|
|
* Copyright (c) 2004-2005 The Trustees of Indiana University and Indiana
|
|
|
|
* University Research and Technology
|
|
|
|
* Corporation. All rights reserved.
|
|
|
|
* Copyright (c) 2004-2005 The University of Tennessee and The University
|
|
|
|
* of Tennessee Research Foundation. All rights
|
|
|
|
* reserved.
|
2004-11-28 23:09:25 +03:00
|
|
|
* Copyright (c) 2004-2005 High Performance Computing Center Stuttgart,
|
|
|
|
* University of Stuttgart. All rights reserved.
|
2005-03-24 15:43:37 +03:00
|
|
|
* Copyright (c) 2004-2005 The Regents of the University of California.
|
|
|
|
* All rights reserved.
|
2004-11-22 04:38:40 +03:00
|
|
|
* $COPYRIGHT$
|
|
|
|
*
|
|
|
|
* Additional copyrights may follow
|
|
|
|
*
|
2004-01-07 21:39:35 +03:00
|
|
|
* $HEADER$
|
2003-12-22 19:29:21 +03:00
|
|
|
*/
|
2004-01-07 21:39:35 +03:00
|
|
|
|
2006-02-12 04:33:29 +03:00
|
|
|
#include "opal_config.h"
|
2004-08-19 03:24:27 +04:00
|
|
|
|
2005-08-15 15:02:01 +04:00
|
|
|
#include "opal/threads/threads.h"
|
2006-02-12 04:33:29 +03:00
|
|
|
#include "opal/constants.h"
|
2003-12-22 19:29:21 +03:00
|
|
|
|
2004-03-03 19:10:26 +03:00
|
|
|
|
2005-07-04 02:45:48 +04:00
|
|
|
static void opal_thread_construct(opal_thread_t *t);
|
2004-11-15 23:03:14 +03:00
|
|
|
|
2005-07-04 02:45:48 +04:00
|
|
|
OBJ_CLASS_INSTANCE(opal_thread_t,
|
2005-07-03 20:06:07 +04:00
|
|
|
opal_object_t,
|
2005-07-04 02:45:48 +04:00
|
|
|
opal_thread_construct, NULL);
|
2004-11-15 23:03:14 +03:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Constructor
|
|
|
|
*/
|
2005-07-04 02:45:48 +04:00
|
|
|
static void opal_thread_construct(opal_thread_t *t)
|
2004-03-03 19:10:26 +03:00
|
|
|
{
|
|
|
|
t->t_run = 0;
|
2004-08-19 03:24:27 +04:00
|
|
|
#ifdef __WINDOWS__
|
2006-08-22 21:55:36 +04:00
|
|
|
t->t_handle = (HANDLE)NULL;
|
2004-08-19 03:24:27 +04:00
|
|
|
#elif OMPI_HAVE_POSIX_THREADS
|
2004-03-18 17:17:23 +03:00
|
|
|
t->t_handle = (pthread_t) -1;
|
2004-11-15 23:03:14 +03:00
|
|
|
#elif OMPI_HAVE_SOLARIS_THREADS
|
|
|
|
t->t_handle = (thread_t) -1;
|
2004-08-19 03:24:27 +04:00
|
|
|
#endif
|
2004-03-03 19:10:26 +03:00
|
|
|
}
|
2004-08-19 03:24:27 +04:00
|
|
|
|
|
|
|
|
2005-12-13 01:01:51 +03:00
|
|
|
#ifdef __WINDOWS__
|
2004-08-19 03:24:27 +04:00
|
|
|
|
2004-11-15 23:03:14 +03:00
|
|
|
/************************************************************************
|
|
|
|
* Windows threads
|
|
|
|
************************************************************************/
|
2004-08-19 03:24:27 +04:00
|
|
|
|
2005-07-04 02:45:48 +04:00
|
|
|
int opal_thread_start(opal_thread_t *t)
|
2004-08-19 03:24:27 +04:00
|
|
|
{
|
|
|
|
DWORD tid;
|
|
|
|
|
|
|
|
if (OMPI_ENABLE_DEBUG) {
|
|
|
|
if (NULL == t->t_run || t->t_handle != (HANDLE) -1L) {
|
2006-02-12 04:33:29 +03:00
|
|
|
return OPAL_ERR_BAD_PARAM;
|
2004-08-19 03:24:27 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
t->t_handle = CreateThread(NULL, /* default security attributes */
|
|
|
|
0, /* default stack size */
|
2006-08-22 21:55:36 +04:00
|
|
|
(LPTHREAD_START_ROUTINE) t->t_run,
|
2004-08-19 03:24:27 +04:00
|
|
|
t, /* argument */
|
|
|
|
0, /* default creation flags */
|
|
|
|
&tid);
|
|
|
|
|
|
|
|
if (t->t_handle == NULL) {
|
2006-02-12 04:33:29 +03:00
|
|
|
return OPAL_ERROR;
|
2004-08-19 03:24:27 +04:00
|
|
|
}
|
|
|
|
|
2006-02-12 04:33:29 +03:00
|
|
|
return OPAL_SUCCESS;
|
2004-08-19 03:24:27 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-07-04 02:45:48 +04:00
|
|
|
int opal_thread_join(opal_thread_t *t, void **thr_return)
|
2004-08-19 03:24:27 +04:00
|
|
|
{
|
|
|
|
DWORD rc;
|
|
|
|
|
|
|
|
if (WaitForSingleObject(t->t_handle, INFINITE) != WAIT_OBJECT_0) {
|
2006-02-12 04:33:29 +03:00
|
|
|
return OPAL_ERROR;
|
2004-08-19 03:24:27 +04:00
|
|
|
}
|
|
|
|
if (!GetExitCodeThread(t->t_handle, &rc)) {
|
2006-02-12 04:33:29 +03:00
|
|
|
return OPAL_ERROR;
|
2004-08-19 03:24:27 +04:00
|
|
|
}
|
|
|
|
|
2006-10-05 09:43:46 +04:00
|
|
|
*thr_return = (void *)((intptr_t)rc);
|
2004-08-19 03:24:27 +04:00
|
|
|
|
2006-02-12 04:33:29 +03:00
|
|
|
return OPAL_SUCCESS;
|
2004-08-19 03:24:27 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-07-04 02:45:48 +04:00
|
|
|
bool opal_thread_self_compare(opal_thread_t *t)
|
2004-11-15 23:03:14 +03:00
|
|
|
{
|
2006-08-22 21:55:36 +04:00
|
|
|
HANDLE thread_handle;
|
|
|
|
thread_handle = GetCurrentThread();
|
|
|
|
return (thread_handle == t->t_handle ? true : false);
|
2004-11-15 23:03:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-07-04 02:45:48 +04:00
|
|
|
opal_thread_t *opal_thread_get_self(void)
|
2004-11-15 23:03:14 +03:00
|
|
|
{
|
2005-07-04 02:45:48 +04:00
|
|
|
opal_thread_t *t = OBJ_NEW(opal_thread_t);
|
2006-08-22 21:55:36 +04:00
|
|
|
t->t_handle = GetCurrentThread();
|
2005-01-20 03:03:23 +03:00
|
|
|
return t;
|
2004-11-15 23:03:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2004-08-19 03:24:27 +04:00
|
|
|
#elif OMPI_HAVE_POSIX_THREADS
|
2004-03-03 19:10:26 +03:00
|
|
|
|
2004-11-15 23:03:14 +03:00
|
|
|
/************************************************************************
|
|
|
|
* POSIX threads
|
|
|
|
************************************************************************/
|
2004-03-03 19:10:26 +03:00
|
|
|
|
2005-07-04 02:45:48 +04:00
|
|
|
int opal_thread_start(opal_thread_t *t)
|
2004-03-03 19:10:26 +03:00
|
|
|
{
|
|
|
|
int rc;
|
2004-08-19 03:24:27 +04:00
|
|
|
|
|
|
|
if (OMPI_ENABLE_DEBUG) {
|
|
|
|
if (NULL == t->t_run || t->t_handle != (pthread_t) -1) {
|
2006-02-12 04:33:29 +03:00
|
|
|
return OPAL_ERR_BAD_PARAM;
|
2004-08-19 03:24:27 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-10-18 19:41:40 +04:00
|
|
|
rc = pthread_create(&t->t_handle, NULL, (void*(*)(void*)) t->t_run, t);
|
2004-08-19 03:24:27 +04:00
|
|
|
|
2006-02-12 04:33:29 +03:00
|
|
|
return (rc == 0) ? OPAL_SUCCESS : OPAL_ERROR;
|
2004-03-03 19:10:26 +03:00
|
|
|
}
|
|
|
|
|
2004-08-19 03:24:27 +04:00
|
|
|
|
2005-07-04 02:45:48 +04:00
|
|
|
int opal_thread_join(opal_thread_t *t, void **thr_return)
|
2004-03-03 19:10:26 +03:00
|
|
|
{
|
|
|
|
int rc = pthread_join(t->t_handle, thr_return);
|
2007-05-21 20:06:14 +04:00
|
|
|
t->t_handle = (pthread_t) -1;
|
2006-02-12 04:33:29 +03:00
|
|
|
return (rc == 0) ? OPAL_SUCCESS : OPAL_ERROR;
|
2004-08-19 03:24:27 +04:00
|
|
|
}
|
|
|
|
|
2004-11-15 23:03:14 +03:00
|
|
|
|
2005-07-04 02:45:48 +04:00
|
|
|
bool opal_thread_self_compare(opal_thread_t *t)
|
2004-08-28 05:15:19 +04:00
|
|
|
{
|
|
|
|
return t->t_handle == pthread_self();
|
|
|
|
}
|
2004-08-19 03:24:27 +04:00
|
|
|
|
2004-11-15 23:03:14 +03:00
|
|
|
|
2005-07-04 02:45:48 +04:00
|
|
|
opal_thread_t *opal_thread_get_self(void)
|
2004-11-15 23:03:14 +03:00
|
|
|
{
|
2005-07-04 02:45:48 +04:00
|
|
|
opal_thread_t *t = OBJ_NEW(opal_thread_t);
|
2004-11-15 23:03:14 +03:00
|
|
|
t->t_handle = pthread_self();
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#elif OMPI_HAVE_SOLARIS_THREADS
|
|
|
|
|
|
|
|
/************************************************************************
|
|
|
|
* Solaris threads
|
|
|
|
************************************************************************/
|
|
|
|
|
2005-07-04 02:45:48 +04:00
|
|
|
int opal_thread_start(opal_thread_t *t)
|
2004-11-15 23:03:14 +03:00
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
if (OMPI_ENABLE_DEBUG) {
|
2007-05-21 20:06:14 +04:00
|
|
|
if (NULL == t->t_run || t->t_handle != (thread_t) -1) {
|
2006-02-12 04:33:29 +03:00
|
|
|
return OPAL_ERR_BAD_PARAM;
|
2004-11-15 23:03:14 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = thr_create(NULL, 0, (void*(*)(void*)) t->t_run, t, NULL,
|
|
|
|
&t->t_handle);
|
|
|
|
|
2006-02-12 04:33:29 +03:00
|
|
|
return (rc == 0) ? OPAL_SUCCESS : OPAL_ERROR;
|
2004-11-15 23:03:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-07-04 02:45:48 +04:00
|
|
|
int opal_thread_join(opal_thread_t *t, void **thr_return)
|
2004-11-15 23:03:14 +03:00
|
|
|
{
|
2005-08-22 01:02:28 +04:00
|
|
|
int rc = thr_join(t->t_handle, NULL, thr_return);
|
2007-05-21 20:06:14 +04:00
|
|
|
t->t_handle = (thread_t) -1;
|
2006-02-12 04:33:29 +03:00
|
|
|
return (rc == 0) ? OPAL_SUCCESS : OPAL_ERROR;
|
2004-11-15 23:03:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-07-04 02:45:48 +04:00
|
|
|
bool opal_thread_self_compare(opal_thread_t *t)
|
2004-11-15 23:03:14 +03:00
|
|
|
{
|
|
|
|
return t->t_handle == thr_self();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-07-04 02:45:48 +04:00
|
|
|
opal_thread_t *opal_thread_get_self(void)
|
2004-11-15 23:03:14 +03:00
|
|
|
{
|
2005-07-04 02:45:48 +04:00
|
|
|
opal_thread_t *t = OBJ_NEW(opal_thread_t);
|
2004-11-15 23:03:14 +03:00
|
|
|
t->t_handle = thr_self();
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-11-16 15:36:56 +03:00
|
|
|
#else
|
|
|
|
|
2004-11-15 23:03:14 +03:00
|
|
|
/************************************************************************
|
|
|
|
* No thread support
|
|
|
|
************************************************************************/
|
|
|
|
|
2005-07-04 02:45:48 +04:00
|
|
|
int opal_thread_start(opal_thread_t *t)
|
2004-08-19 03:24:27 +04:00
|
|
|
{
|
2006-02-12 04:33:29 +03:00
|
|
|
return OPAL_ERROR;
|
2004-03-03 19:10:26 +03:00
|
|
|
}
|
|
|
|
|
2004-08-19 03:24:27 +04:00
|
|
|
|
2005-07-04 02:45:48 +04:00
|
|
|
int opal_thread_join(opal_thread_t *t, void **thr_return)
|
2004-08-19 03:24:27 +04:00
|
|
|
{
|
2006-02-12 04:33:29 +03:00
|
|
|
return OPAL_ERROR;
|
2004-08-19 03:24:27 +04:00
|
|
|
}
|
|
|
|
|
2004-11-15 23:03:14 +03:00
|
|
|
|
2005-07-04 02:45:48 +04:00
|
|
|
bool opal_thread_self_compare(opal_thread_t *t)
|
2004-08-28 05:15:19 +04:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2005-07-04 02:45:48 +04:00
|
|
|
opal_thread_t *opal_thread_get_self(void)
|
2004-11-15 23:03:14 +03:00
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-08-19 03:24:27 +04:00
|
|
|
#endif
|