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.
|
2006-08-23 04:29:35 +04:00
|
|
|
* Copyright (c) 2004-2006 The University of Tennessee and The University
|
2005-11-05 22:57:48 +03:00
|
|
|
* 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.
|
2010-08-05 20:25:32 +04:00
|
|
|
* Copyright (c) 2010 Cisco Systems, Inc. All rights reserved.
|
2004-11-22 04:38:40 +03:00
|
|
|
* $COPYRIGHT$
|
|
|
|
*
|
|
|
|
* Additional copyrights may follow
|
|
|
|
*
|
2004-01-07 11:02:26 +03:00
|
|
|
* $HEADER$
|
2003-12-22 19:29:21 +03:00
|
|
|
*/
|
2004-01-07 21:39:35 +03:00
|
|
|
|
2005-07-04 02:45:48 +04:00
|
|
|
#ifndef OPAL_THREAD_H
|
|
|
|
#define OPAL_THREAD_H 1
|
2003-12-22 19:29:21 +03:00
|
|
|
|
2009-03-04 18:35:54 +03:00
|
|
|
#include "opal_config.h"
|
|
|
|
|
2009-05-07 00:11:28 +04:00
|
|
|
#if OPAL_HAVE_POSIX_THREADS
|
2004-03-03 19:10:26 +03:00
|
|
|
#include <pthread.h>
|
2009-05-07 00:11:28 +04:00
|
|
|
#elif OPAL_HAVE_SOLARIS_THREADS
|
2005-02-16 18:38:37 +03:00
|
|
|
#include <thread.h>
|
2004-10-20 05:03:09 +04:00
|
|
|
#endif
|
2004-08-19 03:24:27 +04:00
|
|
|
|
2005-07-03 20:06:07 +04:00
|
|
|
#include "opal/class/opal_object.h"
|
2010-08-05 20:25:32 +04:00
|
|
|
#if OPAL_ENABLE_DEBUG
|
|
|
|
#include "opal/util/output.h"
|
|
|
|
#endif
|
|
|
|
|
2010-03-22 18:45:03 +03:00
|
|
|
#include "mutex.h"
|
|
|
|
#include "condition.h"
|
2006-08-23 04:29:35 +04:00
|
|
|
|
2009-08-20 15:42:18 +04:00
|
|
|
BEGIN_C_DECLS
|
2003-12-22 19:29:21 +03:00
|
|
|
|
2005-07-04 02:45:48 +04:00
|
|
|
typedef void *(*opal_thread_fn_t) (opal_object_t *);
|
2004-03-03 19:10:26 +03:00
|
|
|
|
2010-01-23 06:57:24 +03:00
|
|
|
#ifdef __WINDOWS__
|
|
|
|
#define OPAL_THREAD_CANCELLED ((void*)1);
|
|
|
|
#elif OPAL_HAVE_POSIX_THREADS
|
|
|
|
#define OPAL_THREAD_CANCELLED ((void*)1);
|
|
|
|
#elif OPAL_HAVE_SOLARIS_THREADS
|
|
|
|
#define OPAL_THREAD_CANCELLED ((void*)1);
|
|
|
|
#endif
|
2004-03-03 19:10:26 +03:00
|
|
|
|
2005-07-04 02:45:48 +04:00
|
|
|
struct opal_thread_t {
|
2005-07-03 20:06:07 +04:00
|
|
|
opal_object_t super;
|
2005-07-04 02:45:48 +04:00
|
|
|
opal_thread_fn_t t_run;
|
2004-10-27 17:50:27 +04:00
|
|
|
void* t_arg;
|
2005-12-13 01:01:51 +03:00
|
|
|
#ifdef __WINDOWS__
|
2004-08-19 03:24:27 +04:00
|
|
|
HANDLE t_handle;
|
2009-05-07 00:11:28 +04:00
|
|
|
#elif OPAL_HAVE_POSIX_THREADS
|
2004-03-03 19:10:26 +03:00
|
|
|
pthread_t t_handle;
|
2009-05-07 00:11:28 +04:00
|
|
|
#elif OPAL_HAVE_SOLARIS_THREADS
|
2004-11-15 23:03:14 +03:00
|
|
|
thread_t t_handle;
|
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-07-04 02:45:48 +04:00
|
|
|
typedef struct opal_thread_t opal_thread_t;
|
2004-03-03 19:10:26 +03:00
|
|
|
|
2010-08-05 20:25:32 +04:00
|
|
|
#if OPAL_ENABLE_DEBUG
|
|
|
|
OPAL_DECLSPEC extern bool opal_debug_threads;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2006-08-20 19:54:04 +04:00
|
|
|
OPAL_DECLSPEC OBJ_CLASS_DECLARATION(opal_thread_t);
|
2003-12-22 19:29:21 +03:00
|
|
|
|
2010-08-05 20:25:32 +04:00
|
|
|
#if OPAL_ENABLE_DEBUG
|
|
|
|
#define OPAL_ACQUIRE_THREAD(lck, cnd, act) \
|
|
|
|
do { \
|
|
|
|
OPAL_THREAD_LOCK((lck)); \
|
|
|
|
if (opal_debug_threads) { \
|
|
|
|
opal_output(0, "Waiting for thread %s:%d", \
|
|
|
|
__FILE__, __LINE__); \
|
|
|
|
} \
|
|
|
|
while (*(act)) { \
|
|
|
|
opal_condition_wait((cnd), (lck)); \
|
|
|
|
} \
|
|
|
|
if (opal_debug_threads) { \
|
|
|
|
opal_output(0, "Thread obtained %s:%d", \
|
|
|
|
__FILE__, __LINE__); \
|
|
|
|
} \
|
|
|
|
*(act) = true; \
|
|
|
|
} while(0);
|
|
|
|
#else
|
|
|
|
#define OPAL_ACQUIRE_THREAD(lck, cnd, act) \
|
|
|
|
do { \
|
|
|
|
OPAL_THREAD_LOCK((lck)); \
|
|
|
|
while (*(act)) { \
|
|
|
|
opal_condition_wait((cnd), (lck)); \
|
|
|
|
} \
|
|
|
|
*(act) = true; \
|
2010-03-22 18:45:03 +03:00
|
|
|
} while(0);
|
2010-08-05 20:25:32 +04:00
|
|
|
#endif
|
|
|
|
|
2010-03-22 18:45:03 +03:00
|
|
|
|
2010-08-05 20:25:32 +04:00
|
|
|
#if OPAL_ENABLE_DEBUG
|
|
|
|
#define OPAL_RELEASE_THREAD(lck, cnd, act) \
|
|
|
|
do { \
|
|
|
|
if (opal_debug_threads) { \
|
|
|
|
opal_output(0, "Releasing thread %s:%d", \
|
|
|
|
__FILE__, __LINE__); \
|
|
|
|
} \
|
|
|
|
*(act) = false; \
|
|
|
|
opal_condition_broadcast((cnd)); \
|
|
|
|
OPAL_THREAD_UNLOCK((lck)); \
|
|
|
|
} while(0);
|
|
|
|
#else
|
|
|
|
#define OPAL_RELEASE_THREAD(lck, cnd, act) \
|
|
|
|
do { \
|
|
|
|
*(act) = false; \
|
|
|
|
opal_condition_broadcast((cnd)); \
|
|
|
|
OPAL_THREAD_UNLOCK((lck)); \
|
2010-03-22 18:45:03 +03:00
|
|
|
} while(0);
|
2010-08-05 20:25:32 +04:00
|
|
|
#endif
|
|
|
|
|
2010-03-22 18:45:03 +03:00
|
|
|
|
2010-04-07 02:40:45 +04:00
|
|
|
#define OPAL_WAKEUP_THREAD(cnd, act) \
|
|
|
|
do { \
|
|
|
|
*(act) = false; \
|
|
|
|
opal_condition_broadcast((cnd)); \
|
|
|
|
} while(0);
|
|
|
|
|
2010-03-22 18:45:03 +03:00
|
|
|
|
2006-08-23 04:29:35 +04:00
|
|
|
OPAL_DECLSPEC int opal_thread_start(opal_thread_t *);
|
|
|
|
OPAL_DECLSPEC int opal_thread_join(opal_thread_t *, void **thread_return);
|
|
|
|
OPAL_DECLSPEC bool opal_thread_self_compare(opal_thread_t*);
|
|
|
|
OPAL_DECLSPEC opal_thread_t *opal_thread_get_self(void);
|
2006-08-20 19:54:04 +04:00
|
|
|
|
2009-08-20 15:42:18 +04:00
|
|
|
END_C_DECLS
|
2003-12-22 19:29:21 +03:00
|
|
|
|
2005-07-04 02:45:48 +04:00
|
|
|
#endif /* OPAL_THREAD_H */
|