1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
|
#include <pmix_server.h>
#include <pmix_tool.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <stdarg.h>
static void hide_unused_params(int x, ...)
{
va_list ap;
va_start(ap, x);
va_end(ap);
}
typedef struct {
pthread_mutex_t mutex;
pthread_cond_t cond;
volatile bool active;
pmix_status_t status;
} mylock_t;
typedef struct {
mylock_t lock;
pmix_info_t *info;
size_t ninfo;
} myxfer_t;
#define DEBUG_CONSTRUCT_LOCK(l) \
do { \
pthread_mutex_init(&(l)->mutex, NULL); \
pthread_cond_init(&(l)->cond, NULL); \
(l)->active = true; \
(l)->status = PMIX_SUCCESS; \
} while (0)
#define DEBUG_DESTRUCT_LOCK(l) \
do { \
pthread_mutex_destroy(&(l)->mutex); \
pthread_cond_destroy(&(l)->cond); \
} while (0)
#define DEBUG_WAIT_THREAD(lck) \
do { \
pthread_mutex_lock(&(lck)->mutex); \
while ((lck)->active) { \
pthread_cond_wait(&(lck)->cond, &(lck)->mutex); \
} \
pthread_mutex_unlock(&(lck)->mutex); \
} while (0)
#define DEBUG_WAKEUP_THREAD(lck) \
do { \
pthread_mutex_lock(&(lck)->mutex); \
(lck)->active = false; \
pthread_cond_broadcast(&(lck)->cond); \
pthread_mutex_unlock(&(lck)->mutex); \
} while (0)
static void setup_cbfunc(pmix_status_t status, pmix_info_t info[], size_t ninfo,
void *provided_cbdata, pmix_op_cbfunc_t cbfunc, void *cbdata)
{
myxfer_t *x = (myxfer_t *) provided_cbdata;
size_t n;
/* transfer it to the caddy for return to the main thread */
x->lock.status = status;
if (0 < ninfo) {
PMIX_INFO_CREATE(x->info, ninfo);
x->ninfo = ninfo;
for (n = 0; n < ninfo; n++) {
PMIX_INFO_XFER(&x->info[n], &info[n]);
}
}
/* let the library release the data and cleanup from
* the operation */
if (NULL != cbfunc) {
cbfunc(PMIX_SUCCESS, cbdata);
}
DEBUG_WAKEUP_THREAD(&x->lock);
}
int main(int argc, char **argv)
{
pmix_status_t rc=0;
myxfer_t x;
pmix_proc_t myproc;
pmix_info_t *info;
size_t n, ninfo;
char *myvni = NULL;
hide_unused_params(rc, argc, argv);
/* setup the PMIx tool library - don't need to connect
* to a PMIx server */
ninfo = 2;
PMIX_INFO_CREATE(info, ninfo);
n = 0;
PMIX_INFO_LOAD(&info[n], PMIX_TOOL_DO_NOT_CONNECT, NULL, PMIX_BOOL);
++n;
PMIX_INFO_LOAD(&info[n], PMIX_LAUNCHER, NULL, PMIX_BOOL);
if (PMIX_SUCCESS != (rc = PMIx_tool_init(&myproc, info, ninfo))) {
fprintf(stderr, "Init failed with error %s\n", PMIx_Error_string(rc));
return rc;
}
PMIX_INFO_FREE(info, ninfo);
/* ask for a security credential */
DEBUG_CONSTRUCT_LOCK(&x.lock);
x.info = NULL;
x.ninfo = 0;
ninfo = 1;
PMIX_INFO_CREATE(info, ninfo);
n = 0;
PMIX_INFO_LOAD(&info[n], PMIX_ALLOC_FABRIC_SEC_KEY, NULL, PMIX_BOOL);
rc = PMIx_server_setup_application(myproc.nspace, info, ninfo, setup_cbfunc, &x);
if (PMIX_SUCCESS != rc) {
/* the thread won't be called back */
PMIX_INFO_FREE(info, ninfo);
goto done;
}
DEBUG_WAIT_THREAD(&x.lock);
PMIX_INFO_FREE(info, ninfo);
if (PMIX_SUCCESS != x.lock.status) {
/* couldn't allocate it */
rc = x.lock.status;
goto done;
}
/* find the VNI in the returned data - shouldn't be anything else */
for (n = 0; n < x.ninfo; n++) {
if (PMIX_CHECK_KEY(&x.info[n], PMIX_CREDENTIAL)) {
myvni = strdup(x.info[n].value.data.string);
break;
}
}
fprintf(stderr, "VNI: %s\n", (NULL == myvni) ? "NULL" : myvni);
done:
rc = PMIx_tool_finalize();
return rc;
}
|